#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;
scalar_t__ ftok(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
char *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmget(scalar_t__, int, int);
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);
  printf("%s\n", addr);
  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    0xe93(%rip),%rdi
mov    $0x7dd,%esi
call   1070 <ftok@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1192 <main+0x42>
lea    0xe76(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e3c(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    $0x400,%esi
call   1060 <shmget@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11cf <main+0x7f>
lea    0xe3e(%rip),%rdi
call   1040 <perror@plt>
mov    -0x1c(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <shmat@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
lea    0xe23(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
lea    0xea8(%rip),%rdi
mov    $0x7dd,%esi
call   1070 <ftok@plt>
mov    %rax,%rbx
test   %rax,%rax
js     11a9 <main+0x59>
mov    0x2e6f(%rip),%rax
mov    $0x1b6,%edx
or     (%rax),%edx
mov    %rbx,%rdi
mov    $0x400,%esi
call   1060 <shmget@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11b7 <main+0x67>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <shmat@plt>
lea    0xe72(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xe53(%rip),%rdi
call   1040 <perror@plt>
jmp    116a <main+0x1a>
lea    0xe4a(%rip),%rdi
call   1040 <perror@plt>
jmp    118b <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 = [4 x i8] c"%s\0A\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 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %26)
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 = [4 x i8] c"%s\0A\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 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %15)
ret i32 0
}