#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*/ exit(int);
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;
int main(void) {
  char c, tmp;
  int shmId;
  key_t key;
  char *shm, *s;
  key = 1234;
  if ((shmId = shmget(key, 1024, IPC_CREAT | 0666)) < 0) {
    perror("shmget");
    exit(1);
  }
  if ((shm = shmat(shmId, NULL, 0)) == (char *)-1) {
    perror("shmat");
    exit(1);
  }
  memset(shm, 0, 1024);
  s = shm;
  while (*shm != 'q') {
    sleep(1);
    if (tmp == *shm)
      continue;
    fprintf(stdout, "%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    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x4d2,-0x10(%rbp)
mov    -0x10(%rbp),%edi
mov    0x2e30(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    $0x400,%esi
call   1080 <shmget@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11d4 <main+0x54>
lea    0xe3b(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0xc(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1070 <shmat@plt>
mov    %rax,-0x18(%rbp)
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    120c <main+0x8c>
lea    0xe0a(%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    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x71,%eax
je     1282 <main+0x102>
mov    $0x1,%edi
call   10a0 <sleep@plt>
movsbl -0x6(%rbp),%eax
mov    -0x18(%rbp),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    1256 <main+0xd6>
jmp    1224 <main+0xa4>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rax
movsbl (%rax),%edx
lea    0xda0(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x6(%rbp)
jmp    1224 <main+0xa4>
mov    -0x18(%rbp),%rdi
call   1030 <shmdt@plt>
cmp    $0x0,%rax
je     12ac <main+0x12c>
mov    0x2d44(%rip),%rax
mov    (%rax),%edi
lea    0xd6d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    0x2e41(%rip),%rax
mov    $0x1b6,%edx
or     (%rax),%edx
mov    $0x4d2,%edi
mov    $0x400,%esi
call   1080 <shmget@plt>
test   %eax,%eax
js     124e <main+0xce>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     1257 <main+0xd7>
mov    %rax,%r12
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1040 <memset@plt>
mov    (%r12),%bl
mov    0x2df9(%rip),%r15
lea    0xe27(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
cmp    $0x71,%bl
je     121c <main+0x9c>
mov    $0x1,%edi
call   10a0 <sleep@plt>
movsbl (%r12),%edx
cmp    %dl,%bl
je     11f0 <main+0x70>
mov    (%r15),%edi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
movzbl (%r12),%ebx
jmp    11f0 <main+0x70>
mov    %r12,%rdi
call   1030 <shmdt@plt>
test   %rax,%rax
je     1240 <main+0xc0>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
lea    0xdd9(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xdab(%rip),%rdi
jmp    125e <main+0xde>
lea    0xda9(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

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

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 = [5 x i8] c"%c \0A\00"
@stderr = global i32 0
@.str.3 = [33 x i8] c"Could not close memory segment.\0A\00"
define i32 @main() {
%1 = load i32, i32* @IPC_CREAT
%2 = or i32 %1, 438
%3 = tail call i32 @shmget(i32 1234, i32 1024, i32 %2)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = tail call i8* @shmat(i32 %3, i32* null, i32 0)
%10 = icmp eq i8* %9, inttoptr (i64 -1 to i8*)
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %8
%15 = tail call i32 @memset(i8* %9, i32 0, i32 1024)
%16 = load i8, i8* %9
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* %9
%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 ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i32 %28)
%31 = load i8, i8* %9
br label %17
32: ; preds = %20
%33 = tail call i64 @shmdt(i8* %9)
%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
ret i32 0
}