#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 scalar_t__ key_t;

/* Variables and functions */
int IPC_CREAT;
int /*<<< orphan*/ bzero(char *, int);
scalar_t__ ftok(char *, int);
int /*<<< orphan*/ perror(char *);
char *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmget(scalar_t__, int, int);
int /*<<< orphan*/ strcpy(char *, char *);
int main(int argc, char *argv[]) {
  key_t key;
  key = ftok("./", 2013);
  if (key < 0) {
    perror("ftok");
  }
  int shmid = -1;
  shmid = shmget(key, 1024, IPC_CREAT | 0666);
  if (shmid < 0) {
    perror("shmget");
  }
  char *addr = shmat(shmid, NULL, 0);
  bzero(addr, 1024);
  strcpy(addr, "hello world");
  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)
lea    0xe83(%rip),%rdi
mov    $0x7dd,%esi
call   1080 <ftok@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    11a2 <main+0x42>
lea    0xe66(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e2c(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    $0x400,%esi
call   1070 <shmget@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11df <main+0x7f>
lea    0xe2e(%rip),%rdi
call   1040 <perror@plt>
mov    -0x1c(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x400,%esi
call   1050 <bzero@plt>
mov    -0x28(%rbp),%rdi
lea    0xe05(%rip),%rsi
call   1030 <strcpy@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xe98(%rip),%rdi
mov    $0x7dd,%esi
call   1080 <ftok@plt>
mov    %rax,%rbx
test   %rax,%rax
js     11c9 <main+0x69>
mov    0x2e5f(%rip),%rax
mov    $0x1b6,%edx
or     (%rax),%edx
mov    %rbx,%rdi
mov    $0x400,%esi
call   1070 <shmget@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11d7 <main+0x77>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x400,%esi
call   1050 <bzero@plt>
lea    0xe52(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe33(%rip),%rdi
call   1040 <perror@plt>
jmp    117a <main+0x1a>
lea    0xe2a(%rip),%rdi
call   1040 <perror@plt>
jmp    119b <main+0x3b>

LLVM IR (-O0):
@.str = [3 x i8] c"./\00"
@.str.1 = [5 x i8] c"ftok\00"
@IPC_CREAT = global i32 0
@.str.2 = [7 x i8] c"shmget\00"
@.str.3 = [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 i64
%7 = alloca i32
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i64 @ftok(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2013)
store i64 %9, i64* %6
%10 = load i64, i64* %6
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %2
store i32 -1, i32* %7
%15 = load i64, i64* %6
%16 = load i32, i32* @IPC_CREAT
%17 = or i32 %16, 438
%18 = call i32 @shmget(i64 %15, i32 1024, i32 %17)
store i32 %18, i32* %7
%19 = load i32, i32* %7
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %23
23: ; preds = %21, %14
%24 = load i32, i32* %7
%25 = call i8* @shmat(i32 %24, i32* null, i32 0)
store i8* %25, i8** %8
%26 = load i8*, i8** %8
%27 = call i32 @bzero(i8* %26, i32 1024)
%28 = load i8*, i8** %8
%29 = call i32 @strcpy(i8* %28, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"./\00"
@.str.1 = [5 x i8] c"ftok\00"
@IPC_CREAT = global i32 0
@.str.2 = [7 x i8] c"shmget\00"
@.str.3 = [12 x i8] c"hello world\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i64 @ftok(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2013)
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %7
7: ; preds = %5, %2
%8 = load i32, i32* @IPC_CREAT
%9 = or i32 %8, 438
%10 = tail call i32 @shmget(i64 %3, i32 1024, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %14
14: ; preds = %12, %7
%15 = tail call i8* @shmat(i32 %10, i32* null, i32 0)
%16 = tail call i32 @bzero(i8* %15, i32 1024)
%17 = tail call i32 @strcpy(i8* %15, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
ret i32 0
}