#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*/ memcpy(char *, char *, int);
int /*<<< orphan*/ perror(char *);
char *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmget(int, int, int);
int /*<<< orphan*/ sleep(int);
int main(int argc, const char *argv[]) {
  int shmid;
  key_t key;
  char *shm;
  char *s;
  key = 9876;
  shmid = shmget(key, 100, IPC_CREAT | 0666);
  if (shmid < 0) {
    perror("shmget");
    exit(1);
  }
  shm = shmat(shmid, NULL, 0);
  if (shm == (char *)-1) {
    perror("shmat");
    exit(1);
  }
  memcpy(shm, "Hello World", 11);
  s = shm;
  s += 11;
  *s = 0;
  while (*shm != '*') {
    sleep(1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x2694,-0x18(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2e59(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    $0x64,%esi
call   1060 <shmget@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11bc <main+0x5c>
lea    0xe53(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <shmat@plt>
mov    %rax,-0x20(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x20(%rbp)
jne    11f5 <main+0x95>
lea    0xe21(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x20(%rbp),%rdi
lea    0xe0d(%rip),%rsi
mov    $0xb,%edx
call   1030 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    $0xb,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2a,%eax
je     1244 <main+0xe4>
mov    $0x1,%edi
call   1080 <sleep@plt>
jmp    1225 <main+0xc5>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e78(%rip),%rax
mov    $0x1b6,%edx
or     (%rax),%edx
mov    $0x2694,%edi
mov    $0x64,%esi
call   1060 <shmget@plt>
test   %eax,%eax
js     11d3 <main+0x73>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     11dc <main+0x7c>
mov    %rax,%rbx
lea    0xe70(%rip),%rsi
mov    %rax,%rdi
mov    $0xb,%edx
call   1030 <memcpy@plt>
movb   $0x0,0xb(%rbx)
cmpb   $0x2a,(%rbx)
je     11cf <main+0x6f>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    $0x1,%edi
call   1080 <sleep@plt>
cmpb   $0x2a,(%rbx)
jne    11c0 <main+0x60>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe26(%rip),%rdi
jmp    11e3 <main+0x83>
lea    0xe24(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1070 <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"
@.str.2 = [12 x i8] c"Hello World\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 9876, i32* %7
%10 = load i32, i32* %7
%11 = load i32, i32* @IPC_CREAT
%12 = or i32 %11, 438
%13 = call i32 @shmget(i32 %10, i32 100, i32 %12)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = load i32, i32* %6
%21 = call i8* @shmat(i32 %20, i32* null, i32 0)
store i8* %21, i8** %8
%22 = load i8*, i8** %8
%23 = icmp eq i8* %22, inttoptr (i64 -1 to i8*)
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %19
%28 = load i8*, i8** %8
%29 = call i32 @memcpy(i8* %28, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 11)
%30 = load i8*, i8** %8
store i8* %30, i8** %9
%31 = load i8*, i8** %9
%32 = getelementptr inbounds i8, i8* %31, i64 11
store i8* %32, i8** %9
%33 = load i8*, i8** %9
store i8 0, i8* %33
br label %34
34: ; preds = %39, %27
%35 = load i8*, i8** %8
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp ne i32 %37, 42
br i1 %38, label %39, label %41
39: ; preds = %34
%40 = call i32 @sleep(i32 1)
br label %34
41: ; preds = %34
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"
@.str.2 = [12 x i8] c"Hello World\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @IPC_CREAT
%4 = or i32 %3, 438
%5 = tail call i32 @shmget(i32 9876, i32 100, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = tail call i8* @shmat(i32 %5, i32* null, i32 0)
%12 = icmp eq i8* %11, inttoptr (i64 -1 to i8*)
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %10
%17 = tail call i32 @memcpy(i8* %11, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 11)
%18 = getelementptr inbounds i8, i8* %11, i64 11
store i8 0, i8* %18
%19 = load i8, i8* %11
%20 = icmp eq i8 %19, 42
br i1 %20, label %25, label %21
21: ; preds = %16, %21
%22 = tail call i32 @sleep(i32 1)
%23 = load i8, i8* %11
%24 = icmp eq i8 %23, 42
br i1 %24, label %25, label %21
25: ; preds = %21, %16
ret i32 0
}