#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 random();
int semget(int, int, int);
int semop(int, struct sembuf *, int);
scalar_t__ shmat(int, int /*<<< orphan*/, int /*<<< orphan*/);
int shmget(int, int, int /*<<< orphan*/);
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 = (char *)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;
    operations[1].sem_num = 0;
    operations[1].sem_op = 0;
    operations[1].sem_flg = SEM_UNDO;
    retval = semop(id, operations, 2);
    if (retval != 0) {
      perror("semop");
      exit(1);
    }
    *shared = (char)(random() % 26 + 65);
    printf("Wrote '%c'\n", *shared);
    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);
    }
  }
  return 0;
}


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   1060 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    $0xabc,%edi
mov    $0x1,%esi
xor    %edx,%edx
call   1080 <shmget@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11fa <main+0x7a>
lea    0xe3a(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0xc(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1070 <shmat@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    $0xffffffff,%eax
jne    122d <main+0xad>
lea    0xe0e(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   10a0 <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)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    0x2d8d(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x2,%edx
call   1090 <semop@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     129e <main+0x11e>
lea    0xda3(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   1050 <random@plt>
mov    $0x1a,%ecx
cltd
idiv   %ecx
add    $0x41,%edx
mov    %dl,%cl
mov    -0x38(%rbp),%rax
mov    %cl,(%rax)
mov    -0x38(%rbp),%rax
lea    0xd75(%rip),%rdi
movsbl (%rax),%esi
call   1040 <printf@plt>
movl   $0x1,-0x30(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    0x2d10(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x1,%edx
call   1090 <semop@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     131b <main+0x19b>
lea    0xd26(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@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    12ba <main+0x13a>
mov    %eax,%ebx
mov    $0xabc,%edi
mov    $0x1,%esi
xor    %edx,%edx
call   1080 <shmget@plt>
cmp    $0xffffffff,%eax
jne    11d6 <main+0x56>
lea    0xe54(%rip),%rdi
jmp    12ba <main+0x13a>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <shmat@plt>
mov    %rax,%r14
cmp    $0xffffffff,%r14d
jne    11f6 <main+0x76>
lea    0xe3b(%rip),%rdi
jmp    12ba <main+0x13a>
mov    0x2df3(%rip),%rbp
mov    %rsp,%r15
lea    0xe31(%rip),%r12
movabs $0x100000001,%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movabs $0xffffffff00000001,%rax
ff ff ff
mov    %rax,(%rsp)
mov    0x0(%rbp),%rax
mov    %rax,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    %rax,0x18(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x2,%edx
call   1090 <semop@plt>
test   %eax,%eax
jne    12b3 <main+0x133>
xor    %eax,%eax
call   1050 <random@plt>
cltq
imul   $0x4ec4ec4f,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x23,%rcx
add    %edx,%ecx
lea    (%rcx,%rcx,4),%edx
lea    (%rdx,%rdx,4),%edx
add    %ecx,%edx
sub    %edx,%eax
add    $0x41,%al
mov    %al,(%r14)
movsbl %al,%esi
mov    %r12,%rdi
call   1040 <printf@plt>
mov    %r13,(%rsp)
mov    0x0(%rbp),%rax
mov    %rax,0x8(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1090 <semop@plt>
test   %eax,%eax
je     1220 <main+0xa0>
lea    0xd78(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   10a0 <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 = [12 x i8] c"Wrote '%c'\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 i64 @shmat(i32 %21, i32 0, i32 0)
%23 = inttoptr i64 %22 to i8*
store i8* %23, i8** %6
%24 = load i8*, i8** %6
%25 = ptrtoint i8* %24 to i32
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %20
%28 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %20
br label %31
31: ; preds = %30, %78
%32 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %5, i64 0, i64 0
%33 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %32, i32 0, i32 0
store i32 1, i32* %33
%34 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %5, i64 0, i64 0
%35 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %34, i32 0, i32 1
store i32 -1, i32* %35
%36 = load i8*, i8** @SEM_UNDO
%37 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %5, i64 0, i64 0
%38 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %37, i32 0, i32 2
store i8* %36, i8** %38
%39 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %5, i64 0, i64 1
%40 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %39, i32 0, i32 0
store i32 0, i32* %40
%41 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %5, i64 0, i64 1
%42 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %41, i32 0, i32 1
store i32 0, i32* %42
%43 = load i8*, i8** @SEM_UNDO
%44 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %5, i64 0, i64 1
%45 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %44, i32 0, i32 2
store i8* %43, i8** %45
%46 = load i32, i32* %2
%47 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %5, i64 0, i64 0
%48 = call i32 @semop(i32 %46, %struct.sembuf* %47, i32 2)
store i32 %48, i32* %4
%49 = load i32, i32* %4
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %54
51: ; preds = %31
%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 = %31
%55 = call i32 (...) @random()
%56 = srem i32 %55, 26
%57 = add nsw i32 %56, 65
%58 = trunc i32 %57 to i8
%59 = load i8*, i8** %6
store i8 %58, i8* %59
%60 = load i8*, i8** %6
%61 = load i8, i8* %60
%62 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8 signext %61)
%63 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %5, i64 0, i64 0
%64 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %63, i32 0, i32 0
store i32 1, i32* %64
%65 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %5, i64 0, i64 0
%66 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %65, i32 0, i32 1
store i32 1, i32* %66
%67 = load i8*, i8** @SEM_UNDO
%68 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %5, i64 0, i64 0
%69 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %68, i32 0, i32 2
store i8* %67, i8** %69
%70 = load i32, i32* %2
%71 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %5, i64 0, i64 0
%72 = call i32 @semop(i32 %70, %struct.sembuf* %71, i32 1)
store i32 %72, i32* %4
%73 = load i32, i32* %4
%74 = icmp ne i32 %73, 0
br i1 %74, label %75, label %78
75: ; preds = %54
%76 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%77 = call i32 @exit(i32 1)
unreachable
78: ; preds = %54
br label %31
}

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 = [12 x i8] c"Wrote '%c'\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 i64 @shmat(i32 %9, i32 0, i32 0)
%16 = inttoptr i64 %15 to i8*
%17 = trunc i64 %15 to i32
%18 = icmp eq i32 %17, -1
br i1 %18, label %27, 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
%24 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %1, i64 0, i64 1, i32 0
%25 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %1, i64 0, i64 1, i32 1
%26 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %1, i64 0, i64 1, i32 2
br label %30
27: ; preds = %14
%28 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%29 = tail call i32 @exit(i32 1)
unreachable
30: ; preds = %19, %37
store i32 1, i32* %21
store i32 -1, i32* %22
%31 = load i8*, i8** @SEM_UNDO
store i8* %31, i8** %23
store i32 0, i32* %24
store i32 0, i32* %25
store i8* %31, i8** %26
%32 = call i32 @semop(i32 %3, %struct.sembuf* %20, i32 2)
%33 = icmp eq i32 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %30
%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 = %30
%38 = call i32 (...) @random()
%39 = srem i32 %38, 26
%40 = trunc i32 %39 to i8
%41 = add nsw i8 %40, 65
store i8 %41, i8* %16
%42 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8 signext %41)
store i32 1, i32* %21
store i32 1, i32* %22
%43 = load i8*, i8** @SEM_UNDO
store i8* %43, i8** %23
%44 = call i32 @semop(i32 %3, %struct.sembuf* %20, i32 1)
%45 = icmp eq i32 %44, 0
br i1 %45, label %30, label %46
46: ; preds = %37
%47 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%48 = call i32 @exit(i32 1)
unreachable
}