FLIP-FLOPPED feet saunter across a wet concrete floor. With each step, the water reddens until the camera comes to rest on the bodies of three prisoners. Severed heads lie on top of two of the corpses. The video was filmed in Pedrinhas, the biggest prison complex in the northern state of Maranhão, and published on January 7th by Folha de São Paulo, a newspaper. The footage has woken up many Brazilians to the hellishness of their prisons.

At least 218 inmates have been murdered since January 2013 in 24 of Brazil’s 27 states. (The other three do not disclose figures.) Dozens more have died in suspicious circumstances. Severe overcrowding is the root of the problem. In the past 20 years Brazil’s population has grown by 30%, while that of its prisons and police cells has almost quintupled, to 550,000—the fourth-highest in the world, behind the United States, China and Russia.

Officially, Brazilian penitentiaries have room for around 300,000 people. There is federal money to spend on building extra prisons, which are largely run by the states. But it can flow only once a project is approved by a local town. They are reluctant hosts, fearing that penitentiaries both bring crime when prisoners are released and also divert resources from other public works. “Everyone wants hospitals and schools,” says Antonio Ferreira Pinto, a former security secretary in São Paulo state. “No one wants a prison.” Federal-prison spending fell in 2012.

Brazil needs cells to house genuine criminals: the murder rate stood at 24.3 per 100,000 in 2012, more than six times higher than in Chile. But really it needs fewer inmates. Lucia Nader of Conectas, a human-rights group, attributes an upsurge in prisoners since 2006 to a law that decriminalised possession of drugs for personal use but stiffened penalties for trafficking. The distinction between the two is left to the arresting officer. “A light-skinned yuppie smoking pot on the beach is a user and left in peace,” says Ms Nader. “A dark-skinned slum-dweller lighting a spliff on the street is a peddler and thrown in jail.” Since the law’s introduction, the number of people held for trafficking has swelled from 33,000 in 2005 to 138,000 in 2012.

This flood of inmates hits two bottlenecks, says Julita Lemgruber, a former director of Rio de Janeiro’s prisons department and now an academic at Candido Mendes University. At the “entrance” 41% of all prisoners languish in pre-trial detention. Ms Lemgruber and colleagues have found that half of the 5,000 or so pre-trial detainees in Rio whose cases made it to court in 2012 ended up without a prison sentence. At the “exit”, meanwhile, convicts do not benefit from Brazil’s theoretically world-class laws on parole and alternative sentences like community service.

A shortage of legal advice for prisoners helps to explain both bottlenecks. Most detainees cannot afford a lawyer and public defenders are in short supply. The federal government has pledged to send a task-force of lawyers to plough through a backlog of cases in Maranhão. It is not clear how that will help prisons elsewhere. For each public attorney in São Paulo’s main criminal court, 2,500 cases are pending.

With too many prisoners flowing in, and not enough flowing out, a cesspool festers in the middle. On paper Brazil’s prisons are a paragon of modernity. In practice, says Marcos Fuchs of Instituto Pro Bono, another human-rights group, they are medieval. In one São Paulo penitentiary he visited, 62 people were crammed in a cell meant for 12, taking turns to sleep on the floor or by leaning against a wall. According to official figures, half a million inmates received care from 367 doctors in 2012. Fifteen gynaecologists served 32,000 female prisoners, many of whom use bread to stanch menstrual bleeding.

Dante would blanch

Criminal gangs have filled the vacuum left by the state. In exchange for loyalty and a membership fee, gangs offer protection, bring supplies (including sanitary towels), bus families in for visits and even pay for lawyers. They also maintain order—until a rival outfit emerges. A challenge to an established gang seems to have been behind the violence in Pedrinhas.

Brazil’s criminal code includes neither the death penalty nor a life sentence. In theory, every inmate will re-emerge into the outside world. But they do so brutalised, lacking skills and ostracised by a society with a punitive attitude towards criminals. That pushes recidivism rates above 60%, starting the ghastly cycle anew.