#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*/ EXIT_FAILURE;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ *SEM_FAILED;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, long);
scalar_t__ sem_close(int /*<<< orphan*/ *);
int /*<<< orphan*/ *sem_open(char *, int /*<<< orphan*/);
scalar_t__ sem_post(int /*<<< orphan*/ *);
scalar_t__ sem_wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ sleep(int);
int main(void) {
  sem_t *semaphore = sem_open("/semaphore_example", O_RDWR);
  if (semaphore == SEM_FAILED) {
    perror("sem_open(3) failed");
    exit(EXIT_FAILURE);
  }
  int i;
  for (i = 0; i < 10; i++) {
    if (sem_wait(semaphore) < 0) {
      perror("sem_wait(3) failed on child");
      continue;
    }
    printf("PID %ld acquired semaphore\n", (long)getpid());
    if (sem_post(semaphore) < 0) {
      perror("sem_post(3) error on child");
    }
    sleep(1);
  }
  if (sem_close(semaphore) < 0)
    perror("sem_close(3) failed");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
lea    0xe51(%rip),%rdi
call   1070 <sem_open@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2e1d(%rip),%rcx
cmp    (%rcx),%rax
jne    11e6 <main+0x56>
lea    0xe40(%rip),%rdi
call   1090 <perror@plt>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0xa,-0x14(%rbp)
jge    1268 <main+0xd8>
mov    -0x10(%rbp),%rdi
call   1030 <sem_wait@plt>
cmp    $0x0,%rax
jge    121b <main+0x8b>
lea    0xe15(%rip),%rdi
call   1090 <perror@plt>
jmp    125a <main+0xca>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %rax,%rsi
lea    0xe16(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1060 <sem_post@plt>
cmp    $0x0,%rax
jge    1250 <main+0xc0>
lea    0xe13(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11ed <main+0x5d>
mov    -0x10(%rbp),%rdi
call   1080 <sem_close@plt>
cmp    $0x0,%rax
jge    1287 <main+0xf7>
lea    0xdf7(%rip),%rdi
call   1090 <perror@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
lea    0xe58(%rip),%rdi
call   1070 <sem_open@plt>
mov    %rax,%rbx
mov    0x2e29(%rip),%rax
cmp    (%rax),%rbx
je     125c <main+0xcc>
mov    $0xa,%ebp
lea    0xe5a(%rip),%r14
lea    0xe6f(%rip),%r15
lea    0xe84(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1030 <sem_wait@plt>
test   %rax,%rax
js     121d <main+0x8d>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r15,%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1060 <sem_post@plt>
test   %rax,%rax
js     122c <main+0x9c>
mov    $0x1,%edi
call   10b0 <sleep@plt>
add    $0xffffffff,%ebp
jne    11e0 <main+0x50>
jmp    1236 <main+0xa6>
mov    %r14,%rdi
call   1090 <perror@plt>
add    $0xffffffff,%ebp
jne    11e0 <main+0x50>
jmp    1236 <main+0xa6>
mov    %r12,%rdi
call   1090 <perror@plt>
jmp    120c <main+0x7c>
mov    %rbx,%rdi
call   1080 <sem_close@plt>
test   %rax,%rax
js     124e <main+0xbe>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe24(%rip),%rdi
call   1090 <perror@plt>
jmp    1243 <main+0xb3>
lea    0xdb0(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d61(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"/semaphore_example\00"
@O_RDWR = global i32 0
@SEM_FAILED = global i32* null
@.str.1 = [19 x i8] c"sem_open(3) failed\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [28 x i8] c"sem_wait(3) failed on child\00"
@.str.3 = [28 x i8] c"PID %ld acquired semaphore\0A\00"
@.str.4 = [27 x i8] c"sem_post(3) error on child\00"
@.str.5 = [20 x i8] c"sem_close(3) failed\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 = call i32* @sem_open(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %4)
store i32* %5, i32** %2
%6 = load i32*, i32** %2
%7 = load i32*, i32** @SEM_FAILED
%8 = icmp eq i32* %6, %7
br i1 %8, label %9, label %13
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = call i32 @exit(i32 %11)
unreachable
13: ; preds = %0
store i32 0, i32* %3
br label %14
14: ; preds = %33, %13
%15 = load i32, i32* %3
%16 = icmp slt i32 %15, 10
br i1 %16, label %17, label %36
17: ; preds = %14
%18 = load i32*, i32** %2
%19 = call i64 @sem_wait(i32* %18)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %33
23: ; preds = %17
%24 = call i64 (...) @getpid()
%25 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i64 %24)
%26 = load i32*, i32** %2
%27 = call i64 @sem_post(i32* %26)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %23
%30 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
br label %31
31: ; preds = %29, %23
%32 = call i32 @sleep(i32 1)
br label %33
33: ; preds = %31, %21
%34 = load i32, i32* %3
%35 = add nsw i32 %34, 1
store i32 %35, i32* %3
br label %14
36: ; preds = %14
%37 = load i32*, i32** %2
%38 = call i64 @sem_close(i32* %37)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
br label %42
42: ; preds = %40, %36
ret i32 0
}

LLVM IR (-O2):
@.str = [19 x i8] c"/semaphore_example\00"
@O_RDWR = global i32 0
@SEM_FAILED = global i32* null
@.str.1 = [19 x i8] c"sem_open(3) failed\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [28 x i8] c"sem_wait(3) failed on child\00"
@.str.3 = [28 x i8] c"PID %ld acquired semaphore\0A\00"
@.str.4 = [27 x i8] c"sem_post(3) error on child\00"
@.str.5 = [20 x i8] c"sem_close(3) failed\00"
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = tail call i32* @sem_open(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = load i32*, i32** @SEM_FAILED
%4 = icmp eq i32* %2, %3
br i1 %4, label %5, label %9
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %0, %24
%10 = phi i32 [ %25, %24 ], [ 0, %0 ]
%11 = tail call i64 @sem_wait(i32* %2)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %24
15: ; preds = %9
%16 = tail call i64 (...) @getpid()
%17 = tail call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i64 %16)
%18 = tail call i64 @sem_post(i32* %2)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
br label %22
22: ; preds = %20, %15
%23 = tail call i32 @sleep(i32 1)
br label %24
24: ; preds = %22, %13
%25 = add nuw nsw i32 %10, 1
%26 = icmp eq i32 %25, 10
br i1 %26, label %27, label %9
27: ; preds = %24
%28 = tail call i64 @sem_close(i32* %2)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %27
%31 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
br label %32
32: ; preds = %30, %27
ret i32 0
}