#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 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, int);
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 write(int, int *, int);
int main(int argc, char **argv) {
  int fd, i, nloop, zero = 0;
  int *ptr;
  sem_t *sem;
  if (3 != argc) {
    printf("usage: %s <pathname> <#loops>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  nloop = atoi(argv[2]);
  if ((fd = open(argv[1], O_RDWR | O_CREAT,
                 (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH))) == -1) {
    printf("open error: %s\n", strerror(errno));
    exit(EXIT_FAILURE);
  }
  if (write(fd, &zero, sizeof(int)) == -1) {
    printf("write 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)
movl   $0x0,-0x20(%rbp)
mov    $0x3,%eax
cmp    -0x8(%rbp),%eax
je     122c <main+0x4c>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xde7(%rip),%rdi
call   1070 <printf@plt>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d85(%rip),%rax
mov    (%rax),%esi
mov    0x2da4(%rip),%rax
or     (%rax),%esi
mov    0x2d23(%rip),%rax
mov    (%rax),%edx
mov    0x2d5a(%rip),%rax
or     (%rax),%edx
mov    0x2d31(%rip),%rax
or     (%rax),%edx
mov    0x2d20(%rip),%rax
or     (%rax),%edx
call   10b0 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    12b6 <main+0xd6>
mov    0x2d16(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    %rax,%rsi
lea    0xdbb(%rip),%rdi
call   1070 <printf@plt>
mov    0x2ce1(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
jne    12fb <main+0x11b>
mov    0x2cd1(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    %rax,%rsi
lea    0xd37(%rip),%rdi
call   1070 <printf@plt>
mov    0x2c9c(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    0x2cee(%rip),%rax
mov    (%rax),%edx
mov    0x2c7d(%rip),%rax
or     (%rax),%edx
mov    0x2ccc(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x4,%esi
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x28(%rbp)
mov    0x2c32(%rip),%rcx
cmp    (%rcx),%rax
jne    136a <main+0x18a>
mov    0x2c62(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    %rax,%rsi
lea    0xcd9(%rip),%rdi
call   1070 <printf@plt>
mov    0x2c2d(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
cmp    $0xffffffff,%eax
jne    13a6 <main+0x1c6>
mov    0x2c26(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    %rax,%rsi
lea    0xcad(%rip),%rdi
call   1070 <printf@plt>
mov    0x2bf1(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    0x2c4b(%rip),%rax
mov    (%rax),%esi
mov    0x2bfa(%rip),%rax
or     (%rax),%esi
mov    0x2bc1(%rip),%rax
mov    (%rax),%edx
mov    0x2bf8(%rip),%rax
or     (%rax),%edx
mov    0x2bcf(%rip),%rax
or     (%rax),%edx
mov    0x2bbe(%rip),%rax
or     (%rax),%edx
lea    0xc6e(%rip),%rdi
mov    $0x1,%ecx
call   10a0 <sem_open@plt>
mov    %rax,-0x30(%rbp)
mov    0x2bd0(%rip),%rcx
cmp    (%rcx),%rax
jne    142c <main+0x24c>
mov    0x2ba0(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    %rax,%rsi
lea    0xc41(%rip),%rdi
call   1070 <printf@plt>
mov    0x2b6b(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
lea    0xc1e(%rip),%rdi
call   10f0 <sem_unlink@plt>
cmp    $0xffffffff,%eax
jne    146c <main+0x28c>
mov    0x2b60(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    %rax,%rsi
lea    0xc15(%rip),%rdi
call   1070 <printf@plt>
mov    0x2b2b(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    0x2b05(%rip),%rax
mov    (%rax),%edi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <setbuf@plt>
mov    $0x0,%al
call   1100 <fork@plt>
cmp    $0x0,%rax
jne    1559 <main+0x379>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    154b <main+0x36b>
mov    -0x30(%rbp),%rdi
call   1030 <sem_wait@plt>
cmp    $0xffffffff,%eax
jne    14df <main+0x2ff>
mov    0x2aed(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    %rax,%rsi
lea    0xbb8(%rip),%rdi
call   1070 <printf@plt>
mov    0x2ab8(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x28(%rbp),%rcx
mov    (%rcx),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,(%rcx)
movslq %eax,%rsi
lea    0xba9(%rip),%rdi
call   1070 <printf@plt>
mov    -0x30(%rbp),%rdi
call   1090 <sem_post@plt>
cmp    $0xffffffff,%eax
jne    1538 <main+0x358>
mov    0x2a94(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    %rax,%rsi
lea    0xb85(%rip),%rdi
call   1070 <printf@plt>
mov    0x2a5f(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
jmp    153d <main+0x35d>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1496 <main+0x2b6>
mov    0x2a1e(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1615 <main+0x435>
mov    -0x30(%rbp),%rdi
call   1030 <sem_wait@plt>
cmp    $0xffffffff,%eax
jne    15a9 <main+0x3c9>
mov    0x2a23(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    %rax,%rsi
lea    0xb2e(%rip),%rdi
call   1070 <printf@plt>
mov    0x29ee(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x28(%rbp),%rcx
mov    (%rcx),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,(%rcx)
movslq %eax,%rsi
lea    0xb1f(%rip),%rdi
call   1070 <printf@plt>
mov    -0x30(%rbp),%rdi
call   1090 <sem_post@plt>
cmp    $0xffffffff,%eax
jne    1602 <main+0x422>
mov    0x29ca(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
mov    %rax,%rsi
lea    0xafc(%rip),%rdi
call   1070 <printf@plt>
mov    0x2995(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
jmp    1607 <main+0x427>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1560 <main+0x380>
mov    0x2954(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbp
movl   $0x0,0x4(%rsp)
cmp    $0x3,%edi
jne    1269 <main+0x89>
mov    0x10(%rbp),%rdi
call   10c0 <atoi@plt>
mov    0x2dc5(%rip),%rcx
mov    0x2de6(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
mov    0x2d63(%rip),%rcx
mov    0x2d9c(%rip),%r13
mov    0x0(%r13),%edx
or     (%rcx),%edx
mov    %eax,%ebx
mov    0x2d6d(%rip),%r15
or     (%r15),%edx
mov    0x8(%rbp),%rdi
mov    0x2d57(%rip),%r12
or     (%r12),%edx
call   10b0 <open@plt>
cmp    $0xffffffff,%eax
jne    1279 <main+0x99>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xdfa(%rip),%rdi
jmp    13aa <main+0x1ca>
mov    0x0(%rbp),%rsi
lea    0xd8c(%rip),%rdi
jmp    13ad <main+0x1cd>
mov    %eax,%ebp
lea    0x4(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
jne    12ab <main+0xcb>
mov    0x2d10(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xd79(%rip),%rdi
jmp    13aa <main+0x1ca>
mov    0x2d3e(%rip),%rax
mov    0x2ccf(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d1c(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x4,%esi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%r14
mov    0x2c86(%rip),%rax
cmp    (%rax),%r14
jne    1301 <main+0x121>
mov    0x2cba(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xd34(%rip),%rdi
jmp    13aa <main+0x1ca>
mov    %ebp,%edi
call   1080 <close@plt>
cmp    $0xffffffff,%eax
jne    1327 <main+0x147>
mov    0x2c94(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xd1e(%rip),%rdi
jmp    13aa <main+0x1ca>
mov    0x2c82(%rip),%rax
mov    (%rax),%esi
mov    0x2cc1(%rip),%rax
or     (%rax),%esi
mov    0x0(%r13),%edx
mov    0x2c3c(%rip),%rax
or     (%rax),%edx
or     (%r15),%edx
or     (%r12),%edx
lea    0xcfd(%rip),%rdi
mov    $0x1,%ecx
call   10a0 <sem_open@plt>
mov    %rax,%r15
mov    0x2c60(%rip),%rax
cmp    (%rax),%r15
jne    1384 <main+0x1a4>
mov    0x2c34(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xcd8(%rip),%rdi
jmp    13aa <main+0x1ca>
lea    0xcc6(%rip),%rdi
call   10f0 <sem_unlink@plt>
cmp    $0xffffffff,%eax
jne    13c0 <main+0x1e0>
mov    0x2c0c(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xcc4(%rip),%rdi
mov    %rax,%rsi
call   1070 <printf@plt>
mov    0x2bd7(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    0x2bb1(%rip),%rax
mov    (%rax),%edi
xor    %esi,%esi
call   1060 <setbuf@plt>
xor    %eax,%eax
call   1100 <fork@plt>
test   %rax,%rax
jne    1422 <main+0x242>
test   %ebx,%ebx
jle    1460 <main+0x280>
lea    0xcb4(%rip),%rbp
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1030 <sem_wait@plt>
cmp    $0xffffffff,%eax
je     146e <main+0x28e>
movslq (%r14),%rsi
lea    0x1(%rsi),%eax
mov    %eax,(%r14)
mov    %rbp,%rdi
call   1070 <printf@plt>
mov    %r15,%rdi
call   1090 <sem_post@plt>
cmp    $0xffffffff,%eax
je     1488 <main+0x2a8>
add    $0xffffffff,%ebx
jne    13f0 <main+0x210>
jmp    1460 <main+0x280>
test   %ebx,%ebx
jle    1460 <main+0x280>
lea    0xcb2(%rip),%rbp
nopl   (%rax)
mov    %r15,%rdi
call   1030 <sem_wait@plt>
cmp    $0xffffffff,%eax
je     14a2 <main+0x2c2>
movslq (%r14),%rsi
lea    0x1(%rsi),%eax
mov    %eax,(%r14)
mov    %rbp,%rdi
call   1070 <printf@plt>
mov    %r15,%rdi
call   1090 <sem_post@plt>
cmp    $0xffffffff,%eax
je     14bc <main+0x2dc>
add    $0xffffffff,%ebx
jne    1430 <main+0x250>
mov    0x2b09(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    0x2b33(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xc01(%rip),%rdi
jmp    13aa <main+0x1ca>
mov    0x2b19(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xc0d(%rip),%rdi
jmp    13aa <main+0x1ca>
mov    0x2aff(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xc0d(%rip),%rdi
jmp    13aa <main+0x1ca>
mov    0x2ae5(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xc1a(%rip),%rdi
jmp    13aa <main+0x1ca>

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

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