#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*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
char *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
scalar_t__ shmdt(char *);
int shmget(int, int, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
main(int argc, char **argv) {
  char c, tmp;
  int shmid;
  key_t key;
  char *shm, *s;
  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);
  s = shm;
  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");
  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    0x2e39(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    $0x400,%esi
call   1080 <shmget@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11cd <main+0x5d>
lea    0xe44(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ae <main+0x13e>
mov    -0x18(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1070 <shmat@plt>
mov    %rax,-0x28(%rbp)
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    1207 <main+0x97>
lea    0xe11(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ae <main+0x13e>
mov    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1040 <memset@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x71,%eax
je     127d <main+0x10d>
mov    $0x1,%edi
call   1090 <sleep@plt>
movsbl -0x12(%rbp),%eax
mov    -0x28(%rbp),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    1251 <main+0xe1>
jmp    121f <main+0xaf>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rax
movsbl (%rax),%edx
lea    0xda5(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x12(%rbp)
jmp    121f <main+0xaf>
mov    -0x28(%rbp),%rdi
call   1030 <shmdt@plt>
cmp    $0x0,%rax
je     12a7 <main+0x137>
mov    0x2d49(%rip),%rax
mov    (%rax),%edi
lea    0xd7d(%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   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    0x2e51(%rip),%rax
mov    $0x1b6,%edx
or     (%rax),%edx
mov    $0x4d2,%edi
mov    $0x400,%esi
call   1080 <shmget@plt>
test   %eax,%eax
js     1238 <main+0xc8>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     1241 <main+0xd1>
mov    %rax,%r12
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1040 <memset@plt>
mov    (%r12),%bl
mov    0x2e09(%rip),%r15
lea    0xe37(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
cmp    $0x71,%bl
je     120c <main+0x9c>
mov    $0x1,%edi
call   1090 <sleep@plt>
movsbl (%r12),%edx
cmp    %dl,%bl
je     11e0 <main+0x70>
mov    (%r15),%edi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
movzbl (%r12),%ebx
jmp    11e0 <main+0x70>
mov    %r12,%rdi
call   1030 <shmdt@plt>
test   %rax,%rax
je     1234 <main+0xc4>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
lea    0xdf4(%rip),%rsi
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1252 <main+0xe2>
xor    %ebx,%ebx
jmp    1252 <main+0xe2>
lea    0xdc1(%rip),%rdi
jmp    1248 <main+0xd8>
lea    0xdbf(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
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"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8
%7 = alloca i8
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1234, i32* %9
%12 = load i32, i32* %9
%13 = load i32, i32* @IPC_CREAT
%14 = or i32 %13, 438
%15 = call i32 @shmget(i32 %12, i32 1024, i32 %14)
store i32 %15, i32* %8
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %59
19: ; preds = %2
%20 = load i32, i32* %8
%21 = call i8* @shmat(i32 %20, i32* null, i32 0)
store i8* %21, i8** %10
%22 = icmp eq i8* %21, inttoptr (i64 -1 to i8*)
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = 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 %59
25: ; preds = %19
%26 = load i8*, i8** %10
%27 = call i32 @memset(i8* %26, i32 0, i32 1024)
%28 = load i8*, i8** %10
store i8* %28, i8** %11
br label %29
29: ; preds = %43, %42, %25
%30 = load i8*, i8** %10
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = icmp ne i32 %32, 113
br i1 %33, label %34, label %51
34: ; preds = %29
%35 = call i32 @sleep(i32 1)
%36 = load i8, i8* %7
%37 = sext i8 %36 to i32
%38 = load i8*, i8** %10
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp eq i32 %37, %40
br i1 %41, label %42, label %43
42: ; preds = %34
br label %29
43: ; preds = %34
%44 = load i32, i32* @stdout
%45 = load i8*, i8** %10
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %47)
%49 = load i8*, i8** %10
%50 = load i8, i8* %49
store i8 %50, i8* %7
br label %29
51: ; preds = %29
%52 = load i8*, i8** %10
%53 = call i64 @shmdt(i8* %52)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %58
55: ; preds = %51
%56 = load i32, i32* @stderr
%57 = call i32 (i32, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
br label %58
58: ; preds = %55, %51
store i32 0, i32* %3
br label %59
59: ; preds = %58, %23, %17
%60 = load i32, i32* %3
ret i32 %60
}

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"
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 %38
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 %38
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 = %32, %35, %12, %7
%39 = phi i32 [ 1, %7 ], [ 1, %12 ], [ 0, %35 ], [ 0, %32 ]
ret i32 %39
}