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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEM_UNDO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int getpid();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int read(int, int /*<<< orphan*/ *, int);
int semget(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ semop(int, struct sembuf *, int);
int /*<<< orphan*/ sleep(int);
int main(int argc, char *argv[]) {
  int sem_fd;
  key_t sem_key;
  int sem_id;
  int i;
  struct sembuf sop;
  sem_fd = open(("sem.key"), O_RDONLY);
  if (sem_fd < 0) {
    perror("Could not open sem key for reading");
    exit(1);
  }
  if (read(sem_fd, &sem_key, sizeof(key_t)) != sizeof(key_t)) {
    perror("Error reading the semaphore key");
    exit(2);
  }
  close(sem_fd);
  sem_id = semget(sem_key, 0, 0);
  if (sem_id < 0) {
    perror("Could not obtain semaphore");
    exit(3);
  }
  for (i = 0; i < 5; ++i) {
    sop.sem_num = 0;
    sop.sem_op = -1;
    sop.sem_flg = SEM_UNDO;
    printf("Client #%d waiting\n", getpid());
    semop(sem_id, &sop, 1);
    printf("Client #%d acquired. Sleeping\n", getpid());
    sleep(1);
    printf("Client #%d releasing\n", getpid());
    sop.sem_op = 1;
    semop(sem_id, &sop, 1);
  }
  exit(0);
}


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    0x2e23(%rip),%rax
mov    (%rax),%esi
lea    0xe3a(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11ee <main+0x4e>
lea    0xe29(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1070 <read@plt>
cltq
cmp    $0x4,%rax
je     1221 <main+0x81>
lea    0xe19(%rip),%rdi
call   1090 <perror@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1030 <semget@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1258 <main+0xb8>
lea    0xe02(%rip),%rdi
call   1090 <perror@plt>
mov    $0x3,%edi
call   10b0 <exit@plt>
movl   $0x0,-0x20(%rbp)
cmpl   $0x5,-0x20(%rbp)
jge    1304 <main+0x164>
movq   $0x0,-0x28(%rbp)
movl   $0xffffffff,-0x30(%rbp)
mov    0x2d71(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xdd2(%rip),%rdi
call   1050 <printf@plt>
mov    -0x1c(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x1,%edx
call   10a0 <semop@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xdc0(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xdc0(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x30(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x1,%edx
call   10a0 <semop@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    125f <main+0xbf>
xor    %edi,%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
lea    0xe42(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
jns    11dd <main+0x3d>
lea    0xe3a(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    %eax,%ebx
lea    0x14(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1070 <read@plt>
cmp    $0x4,%eax
jne    1223 <main+0x83>
mov    %ebx,%edi
call   1060 <close@plt>
mov    0x14(%rsp),%edi
xor    %esi,%esi
xor    %edx,%edx
call   1030 <semget@plt>
test   %eax,%eax
jns    1239 <main+0x99>
lea    0xe37(%rip),%rdi
call   1090 <perror@plt>
mov    $0x3,%edi
call   10b0 <exit@plt>
lea    0xe01(%rip),%rdi
call   1090 <perror@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>
mov    %eax,%ebx
movq   $0x0,0x8(%rsp)
movl   $0xffffffff,(%rsp)
mov    0x2d9e(%rip),%rbp
mov    0x0(%rbp),%eax
mov    %eax,0x4(%rsp)
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xdff(%rip),%r15
mov    %r15,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    %rsp,%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   10a0 <semop@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xde9(%rip),%r12
mov    %r12,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xde6(%rip),%r13
mov    %r13,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
movl   $0x1,(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   10a0 <semop@plt>
movq   $0x0,0x8(%rsp)
movl   $0xffffffff,(%rsp)
mov    0x0(%rbp),%eax
mov    %eax,0x4(%rsp)
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r15,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   10a0 <semop@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r12,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r13,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
movl   $0x1,(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   10a0 <semop@plt>
movq   $0x0,0x8(%rsp)
movl   $0xffffffff,(%rsp)
mov    0x0(%rbp),%eax
mov    %eax,0x4(%rsp)
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r15,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   10a0 <semop@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r12,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r13,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
movl   $0x1,(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   10a0 <semop@plt>
movq   $0x0,0x8(%rsp)
movl   $0xffffffff,(%rsp)
mov    0x0(%rbp),%eax
mov    %eax,0x4(%rsp)
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r15,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   10a0 <semop@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r12,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r13,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
movl   $0x1,(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   10a0 <semop@plt>
movq   $0x0,0x8(%rsp)
movl   $0xffffffff,(%rsp)
mov    0x0(%rbp),%eax
mov    %eax,0x4(%rsp)
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r15,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   10a0 <semop@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r12,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <sleep@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r13,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
movl   $0x1,(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   10a0 <semop@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.sembuf = type { i32, i32, i64 }
@.str = [8 x i8] c"sem.key\00"
@O_RDONLY = global i32 0
@.str.1 = [35 x i8] c"Could not open sem key for reading\00"
@.str.2 = [32 x i8] c"Error reading the semaphore key\00"
@.str.3 = [27 x i8] c"Could not obtain semaphore\00"
@SEM_UNDO = global i32 0
@.str.4 = [20 x i8] c"Client #%d waiting\0A\00"
@.str.5 = [31 x i8] c"Client #%d acquired. Sleeping\0A\00"
@.str.6 = [22 x i8] c"Client #%d releasing\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 %struct.sembuf
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %11)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i32, i32* %6
%20 = call i32 @read(i32 %19, i32* %7, i32 4)
%21 = sext i32 %20 to i64
%22 = icmp ne i64 %21, 4
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%25 = call i32 @exit(i32 2)
unreachable
26: ; preds = %18
%27 = load i32, i32* %6
%28 = call i32 @close(i32 %27)
%29 = load i32, i32* %7
%30 = call i32 @semget(i32 %29, i32 0, i32 0)
store i32 %30, i32* %8
%31 = load i32, i32* %8
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %26
%34 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%35 = call i32 @exit(i32 3)
unreachable
36: ; preds = %26
store i32 0, i32* %9
br label %37
37: ; preds = %57, %36
%38 = load i32, i32* %9
%39 = icmp slt i32 %38, 5
br i1 %39, label %40, label %60
40: ; preds = %37
%41 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %10, i32 0, i32 2
store i64 0, i64* %41
%42 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %10, i32 0, i32 0
store i32 -1, i32* %42
%43 = load i32, i32* @SEM_UNDO
%44 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %10, i32 0, i32 1
store i32 %43, i32* %44
%45 = call i32 (...) @getpid()
%46 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %45)
%47 = load i32, i32* %8
%48 = call i32 @semop(i32 %47, %struct.sembuf* %10, i32 1)
%49 = call i32 (...) @getpid()
%50 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i32 %49)
%51 = call i32 @sleep(i32 1)
%52 = call i32 (...) @getpid()
%53 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i32 %52)
%54 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %10, i32 0, i32 0
store i32 1, i32* %54
%55 = load i32, i32* %8
%56 = call i32 @semop(i32 %55, %struct.sembuf* %10, i32 1)
br label %57
57: ; preds = %40
%58 = load i32, i32* %9
%59 = add nsw i32 %58, 1
store i32 %59, i32* %9
br label %37
60: ; preds = %37
%61 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.sembuf = type { i32, i32, i64 }
@.str = [8 x i8] c"sem.key\00"
@O_RDONLY = global i32 0
@.str.1 = [35 x i8] c"Could not open sem key for reading\00"
@.str.2 = [32 x i8] c"Error reading the semaphore key\00"
@.str.3 = [27 x i8] c"Could not obtain semaphore\00"
@SEM_UNDO = global i32 0
@.str.4 = [20 x i8] c"Client #%d waiting\0A\00"
@.str.5 = [31 x i8] c"Client #%d acquired. Sleeping\0A\00"
@.str.6 = [22 x i8] c"Client #%d releasing\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca %struct.sembuf
%5 = bitcast i32* %3 to i8*
%6 = bitcast %struct.sembuf* %4 to i8*
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = call i32 @read(i32 %8, i32* %3, i32 4)
%15 = icmp eq i32 %14, 4
br i1 %15, label %19, label %16
16: ; preds = %13
%17 = tail call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%18 = tail call i32 @exit(i32 2)
unreachable
19: ; preds = %13
%20 = tail call i32 @close(i32 %8)
%21 = load i32, i32* %3
%22 = tail call i32 @semget(i32 %21, i32 0, i32 0)
%23 = icmp slt i32 %22, 0
br i1 %23, label %79, label %24
24: ; preds = %19
%25 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %4, i64 0, i32 2
%26 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %4, i64 0, i32 0
%27 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %4, i64 0, i32 1
store i64 0, i64* %25
store i32 -1, i32* %26
%28 = load i32, i32* @SEM_UNDO
store i32 %28, i32* %27
%29 = call i32 (...) @getpid()
%30 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %29)
%31 = call i32 @semop(i32 %22, %struct.sembuf* %4, i32 1)
%32 = call i32 (...) @getpid()
%33 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i32 %32)
%34 = call i32 @sleep(i32 1)
%35 = call i32 (...) @getpid()
%36 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i32 %35)
store i32 1, i32* %26
%37 = call i32 @semop(i32 %22, %struct.sembuf* %4, i32 1)
store i64 0, i64* %25
store i32 -1, i32* %26
%38 = load i32, i32* @SEM_UNDO
store i32 %38, i32* %27
%39 = call i32 (...) @getpid()
%40 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %39)
%41 = call i32 @semop(i32 %22, %struct.sembuf* %4, i32 1)
%42 = call i32 (...) @getpid()
%43 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i32 %42)
%44 = call i32 @sleep(i32 1)
%45 = call i32 (...) @getpid()
%46 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i32 %45)
store i32 1, i32* %26
%47 = call i32 @semop(i32 %22, %struct.sembuf* %4, i32 1)
store i64 0, i64* %25
store i32 -1, i32* %26
%48 = load i32, i32* @SEM_UNDO
store i32 %48, i32* %27
%49 = call i32 (...) @getpid()
%50 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %49)
%51 = call i32 @semop(i32 %22, %struct.sembuf* %4, i32 1)
%52 = call i32 (...) @getpid()
%53 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i32 %52)
%54 = call i32 @sleep(i32 1)
%55 = call i32 (...) @getpid()
%56 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i32 %55)
store i32 1, i32* %26
%57 = call i32 @semop(i32 %22, %struct.sembuf* %4, i32 1)
store i64 0, i64* %25
store i32 -1, i32* %26
%58 = load i32, i32* @SEM_UNDO
store i32 %58, i32* %27
%59 = call i32 (...) @getpid()
%60 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %59)
%61 = call i32 @semop(i32 %22, %struct.sembuf* %4, i32 1)
%62 = call i32 (...) @getpid()
%63 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i32 %62)
%64 = call i32 @sleep(i32 1)
%65 = call i32 (...) @getpid()
%66 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i32 %65)
store i32 1, i32* %26
%67 = call i32 @semop(i32 %22, %struct.sembuf* %4, i32 1)
store i64 0, i64* %25
store i32 -1, i32* %26
%68 = load i32, i32* @SEM_UNDO
store i32 %68, i32* %27
%69 = call i32 (...) @getpid()
%70 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %69)
%71 = call i32 @semop(i32 %22, %struct.sembuf* %4, i32 1)
%72 = call i32 (...) @getpid()
%73 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i32 %72)
%74 = call i32 @sleep(i32 1)
%75 = call i32 (...) @getpid()
%76 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i32 %75)
store i32 1, i32* %26
%77 = call i32 @semop(i32 %22, %struct.sembuf* %4, i32 1)
%78 = call i32 @exit(i32 0)
unreachable
79: ; preds = %19
%80 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%81 = tail call i32 @exit(i32 3)
unreachable
}