#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 */
struct sembuf {
  int sem_op;
  scalar_t__ sem_flg;
  scalar_t__ sem_num;
};
typedef int /*<<< orphan*/ key_t;

/* Variables and functions */
scalar_t__ ECHILD;
int IPC_CREAT;
int IPC_EXCL;
int /*<<< orphan*/ IPC_RMID;
int O_CREAT;
int O_EXCL;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ SETVAL;
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ execl(char *, char *, char *);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ ftok(char *, int);
int /*<<< orphan*/ getcwd(char *, int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ semctl(int, int /*<<< orphan*/, int /*<<< orphan*/, ...);
int semget(int /*<<< orphan*/, int, int);
scalar_t__ semop(int, struct sembuf *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
scalar_t__ unlink(char *);
int wait(int *);
scalar_t__ write(int, int /*<<< orphan*/ *, int);
int main(int argc, char *argv[]) {
  key_t sem_key;
  int sem_id;
  int sem_fd;
  char client_exe[255];
  int dir_len;
  int i;
  struct sembuf sop;
  int pid;
  int status;
  sem_key = ftok("./sem_server.c", 42);
  sem_fd = open(("sem.key"), O_WRONLY | O_TRUNC | O_EXCL | O_CREAT, 0644);
  if (sem_fd < 0) {
    perror("Could not open sem.key");
    exit(1);
  }
  if (write(sem_fd, &sem_key, sizeof(key_t)) < 0) {
    perror("Could not write key to file");
    exit(2);
  }
  close(sem_fd);
  sem_id = semget(sem_key, 1, IPC_CREAT | IPC_EXCL | 0600);
  if (sem_id < 0) {
    perror("Could not create sem");
    unlink(("sem.key"));
    exit(3);
  }
  if (semctl(sem_id, 0, SETVAL, 0) < 0) {
    perror("Could not set value of semaphore");
    exit(4);
  }
  getcwd(client_exe, 255);
  dir_len = strlen(client_exe);
  strcpy(client_exe + dir_len, "/sem_client");
  printf("%s\n", client_exe);
  for (i = 0; i < 5; ++i) {
    if ((pid = fork()) < 0) {
      perror("Could not fork, please create clients manually");
    } else if (pid == 0) {
      execl(client_exe, "sem_client", (char *)0);
      _exit(127);
    }
  }
  printf("Done creating clients, sleeping for a while\n");
  sleep(5);
  printf("Increasing sem count\n");
  sop.sem_num = 0;
  sop.sem_op = 1;
  sop.sem_flg = 0;
  if (semop(sem_id, &sop, 1)) {
    perror("Could not increment semaphore");
    exit(5);
  }
  for (;;) {
    pid = wait(&status);
    if (pid < 0) {
      if (errno == ECHILD) {
        printf("All children have exited\n");
        break;
      } else {
        perror("Could not wait");
      }
    } else {
      printf("Child %d exited with status %d\n", pid, status);
    }
  }
  if (unlink(("sem.key")) < 0) {
    perror("Could not unlink key file");
  }
  if (semctl(sem_id, 0, IPC_RMID) < 0) {
    perror("Could not delete semaphore");
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xdb0(%rip),%rdi
mov    $0x2a,%esi
call   1110 <ftok@plt>
mov    %eax,-0x14(%rbp)
mov    0x2d2c(%rip),%rax
mov    (%rax),%esi
mov    0x2d83(%rip),%rax
or     (%rax),%esi
mov    0x2d52(%rip),%rax
or     (%rax),%esi
mov    0x2d79(%rip),%rax
or     (%rax),%esi
lea    0xd96(%rip),%rdi
mov    $0x1a4,%edx
call   10c0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    12b5 <main+0x85>
lea    0xd69(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0x1c(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x4,%edx
call   1070 <write@plt>
cmp    $0x0,%rax
jge    12e6 <main+0xb6>
lea    0xd4f(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x2,%edi
call   1100 <exit@plt>
mov    -0x1c(%rbp),%edi
call   10b0 <close@plt>
mov    -0x14(%rbp),%edi
mov    0x2cb0(%rip),%rax
mov    (%rax),%edx
mov    0x2cdf(%rip),%rax
or     (%rax),%edx
or     $0x180,%edx
mov    $0x1,%esi
call   1030 <semget@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1342 <main+0x112>
lea    0xd1b(%rip),%rdi
call   10d0 <perror@plt>
lea    0xceb(%rip),%rdi
call   1040 <unlink@plt>
mov    $0x3,%edi
call   1100 <exit@plt>
mov    -0x18(%rbp),%edi
mov    0x2c54(%rip),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
mov    %ecx,%esi
mov    $0x0,%al
call   10e0 <semctl@plt>
cmp    $0x0,%rax
jge    1379 <main+0x149>
lea    0xced(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x4,%edi
call   1100 <exit@plt>
lea    -0x120(%rbp),%rdi
mov    $0xff,%esi
call   10a0 <getcwd@plt>
lea    -0x120(%rbp),%rdi
call   1080 <strlen@plt>
mov    %eax,-0x124(%rbp)
lea    -0x120(%rbp),%rdi
movslq -0x124(%rbp),%rax
add    %rax,%rdi
lea    0xcc4(%rip),%rsi
call   1060 <strcpy@plt>
lea    -0x120(%rbp),%rsi
lea    0xcbd(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
movl   $0x0,-0x128(%rbp)
cmpl   $0x5,-0x128(%rbp)
jge    1458 <main+0x228>
mov    $0x0,%al
call   1150 <fork@plt>
mov    %eax,-0x144(%rbp)
cmp    $0x0,%eax
jge    140c <main+0x1dc>
lea    0xc86(%rip),%rdi
call   10d0 <perror@plt>
jmp    143f <main+0x20f>
cmpl   $0x0,-0x144(%rbp)
jne    143a <main+0x20a>
lea    -0x120(%rbp),%rdi
lea    0xc52(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1120 <execl@plt>
mov    $0x7f,%edi
call   1050 <_exit@plt>
jmp    143f <main+0x20f>
jmp    1444 <main+0x214>
mov    -0x128(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x128(%rbp)
jmp    13d8 <main+0x1a8>
lea    0xc58(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
mov    $0x5,%edi
call   1130 <sleep@plt>
lea    0xc6d(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
movq   $0x0,-0x130(%rbp)
movl   $0x1,-0x140(%rbp)
movq   $0x0,-0x138(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x140(%rbp),%rsi
mov    $0x1,%edx
call   10f0 <semop@plt>
cmp    $0x0,%rax
je     14d2 <main+0x2a2>
lea    0xc37(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x5,%edi
call   1100 <exit@plt>
jmp    14d7 <main+0x2a7>
lea    -0x148(%rbp),%rdi
call   1140 <wait@plt>
mov    %eax,-0x144(%rbp)
cmpl   $0x0,-0x144(%rbp)
jge    1534 <main+0x304>
mov    0x2ac3(%rip),%rax
mov    (%rax),%rax
mov    0x2aa9(%rip),%rcx
cmp    (%rcx),%rax
jne    1523 <main+0x2f3>
lea    0xc01(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
jmp    1553 <main+0x323>
lea    0xc08(%rip),%rdi
call   10d0 <perror@plt>
jmp    154e <main+0x31e>
mov    -0x144(%rbp),%esi
mov    -0x148(%rbp),%edx
lea    0xbfa(%rip),%rdi
mov    $0x0,%al
call   1090 <printf@plt>
jmp    14d7 <main+0x2a7>
lea    0xac4(%rip),%rdi
call   1040 <unlink@plt>
cmp    $0x0,%rax
jge    1575 <main+0x345>
lea    0xbf1(%rip),%rdi
call   10d0 <perror@plt>
mov    -0x18(%rbp),%edi
mov    0x2a39(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
mov    $0x0,%al
call   10e0 <semctl@plt>
cmp    $0x0,%rax
jge    15a0 <main+0x370>
lea    0xbe0(%rip),%rdi
call   10d0 <perror@plt>
xor    %edi,%edi
call   1100 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x128,%rsp
lea    0xda8(%rip),%rdi
mov    $0x2a,%esi
call   1120 <ftok@plt>
mov    0x2d27(%rip),%rcx
mov    0x2d80(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
mov    %eax,%ebx
mov    0x2d4b(%rip),%rax
or     (%rax),%esi
mov    %ebx,0x4(%rsp)
mov    0x2d6e(%rip),%rax
or     (%rax),%esi
lea    0xd8b(%rip),%rdi
mov    $0x1a4,%edx
call   10d0 <open@plt>
test   %eax,%eax
js     14eb <main+0x2ab>
mov    %eax,%ebp
lea    0x4(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1080 <write@plt>
test   %rax,%rax
js     1501 <main+0x2c1>
mov    %ebp,%edi
call   10c0 <close@plt>
mov    0x2cd9(%rip),%rax
mov    (%rax),%edx
mov    0x2d08(%rip),%rax
or     (%rax),%edx
or     $0x180,%edx
mov    %ebx,%edi
mov    $0x1,%esi
call   1030 <semget@plt>
test   %eax,%eax
js     1517 <main+0x2d7>
mov    %eax,%r14d
mov    0x2ca2(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
xor    %esi,%esi
xor    %ecx,%ecx
xor    %eax,%eax
call   10f0 <semctl@plt>
test   %rax,%rax
js     1539 <main+0x2f9>
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xff,%esi
call   10b0 <getcwd@plt>
mov    %rbx,%rdi
call   1090 <strlen@plt>
cltq
lea    (%rsp,%rax,1),%rdi
add    $0x20,%rdi
lea    0xd36(%rip),%rsi
call   1060 <strcpy@plt>
mov    %rbx,%rdi
call   1070 <puts@plt>
xor    %eax,%eax
call   1160 <fork@plt>
test   %eax,%eax
jns    1368 <main+0x128>
lea    0xd23(%rip),%rdi
call   10e0 <perror@plt>
jmp    136e <main+0x12e>
je     1555 <main+0x315>
xor    %eax,%eax
call   1160 <fork@plt>
test   %eax,%eax
jns    1387 <main+0x147>
lea    0xd04(%rip),%rdi
call   10e0 <perror@plt>
jmp    138d <main+0x14d>
je     1555 <main+0x315>
xor    %eax,%eax
call   1160 <fork@plt>
test   %eax,%eax
jns    13a6 <main+0x166>
lea    0xce5(%rip),%rdi
call   10e0 <perror@plt>
jmp    13ac <main+0x16c>
je     1555 <main+0x315>
xor    %eax,%eax
call   1160 <fork@plt>
test   %eax,%eax
jns    13c5 <main+0x185>
lea    0xcc6(%rip),%rdi
call   10e0 <perror@plt>
jmp    13cb <main+0x18b>
je     1555 <main+0x315>
xor    %eax,%eax
call   1160 <fork@plt>
test   %eax,%eax
jns    154f <main+0x30f>
lea    0xca3(%rip),%rdi
call   10e0 <perror@plt>
lea    0xd48(%rip),%rdi
call   1070 <puts@plt>
mov    $0x5,%edi
call   1140 <sleep@plt>
lea    0xd5e(%rip),%rdi
call   1070 <puts@plt>
movl   $0x1,0x8(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
lea    0x8(%rsp),%rsi
mov    %r14d,%edi
mov    $0x1,%edx
call   1100 <semop@plt>
test   %rax,%rax
jne    147f <main+0x23f>
mov    %rsp,%rbx
mov    0x2b87(%rip),%r12
mov    0x2b70(%rip),%r13
lea    0xc8a(%rip),%r15
lea    0xc92(%rip),%rbp
jmp    145f <main+0x21f>
mov    (%rsp),%edx
mov    %rbp,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   10a0 <printf@plt>
mov    %rbx,%rdi
call   1150 <wait@plt>
test   %eax,%eax
jns    1450 <main+0x210>
mov    (%r12),%rax
cmp    0x0(%r13),%rax
je     1495 <main+0x255>
mov    %r15,%rdi
call   10e0 <perror@plt>
jmp    145f <main+0x21f>
lea    0xc2d(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x5,%edi
call   1110 <exit@plt>
lea    0xcda(%rip),%rdi
call   1070 <puts@plt>
lea    0xb76(%rip),%rdi
call   1040 <unlink@plt>
test   %rax,%rax
jns    14be <main+0x27e>
lea    0xc47(%rip),%rdi
call   10e0 <perror@plt>
mov    0x2af3(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
xor    %esi,%esi
xor    %eax,%eax
call   10f0 <semctl@plt>
test   %rax,%rax
jns    14e4 <main+0x2a4>
lea    0xc3b(%rip),%rdi
call   10e0 <perror@plt>
xor    %edi,%edi
call   1110 <exit@plt>
lea    0xb1d(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   1110 <exit@plt>
lea    0xb1e(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x2,%edi
call   1110 <exit@plt>
lea    0xb24(%rip),%rdi
call   10e0 <perror@plt>
lea    0xaf4(%rip),%rdi
call   1040 <unlink@plt>
mov    $0x3,%edi
call   1110 <exit@plt>
lea    0xb17(%rip),%rdi
call   10e0 <perror@plt>
mov    $0x4,%edi
call   1110 <exit@plt>
jne    13e6 <main+0x1a6>
lea    0xb1d(%rip),%rsi
lea    0x20(%rsp),%rdi
xor    %edx,%edx
call   1130 <execl@plt>
mov    $0x7f,%edi
call   1050 <_exit@plt>

LLVM IR (-O0):
%struct.sembuf = type { i32, i64, i64 }
@.str = [15 x i8] c"./sem_server.c\00"
@.str.1 = [8 x i8] c"sem.key\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_EXCL = global i32 0
@O_CREAT = global i32 0
@.str.2 = [23 x i8] c"Could not open sem.key\00"
@.str.3 = [28 x i8] c"Could not write key to file\00"
@IPC_CREAT = global i32 0
@IPC_EXCL = global i32 0
@.str.4 = [21 x i8] c"Could not create sem\00"
@SETVAL = global i32 0
@.str.5 = [33 x i8] c"Could not set value of semaphore\00"
@.str.6 = [12 x i8] c"/sem_client\00"
@.str.7 = [4 x i8] c"%s\0A\00"
@.str.8 = [47 x i8] c"Could not fork, please create clients manually\00"
@.str.9 = [11 x i8] c"sem_client\00"
@.str.10 = [45 x i8] c"Done creating clients, sleeping for a while\0A\00"
@.str.11 = [22 x i8] c"Increasing sem count\0A\00"
@.str.12 = [30 x i8] c"Could not increment semaphore\00"
@errno = global i64 0
@ECHILD = global i64 0
@.str.13 = [26 x i8] c"All children have exited\0A\00"
@.str.14 = [15 x i8] c"Could not wait\00"
@.str.15 = [32 x i8] c"Child %d exited with status %d\0A\00"
@.str.16 = [26 x i8] c"Could not unlink key file\00"
@IPC_RMID = global i32 0
@.str.17 = [27 x i8] c"Could not delete semaphore\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 [255 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.sembuf
%13 = alloca i32
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = call i32 @ftok(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 42)
store i32 %15, i32* %6
%16 = load i32, i32* @O_WRONLY
%17 = load i32, i32* @O_TRUNC
%18 = or i32 %16, %17
%19 = load i32, i32* @O_EXCL
%20 = or i32 %18, %19
%21 = load i32, i32* @O_CREAT
%22 = or i32 %20, %21
%23 = call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %22, i32 420)
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %2
%27 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %2
%30 = load i32, i32* %8
%31 = call i64 @write(i32 %30, i32* %6, i32 4)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %29
%34 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%35 = call i32 @exit(i32 2)
unreachable
36: ; preds = %29
%37 = load i32, i32* %8
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* %6
%40 = load i32, i32* @IPC_CREAT
%41 = load i32, i32* @IPC_EXCL
%42 = or i32 %40, %41
%43 = or i32 %42, 384
%44 = call i32 @semget(i32 %39, i32 1, i32 %43)
store i32 %44, i32* %7
%45 = load i32, i32* %7
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %36
%48 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%49 = call i64 @unlink(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%50 = call i32 @exit(i32 3)
unreachable
51: ; preds = %36
%52 = load i32, i32* %7
%53 = load i32, i32* @SETVAL
%54 = call i64 (i32, i32, i32, ...) @semctl(i32 %52, i32 0, i32 %53, i32 0)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %51
%57 = call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0))
%58 = call i32 @exit(i32 4)
unreachable
59: ; preds = %51
%60 = getelementptr inbounds [255 x i8], [255 x i8]* %9, i64 0, i64 0
%61 = call i32 @getcwd(i8* %60, i32 255)
%62 = getelementptr inbounds [255 x i8], [255 x i8]* %9, i64 0, i64 0
%63 = call i32 @strlen(i8* %62)
store i32 %63, i32* %10
%64 = getelementptr inbounds [255 x i8], [255 x i8]* %9, i64 0, i64 0
%65 = load i32, i32* %10
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds i8, i8* %64, i64 %66
%68 = call i32 @strcpy(i8* %67, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
%69 = getelementptr inbounds [255 x i8], [255 x i8]* %9, i64 0, i64 0
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i8* %69)
store i32 0, i32* %11
br label %71
71: ; preds = %88, %59
%72 = load i32, i32* %11
%73 = icmp slt i32 %72, 5
br i1 %73, label %74, label %91
74: ; preds = %71
%75 = call i32 (...) @fork()
store i32 %75, i32* %13
%76 = icmp slt i32 %75, 0
br i1 %76, label %77, label %79
77: ; preds = %74
%78 = call i32 @perror(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.8, i64 0, i64 0))
br label %87
79: ; preds = %74
%80 = load i32, i32* %13
%81 = icmp eq i32 %80, 0
br i1 %81, label %82, label %86
82: ; preds = %79
%83 = getelementptr inbounds [255 x i8], [255 x i8]* %9, i64 0, i64 0
%84 = call i32 @execl(i8* %83, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.9, i64 0, i64 0), i8* null)
%85 = call i32 @_exit(i32 127)
unreachable
86: ; preds = %79
br label %87
87: ; preds = %86, %77
br label %88
88: ; preds = %87
%89 = load i32, i32* %11
%90 = add nsw i32 %89, 1
store i32 %90, i32* %11
br label %71
91: ; preds = %71
%92 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.10, i64 0, i64 0))
%93 = call i32 @sleep(i32 5)
%94 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.11, i64 0, i64 0))
%95 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %12, i32 0, i32 2
store i64 0, i64* %95
%96 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %12, i32 0, i32 0
store i32 1, i32* %96
%97 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %12, i32 0, i32 1
store i64 0, i64* %97
%98 = load i32, i32* %7
%99 = call i64 @semop(i32 %98, %struct.sembuf* %12, i32 1)
%100 = icmp ne i64 %99, 0
br i1 %100, label %101, label %104
101: ; preds = %91
%102 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.12, i64 0, i64 0))
%103 = call i32 @exit(i32 5)
unreachable
104: ; preds = %91
br label %105
105: ; preds = %122, %104
%106 = call i32 @wait(i32* %14)
store i32 %106, i32* %13
%107 = load i32, i32* %13
%108 = icmp slt i32 %107, 0
br i1 %108, label %109, label %118
109: ; preds = %105
%110 = load i64, i64* @errno
%111 = load i64, i64* @ECHILD
%112 = icmp eq i64 %110, %111
br i1 %112, label %113, label %115
113: ; preds = %109
%114 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.13, i64 0, i64 0))
br label %123
115: ; preds = %109
%116 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.14, i64 0, i64 0))
br label %117
117: ; preds = %115
br label %122
118: ; preds = %105
%119 = load i32, i32* %13
%120 = load i32, i32* %14
%121 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.15, i64 0, i64 0), i32 %119, i32 %120)
br label %122
122: ; preds = %118, %117
br label %105
123: ; preds = %113
%124 = call i64 @unlink(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%125 = icmp slt i64 %124, 0
br i1 %125, label %126, label %128
126: ; preds = %123
%127 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.16, i64 0, i64 0))
br label %128
128: ; preds = %126, %123
%129 = load i32, i32* %7
%130 = load i32, i32* @IPC_RMID
%131 = call i64 (i32, i32, i32, ...) @semctl(i32 %129, i32 0, i32 %130)
%132 = icmp slt i64 %131, 0
br i1 %132, label %133, label %135
133: ; preds = %128
%134 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.17, i64 0, i64 0))
br label %135
135: ; preds = %133, %128
%136 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.sembuf = type { i32, i64, i64 }
@.str = [15 x i8] c"./sem_server.c\00"
@.str.1 = [8 x i8] c"sem.key\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_EXCL = global i32 0
@O_CREAT = global i32 0
@.str.2 = [23 x i8] c"Could not open sem.key\00"
@.str.3 = [28 x i8] c"Could not write key to file\00"
@IPC_CREAT = global i32 0
@IPC_EXCL = global i32 0
@.str.4 = [21 x i8] c"Could not create sem\00"
@SETVAL = global i32 0
@.str.5 = [33 x i8] c"Could not set value of semaphore\00"
@.str.6 = [12 x i8] c"/sem_client\00"
@.str.8 = [47 x i8] c"Could not fork, please create clients manually\00"
@.str.9 = [11 x i8] c"sem_client\00"
@.str.12 = [30 x i8] c"Could not increment semaphore\00"
@errno = global i64 0
@ECHILD = global i64 0
@.str.14 = [15 x i8] c"Could not wait\00"
@.str.15 = [32 x i8] c"Child %d exited with status %d\0A\00"
@.str.16 = [26 x i8] c"Could not unlink key file\00"
@IPC_RMID = global i32 0
@.str.17 = [27 x i8] c"Could not delete semaphore\00"
@str = [44 x i8] c"Done creating clients, sleeping for a while\00"
@str.18 = [21 x i8] c"Increasing sem count\00"
@str.19 = [25 x i8] c"All children have exited\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca [255 x i8]
%5 = alloca %struct.sembuf
%6 = alloca i32
%7 = bitcast i32* %3 to i8*
%8 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%9 = bitcast %struct.sembuf* %5 to i8*
%10 = bitcast i32* %6 to i8*
%11 = tail call i32 @ftok(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 42)
store i32 %11, i32* %3
%12 = load i32, i32* @O_WRONLY
%13 = load i32, i32* @O_TRUNC
%14 = or i32 %13, %12
%15 = load i32, i32* @O_EXCL
%16 = or i32 %14, %15
%17 = load i32, i32* @O_CREAT
%18 = or i32 %16, %17
%19 = tail call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %18, i32 420)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %2
%22 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %2
%25 = call i64 @write(i32 %19, i32* %3, i32 4)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%29 = tail call i32 @exit(i32 2)
unreachable
30: ; preds = %24
%31 = tail call i32 @close(i32 %19)
%32 = load i32, i32* @IPC_CREAT
%33 = load i32, i32* @IPC_EXCL
%34 = or i32 %32, %33
%35 = or i32 %34, 384
%36 = tail call i32 @semget(i32 %11, i32 1, i32 %35)
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %30
%39 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
%40 = tail call i64 @unlink(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%41 = tail call i32 @exit(i32 3)
unreachable
42: ; preds = %30
%43 = load i32, i32* @SETVAL
%44 = tail call i64 (i32, i32, i32, ...) @semctl(i32 %36, i32 0, i32 %43, i32 0)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %42
%47 = tail call i32 @perror(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0))
%48 = tail call i32 @exit(i32 4)
unreachable
49: ; preds = %42
%50 = call i32 @getcwd(i8* %8, i32 255)
%51 = call i32 @strlen(i8* %8)
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 %52
%54 = call i32 @strcpy(i8* %53, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
%55 = call i32 @puts(i8* %8)
%56 = call i32 (...) @fork()
%57 = icmp slt i32 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %49
%59 = call i32 @perror(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.8, i64 0, i64 0))
br label %65
60: ; preds = %49
%61 = icmp eq i32 %56, 0
br i1 %61, label %62, label %65
62: ; preds = %89, %82, %75, %68, %60
%63 = call i32 @execl(i8* %8, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.9, i64 0, i64 0), i8* null)
%64 = call i32 @_exit(i32 127)
unreachable
65: ; preds = %58, %60
%66 = call i32 (...) @fork()
%67 = icmp slt i32 %66, 0
br i1 %67, label %70, label %68
68: ; preds = %65
%69 = icmp eq i32 %66, 0
br i1 %69, label %62, label %72
70: ; preds = %65
%71 = call i32 @perror(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.8, i64 0, i64 0))
br label %72
72: ; preds = %70, %68
%73 = call i32 (...) @fork()
%74 = icmp slt i32 %73, 0
br i1 %74, label %77, label %75
75: ; preds = %72
%76 = icmp eq i32 %73, 0
br i1 %76, label %62, label %79
77: ; preds = %72
%78 = call i32 @perror(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.8, i64 0, i64 0))
br label %79
79: ; preds = %77, %75
%80 = call i32 (...) @fork()
%81 = icmp slt i32 %80, 0
br i1 %81, label %84, label %82
82: ; preds = %79
%83 = icmp eq i32 %80, 0
br i1 %83, label %62, label %86
84: ; preds = %79
%85 = call i32 @perror(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.8, i64 0, i64 0))
br label %86
86: ; preds = %84, %82
%87 = call i32 (...) @fork()
%88 = icmp slt i32 %87, 0
br i1 %88, label %91, label %89
89: ; preds = %86
%90 = icmp eq i32 %87, 0
br i1 %90, label %62, label %93
91: ; preds = %86
%92 = call i32 @perror(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.8, i64 0, i64 0))
br label %93
93: ; preds = %91, %89
%94 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str, i64 0, i64 0))
%95 = call i32 @sleep(i32 5)
%96 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.18, i64 0, i64 0))
%97 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %5, i64 0, i32 0
store i32 1, i32* %97
%98 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %5, i64 0, i32 1
%99 = bitcast i64* %98 to <2 x i64>*
store <2 x i64> zeroinitializer, <2 x i64>* %99
%100 = call i64 @semop(i32 %36, %struct.sembuf* %5, i32 1)
%101 = icmp eq i64 %100, 0
br i1 %101, label %105, label %102
102: ; preds = %93
%103 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.12, i64 0, i64 0))
%104 = call i32 @exit(i32 5)
unreachable
105: ; preds = %93, %121
%106 = call i32 @wait(i32* %6)
%107 = icmp slt i32 %106, 0
br i1 %107, label %108, label %118
108: ; preds = %105
%109 = load i64, i64* @errno
%110 = load i64, i64* @ECHILD
%111 = icmp eq i64 %109, %110
br i1 %111, label %112, label %116
112: ; preds = %108
%113 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.19, i64 0, i64 0))
%114 = call i64 @unlink(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%115 = icmp slt i64 %114, 0
br i1 %115, label %122, label %124
116: ; preds = %108
%117 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.14, i64 0, i64 0))
br label %121
118: ; preds = %105
%119 = load i32, i32* %6
%120 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.15, i64 0, i64 0), i32 %106, i32 %119)
br label %121
121: ; preds = %118, %116
br label %105
122: ; preds = %112
%123 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.16, i64 0, i64 0))
br label %124
124: ; preds = %122, %112
%125 = load i32, i32* @IPC_RMID
%126 = call i64 (i32, i32, i32, ...) @semctl(i32 %36, i32 0, i32 %125)
%127 = icmp slt i64 %126, 0
br i1 %127, label %128, label %130
128: ; preds = %124
%129 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.17, i64 0, i64 0))
br label %130
130: ; preds = %128, %124
%131 = call i32 @exit(i32 0)
unreachable
}