#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 */
typedef int key_t;

/* Variables and functions */
int IPC_CREAT;
int /*<<< orphan*/ IPC_RMID;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
char *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmctl(int, int /*<<< orphan*/, int /*<<< orphan*/ *);
scalar_t__ shmdt(char *);
int shmget(int, int, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main(int argc, char **argv) {
  char tmp;
  int shmid;
  key_t key;
  char *shm;
  key = 1234;
  if ((shmid = shmget(key, 1024, IPC_CREAT | 0666)) < 0) {
    perror("shmget");
    return 1;
  }
  if ((shm = shmat(shmid, NULL, 0)) == (char *)-1) {
    perror("shmat");
    return 1;
  }
  memset(shm, 0, 1024);
  while (*shm != 'q') {
    sleep(1);
    if (tmp == *shm)
      continue;
    fprintf(stdout, "You pressed %c\n", *shm);
    tmp = *shm;
  }
  if (shmdt(shm) != 0)
    fprintf(stderr, "Could not close memory segment.\n");
  if (shmctl(shmid, IPC_RMID, NULL) == -1) {
    fprintf(stderr, "Could not destroy memory segment. \n");
  }
  return 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)
movl   $0x4d2,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2e21(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    $0x400,%esi
call   1090 <shmget@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11dd <main+0x5d>
lea    0xe34(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12eb <main+0x16b>
mov    -0x18(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1080 <shmat@plt>
mov    %rax,-0x28(%rbp)
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    1217 <main+0x97>
lea    0xe01(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12eb <main+0x16b>
mov    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1040 <memset@plt>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x71,%eax
je     1285 <main+0x105>
mov    $0x1,%edi
call   10a0 <sleep@plt>
movsbl -0x11(%rbp),%eax
mov    -0x28(%rbp),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    1259 <main+0xd9>
jmp    1227 <main+0xa7>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rax
movsbl (%rax),%edx
lea    0xd9d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x11(%rbp)
jmp    1227 <main+0xa7>
mov    -0x28(%rbp),%rdi
call   1030 <shmdt@plt>
cmp    $0x0,%rax
je     12af <main+0x12f>
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
lea    0xd75(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%edi
mov    0x2d1f(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1060 <shmctl@plt>
cmp    $0xffffffff,%eax
jne    12e4 <main+0x164>
mov    0x2d0c(%rip),%rax
mov    (%rax),%edi
lea    0xd61(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2e39(%rip),%rax
mov    $0x1b6,%edx
or     (%rax),%edx
mov    $0x4d2,%edi
mov    $0x400,%esi
call   1090 <shmget@plt>
test   %eax,%eax
js     1273 <main+0xf3>
mov    %eax,%r12d
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     127c <main+0xfc>
mov    %rax,%rbp
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1040 <memset@plt>
mov    0x0(%rbp),%bl
mov    0x2def(%rip),%r15
lea    0xe25(%rip),%r14
nopl   0x0(%rax,%rax,1)
cmp    $0x71,%bl
je     121a <main+0x9a>
mov    $0x1,%edi
call   10a0 <sleep@plt>
movsbl 0x0(%rbp),%edx
cmp    %dl,%bl
je     11f0 <main+0x70>
mov    (%r15),%edi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
movzbl 0x0(%rbp),%ebx
jmp    11f0 <main+0x70>
mov    %rbp,%rdi
call   1030 <shmdt@plt>
test   %rax,%rax
je     123e <main+0xbe>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
lea    0xde6(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    0x2d93(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
mov    %r12d,%edi
xor    %edx,%edx
call   1060 <shmctl@plt>
cmp    $0xffffffff,%eax
jne    128d <main+0x10d>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
lea    0xdd6(%rip),%rsi
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    128d <main+0x10d>
lea    0xd86(%rip),%rdi
jmp    1283 <main+0x103>
lea    0xd84(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@IPC_CREAT = global i32 0
@.str = [7 x i8] c"shmget\00"
@.str.1 = [6 x i8] c"shmat\00"
@stdout = global i32 0
@.str.2 = [16 x i8] c"You pressed %c\0A\00"
@stderr = global i32 0
@.str.3 = [33 x i8] c"Could not close memory segment.\0A\00"
@IPC_RMID = global i32 0
@.str.4 = [36 x i8] c"Could not destroy memory segment. \0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1234, i32* %8
%10 = load i32, i32* %8
%11 = load i32, i32* @IPC_CREAT
%12 = or i32 %11, 438
%13 = call i32 @shmget(i32 %10, i32 1024, i32 %12)
store i32 %13, i32* %7
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %64
17: ; preds = %2
%18 = load i32, i32* %7
%19 = call i8* @shmat(i32 %18, i32* null, i32 0)
store i8* %19, i8** %9
%20 = icmp eq i8* %19, inttoptr (i64 -1 to i8*)
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %64
23: ; preds = %17
%24 = load i8*, i8** %9
%25 = call i32 @memset(i8* %24, i32 0, i32 1024)
br label %26
26: ; preds = %40, %39, %23
%27 = load i8*, i8** %9
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = icmp ne i32 %29, 113
br i1 %30, label %31, label %48
31: ; preds = %26
%32 = call i32 @sleep(i32 1)
%33 = load i8, i8* %6
%34 = sext i8 %33 to i32
%35 = load i8*, i8** %9
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %34, %37
br i1 %38, label %39, label %40
39: ; preds = %31
br label %26
40: ; preds = %31
%41 = load i32, i32* @stdout
%42 = load i8*, i8** %9
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %44)
%46 = load i8*, i8** %9
%47 = load i8, i8* %46
store i8 %47, i8* %6
br label %26
48: ; preds = %26
%49 = load i8*, i8** %9
%50 = call i64 @shmdt(i8* %49)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %48
%53 = load i32, i32* @stderr
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
br label %55
55: ; preds = %52, %48
%56 = load i32, i32* %7
%57 = load i32, i32* @IPC_RMID
%58 = call i32 @shmctl(i32 %56, i32 %57, i32* null)
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %63
60: ; preds = %55
%61 = load i32, i32* @stderr
%62 = call i32 (i32, i8*, ...) @fprintf(i32 %61, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
br label %63
63: ; preds = %60, %55
store i32 0, i32* %3
br label %64
64: ; preds = %63, %21, %15
%65 = load i32, i32* %3
ret i32 %65
}

LLVM IR (-O2):
@IPC_CREAT = global i32 0
@.str = [7 x i8] c"shmget\00"
@.str.1 = [6 x i8] c"shmat\00"
@stdout = global i32 0
@.str.2 = [16 x i8] c"You pressed %c\0A\00"
@stderr = global i32 0
@.str.3 = [33 x i8] c"Could not close memory segment.\0A\00"
@IPC_RMID = global i32 0
@.str.4 = [36 x i8] c"Could not destroy memory segment. \0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @IPC_CREAT
%4 = or i32 %3, 438
%5 = tail call i32 @shmget(i32 1234, i32 1024, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %45
9: ; preds = %2
%10 = tail call i8* @shmat(i32 %5, i32* null, i32 0)
%11 = icmp eq i8* %10, inttoptr (i64 -1 to i8*)
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %45
14: ; preds = %9
%15 = tail call i32 @memset(i8* %10, i32 0, i32 1024)
%16 = load i8, i8* %10
br label %17
17: ; preds = %14, %27
%18 = phi i8 [ %16, %14 ], [ %31, %27 ]
%19 = phi i8 [ undef, %14 ], [ %31, %27 ]
br label %20
20: ; preds = %17, %23
%21 = phi i8 [ %19, %23 ], [ %18, %17 ]
%22 = icmp eq i8 %21, 113
br i1 %22, label %32, label %23
23: ; preds = %20
%24 = tail call i32 @sleep(i32 1)
%25 = load i8, i8* %10
%26 = icmp eq i8 %19, %25
br i1 %26, label %20, label %27
27: ; preds = %23
%28 = sext i8 %25 to i32
%29 = load i32, i32* @stdout
%30 = tail call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %28)
%31 = load i8, i8* %10
br label %17
32: ; preds = %20
%33 = tail call i64 @shmdt(i8* %10)
%34 = icmp eq i64 %33, 0
br i1 %34, label %38, label %35
35: ; preds = %32
%36 = load i32, i32* @stderr
%37 = tail call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
br label %38
38: ; preds = %35, %32
%39 = load i32, i32* @IPC_RMID
%40 = tail call i32 @shmctl(i32 %5, i32 %39, i32* null)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %45
42: ; preds = %38
%43 = load i32, i32* @stderr
%44 = tail call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
br label %45
45: ; preds = %38, %42, %12, %7
%46 = phi i32 [ 1, %7 ], [ 1, %12 ], [ 0, %42 ], [ 0, %38 ]
ret i32 %46
}