#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_num;
  int sem_op;
  void *sem_flg;
};

/* Variables and functions */
void *SEM_UNDO;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char);
int semget(int, int, int);
int semop(int, struct sembuf *, int);
char *shmat(int, int /*<<< orphan*/, int /*<<< orphan*/);
int shmget(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int main() {
  int id, shmid, retval;
  struct sembuf operations[2];
  char *shared;
  id = semget(0xABC, 2, 0666);
  if (id < 0) {
    perror("Could not create semaphore, exiting.");
    exit(1);
  }
  shmid = shmget(0xABC, 1, 0);
  if (shmid == -1) {
    perror("shmget");
    exit(1);
  }
  shared = shmat(shmid, 0, 0);
  if ((int)shared == -1) {
    perror("shmat");
    exit(1);
  }
  while (1) {
    operations[0].sem_num = 1;
    operations[0].sem_op = -1;
    operations[0].sem_flg = SEM_UNDO;
    retval = semop(id, operations, 1);
    if (retval != 0) {
      perror("semop");
      exit(1);
    }
    operations[0].sem_num = 1;
    operations[0].sem_op = 1;
    operations[0].sem_flg = SEM_UNDO;
    operations[1].sem_num = 0;
    operations[1].sem_op = 1;
    operations[1].sem_flg = SEM_UNDO;
    retval = semop(id, operations, 2);
    if (retval != 0) {
      perror("semop");
      exit(1);
    }
    printf("Read '%c' sleeping\n", *shared);
    sleep(2);
    operations[0].sem_num = 0;
    operations[0].sem_op = -1;
    operations[0].sem_flg = SEM_UNDO;
    retval = semop(id, operations, 1);
    if (retval != 0) {
      perror("semop");
      exit(1);
    }
    sleep(5);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0xabc,%edi
mov    $0x2,%esi
mov    $0x1b6,%edx
call   1030 <semget@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11c6 <main+0x46>
lea    0xe49(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    $0xabc,%edi
mov    $0x1,%esi
xor    %edx,%edx
call   1070 <shmget@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11fa <main+0x7a>
lea    0xe3a(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0xc(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1060 <shmat@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    $0xffffffff,%eax
jne    122d <main+0xad>
lea    0xe0e(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    1232 <main+0xb2>
movl   $0x1,-0x30(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
mov    0x2da9(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x1,%edx
call   1080 <semop@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     1282 <main+0x102>
lea    0xdbf(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
movl   $0x1,-0x30(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    0x2d59(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    0x2d3d(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x2,%edx
call   1080 <semop@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     12ee <main+0x16e>
lea    0xd53(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x38(%rbp),%rax
lea    0xd3f(%rip),%rdi
movsbl (%rax),%esi
call   1040 <printf@plt>
mov    $0x2,%edi
call   10a0 <sleep@plt>
movl   $0x0,-0x30(%rbp)
movl   $0xffffffff,-0x2c(%rbp)
mov    0x2cd0(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x1,%edx
call   1080 <semop@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     135b <main+0x1db>
lea    0xce6(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    $0x5,%edi
call   10a0 <sleep@plt>
jmp    1232 <main+0xb2>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    $0xabc,%edi
mov    $0x2,%esi
mov    $0x1b6,%edx
call   1030 <semget@plt>
test   %eax,%eax
jns    11b2 <main+0x32>
lea    0xe53(%rip),%rdi
jmp    12f3 <main+0x173>
mov    %eax,%ebx
mov    $0xabc,%edi
mov    $0x1,%esi
xor    %edx,%edx
call   1070 <shmget@plt>
cmp    $0xffffffff,%eax
jne    11d6 <main+0x56>
lea    0xe54(%rip),%rdi
jmp    12f3 <main+0x173>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
mov    %rax,%r14
cmp    $0xffffffff,%r14d
jne    11f6 <main+0x76>
lea    0xe3b(%rip),%rdi
jmp    12f3 <main+0x173>
movabs $0xffffffff00000001,%rax
ff ff ff
mov    %rax,(%rsp)
mov    0x2de5(%rip),%rbp
mov    0x0(%rbp),%rax
mov    %rax,0x8(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1080 <semop@plt>
test   %eax,%eax
jne    12ec <main+0x16c>
mov    %rsp,%r15
lea    0xe03(%rip),%r12
movabs $0xffffffff00000000,%r13
ff ff ff
nop
movabs $0x100000001,%rax
mov    %rax,(%rsp)
mov    0x0(%rbp),%rax
mov    %rax,0x8(%rsp)
movabs $0x100000000,%rcx
mov    %rcx,0x10(%rsp)
mov    %rax,0x18(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x2,%edx
call   1080 <semop@plt>
test   %eax,%eax
jne    12ec <main+0x16c>
movsbl (%r14),%esi
mov    %r12,%rdi
call   1040 <printf@plt>
mov    $0x2,%edi
call   10a0 <sleep@plt>
mov    %r13,(%rsp)
mov    0x0(%rbp),%rax
mov    %rax,0x8(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1080 <semop@plt>
test   %eax,%eax
jne    12ec <main+0x16c>
mov    $0x5,%edi
call   10a0 <sleep@plt>
movabs $0xffffffff00000001,%rax
ff ff ff
mov    %rax,(%rsp)
mov    0x0(%rbp),%rax
mov    %rax,0x8(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1080 <semop@plt>
test   %eax,%eax
je     1240 <main+0xc0>
lea    0xd3f(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.sembuf = type { i32, i32, i8* }
@.str = [37 x i8] c"Could not create semaphore, exiting.\00"
@.str.1 = [7 x i8] c"shmget\00"
@.str.2 = [6 x i8] c"shmat\00"
@SEM_UNDO = global i8* null
@.str.3 = [6 x i8] c"semop\00"
@.str.4 = [20 x i8] c"Read '%c' sleeping\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [2 x %struct.sembuf]
%6 = alloca i8*
store i32 0, i32* %1
%7 = call i32 @semget(i32 2748, i32 2, i32 438)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = call i32 @shmget(i32 2748, i32 1, i32 0)
store i32 %14, i32* %3
%15 = load i32, i32* %3
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %13
%21 = load i32, i32* %3
%22 = call i8* @shmat(i32 %21, i32 0, i32 0)
store i8* %22, i8** %6
%23 = load i8*, i8** %6
%24 = ptrtoint i8* %23 to i32
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %20
%27 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %20
br label %30
30: ; preds = %29, %89
%31 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %5, i64 0, i64 0
%32 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %31, i32 0, i32 0
store i32 1, i32* %32
%33 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %5, i64 0, i64 0
%34 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %33, i32 0, i32 1
store i32 -1, i32* %34
%35 = load i8*, i8** @SEM_UNDO
%36 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %5, i64 0, i64 0
%37 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %36, i32 0, i32 2
store i8* %35, i8** %37
%38 = load i32, i32* %2
%39 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %5, i64 0, i64 0
%40 = call i32 @semop(i32 %38, %struct.sembuf* %39, i32 1)
store i32 %40, i32* %4
%41 = load i32, i32* %4
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %30
%44 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %30
%47 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %5, i64 0, i64 0
%48 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %47, i32 0, i32 0
store i32 1, i32* %48
%49 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %5, i64 0, i64 0
%50 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %49, i32 0, i32 1
store i32 1, i32* %50
%51 = load i8*, i8** @SEM_UNDO
%52 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %5, i64 0, i64 0
%53 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %52, i32 0, i32 2
store i8* %51, i8** %53
%54 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %5, i64 0, i64 1
%55 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %54, i32 0, i32 0
store i32 0, i32* %55
%56 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %5, i64 0, i64 1
%57 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %56, i32 0, i32 1
store i32 1, i32* %57
%58 = load i8*, i8** @SEM_UNDO
%59 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %5, i64 0, i64 1
%60 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %59, i32 0, i32 2
store i8* %58, i8** %60
%61 = load i32, i32* %2
%62 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %5, i64 0, i64 0
%63 = call i32 @semop(i32 %61, %struct.sembuf* %62, i32 2)
store i32 %63, i32* %4
%64 = load i32, i32* %4
%65 = icmp ne i32 %64, 0
br i1 %65, label %66, label %69
66: ; preds = %46
%67 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%68 = call i32 @exit(i32 1)
unreachable
69: ; preds = %46
%70 = load i8*, i8** %6
%71 = load i8, i8* %70
%72 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8 signext %71)
%73 = call i32 @sleep(i32 2)
%74 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %5, i64 0, i64 0
%75 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %74, i32 0, i32 0
store i32 0, i32* %75
%76 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %5, i64 0, i64 0
%77 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %76, i32 0, i32 1
store i32 -1, i32* %77
%78 = load i8*, i8** @SEM_UNDO
%79 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %5, i64 0, i64 0
%80 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %79, i32 0, i32 2
store i8* %78, i8** %80
%81 = load i32, i32* %2
%82 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %5, i64 0, i64 0
%83 = call i32 @semop(i32 %81, %struct.sembuf* %82, i32 1)
store i32 %83, i32* %4
%84 = load i32, i32* %4
%85 = icmp ne i32 %84, 0
br i1 %85, label %86, label %89
86: ; preds = %69
%87 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%88 = call i32 @exit(i32 1)
unreachable
89: ; preds = %69
%90 = call i32 @sleep(i32 5)
br label %30
}

LLVM IR (-O2):
%struct.sembuf = type { i32, i32, i8* }
@.str = [37 x i8] c"Could not create semaphore, exiting.\00"
@.str.1 = [7 x i8] c"shmget\00"
@.str.2 = [6 x i8] c"shmat\00"
@SEM_UNDO = global i8* null
@.str.3 = [6 x i8] c"semop\00"
@.str.4 = [20 x i8] c"Read '%c' sleeping\0A\00"
define i32 @main() {
%1 = alloca [2 x %struct.sembuf]
%2 = bitcast [2 x %struct.sembuf]* %1 to i8*
%3 = tail call i32 @semget(i32 2748, i32 2, i32 438)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = tail call i32 @shmget(i32 2748, i32 1, i32 0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %8
%15 = tail call i8* @shmat(i32 %9, i32 0, i32 0)
%16 = ptrtoint i8* %15 to i64
%17 = trunc i64 %16 to i32
%18 = icmp eq i32 %17, -1
br i1 %18, label %31, label %19
19: ; preds = %14
%20 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %1, i64 0, i64 0
%21 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %1, i64 0, i64 0, i32 0
%22 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %1, i64 0, i64 0, i32 1
%23 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %1, i64 0, i64 0, i32 2
store i32 1, i32* %21
store i32 -1, i32* %22
%24 = load i8*, i8** @SEM_UNDO
store i8* %24, i8** %23
%25 = call i32 @semop(i32 %3, %struct.sembuf* %20, i32 1)
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %34
27: ; preds = %19
%28 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %1, i64 0, i64 1, i32 0
%29 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %1, i64 0, i64 1, i32 1
%30 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %1, i64 0, i64 1, i32 2
br label %37
31: ; preds = %14
%32 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%33 = tail call i32 @exit(i32 1)
unreachable
34: ; preds = %54, %19
%35 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %27, %54
store i32 1, i32* %21
store i32 1, i32* %22
%38 = load i8*, i8** @SEM_UNDO
store i8* %38, i8** %23
store i32 0, i32* %28
store i32 1, i32* %29
store i8* %38, i8** %30
%39 = call i32 @semop(i32 %3, %struct.sembuf* %20, i32 2)
%40 = icmp eq i32 %39, 0
br i1 %40, label %44, label %41
41: ; preds = %37
%42 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %37
%45 = load i8, i8* %15
%46 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8 signext %45)
%47 = call i32 @sleep(i32 2)
store i32 0, i32* %21
store i32 -1, i32* %22
%48 = load i8*, i8** @SEM_UNDO
store i8* %48, i8** %23
%49 = call i32 @semop(i32 %3, %struct.sembuf* %20, i32 1)
%50 = icmp eq i32 %49, 0
br i1 %50, label %54, label %51
51: ; preds = %44
%52 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%53 = call i32 @exit(i32 1)
unreachable
54: ; preds = %44
%55 = call i32 @sleep(i32 5)
store i32 1, i32* %21
store i32 -1, i32* %22
%56 = load i8*, i8** @SEM_UNDO
store i8* %56, i8** %23
%57 = call i32 @semop(i32 %3, %struct.sembuf* %20, i32 1)
%58 = icmp eq i32 %57, 0
br i1 %58, label %37, label %34
}