#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*/ exit(int);
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 main(void) {
  int shmId;
  key_t key;
  char *shm, *s;
  key = 1234;
  if ((shmId = shmget(key, 1024, 0666)) < 0) {
    perror("shmget");
    exit(1);
  }
  if ((shm = shmat(shmId, NULL, 0)) < 0) {
    perror("shmget");
    exit(1);
  }
  memset(shm, 0, 1024);
  s = shm;
  for (;;) {
    char tmp = getchar();
    getchar();
    if (tmp == 'q') {
      *shm = 'q';
      break;
    }
    *shm = tmp;
  }
  if (shmdt(shm) != 0) {
    perror("shmdt");
    exit(1);
  }
  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   1080 <shmget@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11ba <main+0x4a>
lea    0xe55(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1070 <shmat@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jae    11ec <main+0x7c>
lea    0xe23(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
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    122e <main+0xbe>
mov    -0x18(%rbp),%rax
movb   $0x71,(%rax)
jmp    123c <main+0xcc>
mov    -0x21(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
jmp    1204 <main+0x94>
mov    -0x18(%rbp),%rdi
call   1030 <shmdt@plt>
cmp    $0x0,%rax
je     1265 <main+0xf5>
lea    0xdb1(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
xor    %eax,%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   1080 <shmget@plt>
test   %eax,%eax
js     11df <main+0x6f>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <shmat@plt>
mov    %rax,%r14
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1040 <memset@plt>
nopl   0x0(%rax)
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>
test   %rax,%rax
jne    11e8 <main+0x78>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe1a(%rip),%rdi
jmp    11ef <main+0x7f>
lea    0xe18(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"shmget\00"
@.str.1 = [6 x i8] c"shmdt\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 %13
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = load i32, i32* %2
%15 = call i8* @shmat(i32 %14, i32* null, i32 0)
store i8* %15, i8** %4
%16 = icmp ult i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %13
%21 = load i8*, i8** %4
%22 = call i32 @memset(i8* %21, i32 0, i32 1024)
%23 = load i8*, i8** %4
store i8* %23, i8** %5
br label %24
24: ; preds = %32, %20
%25 = call signext i8 (...) @getchar()
store i8 %25, i8* %6
%26 = call signext i8 (...) @getchar()
%27 = load i8, i8* %6
%28 = sext i8 %27 to i32
%29 = icmp eq i32 %28, 113
br i1 %29, label %30, label %32
30: ; preds = %24
%31 = load i8*, i8** %4
store i8 113, i8* %31
br label %35
32: ; preds = %24
%33 = load i8, i8* %6
%34 = load i8*, i8** %4
store i8 %33, i8* %34
br label %24
35: ; preds = %30
%36 = load i8*, i8** %4
%37 = call i64 @shmdt(i8* %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %35
%40 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %35
ret i32 0
}

LLVM IR (-O2):
@.str = [7 x i8] c"shmget\00"
@.str.1 = [6 x i8] c"shmdt\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 %6
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 @exit(i32 1)
unreachable
6: ; preds = %0
%7 = tail call i8* @shmat(i32 %1, i32* null, i32 0)
%8 = tail call i32 @memset(i8* %7, i32 0, i32 1024)
br label %9
9: ; preds = %9, %6
%10 = tail call signext i8 (...) @getchar()
%11 = tail call signext i8 (...) @getchar()
%12 = icmp eq i8 %10, 113
store i8 %10, i8* %7
br i1 %12, label %13, label %9
13: ; preds = %9
%14 = tail call i64 @shmdt(i8* %7)
%15 = icmp eq i64 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %13
%17 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %13
ret i32 0
}