#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 /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char getchar();
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*/ stderr;
int main() {
  int shmid;
  key_t key;
  char *shm;
  key = 1234;
  if ((shmid = shmget(key, 1024, 0666)) < 0) {
    perror("shmget");
    return 1;
  }
  if ((shm = shmat(shmid, NULL, 0)) == (char *)-1) {
    perror("shmat");
    return 1;
  }
  memset(shm, 0, 1024);
  for (;;) {
    char tmp = getchar();
    getchar();
    if (tmp == 'q') {
      *shm = 'q';
      break;
    }
    *shm = tmp;
  }
  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    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x4d2,-0xc(%rbp)
mov    -0xc(%rbp),%edi
mov    $0x400,%esi
mov    $0x1b6,%edx
call   1090 <shmget@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11bc <main+0x4c>
lea    0xe55(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    126d <main+0xfd>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1080 <shmat@plt>
mov    %rax,-0x18(%rbp)
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    11f6 <main+0x86>
lea    0xe22(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    126d <main+0xfd>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1040 <memset@plt>
mov    $0x0,%al
call   1050 <getchar@plt>
mov    %al,-0x19(%rbp)
mov    $0x0,%al
call   1050 <getchar@plt>
movsbl -0x19(%rbp),%eax
cmp    $0x71,%eax
jne    1230 <main+0xc0>
mov    -0x18(%rbp),%rax
movb   $0x71,(%rax)
jmp    123e <main+0xce>
mov    -0x19(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
jmp    1206 <main+0x96>
mov    -0x18(%rbp),%rdi
call   1030 <shmdt@plt>
cmp    $0x0,%rax
je     1266 <main+0xf6>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
lea    0xdac(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    $0x4d2,%edi
mov    $0x400,%esi
mov    $0x1b6,%edx
call   1090 <shmget@plt>
test   %eax,%eax
js     11f6 <main+0x86>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     11ff <main+0x8f>
mov    %rax,%r14
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1040 <memset@plt>
nop
xor    %eax,%eax
call   1050 <getchar@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1050 <getchar@plt>
mov    %bl,(%r14)
cmp    $0x71,%bl
jne    11b0 <main+0x40>
mov    %r14,%rdi
call   1030 <shmdt@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     11ec <main+0x7c>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
lea    0xe26(%rip),%rsi
call   1060 <fprintf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe03(%rip),%rdi
jmp    1206 <main+0x96>
lea    0xe01(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%ebx
jmp    11ec <main+0x7c>

LLVM IR (-O0):
@.str = [7 x i8] c"shmget\00"
@.str.1 = [6 x i8] c"shmat\00"
@stderr = global i32 0
@.str.2 = [33 x i8] c"Could not close memory segment.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8
store i32 0, i32* %1
store i32 1234, i32* %3
%6 = load i32, i32* %3
%7 = call i32 @shmget(i32 %6, i32 1024, i32 438)
store i32 %7, i32* %2
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %39
11: ; preds = %0
%12 = load i32, i32* %2
%13 = call i8* @shmat(i32 %12, i32* null, i32 0)
store i8* %13, i8** %4
%14 = icmp eq i8* %13, inttoptr (i64 -1 to i8*)
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %39
17: ; preds = %11
%18 = load i8*, i8** %4
%19 = call i32 @memset(i8* %18, i32 0, i32 1024)
br label %20
20: ; preds = %28, %17
%21 = call signext i8 (...) @getchar()
store i8 %21, i8* %5
%22 = call signext i8 (...) @getchar()
%23 = load i8, i8* %5
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 113
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = load i8*, i8** %4
store i8 113, i8* %27
br label %31
28: ; preds = %20
%29 = load i8, i8* %5
%30 = load i8*, i8** %4
store i8 %29, i8* %30
br label %20
31: ; preds = %26
%32 = load i8*, i8** %4
%33 = call i64 @shmdt(i8* %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = load i32, i32* @stderr
%37 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %38
38: ; preds = %35, %31
store i32 0, i32* %1
br label %39
39: ; preds = %38, %15, %9
%40 = load i32, i32* %1
ret i32 %40
}

LLVM IR (-O2):
@.str = [7 x i8] c"shmget\00"
@.str.1 = [6 x i8] c"shmat\00"
@stderr = global i32 0
@.str.2 = [33 x i8] c"Could not close memory segment.\0A\00"
define i32 @main() {
%1 = tail call i32 @shmget(i32 1234, i32 1024, i32 438)
%2 = icmp slt i32 %1, 0
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %22
5: ; preds = %0
%6 = tail call i8* @shmat(i32 %1, i32* null, i32 0)
%7 = icmp eq i8* %6, inttoptr (i64 -1 to i8*)
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %22
10: ; preds = %5
%11 = tail call i32 @memset(i8* %6, i32 0, i32 1024)
br label %12
12: ; preds = %12, %10
%13 = tail call signext i8 (...) @getchar()
%14 = tail call signext i8 (...) @getchar()
%15 = icmp eq i8 %13, 113
store i8 %13, i8* %6
br i1 %15, label %16, label %12
16: ; preds = %12
%17 = tail call i64 @shmdt(i8* %6)
%18 = icmp eq i64 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %16
%20 = load i32, i32* @stderr
%21 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %22
22: ; preds = %16, %19, %8, %3
%23 = phi i32 [ 1, %3 ], [ 1, %8 ], [ 0, %19 ], [ 0, %16 ]
ret i32 %23
}