#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;
typedef int /*<<< orphan*/ pids;
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int O_CREAT;
int O_EXCL;
int /*<<< orphan*/ *SEM_FAILED;
int S_IRGRP;
int S_IRUSR;
int S_IWGRP;
int S_IWUSR;
scalar_t__ execl(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
scalar_t__ sem_close(int /*<<< orphan*/ *);
int /*<<< orphan*/ *sem_open(char *, int, int, int);
scalar_t__ sem_unlink(char *);
scalar_t__ waitpid(scalar_t__, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(void) {
  sem_t *semaphore = sem_open("/semaphore_example", O_CREAT | O_EXCL,
                              (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP), 1);
  if (semaphore == SEM_FAILED) {
    perror("sem_open(3) error");
    exit(EXIT_FAILURE);
  }
  if (sem_close(semaphore) < 0) {
    perror("sem_close(3) failed");
    sem_unlink("/semaphore_example");
    exit(EXIT_FAILURE);
  }
  pid_t pids[2];
  size_t i;
  for (i = 0; i < sizeof(pids) / sizeof(pids[0]); i++) {
    if ((pids[i] = fork()) < 0) {
      perror("fork(2) failed");
      exit(EXIT_FAILURE);
    }
    if (pids[i] == 0) {
      if (execl("./sem_chld", "./sem_chld", NULL) < 0) {
        perror("execl(2) failed");
        exit(EXIT_FAILURE);
      }
    }
  }
  for (i = 0; i < sizeof(pids) / sizeof(pids[0]); i++)
    if (waitpid(pids[i], NULL, 0) < 0)
      perror("waitpid(2) failed");
  if (sem_unlink("/semaphore_example") < 0)
    perror("sem_unlink(3) failed");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
mov    0x2e21(%rip),%rax
or     (%rax),%esi
mov    0x2e00(%rip),%rax
mov    (%rax),%edx
mov    0x2e1f(%rip),%rax
or     (%rax),%edx
mov    0x2dfe(%rip),%rax
or     (%rax),%edx
mov    0x2e15(%rip),%rax
or     (%rax),%edx
lea    0xe34(%rip),%rdi
mov    $0x1,%ecx
call   1030 <sem_open@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2dfb(%rip),%rcx
cmp    (%rcx),%rax
jne    1208 <main+0x88>
lea    0xe1e(%rip),%rdi
call   1060 <perror@plt>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rdi
call   1050 <sem_close@plt>
cmp    $0x0,%rax
jge    1241 <main+0xc1>
lea    0xe03(%rip),%rdi
call   1060 <perror@plt>
lea    0xdd2(%rip),%rdi
call   1090 <sem_unlink@plt>
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
movq   $0x0,-0x28(%rbp)
cmpq   $0x2,-0x28(%rbp)
jae    12ea <main+0x16a>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    -0x28(%rbp),%rcx
mov    %rax,-0x20(%rbp,%rcx,8)
cmp    $0x0,%rax
jge    1288 <main+0x108>
lea    0xdc4(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x28(%rbp),%rax
cmpq   $0x0,-0x20(%rbp,%rax,8)
jne    12d4 <main+0x154>
lea    0xda9(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
call   1080 <execl@plt>
cmp    $0x0,%rax
jge    12cf <main+0x14f>
lea    0xd97(%rip),%rdi
call   1060 <perror@plt>
mov    0x2ce8(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
jmp    12d4 <main+0x154>
jmp    12d9 <main+0x159>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1249 <main+0xc9>
movq   $0x0,-0x28(%rbp)
cmpq   $0x2,-0x28(%rbp)
jae    133d <main+0x1bd>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp,%rax,8),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1040 <waitpid@plt>
cmp    $0x0,%rax
jge    1327 <main+0x1a7>
lea    0xd41(%rip),%rdi
call   1060 <perror@plt>
jmp    132c <main+0x1ac>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    12f2 <main+0x172>
lea    0xcbc(%rip),%rdi
call   1090 <sem_unlink@plt>
cmp    $0x0,%rax
jge    135f <main+0x1df>
lea    0xd1b(%rip),%rdi
call   1060 <perror@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x2e6d(%rip),%rax
mov    0x2e2e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e0b(%rip),%rax
mov    0x2e2c(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e09(%rip),%rax
or     (%rax),%edx
mov    0x2e20(%rip),%rax
or     (%rax),%edx
lea    0xe3f(%rip),%rdi
mov    $0x1,%ecx
call   1030 <sem_open@plt>
mov    0x2e0e(%rip),%rcx
cmp    (%rcx),%rax
je     12ba <main+0x13a>
mov    %rax,%rdi
call   1050 <sem_close@plt>
test   %rax,%rax
js     12c3 <main+0x143>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     12b1 <main+0x131>
mov    %rax,%r14
jne    121b <main+0x9b>
lea    0xe40(%rip),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   1080 <execl@plt>
test   %rax,%rax
js     12dd <main+0x15d>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     12b1 <main+0x131>
mov    %rax,%rbx
jne    124a <main+0xca>
lea    0xe11(%rip),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   1080 <execl@plt>
test   %rax,%rax
js     12dd <main+0x15d>
mov    %r14,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1040 <waitpid@plt>
test   %rax,%rax
js     1287 <main+0x107>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1040 <waitpid@plt>
test   %rax,%rax
js     1295 <main+0x115>
lea    0xd8d(%rip),%rdi
call   1090 <sem_unlink@plt>
test   %rax,%rax
js     12a3 <main+0x123>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdd5(%rip),%rdi
call   1060 <perror@plt>
jmp    125b <main+0xdb>
lea    0xdc7(%rip),%rdi
call   1060 <perror@plt>
jmp    126c <main+0xec>
lea    0xdcb(%rip),%rdi
call   1060 <perror@plt>
jmp    127d <main+0xfd>
lea    0xd81(%rip),%rdi
jmp    12e4 <main+0x164>
lea    0xd52(%rip),%rdi
jmp    12e4 <main+0x164>
lea    0xd5b(%rip),%rdi
call   1060 <perror@plt>
lea    0xd2a(%rip),%rdi
call   1090 <sem_unlink@plt>
jmp    12e9 <main+0x169>
lea    0xd6f(%rip),%rdi
call   1060 <perror@plt>
mov    0x2cc0(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [19 x i8] c"/semaphore_example\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_IWGRP = global i32 0
@SEM_FAILED = global i32* null
@.str.1 = [18 x i8] c"sem_open(3) error\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [20 x i8] c"sem_close(3) failed\00"
@.str.3 = [15 x i8] c"fork(2) failed\00"
@.str.4 = [11 x i8] c"./sem_chld\00"
@.str.5 = [16 x i8] c"execl(2) failed\00"
@.str.6 = [18 x i8] c"waitpid(2) failed\00"
@.str.7 = [21 x i8] c"sem_unlink(3) failed\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [2 x i64]
%4 = alloca i64
store i32 0, i32* %1
%5 = load i32, i32* @O_CREAT
%6 = load i32, i32* @O_EXCL
%7 = or i32 %5, %6
%8 = load i32, i32* @S_IRUSR
%9 = load i32, i32* @S_IWUSR
%10 = or i32 %8, %9
%11 = load i32, i32* @S_IRGRP
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IWGRP
%14 = or i32 %12, %13
%15 = call i32* @sem_open(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %14, i32 1)
store i32* %15, i32** %2
%16 = load i32*, i32** %2
%17 = load i32*, i32** @SEM_FAILED
%18 = icmp eq i32* %16, %17
br i1 %18, label %19, label %23
19: ; preds = %0
%20 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = call i32 @exit(i32 %21)
unreachable
23: ; preds = %0
%24 = load i32*, i32** %2
%25 = call i64 @sem_close(i32* %24)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %32
27: ; preds = %23
%28 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%29 = call i64 @sem_unlink(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %23
store i64 0, i64* %4
br label %33
33: ; preds = %59, %32
%34 = load i64, i64* %4
%35 = icmp ult i64 %34, 2
br i1 %35, label %36, label %62
36: ; preds = %33
%37 = call i64 (...) @fork()
%38 = load i64, i64* %4
%39 = getelementptr inbounds [2 x i64], [2 x i64]* %3, i64 0, i64 %38
store i64 %37, i64* %39
%40 = icmp slt i64 %37, 0
br i1 %40, label %41, label %45
41: ; preds = %36
%42 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%43 = load i32, i32* @EXIT_FAILURE
%44 = call i32 @exit(i32 %43)
unreachable
45: ; preds = %36
%46 = load i64, i64* %4
%47 = getelementptr inbounds [2 x i64], [2 x i64]* %3, i64 0, i64 %46
%48 = load i64, i64* %47
%49 = icmp eq i64 %48, 0
br i1 %49, label %50, label %58
50: ; preds = %45
%51 = call i64 @execl(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32* null)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %57
53: ; preds = %50
%54 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
%55 = load i32, i32* @EXIT_FAILURE
%56 = call i32 @exit(i32 %55)
unreachable
57: ; preds = %50
br label %58
58: ; preds = %57, %45
br label %59
59: ; preds = %58
%60 = load i64, i64* %4
%61 = add i64 %60, 1
store i64 %61, i64* %4
br label %33
62: ; preds = %33
store i64 0, i64* %4
br label %63
63: ; preds = %75, %62
%64 = load i64, i64* %4
%65 = icmp ult i64 %64, 2
br i1 %65, label %66, label %78
66: ; preds = %63
%67 = load i64, i64* %4
%68 = getelementptr inbounds [2 x i64], [2 x i64]* %3, i64 0, i64 %67
%69 = load i64, i64* %68
%70 = call i64 @waitpid(i64 %69, i32* null, i32 0)
%71 = icmp slt i64 %70, 0
br i1 %71, label %72, label %74
72: ; preds = %66
%73 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0))
br label %74
74: ; preds = %72, %66
br label %75
75: ; preds = %74
%76 = load i64, i64* %4
%77 = add i64 %76, 1
store i64 %77, i64* %4
br label %63
78: ; preds = %63
%79 = call i64 @sem_unlink(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%80 = icmp slt i64 %79, 0
br i1 %80, label %81, label %83
81: ; preds = %78
%82 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0))
br label %83
83: ; preds = %81, %78
ret i32 0
}

LLVM IR (-O2):
@.str = [19 x i8] c"/semaphore_example\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_IWGRP = global i32 0
@SEM_FAILED = global i32* null
@.str.1 = [18 x i8] c"sem_open(3) error\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [20 x i8] c"sem_close(3) failed\00"
@.str.3 = [15 x i8] c"fork(2) failed\00"
@.str.4 = [11 x i8] c"./sem_chld\00"
@.str.5 = [16 x i8] c"execl(2) failed\00"
@.str.6 = [18 x i8] c"waitpid(2) failed\00"
@.str.7 = [21 x i8] c"sem_unlink(3) failed\00"
define i32 @main() {
%1 = load i32, i32* @O_CREAT
%2 = load i32, i32* @O_EXCL
%3 = or i32 %2, %1
%4 = load i32, i32* @S_IRUSR
%5 = load i32, i32* @S_IWUSR
%6 = or i32 %5, %4
%7 = load i32, i32* @S_IRGRP
%8 = or i32 %6, %7
%9 = load i32, i32* @S_IWGRP
%10 = or i32 %8, %9
%11 = tail call i32* @sem_open(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %3, i32 %10, i32 1)
%12 = load i32*, i32** @SEM_FAILED
%13 = icmp eq i32* %11, %12
br i1 %13, label %14, label %18
14: ; preds = %0
%15 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = tail call i32 @exit(i32 %16)
unreachable
18: ; preds = %0
%19 = tail call i64 @sem_close(i32* %11)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%23 = tail call i64 @sem_unlink(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = tail call i32 @exit(i32 %24)
unreachable
26: ; preds = %18
%27 = tail call i64 (...) @fork()
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %42, %26
%30 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = tail call i32 @exit(i32 %31)
unreachable
33: ; preds = %26
%34 = icmp eq i64 %27, 0
br i1 %34, label %35, label %42
35: ; preds = %33
%36 = tail call i64 @execl(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32* null)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %47, %35
%39 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
%40 = load i32, i32* @EXIT_FAILURE
%41 = tail call i32 @exit(i32 %40)
unreachable
42: ; preds = %33, %35
%43 = tail call i64 (...) @fork()
%44 = icmp slt i64 %43, 0
br i1 %44, label %29, label %45
45: ; preds = %42
%46 = icmp eq i64 %43, 0
br i1 %46, label %47, label %50
47: ; preds = %45
%48 = tail call i64 @execl(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32* null)
%49 = icmp slt i64 %48, 0
br i1 %49, label %38, label %50
50: ; preds = %47, %45
%51 = tail call i64 @waitpid(i64 %27, i32* null, i32 0)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %50
%54 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0))
br label %55
55: ; preds = %50, %53
%56 = tail call i64 @waitpid(i64 %43, i32* null, i32 0)
%57 = icmp slt i64 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %55
%59 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0))
br label %60
60: ; preds = %58, %55
%61 = tail call i64 @sem_unlink(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%62 = icmp slt i64 %61, 0
br i1 %62, label %63, label %65
63: ; preds = %60
%64 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0))
br label %65
65: ; preds = %63, %60
ret i32 0
}