#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;
main() {
  int shmid;
  key_t key;
  char *shm, *s;
  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);
  s = shm;
  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    $0x30,%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    1275 <main+0x105>
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    1275 <main+0x105>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1040 <memset@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1050 <getchar@plt>
mov    %al,-0x21(%rbp)
mov    $0x0,%al
call   1050 <getchar@plt>
movsbl -0x21(%rbp),%eax
cmp    $0x71,%eax
jne    1238 <main+0xc8>
mov    -0x18(%rbp),%rax
movb   $0x71,(%rax)
jmp    1246 <main+0xd6>
mov    -0x21(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
jmp    120e <main+0x9e>
mov    -0x18(%rbp),%rdi
call   1030 <shmdt@plt>
cmp    $0x0,%rax
je     126e <main+0xfe>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
lea    0xda4(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%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*
%6 = alloca i8
store i32 0, i32* %1
store i32 1234, i32* %3
%7 = load i32, i32* %3
%8 = call i32 @shmget(i32 %7, i32 1024, i32 438)
store i32 %8, i32* %2
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %41
12: ; preds = %0
%13 = load i32, i32* %2
%14 = call i8* @shmat(i32 %13, i32* null, i32 0)
store i8* %14, i8** %4
%15 = icmp eq i8* %14, inttoptr (i64 -1 to i8*)
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = 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 %41
18: ; preds = %12
%19 = load i8*, i8** %4
%20 = call i32 @memset(i8* %19, i32 0, i32 1024)
%21 = load i8*, i8** %4
store i8* %21, i8** %5
br label %22
22: ; preds = %30, %18
%23 = call signext i8 (...) @getchar()
store i8 %23, i8* %6
%24 = call signext i8 (...) @getchar()
%25 = load i8, i8* %6
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %26, 113
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = load i8*, i8** %4
store i8 113, i8* %29
br label %33
30: ; preds = %22
%31 = load i8, i8* %6
%32 = load i8*, i8** %4
store i8 %31, i8* %32
br label %22
33: ; preds = %28
%34 = load i8*, i8** %4
%35 = call i64 @shmdt(i8* %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %33
%38 = load i32, i32* @stderr
%39 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
br label %40
40: ; preds = %37, %33
store i32 0, i32* %1
br label %41
41: ; preds = %40, %16, %10
%42 = load i32, i32* %1
ret i32 %42
}

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
}