#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*/ IPC_RMID;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
char *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ shmctl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ shmdt(char *);
int shmget(int, int, int);
int /*<<< orphan*/ sleep(int);
main() {
  int shmid;
  key_t key;
  char *shm, *s;
  key = 5678;
  if ((shmid = shmget(key, 27, IPC_CREAT | 0666)) < 0) {
    perror("shmget failed");
    exit(1);
  }
  if ((shm = shmat(shmid, NULL, 0)) == (char *)-1) {
    perror("shmat failed");
    exit(1);
  }
  s = shm;
  char c;
  int i = 0;
  for (c = 'a'; c <= 'z'; c++) {
    s[i] = c;
    i++;
  }
  s[i] = 0;
  while (*shm != '*')
    sleep(1);
  printf("Server detected client read\n");
  shmdt(shm);
  shmctl(shmid, IPC_RMID, 0);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x162e,-0xc(%rbp)
mov    -0xc(%rbp),%edi
mov    0x2e38(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    $0x1b,%esi
call   1080 <shmget@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11d4 <main+0x54>
lea    0xe3b(%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)
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    120c <main+0x8c>
lea    0xe11(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
movb   $0x61,-0x21(%rbp)
movsbl -0x21(%rbp),%eax
cmp    $0x7a,%eax
jg     1250 <main+0xd0>
mov    -0x21(%rbp),%dl
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x21(%rbp),%al
add    $0x1,%al
mov    %al,-0x21(%rbp)
jmp    121f <main+0x9f>
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2a,%eax
je     127b <main+0xfb>
mov    $0x1,%edi
call   10a0 <sleep@plt>
jmp    125c <main+0xdc>
lea    0xd99(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <shmdt@plt>
mov    -0x8(%rbp),%edi
mov    0x2d46(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <shmctl@plt>
xor    %edi,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e4e(%rip),%rax
mov    $0x1b6,%edx
or     (%rax),%edx
mov    $0x162e,%edi
mov    $0x1b,%esi
call   1080 <shmget@plt>
test   %eax,%eax
jns    11ad <main+0x2d>
lea    0xe65(%rip),%rdi
jmp    11c7 <main+0x47>
mov    %eax,%ebp
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
jne    11d6 <main+0x56>
lea    0xe57(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    %rax,%rbx
movaps 0xe20(%rip),%xmm0
movups %xmm0,(%rax)
movabs $0x7877767574737271,%rax
76 77 78
mov    %rax,0x10(%rbx)
movw   $0x7a79,0x18(%rbx)
movb   $0x0,0x1a(%rbx)
nopl   0x0(%rax,%rax,1)
mov    $0x1,%edi
call   10a0 <sleep@plt>
cmpb   $0x2a,(%rbx)
jne    1200 <main+0x80>
lea    0xe15(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1040 <shmdt@plt>
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1050 <shmctl@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@IPC_CREAT = global i32 0
@.str = [14 x i8] c"shmget failed\00"
@.str.1 = [13 x i8] c"shmat failed\00"
@.str.2 = [29 x i8] c"Server detected client read\0A\00"
@IPC_RMID = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8
%7 = alloca i32
store i32 0, i32* %1
store i32 5678, i32* %3
%8 = load i32, i32* %3
%9 = load i32, i32* @IPC_CREAT
%10 = or i32 %9, 438
%11 = call i32 @shmget(i32 %8, i32 27, i32 %10)
store i32 %11, i32* %2
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %0
%17 = load i32, i32* %2
%18 = call i8* @shmat(i32 %17, i32* null, i32 0)
store i8* %18, i8** %4
%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 ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %16
%24 = load i8*, i8** %4
store i8* %24, i8** %5
store i32 0, i32* %7
store i8 97, i8* %6
br label %25
25: ; preds = %37, %23
%26 = load i8, i8* %6
%27 = sext i8 %26 to i32
%28 = icmp sle i32 %27, 122
br i1 %28, label %29, label %40
29: ; preds = %25
%30 = load i8, i8* %6
%31 = load i8*, i8** %5
%32 = load i32, i32* %7
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
store i8 %30, i8* %34
%35 = load i32, i32* %7
%36 = add nsw i32 %35, 1
store i32 %36, i32* %7
br label %37
37: ; preds = %29
%38 = load i8, i8* %6
%39 = add i8 %38, 1
store i8 %39, i8* %6
br label %25
40: ; preds = %25
%41 = load i8*, i8** %5
%42 = load i32, i32* %7
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %41, i64 %43
store i8 0, i8* %44
br label %45
45: ; preds = %50, %40
%46 = load i8*, i8** %4
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = icmp ne i32 %48, 42
br i1 %49, label %50, label %52
50: ; preds = %45
%51 = call i32 @sleep(i32 1)
br label %45
52: ; preds = %45
%53 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%54 = load i8*, i8** %4
%55 = call i32 @shmdt(i8* %54)
%56 = load i32, i32* %2
%57 = load i32, i32* @IPC_RMID
%58 = call i32 @shmctl(i32 %56, i32 %57, i32 0)
%59 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@IPC_CREAT = global i32 0
@.str = [14 x i8] c"shmget failed\00"
@.str.1 = [13 x i8] c"shmat failed\00"
@IPC_RMID = global i32 0
@str = [28 x i8] c"Server detected client read\00"
define i32 @main() {
%1 = load i32, i32* @IPC_CREAT
%2 = or i32 %1, 438
%3 = tail call i32 @shmget(i32 5678, i32 27, 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 ([14 x i8], [14 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 ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %8
%15 = bitcast i8* %9 to <16 x i8>*
store <16 x i8> <i8 97, i8 98, i8 99, i8 100, i8 101, i8 102, i8 103, i8 104, i8 105, i8 106, i8 107, i8 108, i8 109, i8 110, i8 111, i8 112>, <16 x i8>* %15
%16 = getelementptr inbounds i8, i8* %9, i64 16
store i8 113, i8* %16
%17 = getelementptr inbounds i8, i8* %9, i64 17
store i8 114, i8* %17
%18 = getelementptr inbounds i8, i8* %9, i64 18
store i8 115, i8* %18
%19 = getelementptr inbounds i8, i8* %9, i64 19
store i8 116, i8* %19
%20 = getelementptr inbounds i8, i8* %9, i64 20
store i8 117, i8* %20
%21 = getelementptr inbounds i8, i8* %9, i64 21
store i8 118, i8* %21
%22 = getelementptr inbounds i8, i8* %9, i64 22
store i8 119, i8* %22
%23 = getelementptr inbounds i8, i8* %9, i64 23
store i8 120, i8* %23
%24 = getelementptr inbounds i8, i8* %9, i64 24
store i8 121, i8* %24
%25 = getelementptr inbounds i8, i8* %9, i64 25
store i8 122, i8* %25
%26 = getelementptr inbounds i8, i8* %9, i64 26
store i8 0, i8* %26
br label %27
27: ; preds = %14, %27
%28 = tail call i32 @sleep(i32 1)
%29 = load i8, i8* %9
%30 = icmp eq i8 %29, 42
br i1 %30, label %31, label %27
31: ; preds = %27
%32 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
%33 = tail call i32 @shmdt(i8* %9)
%34 = load i32, i32* @IPC_RMID
%35 = tail call i32 @shmctl(i32 %3, i32 %34, i32 0)
%36 = tail call i32 @exit(i32 0)
unreachable
}