#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*/ memcpy(char *, char *, int);
int /*<<< orphan*/ printf(char *);
char *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ shmctl(int, int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ shmdt(int);
int shmget(int, int, int);
int /*<<< orphan*/ sleep(int);
int main(int argc, const char **argv) {
  int shmid;
  key_t key = 123456;
  char *shared_memory;
  if ((shmid = shmget(key, 11, IPC_CREAT | 0666)) < 0) {
    printf("Error getting shared memory id");
    exit(1);
  }
  if ((shared_memory = shmat(shmid, NULL, 0)) == (char *)-1) {
    printf("Error attaching shared memory id");
    exit(1);
  }
  memcpy(shared_memory, "Hello World", sizeof("Hello World"));
  sleep(10);
  shmdt(shmid);
  shmctl(shmid, IPC_RMID, NULL);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1e240,-0x18(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2e38(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    $0xb,%esi
call   1080 <shmget@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11d4 <main+0x54>
lea    0xe3b(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1070 <shmat@plt>
mov    %rax,-0x20(%rbp)
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    120c <main+0x8c>
lea    0xe22(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x20(%rbp),%rdi
lea    0xe29(%rip),%rsi
mov    $0xc,%edx
call   1050 <memcpy@plt>
mov    $0xa,%edi
call   10a0 <sleep@plt>
mov    -0x14(%rbp),%edi
call   1030 <shmdt@plt>
mov    -0x14(%rbp),%edi
mov    0x2da3(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1060 <shmctl@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e50(%rip),%rax
mov    $0x1b6,%edx
or     (%rax),%edx
mov    $0x1e240,%edi
mov    $0xb,%esi
call   1080 <shmget@plt>
test   %eax,%eax
js     11f0 <main+0x70>
mov    %eax,%ebx
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     11f9 <main+0x79>
lea    0xe84(%rip),%rsi
mov    %rax,%rdi
mov    $0xc,%edx
call   1050 <memcpy@plt>
mov    $0xa,%edi
call   10a0 <sleep@plt>
mov    %ebx,%edi
call   1030 <shmdt@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1060 <shmctl@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe09(%rip),%rdi
jmp    1200 <main+0x80>
lea    0xe1f(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@IPC_CREAT = global i32 0
@.str = [31 x i8] c"Error getting shared memory id\00"
@.str.1 = [33 x i8] c"Error attaching shared memory id\00"
@.str.2 = [12 x i8] c"Hello World\00"
@IPC_RMID = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
store i32 %0, i32* %3
store i8** %1, i8*** %4
store i32 123456, i32* %6
%8 = load i32, i32* %6
%9 = load i32, i32* @IPC_CREAT
%10 = or i32 %9, 438
%11 = call i32 @shmget(i32 %8, i32 11, i32 %10)
store i32 %11, i32* %5
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = load i32, i32* %5
%18 = call i8* @shmat(i32 %17, i32* null, i32 0)
store i8* %18, i8** %7
%19 = icmp eq i8* %18, inttoptr (i64 -1 to i8*)
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @exit(i32 1)
unreachable
23: ; preds = %16
%24 = load i8*, i8** %7
%25 = call i32 @memcpy(i8* %24, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 12)
%26 = call i32 @sleep(i32 10)
%27 = load i32, i32* %5
%28 = call i32 @shmdt(i32 %27)
%29 = load i32, i32* %5
%30 = load i32, i32* @IPC_RMID
%31 = call i32 @shmctl(i32 %29, i32 %30, i32* null)
ret i32 0
}

LLVM IR (-O2):
@IPC_CREAT = global i32 0
@.str = [31 x i8] c"Error getting shared memory id\00"
@.str.1 = [33 x i8] c"Error attaching shared memory id\00"
@.str.2 = [12 x i8] c"Hello World\00"
@IPC_RMID = global i32 0
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 123456, i32 11, i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 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 @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 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 12)
%18 = tail call i32 @sleep(i32 10)
%19 = tail call i32 @shmdt(i32 %5)
%20 = load i32, i32* @IPC_RMID
%21 = tail call i32 @shmctl(i32 %5, i32 %20, i32* null)
ret i32 0
}