#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 O_CREAT;
int O_RDWR;
int /*<<< orphan*/ *SEM_FAILED;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ *sem_open(char *, int, int, int);
int /*<<< orphan*/ sem_post(int /*<<< orphan*/ *);
int /*<<< orphan*/ sem_wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int main(void) {
  sem_t *sem;
  int i;
  sem = sem_open("/essai", O_RDWR | O_CREAT, 0666, 1);
  if (sem == SEM_FAILED) {
    perror("sem_open");
    exit(1);
  }
  for (i = 0; i < 3; i++) {
    fprintf(stderr, "[%d] attente\n", getpid());
    sem_wait(sem);
    fprintf(stderr, "      [%d] tient le semapore\n", getpid());
    sleep(2);
    fprintf(stderr, "   [%d] lache le semaphore\n", getpid());
    sem_post(sem);
    sleep(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
mov    0x2e59(%rip),%rax
or     (%rax),%esi
lea    0xe58(%rip),%rdi
mov    $0x1b6,%edx
mov    $0x1,%ecx
call   1060 <sem_open@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2e12(%rip),%rcx
cmp    (%rcx),%rax
jne    11e5 <main+0x65>
lea    0xe31(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x3,-0x14(%rbp)
jge    1296 <main+0x116>
mov    0x2dcb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x20(%rbp),%edi
mov    %eax,%edx
lea    0xdfb(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <sem_wait@plt>
mov    0x2d9e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x1c(%rbp),%edi
mov    %eax,%edx
lea    0xddc(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x2,%edi
call   10a0 <sleep@plt>
mov    0x2d70(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x18(%rbp),%edi
mov    %eax,%edx
lea    0xdcc(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <sem_post@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11ec <main+0x6c>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e4e(%rip),%rax
mov    0x2e5f(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe5c(%rip),%rdi
mov    $0x1b6,%edx
mov    $0x1,%ecx
call   1060 <sem_open@plt>
mov    %rax,%rbx
mov    0x2e1b(%rip),%rax
cmp    (%rax),%rbx
je     132e <main+0x1ae>
mov    0x2dfb(%rip),%r13
mov    0x0(%r13),%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe31(%rip),%r14
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <sem_wait@plt>
mov    0x0(%r13),%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe19(%rip),%r15
mov    %ebp,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1070 <fprintf@plt>
mov    $0x2,%edi
call   10a0 <sleep@plt>
mov    0x0(%r13),%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe0f(%rip),%r12
mov    %ebp,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   1050 <sem_post@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    0x0(%r13),%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <sem_wait@plt>
mov    0x0(%r13),%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1070 <fprintf@plt>
mov    $0x2,%edi
call   10a0 <sleep@plt>
mov    0x0(%r13),%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   1050 <sem_post@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    0x0(%r13),%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <sem_wait@plt>
mov    0x0(%r13),%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1070 <fprintf@plt>
mov    $0x2,%edi
call   10a0 <sleep@plt>
mov    0x0(%r13),%ebp
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1070 <fprintf@plt>
mov    %rbx,%rdi
call   1050 <sem_post@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcd2(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"/essai\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@SEM_FAILED = global i32* null
@.str.1 = [9 x i8] c"sem_open\00"
@stderr = global i32 0
@.str.2 = [14 x i8] c"[%d] attente\0A\00"
@.str.3 = [30 x i8] c" [%d] tient le semapore\0A\00"
@.str.4 = [28 x i8] c" [%d] lache le semaphore\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
store i32 0, i32* %1
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_CREAT
%6 = or i32 %4, %5
%7 = call i32* @sem_open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %6, i32 438, i32 1)
store i32* %7, i32** %2
%8 = load i32*, i32** %2
%9 = load i32*, i32** @SEM_FAILED
%10 = icmp eq i32* %8, %9
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %0
store i32 0, i32* %3
br label %15
15: ; preds = %34, %14
%16 = load i32, i32* %3
%17 = icmp slt i32 %16, 3
br i1 %17, label %18, label %37
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = call i32 (...) @getpid()
%21 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %20)
%22 = load i32*, i32** %2
%23 = call i32 @sem_wait(i32* %22)
%24 = load i32, i32* @stderr
%25 = call i32 (...) @getpid()
%26 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %25)
%27 = call i32 @sleep(i32 2)
%28 = load i32, i32* @stderr
%29 = call i32 (...) @getpid()
%30 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %29)
%31 = load i32*, i32** %2
%32 = call i32 @sem_post(i32* %31)
%33 = call i32 @sleep(i32 1)
br label %34
34: ; preds = %18
%35 = load i32, i32* %3
%36 = add nsw i32 %35, 1
store i32 %36, i32* %3
br label %15
37: ; preds = %15
ret i32 0
}

LLVM IR (-O2):
@.str = [7 x i8] c"/essai\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@SEM_FAILED = global i32* null
@.str.1 = [9 x i8] c"sem_open\00"
@stderr = global i32 0
@.str.2 = [14 x i8] c"[%d] attente\0A\00"
@.str.3 = [30 x i8] c" [%d] tient le semapore\0A\00"
@.str.4 = [28 x i8] c" [%d] lache le semaphore\0A\00"
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = load i32, i32* @O_CREAT
%3 = or i32 %2, %1
%4 = tail call i32* @sem_open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %3, i32 438, i32 1)
%5 = load i32*, i32** @SEM_FAILED
%6 = icmp eq i32* %4, %5
br i1 %6, label %47, label %7
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = tail call i32 (...) @getpid()
%10 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%11 = tail call i32 @sem_wait(i32* %4)
%12 = load i32, i32* @stderr
%13 = tail call i32 (...) @getpid()
%14 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %13)
%15 = tail call i32 @sleep(i32 2)
%16 = load i32, i32* @stderr
%17 = tail call i32 (...) @getpid()
%18 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %17)
%19 = tail call i32 @sem_post(i32* %4)
%20 = tail call i32 @sleep(i32 1)
%21 = load i32, i32* @stderr
%22 = tail call i32 (...) @getpid()
%23 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %22)
%24 = tail call i32 @sem_wait(i32* %4)
%25 = load i32, i32* @stderr
%26 = tail call i32 (...) @getpid()
%27 = tail call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %26)
%28 = tail call i32 @sleep(i32 2)
%29 = load i32, i32* @stderr
%30 = tail call i32 (...) @getpid()
%31 = tail call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %30)
%32 = tail call i32 @sem_post(i32* %4)
%33 = tail call i32 @sleep(i32 1)
%34 = load i32, i32* @stderr
%35 = tail call i32 (...) @getpid()
%36 = tail call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %35)
%37 = tail call i32 @sem_wait(i32* %4)
%38 = load i32, i32* @stderr
%39 = tail call i32 (...) @getpid()
%40 = tail call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %39)
%41 = tail call i32 @sleep(i32 2)
%42 = load i32, i32* @stderr
%43 = tail call i32 (...) @getpid()
%44 = tail call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %43)
%45 = tail call i32 @sem_post(i32* %4)
%46 = tail call i32 @sleep(i32 1)
ret i32 0
47: ; preds = %0
%48 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%49 = tail call i32 @exit(i32 1)
unreachable
}