#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 /*<<< orphan*/ GETVAL;
scalar_t__ P(int, int);
scalar_t__ V(int, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ perror(char *);
scalar_t__ semctl(int, int, int /*<<< orphan*/, int /*<<< orphan*/);
int semget(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  char *ingredient[3] = {"Tobacco", "Paper", "Matches"};
  int smoker = argv[1][0] - '0';
  int semid;
  fprintf(stdout, "I'm a smoker. I have %s\n", ingredient[smoker]);
  if ((semid = semget(0x12345678, 0, 0)) < 0)
    do {
      perror("semget");
      exit(1);
    } while (0);
  if (semctl(semid, smoker, GETVAL, 0) < 0)
    do {
      perror("semctl");
      exit(1);
    } while (0);
  while (1) {
    fprintf(stdout, "%s: I'm waiting for the agent.n", ingredient[smoker]);
    sleep(5);
    if (P(semid, smoker) < 0)
      do {
        perror("P failed");
        exit(1);
      } while (0);
    fprintf(stdout, "%s: I get the ingredients, I'm rolling and smoking now.n",
            ingredient[smoker]);
    sleep(5);
    if (V(semid, 3) < 0)
      do {
        perror("V 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    0x2c53(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0x2c50(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0x2c4d(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
movsbl (%rax),%eax
sub    $0x30,%eax
mov    %eax,-0x34(%rbp)
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
movslq -0x34(%rbp),%rax
mov    -0x30(%rbp,%rax,8),%rdx
lea    0xe35(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x12345678,%edi
xor    %edx,%edx
mov    %edx,%esi
call   1030 <semget@plt>
mov    %eax,-0x38(%rbp)
cmp    $0x0,%eax
jge    121b <main+0x9b>
jmp    1205 <main+0x85>
lea    0xe23(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x38(%rbp),%edi
mov    -0x34(%rbp),%esi
mov    0x2db0(%rip),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
call   1080 <semctl@plt>
cmp    $0x0,%rax
jge    1256 <main+0xd6>
jmp    1240 <main+0xc0>
lea    0xdef(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    125b <main+0xdb>
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
movslq -0x34(%rbp),%rax
mov    -0x30(%rbp,%rax,8),%rdx
lea    0xdc9(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x5,%edi
call   10a0 <sleep@plt>
mov    -0x38(%rbp),%edi
mov    -0x34(%rbp),%esi
call   1040 <P@plt>
cmp    $0x0,%rax
jge    12b3 <main+0x133>
jmp    129d <main+0x11d>
lea    0xdb9(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2d16(%rip),%rax
mov    (%rax),%edi
movslq -0x34(%rbp),%rax
mov    -0x30(%rbp,%rax,8),%rdx
lea    0xd9a(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x5,%edi
call   10a0 <sleep@plt>
mov    -0x38(%rbp),%edi
mov    $0x3,%esi
call   1050 <V@plt>
cmp    $0x0,%rax
jge    130d <main+0x18d>
jmp    12f7 <main+0x177>
lea    0xda1(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    125b <main+0xdb>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x8(%rsi),%rax
movsbq (%rax),%r14
mov    0x2e36(%rip),%r13
mov    0x0(%r13),%edi
lea    0x2c4b(%rip),%rax
mov    -0x180(%rax,%r14,8),%rbx
ff
lea    0xe62(%rip),%rsi
mov    %rbx,%rdx
call   1060 <fprintf@plt>
mov    $0x12345678,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1030 <semget@plt>
test   %eax,%eax
jns    11da <main+0x5a>
lea    0xe5a(%rip),%rdi
jmp    1282 <main+0x102>
mov    %eax,%ebp
add    $0xffffffffffffffd0,%r14
mov    0x2df1(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    %r14d,%esi
xor    %ecx,%ecx
call   1080 <semctl@plt>
test   %rax,%rax
jns    1203 <main+0x83>
lea    0xe35(%rip),%rdi
jmp    1282 <main+0x102>
lea    0xe33(%rip),%r15
lea    0xe55(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x0(%r13),%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1060 <fprintf@plt>
mov    $0x5,%edi
call   10a0 <sleep@plt>
mov    %ebp,%edi
mov    %r14d,%esi
call   1040 <P@plt>
test   %rax,%rax
js     127b <main+0xfb>
mov    0x0(%r13),%edi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1060 <fprintf@plt>
mov    $0x5,%edi
call   10a0 <sleep@plt>
mov    %ebp,%edi
mov    $0x3,%esi
call   1050 <V@plt>
test   %rax,%rax
jns    1220 <main+0xa0>
lea    0xe26(%rip),%rdi
jmp    1282 <main+0x102>
lea    0xddb(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"Tobacco\00"
@.str.1 = [6 x i8] c"Paper\00"
@.str.2 = [8 x i8] c"Matches\00"
@__const.main.ingredient = [3 x i8*] [i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i32 0, i32 0)]
@stdout = global i32 0
@.str.3 = [25 x i8] c"I'm a smoker. I have %s\0A\00"
@.str.4 = [7 x i8] c"semget\00"
@GETVAL = global i32 0
@.str.5 = [7 x i8] c"semctl\00"
@.str.6 = [32 x i8] c"%s: I'm waiting for the agent.n\00"
@.str.7 = [9 x i8] c"P failed\00"
@.str.8 = [57 x i8] c"%s: I get the ingredients, I'm rolling and smoking now.n\00"
@.str.9 = [9 x i8] c"V 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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = bitcast [3 x i8*]* %6 to i8*
%10 = load i8**, i8*** %5
%11 = getelementptr inbounds i8*, i8** %10, i64 1
%12 = load i8*, i8** %11
%13 = getelementptr inbounds i8, i8* %12, i64 0
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = sub nsw i32 %15, 48
store i32 %16, i32* %7
%17 = load i32, i32* @stdout
%18 = load i32, i32* %7
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds [3 x i8*], [3 x i8*]* %6, i64 0, i64 %19
%21 = load i8*, i8** %20
%22 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %21)
%23 = call i32 @semget(i32 305419896, i32 0, i32 0)
store i32 %23, i32* %8
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %2
br label %26
26: ; preds = %25
%27 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; No predecessors!
br label %30
30: ; preds = %29, %2
%31 = load i32, i32* %8
%32 = load i32, i32* %7
%33 = load i32, i32* @GETVAL
%34 = call i64 @semctl(i32 %31, i32 %32, i32 %33, i32 0)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %30
br label %37
37: ; preds = %36
%38 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; No predecessors!
br label %41
41: ; preds = %40, %30
br label %42
42: ; preds = %41, %75
%43 = load i32, i32* @stdout
%44 = load i32, i32* %7
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds [3 x i8*], [3 x i8*]* %6, i64 0, i64 %45
%47 = load i8*, i8** %46
%48 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0), i8* %47)
%49 = call i32 @sleep(i32 5)
%50 = load i32, i32* %8
%51 = load i32, i32* %7
%52 = call i64 @P(i32 %50, i32 %51)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %59
54: ; preds = %42
br label %55
55: ; preds = %54
%56 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0))
%57 = call i32 @exit(i32 1)
unreachable
58: ; No predecessors!
br label %59
59: ; preds = %58, %42
%60 = load i32, i32* @stdout
%61 = load i32, i32* %7
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds [3 x i8*], [3 x i8*]* %6, i64 0, i64 %62
%64 = load i8*, i8** %63
%65 = call i32 @fprintf(i32 %60, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.8, i64 0, i64 0), i8* %64)
%66 = call i32 @sleep(i32 5)
%67 = load i32, i32* %8
%68 = call i64 @V(i32 %67, i32 3)
%69 = icmp slt i64 %68, 0
br i1 %69, label %70, label %75
70: ; preds = %59
br label %71
71: ; preds = %70
%72 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.9, i64 0, i64 0))
%73 = call i32 @exit(i32 1)
unreachable
74: ; No predecessors!
br label %75
75: ; preds = %74, %59
br label %42
}

LLVM IR (-O2):
@.str = [8 x i8] c"Tobacco\00"
@.str.1 = [6 x i8] c"Paper\00"
@.str.2 = [8 x i8] c"Matches\00"
@__const.main.ingredient = [3 x i8*] [i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i32 0, i32 0)]
@stdout = global i32 0
@.str.3 = [25 x i8] c"I'm a smoker. I have %s\0A\00"
@.str.4 = [7 x i8] c"semget\00"
@GETVAL = global i32 0
@.str.5 = [7 x i8] c"semctl\00"
@.str.6 = [32 x i8] c"%s: I'm waiting for the agent.n\00"
@.str.7 = [9 x i8] c"P failed\00"
@.str.8 = [57 x i8] c"%s: I get the ingredients, I'm rolling and smoking now.n\00"
@.str.9 = [9 x i8] c"V failed\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = getelementptr inbounds i8*, i8** %1, i64 1
%4 = load i8*, i8** %3
%5 = load i8, i8* %4
%6 = sext i8 %5 to i32
%7 = add nsw i32 %6, -48
%8 = load i32, i32* @stdout
%9 = sext i32 %7 to i64
%10 = getelementptr inbounds [3 x i8*], [3 x i8*]* @__const.main.ingredient, i64 0, i64 %9
%11 = load i8*, i8** %10
%12 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %11)
%13 = tail call i32 @semget(i32 305419896, i32 0, i32 0)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i32, i32* @GETVAL
%20 = tail call i64 @semctl(i32 %13, i32 %7, i32 %19, i32 0)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%24 = tail call i32 @exit(i32 1)
unreachable
25: ; preds = %18, %34
%26 = load i32, i32* @stdout
%27 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0), i8* %11)
%28 = tail call i32 @sleep(i32 5)
%29 = tail call i64 @P(i32 %13, i32 %7)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %25
%32 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0))
%33 = tail call i32 @exit(i32 1)
unreachable
34: ; preds = %25
%35 = load i32, i32* @stdout
%36 = tail call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str.8, i64 0, i64 0), i8* %11)
%37 = tail call i32 @sleep(i32 5)
%38 = tail call i64 @V(i32 %13, i32 3)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %25
40: ; preds = %34
%41 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.9, i64 0, i64 0))
%42 = tail call i32 @exit(i32 1)
unreachable
}