#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 /*<<< orphan*/ key_t;

/* Variables and functions */
int /*<<< orphan*/ IPC_RMID;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ ftok(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ shmctl(int, int /*<<< orphan*/, int /*<<< orphan*/ *);
scalar_t__ shmdt(char *);
int shmget(int /*<<< orphan*/, int, int /*<<< orphan*/ *);
int main() {
  int shmid;
  char pathname[] = "2a.c";
  char *shstring;
  key_t key;
  if ((key = ftok(pathname, 0)) < 0) {
    printf("Can\'t generate key\n");
    exit(-1);
  }
  if ((shmid = shmget(key, 1738 * sizeof(char), NULL)) < 0) {
    printf("Can\'t create shared memory\n");
    exit(-1);
  }
  if ((shstring = (char *)shmat(shmid, NULL, 0)) == (char *)(-1)) {
    printf("Can't attach shared memory\n");
    exit(-1);
  }
  printf("%s\n", shstring);
  if (shmdt(shstring) < 0) {
    printf("Can't detach shared memory\n");
    exit(-1);
  }
  shmctl(shmid, IPC_RMID, NULL);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe7b(%rip),%eax
mov    %eax,-0xd(%rbp)
mov    0xe76(%rip),%al
mov    %al,-0x9(%rbp)
lea    -0xd(%rbp),%rdi
xor    %esi,%esi
call   1090 <ftok@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11c0 <main+0x50>
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x1c(%rbp),%edi
mov    $0x6ca,%esi
xor    %eax,%eax
mov    %eax,%edx
call   1070 <shmget@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11f5 <main+0x85>
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
mov    %rax,-0x18(%rbp)
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    122f <main+0xbf>
lea    0xe17(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%rsi
lea    0xe17(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <shmdt@plt>
cmp    $0x0,%rax
jge    126c <main+0xfc>
lea    0xdfa(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%edi
mov    0x2d6a(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <shmctl@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
movb   $0x0,0x4(%rsp)
movl   $0x632e6132,(%rsp)
mov    %rsp,%rdi
xor    %esi,%esi
call   1090 <ftok@plt>
test   %eax,%eax
js     11e5 <main+0x75>
mov    %eax,%edi
mov    $0x6ca,%esi
xor    %edx,%edx
call   1070 <shmget@plt>
test   %eax,%eax
js     11ee <main+0x7e>
mov    %eax,%ebp
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     11f7 <main+0x87>
mov    %rax,%rbx
mov    %rax,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1040 <shmdt@plt>
test   %rax,%rax
js     1200 <main+0x90>
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1050 <shmctl@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe6a(%rip),%rdi
jmp    1207 <main+0x97>
lea    0xe46(%rip),%rdi
jmp    1207 <main+0x97>
lea    0xe22(%rip),%rdi
jmp    1207 <main+0x97>
lea    0xdfe(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@__const.main.pathname = [5 x i8] c"2a.c\00"
@.str = [20 x i8] c"Can't generate key\0A\00"
@.str.1 = [28 x i8] c"Can't create shared memory\0A\00"
@.str.2 = [28 x i8] c"Can't attach shared memory\0A\00"
@.str.3 = [4 x i8] c"%s\0A\00"
@.str.4 = [28 x i8] c"Can't detach shared memory\0A\00"
@IPC_RMID = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [5 x i8]
%4 = alloca i8*
%5 = alloca i32
store i32 0, i32* %1
%6 = bitcast [5 x i8]* %3 to i8*
%7 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%8 = call i32 @ftok(i8* %7, i32 0)
store i32 %8, i32* %5
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 -1)
unreachable
13: ; preds = %0
%14 = load i32, i32* %5
%15 = call i32 @shmget(i32 %14, i32 1738, i32* null)
store i32 %15, i32* %2
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %13
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @exit(i32 -1)
unreachable
20: ; preds = %13
%21 = load i32, i32* %2
%22 = call i64 @shmat(i32 %21, i32* null, i32 0)
%23 = inttoptr i64 %22 to i8*
store i8* %23, i8** %4
%24 = icmp eq i8* %23, inttoptr (i64 -1 to i8*)
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%27 = call i32 @exit(i32 -1)
unreachable
28: ; preds = %20
%29 = load i8*, i8** %4
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %29)
%31 = load i8*, i8** %4
%32 = call i64 @shmdt(i8* %31)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %28
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
%36 = call i32 @exit(i32 -1)
unreachable
37: ; preds = %28
%38 = load i32, i32* %2
%39 = load i32, i32* @IPC_RMID
%40 = call i32 @shmctl(i32 %38, i32 %39, i32* null)
ret i32 0
}

LLVM IR (-O2):
@__const.main.pathname = [5 x i8] c"2a.c\00"
@IPC_RMID = global i32 0
@str = [27 x i8] c"Can't detach shared memory\00"
@str.5 = [27 x i8] c"Can't attach shared memory\00"
@str.6 = [27 x i8] c"Can't create shared memory\00"
@str.7 = [19 x i8] c"Can't generate key\00"
define i32 @main() {
%1 = alloca [5 x i8]
%2 = getelementptr inbounds [5 x i8], [5 x i8]* %1, i64 0, i64 0
%3 = call i32 @ftok(i8* %2, i32 0)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.7, i64 0, i64 0))
%7 = call i32 @exit(i32 -1)
unreachable
8: ; preds = %0
%9 = call i32 @shmget(i32 %3, i32 1738, i32* null)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.6, i64 0, i64 0))
%13 = call i32 @exit(i32 -1)
unreachable
14: ; preds = %8
%15 = call i64 @shmat(i32 %9, i32* null, i32 0)
%16 = inttoptr i64 %15 to i8*
%17 = icmp eq i8* %16, inttoptr (i64 -1 to i8*)
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.5, i64 0, i64 0))
%20 = call i32 @exit(i32 -1)
unreachable
21: ; preds = %14
%22 = call i32 @puts(i8* dereferenceable(1) %16)
%23 = call i64 @shmdt(i8* %16)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %21
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%27 = call i32 @exit(i32 -1)
unreachable
28: ; preds = %21
%29 = load i32, i32* @IPC_RMID
%30 = call i32 @shmctl(i32 %9, i32 %29, i32* null)
ret i32 0
}