#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*/ ftok(char *, char);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
scalar_t__ shmat(int, void *, int /*<<< orphan*/);
int /*<<< orphan*/ shmctl(int, int /*<<< orphan*/, int /*<<< orphan*/ *);
scalar_t__ shmdt(char *);
int shmget(int /*<<< orphan*/, int, int);
int main(int argc, char **argv) {
  key_t shm_key = ftok("./makefile", 'b');
  int shmid = shmget(shm_key, 1024, 0644);
  char *data = (char *)shmat(shmid, (void *)0, 0);
  if (data == (char *)-1)
    perror("shmat failed");
  printf(data);
  if (shmdt(data) < 0)
    perror("shmdt failed");
  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    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe73(%rip),%rdi
mov    $0x62,%esi
call   1090 <ftok@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    $0x400,%esi
mov    $0x1a4,%edx
call   1080 <shmget@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1070 <shmat@plt>
mov    %rax,-0x20(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x20(%rbp)
jne    11de <main+0x6e>
lea    0xe32(%rip),%rdi
call   1060 <perror@plt>
mov    -0x20(%rbp),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <shmdt@plt>
cmp    $0x0,%rax
jge    1206 <main+0x96>
lea    0xe17(%rip),%rdi
call   1060 <perror@plt>
mov    -0x18(%rbp),%edi
mov    0x2dd0(%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
lea    0xe86(%rip),%rdi
mov    $0x62,%esi
call   1090 <ftok@plt>
mov    %eax,%edi
mov    $0x400,%esi
mov    $0x1a4,%edx
call   1080 <shmget@plt>
mov    %eax,%ebp
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1070 <shmat@plt>
mov    %rax,%rbx
cmp    $0xffffffffffffffff,%rax
je     11db <main+0x6b>
mov    %rbx,%rdi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <shmdt@plt>
test   %rax,%rax
js     11e9 <main+0x79>
mov    0x2e19(%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    0xe29(%rip),%rdi
call   1060 <perror@plt>
jmp    11ab <main+0x3b>
lea    0xe28(%rip),%rdi
call   1060 <perror@plt>
jmp    11c0 <main+0x50>

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

LLVM IR (-O2):
@.str = [11 x i8] c"./makefile\00"
@.str.1 = [13 x i8] c"shmat failed\00"
@.str.2 = [13 x i8] c"shmdt failed\00"
@IPC_RMID = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @ftok(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8 signext 98)
%4 = tail call i32 @shmget(i32 %3, i32 1024, i32 420)
%5 = tail call i64 @shmat(i32 %4, i8* null, i32 0)
%6 = inttoptr i64 %5 to i8*
%7 = icmp eq i8* %6, inttoptr (i64 -1 to i8*)
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %10
10: ; preds = %8, %2
%11 = tail call i32 @printf(i8* dereferenceable(1) %6)
%12 = tail call i64 @shmdt(i8* %6)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %16
16: ; preds = %14, %10
%17 = load i32, i32* @IPC_RMID
%18 = tail call i32 @shmctl(i32 %4, i32 %17, i32* null)
ret i32 0
}