#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*/ EXIT_SUCCESS;
int MAP_ANONYMOUS;
int *MAP_FAILED;
int MAP_SHARED;
int O_CREAT;
int O_EXCL;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ *SEM_FAILED;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int atoi(char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int *mmap(int /*<<< orphan*/ *, int, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ *sem_open(char *, int, int, int);
int sem_post(int /*<<< orphan*/ *);
int sem_unlink(char *);
int sem_wait(int /*<<< orphan*/ *);
int /*<<< orphan*/ setbuf(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ stdout;
char *strerror(int /*<<< orphan*/);
int main(int argc, char **argv) {
  int i, nloop;
  int *ptr;
  sem_t *sem;
  if (2 != argc) {
    printf("usage: %s  <#loops>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  nloop = atoi(argv[1]);
  if ((ptr = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE,
                  MAP_SHARED | MAP_ANONYMOUS, -1, 0)) == MAP_FAILED) {
    printf("mmap error: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  if ((sem = sem_open("/semtest", O_CREAT | O_EXCL,
                      (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH), 1)) ==
      SEM_FAILED) {
    printf("sem_open error: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  if (sem_unlink("/semtest") == -1) {
    printf("sem_unlink error: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  setbuf(stdout, NULL);
  if (fork() == 0) {
    for (i = 0; i < nloop; ++i) {
      if (sem_wait(sem) == -1) {
        printf("sem_wait child: error: %s\n", strerror(errno));
        exit(EXIT_FAILURE);
      }
      printf("child: %d\n", (*ptr)++);
      if (sem_post(sem) == -1) {
        printf("sem_post child error: %s\n", strerror(errno));
        exit(EXIT_FAILURE);
      }
    }
    exit(EXIT_SUCCESS);
  }
  for (i = 0; i < nloop; ++i) {
    if (sem_wait(sem) == -1) {
      printf("sem_wait parent error: %s\n", strerror(errno));
      exit(EXIT_FAILURE);
    }
    printf("parent: %d\n", (*ptr)++);
    if (sem_post(sem) == -1) {
      printf("sem_post parent error: %s\n", strerror(errno));
      exit(EXIT_FAILURE);
    }
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x2,%eax
cmp    -0x8(%rbp),%eax
je     11f5 <main+0x45>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe1e(%rip),%rdi
call   1060 <printf@plt>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    0x2de4(%rip),%rax
mov    (%rax),%edx
mov    0x2d73(%rip),%rax
or     (%rax),%edx
mov    0x2dc2(%rip),%rax
mov    (%rax),%ecx
mov    0x2d99(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0x4,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x20(%rbp)
mov    0x2d1d(%rip),%rcx
cmp    (%rcx),%rax
jne    127f <main+0xcf>
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xda9(%rip),%rdi
call   1060 <printf@plt>
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2d72(%rip),%rax
mov    (%rax),%esi
mov    0x2d21(%rip),%rax
or     (%rax),%esi
mov    0x2ce8(%rip),%rax
mov    (%rax),%edx
mov    0x2d27(%rip),%rax
or     (%rax),%edx
mov    0x2cf6(%rip),%rax
or     (%rax),%edx
mov    0x2ce5(%rip),%rax
or     (%rax),%edx
lea    0xd69(%rip),%rdi
mov    $0x1,%ecx
call   1080 <sem_open@plt>
mov    %rax,-0x28(%rbp)
mov    0x2cff(%rip),%rcx
cmp    (%rcx),%rax
jne    1305 <main+0x155>
mov    0x2cc7(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xd3c(%rip),%rdi
call   1060 <printf@plt>
mov    0x2c92(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xd19(%rip),%rdi
call   10c0 <sem_unlink@plt>
cmp    $0xffffffff,%eax
jne    1345 <main+0x195>
mov    0x2c87(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xd10(%rip),%rdi
call   1060 <printf@plt>
mov    0x2c52(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2c2c(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <setbuf@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
cmp    $0x0,%rax
jne    1432 <main+0x282>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1424 <main+0x274>
mov    -0x28(%rbp),%rdi
call   1030 <sem_wait@plt>
cmp    $0xffffffff,%eax
jne    13b8 <main+0x208>
mov    0x2c14(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xcb3(%rip),%rdi
call   1060 <printf@plt>
mov    0x2bdf(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rcx
mov    (%rcx),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,(%rcx)
movslq %eax,%rsi
lea    0xca4(%rip),%rdi
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1070 <sem_post@plt>
cmp    $0xffffffff,%eax
jne    1411 <main+0x261>
mov    0x2bbb(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xc80(%rip),%rdi
call   1060 <printf@plt>
mov    0x2b86(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    1416 <main+0x266>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    136f <main+0x1bf>
mov    0x2b45(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    14ee <main+0x33e>
mov    -0x28(%rbp),%rdi
call   1030 <sem_wait@plt>
cmp    $0xffffffff,%eax
jne    1482 <main+0x2d2>
mov    0x2b4a(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xc29(%rip),%rdi
call   1060 <printf@plt>
mov    0x2b15(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rcx
mov    (%rcx),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,(%rcx)
movslq %eax,%rsi
lea    0xc1a(%rip),%rdi
call   1060 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1070 <sem_post@plt>
cmp    $0xffffffff,%eax
jne    14db <main+0x32b>
mov    0x2af1(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rsi
lea    0xbf7(%rip),%rdi
call   1060 <printf@plt>
mov    0x2abc(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    14e0 <main+0x330>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1439 <main+0x289>
mov    0x2a7b(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    122a <main+0x7a>
mov    0x8(%rsi),%rdi
call   1090 <atoi@plt>
mov    %eax,%ebx
mov    0x2e21(%rip),%rax
mov    0x2db2(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dff(%rip),%rax
mov    0x2dd8(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    $0x4,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%r14
mov    0x2d5d(%rip),%rax
cmp    (%rax),%r14
jne    1239 <main+0x89>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdf0(%rip),%rdi
jmp    12cc <main+0x11c>
mov    (%rsi),%rsi
lea    0xdcc(%rip),%rdi
jmp    12cf <main+0x11f>
mov    0x2db8(%rip),%rax
mov    0x2d69(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d2e(%rip),%rax
mov    0x2d6f(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d3c(%rip),%rax
or     (%rax),%edx
mov    0x2d2b(%rip),%rax
or     (%rax),%edx
lea    0xdaf(%rip),%rdi
mov    $0x1,%ecx
call   1080 <sem_open@plt>
mov    %rax,%r15
mov    0x2d46(%rip),%rax
cmp    (%rax),%r15
jne    12a6 <main+0xf6>
mov    0x2d12(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd8a(%rip),%rdi
jmp    12cc <main+0x11c>
lea    0xd78(%rip),%rdi
call   10c0 <sem_unlink@plt>
cmp    $0xffffffff,%eax
jne    12e2 <main+0x132>
mov    0x2cea(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd76(%rip),%rdi
mov    %rax,%rsi
call   1060 <printf@plt>
mov    0x2cb5(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2c8f(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1050 <setbuf@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
jne    1342 <main+0x192>
test   %ebx,%ebx
jle    1380 <main+0x1d0>
lea    0xd66(%rip),%r12
nopl   (%rax)
mov    %r15,%rdi
call   1030 <sem_wait@plt>
cmp    $0xffffffff,%eax
je     138e <main+0x1de>
movslq (%r14),%rsi
lea    0x1(%rsi),%eax
mov    %eax,(%r14)
mov    %r12,%rdi
call   1060 <printf@plt>
mov    %r15,%rdi
call   1070 <sem_post@plt>
cmp    $0xffffffff,%eax
je     13a8 <main+0x1f8>
add    $0xffffffff,%ebx
jne    1310 <main+0x160>
jmp    1380 <main+0x1d0>
test   %ebx,%ebx
jle    1380 <main+0x1d0>
lea    0xd66(%rip),%r12
nopl   (%rax)
mov    %r15,%rdi
call   1030 <sem_wait@plt>
cmp    $0xffffffff,%eax
je     13c2 <main+0x212>
movslq (%r14),%rsi
lea    0x1(%rsi),%eax
mov    %eax,(%r14)
mov    %r12,%rdi
call   1060 <printf@plt>
mov    %r15,%rdi
call   1070 <sem_post@plt>
cmp    $0xffffffff,%eax
je     13dc <main+0x22c>
add    $0xffffffff,%ebx
jne    1350 <main+0x1a0>
mov    0x2be9(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2c13(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xcb5(%rip),%rdi
jmp    12cc <main+0x11c>
mov    0x2bf9(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xcc1(%rip),%rdi
jmp    12cc <main+0x11c>
mov    0x2bdf(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xcc1(%rip),%rdi
jmp    12cc <main+0x11c>
mov    0x2bc5(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xcce(%rip),%rdi
jmp    12cc <main+0x11c>

LLVM IR (-O0):
@.str = [21 x i8] c"usage: %s <#loops>\0A\00"
@EXIT_FAILURE = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_FAILED = global i32* null
@.str.1 = [16 x i8] c"mmap error: %s\0A\00"
@errno = global i32 0
@.str.2 = [9 x i8] c"/semtest\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@SEM_FAILED = global i32* null
@.str.3 = [20 x i8] c"sem_open error: %s\0A\00"
@.str.4 = [22 x i8] c"sem_unlink error: %s\0A\00"
@stdout = global i32 0
@.str.5 = [27 x i8] c"sem_wait child: error: %s\0A\00"
@.str.6 = [11 x i8] c"child: %d\0A\00"
@.str.7 = [26 x i8] c"sem_post child error: %s\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.8 = [27 x i8] c"sem_wait parent error: %s\0A\00"
@.str.9 = [12 x i8] c"parent: %d\0A\00"
@.str.10 = [27 x i8] c"sem_post parent error: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 2, %10
br i1 %11, label %12, label %19
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @atoi(i8* %22)
store i32 %23, i32* %7
%24 = load i32, i32* @PROT_READ
%25 = load i32, i32* @PROT_WRITE
%26 = or i32 %24, %25
%27 = load i32, i32* @MAP_SHARED
%28 = load i32, i32* @MAP_ANONYMOUS
%29 = or i32 %27, %28
%30 = call i32* @mmap(i32* null, i32 4, i32 %26, i32 %29, i32 -1, i32 0)
store i32* %30, i32** %8
%31 = load i32*, i32** @MAP_FAILED
%32 = icmp eq i32* %30, %31
br i1 %32, label %33, label %39
33: ; preds = %19
%34 = load i32, i32* @errno
%35 = call i8* @strerror(i32 %34)
%36 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %35)
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %19
%40 = load i32, i32* @O_CREAT
%41 = load i32, i32* @O_EXCL
%42 = or i32 %40, %41
%43 = load i32, i32* @S_IRUSR
%44 = load i32, i32* @S_IWUSR
%45 = or i32 %43, %44
%46 = load i32, i32* @S_IRGRP
%47 = or i32 %45, %46
%48 = load i32, i32* @S_IROTH
%49 = or i32 %47, %48
%50 = call i32* @sem_open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %42, i32 %49, i32 1)
store i32* %50, i32** %9
%51 = load i32*, i32** @SEM_FAILED
%52 = icmp eq i32* %50, %51
br i1 %52, label %53, label %59
53: ; preds = %39
%54 = load i32, i32* @errno
%55 = call i8* @strerror(i32 %54)
%56 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %55)
%57 = load i32, i32* @EXIT_FAILURE
%58 = call i32 @exit(i32 %57)
unreachable
59: ; preds = %39
%60 = call i32 @sem_unlink(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %68
62: ; preds = %59
%63 = load i32, i32* @errno
%64 = call i8* @strerror(i32 %63)
%65 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %64)
%66 = load i32, i32* @EXIT_FAILURE
%67 = call i32 @exit(i32 %66)
unreachable
68: ; preds = %59
%69 = load i32, i32* @stdout
%70 = call i32 @setbuf(i32 %69, i32* null)
%71 = call i64 (...) @fork()
%72 = icmp eq i64 %71, 0
br i1 %72, label %73, label %111
73: ; preds = %68
store i32 0, i32* %6
br label %74
74: ; preds = %105, %73
%75 = load i32, i32* %6
%76 = load i32, i32* %7
%77 = icmp slt i32 %75, %76
br i1 %77, label %78, label %108
78: ; preds = %74
%79 = load i32*, i32** %9
%80 = call i32 @sem_wait(i32* %79)
%81 = icmp eq i32 %80, -1
br i1 %81, label %82, label %88
82: ; preds = %78
%83 = load i32, i32* @errno
%84 = call i8* @strerror(i32 %83)
%85 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i8* %84)
%86 = load i32, i32* @EXIT_FAILURE
%87 = call i32 @exit(i32 %86)
unreachable
88: ; preds = %78
%89 = load i32*, i32** %8
%90 = load i32, i32* %89
%91 = add nsw i32 %90, 1
store i32 %91, i32* %89
%92 = sext i32 %90 to i64
%93 = inttoptr i64 %92 to i8*
%94 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i8* %93)
%95 = load i32*, i32** %9
%96 = call i32 @sem_post(i32* %95)
%97 = icmp eq i32 %96, -1
br i1 %97, label %98, label %104
98: ; preds = %88
%99 = load i32, i32* @errno
%100 = call i8* @strerror(i32 %99)
%101 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0), i8* %100)
%102 = load i32, i32* @EXIT_FAILURE
%103 = call i32 @exit(i32 %102)
unreachable
104: ; preds = %88
br label %105
105: ; preds = %104
%106 = load i32, i32* %6
%107 = add nsw i32 %106, 1
store i32 %107, i32* %6
br label %74
108: ; preds = %74
%109 = load i32, i32* @EXIT_SUCCESS
%110 = call i32 @exit(i32 %109)
unreachable
111: ; preds = %68
store i32 0, i32* %6
br label %112
112: ; preds = %143, %111
%113 = load i32, i32* %6
%114 = load i32, i32* %7
%115 = icmp slt i32 %113, %114
br i1 %115, label %116, label %146
116: ; preds = %112
%117 = load i32*, i32** %9
%118 = call i32 @sem_wait(i32* %117)
%119 = icmp eq i32 %118, -1
br i1 %119, label %120, label %126
120: ; preds = %116
%121 = load i32, i32* @errno
%122 = call i8* @strerror(i32 %121)
%123 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.8, i64 0, i64 0), i8* %122)
%124 = load i32, i32* @EXIT_FAILURE
%125 = call i32 @exit(i32 %124)
unreachable
126: ; preds = %116
%127 = load i32*, i32** %8
%128 = load i32, i32* %127
%129 = add nsw i32 %128, 1
store i32 %129, i32* %127
%130 = sext i32 %128 to i64
%131 = inttoptr i64 %130 to i8*
%132 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.9, i64 0, i64 0), i8* %131)
%133 = load i32*, i32** %9
%134 = call i32 @sem_post(i32* %133)
%135 = icmp eq i32 %134, -1
br i1 %135, label %136, label %142
136: ; preds = %126
%137 = load i32, i32* @errno
%138 = call i8* @strerror(i32 %137)
%139 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.10, i64 0, i64 0), i8* %138)
%140 = load i32, i32* @EXIT_FAILURE
%141 = call i32 @exit(i32 %140)
unreachable
142: ; preds = %126
br label %143
143: ; preds = %142
%144 = load i32, i32* %6
%145 = add nsw i32 %144, 1
store i32 %145, i32* %6
br label %112
146: ; preds = %112
%147 = load i32, i32* @EXIT_SUCCESS
%148 = call i32 @exit(i32 %147)
unreachable
}

LLVM IR (-O2):
@.str = [21 x i8] c"usage: %s <#loops>\0A\00"
@EXIT_FAILURE = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_FAILED = global i32* null
@.str.1 = [16 x i8] c"mmap error: %s\0A\00"
@errno = global i32 0
@.str.2 = [9 x i8] c"/semtest\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@SEM_FAILED = global i32* null
@.str.3 = [20 x i8] c"sem_open error: %s\0A\00"
@.str.4 = [22 x i8] c"sem_unlink error: %s\0A\00"
@stdout = global i32 0
@.str.5 = [27 x i8] c"sem_wait child: error: %s\0A\00"
@.str.6 = [11 x i8] c"child: %d\0A\00"
@.str.7 = [26 x i8] c"sem_post child error: %s\0A\00"
@EXIT_SUCCESS = global i32 0
@.str.8 = [27 x i8] c"sem_wait parent error: %s\0A\00"
@.str.9 = [12 x i8] c"parent: %d\0A\00"
@.str.10 = [27 x i8] c"sem_post parent error: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %9, label %4
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %5)
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @exit(i32 %7)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32 @atoi(i8* %11)
%13 = load i32, i32* @PROT_READ
%14 = load i32, i32* @PROT_WRITE
%15 = or i32 %14, %13
%16 = load i32, i32* @MAP_SHARED
%17 = load i32, i32* @MAP_ANONYMOUS
%18 = or i32 %17, %16
%19 = tail call i32* @mmap(i32* null, i32 4, i32 %15, i32 %18, i32 -1, i32 0)
%20 = load i32*, i32** @MAP_FAILED
%21 = icmp eq i32* %19, %20
br i1 %21, label %22, label %28
22: ; preds = %9
%23 = load i32, i32* @errno
%24 = tail call i8* @strerror(i32 %23)
%25 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %24)
%26 = load i32, i32* @EXIT_FAILURE
%27 = tail call i32 @exit(i32 %26)
unreachable
28: ; preds = %9
%29 = load i32, i32* @O_CREAT
%30 = load i32, i32* @O_EXCL
%31 = or i32 %30, %29
%32 = load i32, i32* @S_IRUSR
%33 = load i32, i32* @S_IWUSR
%34 = or i32 %33, %32
%35 = load i32, i32* @S_IRGRP
%36 = or i32 %34, %35
%37 = load i32, i32* @S_IROTH
%38 = or i32 %36, %37
%39 = tail call i32* @sem_open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32 %31, i32 %38, i32 1)
%40 = load i32*, i32** @SEM_FAILED
%41 = icmp eq i32* %39, %40
br i1 %41, label %42, label %48
42: ; preds = %28
%43 = load i32, i32* @errno
%44 = tail call i8* @strerror(i32 %43)
%45 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %44)
%46 = load i32, i32* @EXIT_FAILURE
%47 = tail call i32 @exit(i32 %46)
unreachable
48: ; preds = %28
%49 = tail call i32 @sem_unlink(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %57
51: ; preds = %48
%52 = load i32, i32* @errno
%53 = tail call i8* @strerror(i32 %52)
%54 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %53)
%55 = load i32, i32* @EXIT_FAILURE
%56 = tail call i32 @exit(i32 %55)
unreachable
57: ; preds = %48
%58 = load i32, i32* @stdout
%59 = tail call i32 @setbuf(i32 %58, i32* null)
%60 = tail call i64 (...) @fork()
%61 = icmp eq i64 %60, 0
%62 = icmp sgt i32 %12, 0
br i1 %61, label %64, label %63
63: ; preds = %57
br i1 %62, label %97, label %122
64: ; preds = %57
br i1 %62, label %67, label %92
65: ; preds = %77
%66 = icmp eq i32 %85, %12
br i1 %66, label %92, label %67
67: ; preds = %64, %65
%68 = phi i32 [ %85, %65 ], [ 0, %64 ]
%69 = tail call i32 @sem_wait(i32* %39)
%70 = icmp eq i32 %69, -1
br i1 %70, label %71, label %77
71: ; preds = %67
%72 = load i32, i32* @errno
%73 = tail call i8* @strerror(i32 %72)
%74 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.5, i64 0, i64 0), i8* %73)
%75 = load i32, i32* @EXIT_FAILURE
%76 = tail call i32 @exit(i32 %75)
unreachable
77: ; preds = %67
%78 = load i32, i32* %19
%79 = add nsw i32 %78, 1
store i32 %79, i32* %19
%80 = sext i32 %78 to i64
%81 = inttoptr i64 %80 to i8*
%82 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i8* %81)
%83 = tail call i32 @sem_post(i32* %39)
%84 = icmp eq i32 %83, -1
%85 = add nuw nsw i32 %68, 1
br i1 %84, label %86, label %65
86: ; preds = %77
%87 = load i32, i32* @errno
%88 = tail call i8* @strerror(i32 %87)
%89 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0), i8* %88)
%90 = load i32, i32* @EXIT_FAILURE
%91 = tail call i32 @exit(i32 %90)
unreachable
92: ; preds = %65, %64
%93 = load i32, i32* @EXIT_SUCCESS
%94 = tail call i32 @exit(i32 %93)
unreachable
95: ; preds = %107
%96 = icmp eq i32 %115, %12
br i1 %96, label %122, label %97
97: ; preds = %63, %95
%98 = phi i32 [ %115, %95 ], [ 0, %63 ]
%99 = tail call i32 @sem_wait(i32* %39)
%100 = icmp eq i32 %99, -1
br i1 %100, label %101, label %107
101: ; preds = %97
%102 = load i32, i32* @errno
%103 = tail call i8* @strerror(i32 %102)
%104 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.8, i64 0, i64 0), i8* %103)
%105 = load i32, i32* @EXIT_FAILURE
%106 = tail call i32 @exit(i32 %105)
unreachable
107: ; preds = %97
%108 = load i32, i32* %19
%109 = add nsw i32 %108, 1
store i32 %109, i32* %19
%110 = sext i32 %108 to i64
%111 = inttoptr i64 %110 to i8*
%112 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.9, i64 0, i64 0), i8* %111)
%113 = tail call i32 @sem_post(i32* %39)
%114 = icmp eq i32 %113, -1
%115 = add nuw nsw i32 %98, 1
br i1 %114, label %116, label %95
116: ; preds = %107
%117 = load i32, i32* @errno
%118 = tail call i8* @strerror(i32 %117)
%119 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.10, i64 0, i64 0), i8* %118)
%120 = load i32, i32* @EXIT_FAILURE
%121 = tail call i32 @exit(i32 %120)
unreachable
122: ; preds = %95, %63
%123 = load i32, i32* @EXIT_SUCCESS
%124 = tail call i32 @exit(i32 %123)
unreachable
}