#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_EXCL;
int /*<<< orphan*/ S_IRUSR;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ printf(char *, int /*<<< orphan*/);
int /*<<< orphan*/ *sem_open(char const *, int, int /*<<< orphan*/, int);
int /*<<< orphan*/ sem_post(int /*<<< orphan*/ *);
int /*<<< orphan*/ sem_unlink(char const *);
int /*<<< orphan*/ sem_wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ setbuf(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ stdout;
void incr1(int *count, int loop, const char *c) {
  int i;
  sem_t *mutex;
  mutex = sem_open(c, O_CREAT | O_EXCL, S_IRUSR, 1);
  sem_unlink(c);
  setbuf(stdout, NULL);
  if (fork() == 0) {
    for (i = 0; i < loop; i++) {
      sem_wait(mutex);
      printf("child: %d\n", (*count)++);
      sem_post(mutex);
    }
    exit(0);
  }
  for (i = 0; i < loop; i++) {
    sem_wait(mutex);
    printf("parent: %d\n", (*count)++);
    sem_post(mutex);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<incr1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
mov    0x2e31(%rip),%rax
or     (%rax),%esi
mov    0x2e20(%rip),%rax
mov    (%rax),%edx
mov    $0x1,%ecx
call   1070 <sem_open@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1090 <sem_unlink@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <setbuf@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
cmp    $0x0,%rax
jne    123f <incr1+0xbf>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1238 <incr1+0xb8>
mov    -0x28(%rbp),%rdi
call   1030 <sem_wait@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%esi
mov    %esi,%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
lea    0xde4(%rip),%rdi
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1060 <sem_post@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11f3 <incr1+0x73>
xor    %edi,%edi
call   1080 <exit@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    128b <incr1+0x10b>
mov    -0x28(%rbp),%rdi
call   1030 <sem_wait@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%esi
mov    %esi,%ecx
add    $0x1,%ecx
mov    %ecx,(%rax)
lea    0xd9c(%rip),%rdi
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1060 <sem_post@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1246 <incr1+0xc6>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<incr1>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r12
mov    %esi,%ebx
mov    %rdi,%r14
mov    0x2e61(%rip),%rax
mov    0x2e3a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e27(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
mov    $0x1,%ecx
call   1070 <sem_open@plt>
mov    %rax,%r15
mov    %r12,%rdi
call   1090 <sem_unlink@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1040 <setbuf@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
jne    1218 <incr1+0x98>
test   %ebx,%ebx
jle    1256 <incr1+0xd6>
lea    0xe16(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1030 <sem_wait@plt>
mov    (%r14),%esi
lea    0x1(%rsi),%eax
mov    %eax,(%r14)
mov    %r12,%rdi
call   1050 <printf@plt>
mov    %r15,%rdi
call   1060 <sem_post@plt>
add    $0xffffffff,%ebx
jne    11f0 <incr1+0x70>
jmp    1256 <incr1+0xd6>
test   %ebx,%ebx
jle    1256 <incr1+0xd6>
lea    0xde8(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15,%rdi
call   1030 <sem_wait@plt>
mov    (%r14),%esi
lea    0x1(%rsi),%eax
mov    %eax,(%r14)
mov    %r12,%rdi
call   1050 <printf@plt>
mov    %r15,%rdi
call   1060 <sem_post@plt>
add    $0xffffffff,%ebx
jne    1230 <incr1+0xb0>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@stdout = global i32 0
@.str = [11 x i8] c"child: %d\0A\00"
@.str.1 = [12 x i8] c"parent: %d\0A\00"
define void @incr1(i32* %0, i32 %1, i8* %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
store i32* %0, i32** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%9 = load i8*, i8** %6
%10 = load i32, i32* @O_CREAT
%11 = load i32, i32* @O_EXCL
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IRUSR
%14 = call i32* @sem_open(i8* %9, i32 %12, i32 %13, i32 1)
store i32* %14, i32** %8
%15 = load i8*, i8** %6
%16 = call i32 @sem_unlink(i8* %15)
%17 = load i32, i32* @stdout
%18 = call i32 @setbuf(i32 %17, i32* null)
%19 = call i64 (...) @fork()
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %40
21: ; preds = %3
store i32 0, i32* %7
br label %22
22: ; preds = %35, %21
%23 = load i32, i32* %7
%24 = load i32, i32* %5
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %38
26: ; preds = %22
%27 = load i32*, i32** %8
%28 = call i32 @sem_wait(i32* %27)
%29 = load i32*, i32** %4
%30 = load i32, i32* %29
%31 = add nsw i32 %30, 1
store i32 %31, i32* %29
%32 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %30)
%33 = load i32*, i32** %8
%34 = call i32 @sem_post(i32* %33)
br label %35
35: ; preds = %26
%36 = load i32, i32* %7
%37 = add nsw i32 %36, 1
store i32 %37, i32* %7
br label %22
38: ; preds = %22
%39 = call i32 @exit(i32 0)
unreachable
40: ; preds = %3
store i32 0, i32* %7
br label %41
41: ; preds = %54, %40
%42 = load i32, i32* %7
%43 = load i32, i32* %5
%44 = icmp slt i32 %42, %43
br i1 %44, label %45, label %57
45: ; preds = %41
%46 = load i32*, i32** %8
%47 = call i32 @sem_wait(i32* %46)
%48 = load i32*, i32** %4
%49 = load i32, i32* %48
%50 = add nsw i32 %49, 1
store i32 %50, i32* %48
%51 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %49)
%52 = load i32*, i32** %8
%53 = call i32 @sem_post(i32* %52)
br label %54
54: ; preds = %45
%55 = load i32, i32* %7
%56 = add nsw i32 %55, 1
store i32 %56, i32* %7
br label %41
57: ; preds = %41
%58 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@stdout = global i32 0
@.str = [11 x i8] c"child: %d\0A\00"
@.str.1 = [12 x i8] c"parent: %d\0A\00"
define void @incr1(i32* nocapture %0, i32 %1, i8* %2) {
%4 = load i32, i32* @O_CREAT
%5 = load i32, i32* @O_EXCL
%6 = or i32 %5, %4
%7 = load i32, i32* @S_IRUSR
%8 = tail call i32* @sem_open(i8* %2, i32 %6, i32 %7, i32 1)
%9 = tail call i32 @sem_unlink(i8* %2)
%10 = load i32, i32* @stdout
%11 = tail call i32 @setbuf(i32 %10, i32* null)
%12 = tail call i64 (...) @fork()
%13 = icmp eq i64 %12, 0
%14 = icmp sgt i32 %1, 0
br i1 %13, label %16, label %15
15: ; preds = %3
br i1 %14, label %28, label %37
16: ; preds = %3
br i1 %14, label %17, label %26
17: ; preds = %16, %17
%18 = phi i32 [ %24, %17 ], [ 0, %16 ]
%19 = tail call i32 @sem_wait(i32* %8)
%20 = load i32, i32* %0
%21 = add nsw i32 %20, 1
store i32 %21, i32* %0
%22 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %20)
%23 = tail call i32 @sem_post(i32* %8)
%24 = add nuw nsw i32 %18, 1
%25 = icmp eq i32 %24, %1
br i1 %25, label %26, label %17
26: ; preds = %17, %16
%27 = tail call i32 @exit(i32 0)
unreachable
28: ; preds = %15, %28
%29 = phi i32 [ %35, %28 ], [ 0, %15 ]
%30 = tail call i32 @sem_wait(i32* %8)
%31 = load i32, i32* %0
%32 = add nsw i32 %31, 1
store i32 %32, i32* %0
%33 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %31)
%34 = tail call i32 @sem_post(i32* %8)
%35 = add nuw nsw i32 %29, 1
%36 = icmp eq i32 %35, %1
br i1 %36, label %37, label %28
37: ; preds = %28, %15
%38 = tail call i32 @exit(i32 0)
unreachable
}