#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 */

/* Variables and functions */
int IPC_CREAT;
scalar_t__ P(int, int);
int /*<<< orphan*/ SETVAL;
scalar_t__ V(int, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ perror(char *);
int rand();
scalar_t__ semctl(int, int, int /*<<< orphan*/, int /*<<< orphan*/);
int semget(int, int, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ srand(unsigned int);
int /*<<< orphan*/ stdout;
scalar_t__ time(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  char *ingredient[3] = {"Paper and matches", "Matches and tobacco",
                         "Tobacco and paper"};
  int ingred_type;
  int semid, i;
  srand((unsigned)time(NULL));
  fprintf(stdout, "I'm the Agent.\n");
  if ((semid = semget(0x12345678, 4, IPC_CREAT | 0660)) < 0)
    do {
      perror("semget");
      exit(1);
    } while (0);
  for (i = 0; i < 4; ++i)
    if (semctl(semid, i, SETVAL, 0) < 0)
      do {
        perror("semctl");
        exit(1);
      } while (0);
  while (1) {
    ingred_type = rand() % 3;
    fprintf(stdout, "Agent%d: I offered %s, waiting for the smoker.\n",
            ingred_type, ingredient[ingred_type]);
    sleep(5);
    if (V(semid, ingred_type) < 0)
      do {
        perror("V failed");
        exit(1);
      } while (0);
    if (P(semid, 3) < 0)
      do {
        perror("P failed");
        exit(1);
      } while (0);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2c23(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0x2c20(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0x2c1d(%rip),%rax
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1080 <time@plt>
mov    %eax,%edi
call   1060 <srand@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
lea    0xe31(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edx
or     $0x1b0,%edx
mov    $0x12345678,%edi
mov    $0x4,%esi
call   1030 <semget@plt>
mov    %eax,-0x38(%rbp)
cmp    $0x0,%eax
jge    1253 <main+0xa3>
jmp    123d <main+0x8d>
lea    0xe04(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
movl   $0x0,-0x3c(%rbp)
cmpl   $0x4,-0x3c(%rbp)
jge    12b2 <main+0x102>
mov    -0x38(%rbp),%edi
mov    -0x3c(%rbp),%esi
mov    0x2d5f(%rip),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
call   10a0 <semctl@plt>
cmp    $0x0,%rax
jge    129f <main+0xef>
jmp    1289 <main+0xd9>
lea    0xdbf(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    12a4 <main+0xf4>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    125a <main+0xaa>
jmp    12b7 <main+0x107>
mov    $0x0,%al
call   10d0 <rand@plt>
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    %edx,-0x34(%rbp)
mov    0x2d10(%rip),%rax
mov    (%rax),%edi
mov    -0x34(%rbp),%edx
movslq -0x34(%rbp),%rax
mov    -0x30(%rbp,%rax,8),%rcx
lea    0xd71(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x5,%edi
call   10c0 <sleep@plt>
mov    -0x38(%rbp),%edi
mov    -0x34(%rbp),%esi
call   1050 <V@plt>
cmp    $0x0,%rax
jge    1326 <main+0x176>
jmp    1310 <main+0x160>
lea    0xd6f(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x38(%rbp),%edi
mov    $0x3,%esi
call   1040 <P@plt>
cmp    $0x0,%rax
jge    1358 <main+0x1a8>
jmp    1342 <main+0x192>
lea    0xd46(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    12b7 <main+0x107>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
xor    %edi,%edi
call   1080 <time@plt>
mov    %eax,%edi
call   1060 <srand@plt>
mov    0x2e13(%rip),%r15
mov    (%r15),%edi
lea    0xe61(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2df3(%rip),%rax
mov    $0x1b0,%edx
or     (%rax),%edx
mov    $0x12345678,%edi
mov    $0x4,%esi
call   1030 <semget@plt>
test   %eax,%eax
jns    1208 <main+0x58>
lea    0xe42(%rip),%rdi
jmp    1269 <main+0xb9>
mov    %eax,%ebp
mov    0x2dbf(%rip),%rbx
mov    (%rbx),%edx
mov    %eax,%edi
xor    %esi,%esi
xor    %ecx,%ecx
call   10a0 <semctl@plt>
test   %rax,%rax
js     1262 <main+0xb2>
mov    (%rbx),%edx
mov    %ebp,%edi
mov    $0x1,%esi
xor    %ecx,%ecx
call   10a0 <semctl@plt>
test   %rax,%rax
js     1262 <main+0xb2>
mov    (%rbx),%edx
mov    %ebp,%edi
mov    $0x2,%esi
xor    %ecx,%ecx
call   10a0 <semctl@plt>
test   %rax,%rax
js     1262 <main+0xb2>
mov    (%rbx),%edx
mov    %ebp,%edi
mov    $0x3,%esi
xor    %ecx,%ecx
call   10a0 <semctl@plt>
test   %rax,%rax
jns    1278 <main+0xc8>
lea    0xde6(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xe19(%rip),%r12
lea    0xdd0(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   10d0 <rand@plt>
cltq
imul   $0x55555556,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
shr    $0x20,%rcx
add    %edx,%ecx
lea    (%rcx,%rcx,2),%ecx
sub    %ecx,%eax
mov    (%r15),%edi
movslq %eax,%rbx
movslq (%r12,%rbx,4),%rcx
add    %r12,%rcx
mov    %r14,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x5,%edi
call   10c0 <sleep@plt>
mov    %ebp,%edi
mov    %ebx,%esi
call   1050 <V@plt>
test   %rax,%rax
js     1300 <main+0x150>
mov    %ebp,%edi
mov    $0x3,%esi
call   1040 <P@plt>
test   %rax,%rax
jns    1290 <main+0xe0>
lea    0xd94(%rip),%rdi
jmp    1269 <main+0xb9>
lea    0xd7f(%rip),%rdi
jmp    1269 <main+0xb9>

LLVM IR (-O0):
@.str = [18 x i8] c"Paper and matches\00"
@.str.1 = [20 x i8] c"Matches and tobacco\00"
@.str.2 = [18 x i8] c"Tobacco and paper\00"
@__const.main.ingredient = [3 x i8*] [i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i32 0, i32 0)]
@stdout = global i32 0
@.str.3 = [16 x i8] c"I'm the Agent.\0A\00"
@IPC_CREAT = global i32 0
@.str.4 = [7 x i8] c"semget\00"
@SETVAL = global i32 0
@.str.5 = [7 x i8] c"semctl\00"
@.str.6 = [48 x i8] c"Agent%d: I offered %s, waiting for the smoker.\0A\00"
@.str.7 = [9 x i8] c"V failed\00"
@.str.8 = [9 x i8] c"P failed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [3 x i8*]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = bitcast [3 x i8*]* %6 to i8*
%11 = call i64 @time(i32* null)
%12 = trunc i64 %11 to i32
%13 = call i32 @srand(i32 %12)
%14 = load i32, i32* @stdout
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%16 = load i32, i32* @IPC_CREAT
%17 = or i32 %16, 432
%18 = call i32 @semget(i32 305419896, i32 4, i32 %17)
store i32 %18, i32* %8
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %2
br label %21
21: ; preds = %20
%22 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; No predecessors!
br label %25
25: ; preds = %24, %2
store i32 0, i32* %9
br label %26
26: ; preds = %41, %25
%27 = load i32, i32* %9
%28 = icmp slt i32 %27, 4
br i1 %28, label %29, label %44
29: ; preds = %26
%30 = load i32, i32* %8
%31 = load i32, i32* %9
%32 = load i32, i32* @SETVAL
%33 = call i64 @semctl(i32 %30, i32 %31, i32 %32, i32 0)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %29
br label %36
36: ; preds = %35
%37 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; No predecessors!
br label %40
40: ; preds = %39, %29
br label %41
41: ; preds = %40
%42 = load i32, i32* %9
%43 = add nsw i32 %42, 1
store i32 %43, i32* %9
br label %26
44: ; preds = %26
br label %45
45: ; preds = %44, %74
%46 = call i32 (...) @rand()
%47 = srem i32 %46, 3
store i32 %47, i32* %7
%48 = load i32, i32* @stdout
%49 = load i32, i32* %7
%50 = load i32, i32* %7
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds [3 x i8*], [3 x i8*]* %6, i64 0, i64 %51
%53 = load i8*, i8** %52
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.6, i64 0, i64 0), i32 %49, i8* %53)
%55 = call i32 @sleep(i32 5)
%56 = load i32, i32* %8
%57 = load i32, i32* %7
%58 = call i64 @V(i32 %56, i32 %57)
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %65
60: ; preds = %45
br label %61
61: ; preds = %60
%62 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0))
%63 = call i32 @exit(i32 1)
unreachable
64: ; No predecessors!
br label %65
65: ; preds = %64, %45
%66 = load i32, i32* %8
%67 = call i64 @P(i32 %66, i32 3)
%68 = icmp slt i64 %67, 0
br i1 %68, label %69, label %74
69: ; preds = %65
br label %70
70: ; preds = %69
%71 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.8, i64 0, i64 0))
%72 = call i32 @exit(i32 1)
unreachable
73: ; No predecessors!
br label %74
74: ; preds = %73, %65
br label %45
}

LLVM IR (-O2):
@.str = [18 x i8] c"Paper and matches\00"
@.str.1 = [20 x i8] c"Matches and tobacco\00"
@.str.2 = [18 x i8] c"Tobacco and paper\00"
@reltable.main = [3 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([18 x i8]* @.str to i64), i64 ptrtoint ([3 x i32]* @reltable.main to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([20 x i8]* @.str.1 to i64), i64 ptrtoint ([3 x i32]* @reltable.main to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([18 x i8]* @.str.2 to i64), i64 ptrtoint ([3 x i32]* @reltable.main to i64)) to i32)]
@stdout = global i32 0
@.str.3 = [16 x i8] c"I'm the Agent.\0A\00"
@IPC_CREAT = global i32 0
@.str.4 = [7 x i8] c"semget\00"
@SETVAL = global i32 0
@.str.5 = [7 x i8] c"semctl\00"
@.str.6 = [48 x i8] c"Agent%d: I offered %s, waiting for the smoker.\0A\00"
@.str.7 = [9 x i8] c"V failed\00"
@.str.8 = [9 x i8] c"P failed\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i64 @time(i32* null)
%4 = trunc i64 %3 to i32
%5 = tail call i32 @srand(i32 %4)
%6 = load i32, i32* @stdout
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%8 = load i32, i32* @IPC_CREAT
%9 = or i32 %8, 432
%10 = tail call i32 @semget(i32 305419896, i32 4, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %16, label %12
12: ; preds = %2
%13 = load i32, i32* @SETVAL
%14 = tail call i64 @semctl(i32 %10, i32 0, i32 %13, i32 0)
%15 = icmp slt i64 %14, 0
br i1 %15, label %31, label %19
16: ; preds = %2
%17 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %12
%20 = load i32, i32* @SETVAL
%21 = tail call i64 @semctl(i32 %10, i32 1, i32 %20, i32 0)
%22 = icmp slt i64 %21, 0
br i1 %22, label %31, label %23
23: ; preds = %19
%24 = load i32, i32* @SETVAL
%25 = tail call i64 @semctl(i32 %10, i32 2, i32 %24, i32 0)
%26 = icmp slt i64 %25, 0
br i1 %26, label %31, label %27
27: ; preds = %23
%28 = load i32, i32* @SETVAL
%29 = tail call i64 @semctl(i32 %10, i32 3, i32 %28, i32 0)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %27, %23, %19, %12
%32 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%33 = tail call i32 @exit(i32 1)
unreachable
34: ; preds = %27, %48
%35 = tail call i32 (...) @rand()
%36 = srem i32 %35, 3
%37 = load i32, i32* @stdout
%38 = sext i32 %36 to i64
%39 = shl i64 %38, 2
%40 = call i8* @llvm.load.relative.i64(i8* bitcast ([3 x i32]* @reltable.main to i8*), i64 %39)
%41 = tail call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.6, i64 0, i64 0), i32 %36, i8* %40)
%42 = tail call i32 @sleep(i32 5)
%43 = tail call i64 @V(i32 %10, i32 %36)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %34
%46 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0))
%47 = tail call i32 @exit(i32 1)
unreachable
48: ; preds = %34
%49 = tail call i64 @P(i32 %10, i32 3)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %34
51: ; preds = %48
%52 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.8, i64 0, i64 0))
%53 = tail call i32 @exit(i32 1)
unreachable
}