#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int /*<<< orphan*/ sem_t;

/* Variables and functions */
int /*<<< orphan*/ printf(char *, int);
int random();
int /*<<< orphan*/ sem_post(int /*<<< orphan*/ *);
int /*<<< orphan*/ sem_wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ sleep(int);
void *Cocinero(void *p) {
  extern int comida;
  extern sem_t cmutex;
  int c;
  for (;;) {
    c = random() % 100;
    sem_wait(&cmutex);
    comida += c;
    printf("Preparada comida: %d\n", c);
    sem_post(&cmutex);
    sleep(5);
  }
}


x86 assembly (clang, -O0):
<Cocinero>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1060 <random@plt>
mov    $0x64,%ecx
cltd
idiv   %ecx
mov    %edx,-0xc(%rbp)
mov    0x2e63(%rip),%rdi
call   1030 <sem_wait@plt>
mov    -0xc(%rbp),%ecx
mov    0x2e64(%rip),%rax
add    (%rax),%ecx
mov    %ecx,-0x10(%rbp)
mov    0x2e58(%rip),%rax
mov    %ecx,(%rax)
mov    -0xc(%rbp),%esi
lea    0xe64(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e30(%rip),%rdi
call   1050 <sem_post@plt>
mov    $0x5,%edi
call   1070 <sleep@plt>
jmp    115c <Cocinero+0xc>

x86 assembly (clang, -O2):
<Cocinero>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    0x2e79(%rip),%r15
mov    0x2e82(%rip),%r12
lea    0xe93(%rip),%r14
nopl   (%rax)
xor    %eax,%eax
call   1060 <random@plt>
movslq %eax,%rbx
imul   $0x51eb851f,%rbx,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x25,%rax
add    %ecx,%eax
imul   $0x64,%eax,%eax
sub    %eax,%ebx
mov    %r15,%rdi
call   1030 <sem_wait@plt>
add    %ebx,(%r12)
mov    %r14,%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
mov    %r15,%rdi
call   1050 <sem_post@plt>
mov    $0x5,%edi
call   1070 <sleep@plt>
jmp    1170 <Cocinero+0x20>

LLVM IR (-O0):
@cmutex = external global i32
@comida = external global i32
@.str = [22 x i8] c"Preparada comida: %d\0A\00"
define i8* @Cocinero(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
store i8* %0, i8** %2
br label %4
4: ; preds = %4, %1
%5 = call i32 (...) @random()
%6 = srem i32 %5, 100
store i32 %6, i32* %3
%7 = call i32 @sem_wait(i32* @cmutex)
%8 = load i32, i32* %3
%9 = load i32, i32* @comida
%10 = add nsw i32 %9, %8
store i32 %10, i32* @comida
%11 = load i32, i32* %3
%12 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = call i32 @sem_post(i32* @cmutex)
%14 = call i32 @sleep(i32 5)
br label %4
}

LLVM IR (-O2):
@cmutex = external global i32
@comida = external global i32
@.str = [22 x i8] c"Preparada comida: %d\0A\00"
define noalias i8* @Cocinero(i8* nocapture readnone %0) {
br label %2
2: ; preds = %2, %1
%3 = tail call i32 (...) @random()
%4 = srem i32 %3, 100
%5 = tail call i32 @sem_wait(i32* @cmutex)
%6 = load i32, i32* @comida
%7 = add nsw i32 %6, %4
store i32 %7, i32* @comida
%8 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %4)
%9 = tail call i32 @sem_post(i32* @cmutex)
%10 = tail call i32 @sleep(i32 5)
br label %2
}