#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_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_CREAT;
int O_EXCL;
int /*<<< orphan*/ O_RDWR;
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 close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int *mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int,
          int /*<<< orphan*/);
int open(char *, 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 fd, 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 ((fd = open("/dev/zero", O_RDWR)) == -1) {
    printf("open error: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  if ((ptr = mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd,
                  0)) == MAP_FAILED) {
    printf("mmap error: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  if (close(fd) == -1) {
    printf("close 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     1215 <main+0x45>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdfe(%rip),%rdi
call   1060 <printf@plt>
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
lea    0xde0(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1271 <main+0xa1>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
lea    0xdef(%rip),%rdi
call   1060 <printf@plt>
mov    0x2d26(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    0x2d78(%rip),%rax
mov    (%rax),%edx
mov    0x2d07(%rip),%rax
or     (%rax),%edx
mov    0x2d56(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x4,%esi
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    0x2cbc(%rip),%rcx
cmp    (%rcx),%rax
jne    12e0 <main+0x110>
mov    0x2cec(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
lea    0xd52(%rip),%rdi
call   1060 <printf@plt>
mov    0x2cb7(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
cmp    $0xffffffff,%eax
jne    131c <main+0x14c>
mov    0x2cb0(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
lea    0xd26(%rip),%rdi
call   1060 <printf@plt>
mov    0x2c7b(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    0x2cd5(%rip),%rax
mov    (%rax),%esi
mov    0x2c84(%rip),%rax
or     (%rax),%esi
mov    0x2c4b(%rip),%rax
mov    (%rax),%edx
mov    0x2c82(%rip),%rax
or     (%rax),%edx
mov    0x2c59(%rip),%rax
or     (%rax),%edx
mov    0x2c48(%rip),%rax
or     (%rax),%edx
lea    0xce7(%rip),%rdi
mov    $0x1,%ecx
call   1090 <sem_open@plt>
mov    %rax,-0x30(%rbp)
mov    0x2c5a(%rip),%rcx
cmp    (%rcx),%rax
jne    13a2 <main+0x1d2>
mov    0x2c2a(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
lea    0xcba(%rip),%rdi
call   1060 <printf@plt>
mov    0x2bf5(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
lea    0xc97(%rip),%rdi
call   10e0 <sem_unlink@plt>
cmp    $0xffffffff,%eax
jne    13e2 <main+0x212>
mov    0x2bea(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
lea    0xc8e(%rip),%rdi
call   1060 <printf@plt>
mov    0x2bb5(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    0x2b8f(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <setbuf@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
cmp    $0x0,%rax
jne    14cf <main+0x2ff>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    14c1 <main+0x2f1>
mov    -0x30(%rbp),%rdi
call   1030 <sem_wait@plt>
cmp    $0xffffffff,%eax
jne    1455 <main+0x285>
mov    0x2b77(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
lea    0xc31(%rip),%rdi
call   1060 <printf@plt>
mov    0x2b42(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x28(%rbp),%rcx
mov    (%rcx),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,(%rcx)
movslq %eax,%rsi
lea    0xc22(%rip),%rdi
call   1060 <printf@plt>
mov    -0x30(%rbp),%rdi
call   1080 <sem_post@plt>
cmp    $0xffffffff,%eax
jne    14ae <main+0x2de>
mov    0x2b1e(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
lea    0xbfe(%rip),%rdi
call   1060 <printf@plt>
mov    0x2ae9(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
jmp    14b3 <main+0x2e3>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    140c <main+0x23c>
mov    0x2aa8(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    158b <main+0x3bb>
mov    -0x30(%rbp),%rdi
call   1030 <sem_wait@plt>
cmp    $0xffffffff,%eax
jne    151f <main+0x34f>
mov    0x2aad(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
lea    0xba7(%rip),%rdi
call   1060 <printf@plt>
mov    0x2a78(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    -0x28(%rbp),%rcx
mov    (%rcx),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,(%rcx)
movslq %eax,%rsi
lea    0xb98(%rip),%rdi
call   1060 <printf@plt>
mov    -0x30(%rbp),%rdi
call   1080 <sem_post@plt>
cmp    $0xffffffff,%eax
jne    1578 <main+0x3a8>
mov    0x2a54(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
lea    0xb75(%rip),%rdi
call   1060 <printf@plt>
mov    0x2a1f(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
jmp    157d <main+0x3ad>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    14d6 <main+0x306>
mov    0x29de(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
cmp    $0x2,%edi
jne    121c <main+0x4c>
mov    0x8(%rsi),%rdi
call   10b0 <atoi@plt>
mov    %eax,%ebx
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
lea    0xe1d(%rip),%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
jne    122b <main+0x5b>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xe36(%rip),%rdi
jmp    133c <main+0x16c>
mov    (%rsi),%rsi
lea    0xdda(%rip),%rdi
jmp    133f <main+0x16f>
mov    %eax,%ebp
mov    0x2dbc(%rip),%rax
mov    0x2d4d(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d9a(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x4,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1040 <mmap@plt>
mov    %rax,%r14
mov    0x2d04(%rip),%rax
cmp    (%rax),%r14
jne    1283 <main+0xb3>
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xda1(%rip),%rdi
jmp    133c <main+0x16c>
mov    %ebp,%edi
call   1070 <close@plt>
cmp    $0xffffffff,%eax
jne    12a9 <main+0xd9>
mov    0x2d12(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd8b(%rip),%rdi
jmp    133c <main+0x16c>
mov    0x2d48(%rip),%rax
mov    0x2cf9(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2cbe(%rip),%rax
mov    0x2cf7(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2ccc(%rip),%rax
or     (%rax),%edx
mov    0x2cbb(%rip),%rax
or     (%rax),%edx
lea    0xd5a(%rip),%rdi
mov    $0x1,%ecx
call   1090 <sem_open@plt>
mov    %rax,%r15
mov    0x2cce(%rip),%rax
cmp    (%rax),%r15
jne    1316 <main+0x146>
mov    0x2ca2(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd35(%rip),%rdi
jmp    133c <main+0x16c>
lea    0xd23(%rip),%rdi
call   10e0 <sem_unlink@plt>
cmp    $0xffffffff,%eax
jne    1352 <main+0x182>
mov    0x2c7a(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xd21(%rip),%rdi
mov    %rax,%rsi
call   1060 <printf@plt>
mov    0x2c45(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    0x2c1f(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1050 <setbuf@plt>
xor    %eax,%eax
call   10f0 <fork@plt>
test   %rax,%rax
jne    13b2 <main+0x1e2>
test   %ebx,%ebx
jle    13f0 <main+0x220>
lea    0xd15(%rip),%r12
nopl   0x0(%rax)
mov    %r15,%rdi
call   1030 <sem_wait@plt>
cmp    $0xffffffff,%eax
je     13fe <main+0x22e>
movslq (%r14),%rsi
lea    0x1(%rsi),%eax
mov    %eax,(%r14)
mov    %r12,%rdi
call   1060 <printf@plt>
mov    %r15,%rdi
call   1080 <sem_post@plt>
cmp    $0xffffffff,%eax
je     1418 <main+0x248>
add    $0xffffffff,%ebx
jne    1380 <main+0x1b0>
jmp    13f0 <main+0x220>
test   %ebx,%ebx
jle    13f0 <main+0x220>
lea    0xd11(%rip),%r12
nopl   (%rax)
mov    %r15,%rdi
call   1030 <sem_wait@plt>
cmp    $0xffffffff,%eax
je     1432 <main+0x262>
movslq (%r14),%rsi
lea    0x1(%rsi),%eax
mov    %eax,(%r14)
mov    %r12,%rdi
call   1060 <printf@plt>
mov    %r15,%rdi
call   1080 <sem_post@plt>
cmp    $0xffffffff,%eax
je     144c <main+0x27c>
add    $0xffffffff,%ebx
jne    13c0 <main+0x1f0>
mov    0x2b79(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    0x2ba3(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xc60(%rip),%rdi
jmp    133c <main+0x16c>
mov    0x2b89(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xc6c(%rip),%rdi
jmp    133c <main+0x16c>
mov    0x2b6f(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xc6c(%rip),%rdi
jmp    133c <main+0x16c>
mov    0x2b55(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xc79(%rip),%rdi
jmp    133c <main+0x16c>

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

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