#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 */

/* Variables and functions */
int /*<<< orphan*/ free_left(int);
int /*<<< orphan*/ free_right(int);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ printf(char *, int);
int rand();
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ wait_for_left(int);
int /*<<< orphan*/ wait_for_right(int);
void philosopher(int no) {
  srand(getpid());
  for (;;) {
    printf("%d is thinking...\n", no);
    sleep(rand() % 5 + 1);
    printf("%d is hungry\n", no);
    wait_for_left(no);
    sleep(rand() % 5 + 1);
    wait_for_right(no);
    printf("%d is eating\n", no);
    sleep(rand() % 3 + 1);
    free_left(no);
    sleep(rand() % 5 + 1);
    free_right(no);
  }
}


x86 assembly (clang, -O0):
<philosopher>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%edi
call   1050 <srand@plt>
mov    -0x4(%rbp),%esi
lea    0xe4d(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   10b0 <rand@plt>
mov    $0x5,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
add    $0x1,%edi
mov    %edi,-0x14(%rbp)
call   1090 <sleep@plt>
mov    -0x4(%rbp),%esi
lea    0xe35(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
call   1080 <wait_for_left@plt>
mov    $0x0,%al
call   10b0 <rand@plt>
mov    $0x5,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
add    $0x1,%edi
mov    %edi,-0x10(%rbp)
call   1090 <sleep@plt>
mov    -0x4(%rbp),%edi
call   10a0 <wait_for_right@plt>
mov    -0x4(%rbp),%esi
lea    0xe08(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   10b0 <rand@plt>
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
add    $0x1,%edi
mov    %edi,-0xc(%rbp)
call   1090 <sleep@plt>
mov    -0x4(%rbp),%edi
call   1060 <free_left@plt>
mov    $0x0,%al
call   10b0 <rand@plt>
mov    $0x5,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
add    $0x1,%edi
mov    %edi,-0x8(%rbp)
call   1090 <sleep@plt>
mov    -0x4(%rbp),%edi
call   1070 <free_right@plt>
jmp    11a9 <philosopher+0x19>

x86 assembly (clang, -O2):
<philosopher>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %edi,%ebx
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%edi
call   1050 <srand@plt>
lea    0xe51(%rip),%r14
lea    0xe5d(%rip),%r15
lea    0xe64(%rip),%r12
nopl   (%rax)
mov    %r14,%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   10b0 <rand@plt>
cltq
imul   $0x66666667,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
shr    $0x20,%rcx
sar    %ecx
add    %edx,%ecx
lea    (%rcx,%rcx,4),%ecx
neg    %ecx
lea    (%rax,%rcx,1),%edi
add    $0x1,%edi
call   1090 <sleep@plt>
mov    %r15,%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
mov    %ebx,%edi
call   1080 <wait_for_left@plt>
xor    %eax,%eax
call   10b0 <rand@plt>
cltq
imul   $0x66666667,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
shr    $0x20,%rcx
sar    %ecx
add    %edx,%ecx
lea    (%rcx,%rcx,4),%ecx
neg    %ecx
lea    (%rax,%rcx,1),%edi
add    $0x1,%edi
call   1090 <sleep@plt>
mov    %ebx,%edi
call   10a0 <wait_for_right@plt>
mov    %r12,%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   10b0 <rand@plt>
cltq
imul   $0x55555556,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
shr    $0x20,%rcx
add    %edx,%ecx
lea    (%rcx,%rcx,2),%ecx
neg    %ecx
lea    (%rax,%rcx,1),%edi
add    $0x1,%edi
call   1090 <sleep@plt>
mov    %ebx,%edi
call   1060 <free_left@plt>
xor    %eax,%eax
call   10b0 <rand@plt>
cltq
imul   $0x66666667,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x21,%rcx
add    %edx,%ecx
lea    (%rcx,%rcx,4),%ecx
neg    %ecx
lea    (%rax,%rcx,1),%edi
add    $0x1,%edi
call   1090 <sleep@plt>
mov    %ebx,%edi
call   1070 <free_right@plt>
jmp    11c0 <philosopher+0x30>

LLVM IR (-O0):
@.str = [19 x i8] c"%d is thinking...\0A\00"
@.str.1 = [14 x i8] c"%d is hungry\0A\00"
@.str.2 = [14 x i8] c"%d is eating\0A\00"
define void @philosopher(i32 %0) {
%2 = alloca i32
store i32 %0, i32* %2
%3 = call i32 (...) @getpid()
%4 = call i32 @srand(i32 %3)
br label %5
5: ; preds = %5, %1
%6 = load i32, i32* %2
%7 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = call i32 (...) @rand()
%9 = srem i32 %8, 5
%10 = add nsw i32 %9, 1
%11 = call i32 @sleep(i32 %10)
%12 = load i32, i32* %2
%13 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = load i32, i32* %2
%15 = call i32 @wait_for_left(i32 %14)
%16 = call i32 (...) @rand()
%17 = srem i32 %16, 5
%18 = add nsw i32 %17, 1
%19 = call i32 @sleep(i32 %18)
%20 = load i32, i32* %2
%21 = call i32 @wait_for_right(i32 %20)
%22 = load i32, i32* %2
%23 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %22)
%24 = call i32 (...) @rand()
%25 = srem i32 %24, 3
%26 = add nsw i32 %25, 1
%27 = call i32 @sleep(i32 %26)
%28 = load i32, i32* %2
%29 = call i32 @free_left(i32 %28)
%30 = call i32 (...) @rand()
%31 = srem i32 %30, 5
%32 = add nsw i32 %31, 1
%33 = call i32 @sleep(i32 %32)
%34 = load i32, i32* %2
%35 = call i32 @free_right(i32 %34)
br label %5
}

LLVM IR (-O2):
@.str = [19 x i8] c"%d is thinking...\0A\00"
@.str.1 = [14 x i8] c"%d is hungry\0A\00"
@.str.2 = [14 x i8] c"%d is eating\0A\00"
define void @philosopher(i32 %0) {
%2 = tail call i32 (...) @getpid()
%3 = tail call i32 @srand(i32 %2)
br label %4
4: ; preds = %4, %1
%5 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = tail call i32 (...) @rand()
%7 = srem i32 %6, 5
%8 = add nsw i32 %7, 1
%9 = tail call i32 @sleep(i32 %8)
%10 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%11 = tail call i32 @wait_for_left(i32 %0)
%12 = tail call i32 (...) @rand()
%13 = srem i32 %12, 5
%14 = add nsw i32 %13, 1
%15 = tail call i32 @sleep(i32 %14)
%16 = tail call i32 @wait_for_right(i32 %0)
%17 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %0)
%18 = tail call i32 (...) @rand()
%19 = srem i32 %18, 3
%20 = add nsw i32 %19, 1
%21 = tail call i32 @sleep(i32 %20)
%22 = tail call i32 @free_left(i32 %0)
%23 = tail call i32 (...) @rand()
%24 = srem i32 %23, 5
%25 = add nsw i32 %24, 1
%26 = tail call i32 @sleep(i32 %25)
%27 = tail call i32 @free_right(i32 %0)
br label %4
}