#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*/ printf(char *, int);
int random();
int /*<<< orphan*/ sem_signal(int);
int /*<<< orphan*/ sem_wait(int);
int /*<<< orphan*/ sleep(int);
void *f(void *a) {
  int id = *(int *)a;
  for (;;) {
    sleep(random() % 5);
    sem_wait(5);
    printf("%d dentro \n", id);
    sleep(random() % 5);
    sem_signal(5);
    printf("%d fuera \n", id);
  }
  return NULL;
}


x86 assembly (clang, -O0):
<f>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1060 <random@plt>
mov    $0x5,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
call   1070 <sleep@plt>
mov    $0x5,%edi
call   1030 <sem_wait@plt>
mov    -0xc(%rbp),%esi
lea    0xe71(%rip),%rdi
call   1050 <printf@plt>
mov    $0x0,%al
call   1060 <random@plt>
mov    $0x5,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
call   1070 <sleep@plt>
mov    $0x5,%edi
call   1040 <sem_signal@plt>
mov    -0xc(%rbp),%esi
lea    0xe4e(%rip),%rdi
call   1050 <printf@plt>
jmp    1165 <f+0x15>

x86 assembly (clang, -O2):
<f>:
push   %r15
push   %r14
push   %rbx
mov    (%rdi),%ebx
lea    0xea2(%rip),%r14
lea    0xea7(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   1060 <random@plt>
movslq %eax,%rdi
imul   $0x66666667,%rdi,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x21,%rax
add    %ecx,%eax
lea    (%rax,%rax,4),%eax
sub    %eax,%edi
call   1070 <sleep@plt>
mov    $0x5,%edi
call   1030 <sem_wait@plt>
mov    %r14,%rdi
mov    %ebx,%esi
call   1050 <printf@plt>
xor    %eax,%eax
call   1060 <random@plt>
movslq %eax,%rdi
imul   $0x66666667,%rdi,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x21,%rax
add    %ecx,%eax
lea    (%rax,%rax,4),%eax
sub    %eax,%edi
call   1070 <sleep@plt>
mov    $0x5,%edi
call   1040 <sem_signal@plt>
mov    %r15,%rdi
mov    %ebx,%esi
call   1050 <printf@plt>
jmp    1170 <f+0x20>

LLVM IR (-O0):
@.str = [12 x i8] c"%d dentro \0A\00"
@.str.1 = [11 x i8] c"%d fuera \0A\00"
define i8* @f(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = bitcast i8* %4 to i32*
%6 = load i32, i32* %5
store i32 %6, i32* %3
br label %7
7: ; preds = %7, %1
%8 = call i32 (...) @random()
%9 = srem i32 %8, 5
%10 = call i32 @sleep(i32 %9)
%11 = call i32 @sem_wait(i32 5)
%12 = load i32, i32* %3
%13 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = call i32 (...) @random()
%15 = srem i32 %14, 5
%16 = call i32 @sleep(i32 %15)
%17 = call i32 @sem_signal(i32 5)
%18 = load i32, i32* %3
%19 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %18)
br label %7
}

LLVM IR (-O2):
@.str = [12 x i8] c"%d dentro \0A\00"
@.str.1 = [11 x i8] c"%d fuera \0A\00"
define noalias i8* @f(i8* nocapture %0) {
%2 = bitcast i8* %0 to i32*
%3 = load i32, i32* %2
br label %4
4: ; preds = %4, %1
%5 = tail call i32 (...) @random()
%6 = srem i32 %5, 5
%7 = tail call i32 @sleep(i32 %6)
%8 = tail call i32 @sem_wait(i32 5)
%9 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %3)
%10 = tail call i32 (...) @random()
%11 = srem i32 %10, 5
%12 = tail call i32 @sleep(i32 %11)
%13 = tail call i32 @sem_signal(i32 5)
%14 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %3)
br label %4
}