#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*/ pthread_mutex_t;
typedef int /*<<< orphan*/ pthread_cond_t;

/* Variables and functions */
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ pthread_cond_wait(int /*<<< orphan*/ *,
                                     int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_mutex_lock(int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_mutex_unlock(int /*<<< orphan*/ *);
int random();
int /*<<< orphan*/ sleep(int);
void Comer(int i) {
  int s, c;
  extern int comida;
  extern pthread_mutex_t cmutex;
  extern pthread_cond_t no_comida;
  c = random() % 5;
  for (;;) {
    pthread_mutex_lock(&cmutex);
    if (comida > c) {
      comida -= c;
      printf("Filosofo %d comera %d unidades de %d\n", i, c, comida + c);
      pthread_mutex_unlock(&cmutex);
      break;
    }
    printf("Filosofo %d se bloquea en espera de comida\n", i);
    pthread_cond_wait(&no_comida, &cmutex);
    pthread_mutex_unlock(&cmutex);
  }
  s = random() % 10;
  printf("Filosofo %d comiendo %d segundos\n", i, s);
  sleep(s);
}


x86 assembly (clang, -O0):
<Comer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x0,%al
call   1050 <random@plt>
mov    $0x5,%ecx
cltd
idiv   %ecx
mov    %edx,-0xc(%rbp)
mov    0x2e4c(%rip),%rdi
call   1080 <pthread_mutex_lock@plt>
mov    0x2e58(%rip),%rax
mov    (%rax),%eax
cmp    -0xc(%rbp),%eax
jle    11e3 <Comer+0x83>
mov    -0xc(%rbp),%eax
mov    0x2e43(%rip),%rcx
mov    (%rcx),%ecx
sub    %eax,%ecx
mov    0x2e38(%rip),%rax
mov    %ecx,(%rax)
mov    -0x4(%rbp),%esi
mov    -0xc(%rbp),%edx
mov    0x2e29(%rip),%rax
mov    (%rax),%ecx
add    -0xc(%rbp),%ecx
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2df7(%rip),%rdi
call   1060 <pthread_mutex_unlock@plt>
jmp    1218 <Comer+0xb8>
mov    -0x4(%rbp),%esi
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2ddd(%rip),%rdi
mov    0x2dce(%rip),%rsi
call   1030 <pthread_cond_wait@plt>
mov    0x2dc2(%rip),%rdi
call   1060 <pthread_mutex_unlock@plt>
jmp    117d <Comer+0x1d>
mov    $0x0,%al
call   1050 <random@plt>
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %edx,-0x8(%rbp)
mov    -0x4(%rbp),%esi
mov    -0x8(%rbp),%edx
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1070 <sleep@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Comer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r14d
xor    %eax,%eax
call   1050 <random@plt>
movslq %eax,%rbx
imul   $0x66666667,%rbx,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x21,%rax
add    %ecx,%eax
lea    (%rax,%rax,4),%eax
sub    %eax,%ebx
mov    0x2e38(%rip),%rdi
call   1080 <pthread_mutex_lock@plt>
mov    0x2e44(%rip),%r13
mov    0x0(%r13),%ecx
cmp    %ebx,%ecx
jg     1200 <Comer+0xa0>
lea    0xe73(%rip),%r15
mov    0x2e1e(%rip),%r12
mov    0x2e0f(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r12,%rdi
mov    %rbp,%rsi
call   1030 <pthread_cond_wait@plt>
mov    %rbp,%rdi
call   1060 <pthread_mutex_unlock@plt>
mov    %rbp,%rdi
call   1080 <pthread_mutex_lock@plt>
mov    0x0(%r13),%ecx
cmp    %ebx,%ecx
jle    11d0 <Comer+0x70>
mov    %ecx,%eax
sub    %ebx,%eax
mov    %eax,0x0(%r13)
lea    0xdf1(%rip),%rdi
mov    %r14d,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2dae(%rip),%rdi
call   1060 <pthread_mutex_unlock@plt>
xor    %eax,%eax
call   1050 <random@plt>
movslq %eax,%rbx
imul   $0x66666667,%rbx,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x22,%rax
add    %ecx,%eax
add    %eax,%eax
lea    (%rax,%rax,4),%eax
sub    %eax,%ebx
lea    0xdff(%rip),%rdi
mov    %r14d,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <sleep@plt>

LLVM IR (-O0):
@cmutex = external global i32
@comida = external global i32
@.str = [38 x i8] c"Filosofo %d comera %d unidades de %d\0A\00"
@.str.1 = [44 x i8] c"Filosofo %d se bloquea en espera de comida\0A\00"
@no_comida = external global i32
@.str.2 = [34 x i8] c"Filosofo %d comiendo %d segundos\0A\00"
define void @Comer(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = call i32 (...) @random()
%6 = srem i32 %5, 5
store i32 %6, i32* %4
br label %7
7: ; preds = %23, %1
%8 = call i32 @pthread_mutex_lock(i32* @cmutex)
%9 = load i32, i32* @comida
%10 = load i32, i32* %4
%11 = icmp sgt i32 %9, %10
br i1 %11, label %12, label %23
12: ; preds = %7
%13 = load i32, i32* %4
%14 = load i32, i32* @comida
%15 = sub nsw i32 %14, %13
store i32 %15, i32* @comida
%16 = load i32, i32* %2
%17 = load i32, i32* %4
%18 = load i32, i32* @comida
%19 = load i32, i32* %4
%20 = add nsw i32 %18, %19
%21 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %16, i32 %17, i32 %20)
%22 = call i32 @pthread_mutex_unlock(i32* @cmutex)
br label %28
23: ; preds = %7
%24 = load i32, i32* %2
%25 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i32 %24)
%26 = call i32 @pthread_cond_wait(i32* @no_comida, i32* @cmutex)
%27 = call i32 @pthread_mutex_unlock(i32* @cmutex)
br label %7
28: ; preds = %12
%29 = call i32 (...) @random()
%30 = srem i32 %29, 10
store i32 %30, i32* %3
%31 = load i32, i32* %2
%32 = load i32, i32* %3
%33 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %31, i32 %32)
%34 = load i32, i32* %3
%35 = call i32 @sleep(i32 %34)
ret void
}

LLVM IR (-O2):
@cmutex = external global i32
@comida = external global i32
@.str = [38 x i8] c"Filosofo %d comera %d unidades de %d\0A\00"
@.str.1 = [44 x i8] c"Filosofo %d se bloquea en espera de comida\0A\00"
@no_comida = external global i32
@.str.2 = [34 x i8] c"Filosofo %d comiendo %d segundos\0A\00"
define void @Comer(i32 %0) {
%2 = tail call i32 (...) @random()
%3 = srem i32 %2, 5
%4 = tail call i32 @pthread_mutex_lock(i32* @cmutex)
%5 = load i32, i32* @comida
%6 = icmp sgt i32 %5, %3
br i1 %6, label %7, label %16
7: ; preds = %16, %1
%8 = phi i32 [ %5, %1 ], [ %21, %16 ]
%9 = sub nsw i32 %8, %3
store i32 %9, i32* @comida
%10 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %3, i32 %8)
%11 = tail call i32 @pthread_mutex_unlock(i32* @cmutex)
%12 = tail call i32 (...) @random()
%13 = srem i32 %12, 10
%14 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i32 %0, i32 %13)
%15 = tail call i32 @sleep(i32 %13)
ret void
16: ; preds = %1, %16
%17 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%18 = tail call i32 @pthread_cond_wait(i32* @no_comida, i32* @cmutex)
%19 = tail call i32 @pthread_mutex_unlock(i32* @cmutex)
%20 = tail call i32 @pthread_mutex_lock(i32* @cmutex)
%21 = load i32, i32* @comida
%22 = icmp sgt i32 %21, %3
br i1 %22, label %7, label %16
}