#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*/ atoi(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ ftok(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, void *, int);
void *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmget(int /*<<< orphan*/, int /*<<< orphan*/, int);
int main(int argc, char *argv[]) {
  key_t shmkey;
  int shmid;
  void *addr;
  if (argc != 3)
    return (1);
  if ((shmkey = ftok(argv[1], atoi(argv[2]))) == (key_t)-1) {
    perror("could not generate shm key");
    exit(1);
  }
  if ((shmid = shmget(shmkey, 0, 0666)) == -1) {
    perror("could not get shmid");
    exit(1);
  }
  if ((addr = shmat(shmid, NULL, 0)) == (void *)-1) {
    perror("could not attach to shared memory");
    exit(1);
  }
  printf("%p: contains %d entries\n", addr, *(int *)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)
cmpl   $0x3,-0x8(%rbp)
je     119c <main+0x2c>
movl   $0x1,-0x4(%rbp)
jmp    1268 <main+0xf8>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1070 <atoi@plt>
mov    -0x28(%rbp),%rdi
mov    %eax,%esi
call   1090 <ftok@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11e2 <main+0x72>
lea    0xe2d(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
mov    $0x1b6,%edx
call   1060 <shmget@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    1213 <main+0xa3>
lea    0xe17(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <shmat@plt>
mov    %rax,-0x20(%rbp)
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    124b <main+0xdb>
lea    0xdf3(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    (%rax),%edx
lea    0xdf5(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    $0x1,%ebx
cmp    $0x3,%edi
jne    11ce <main+0x5e>
mov    0x8(%rsi),%rbx
mov    0x10(%rsi),%rdi
call   1070 <atoi@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1090 <ftok@plt>
cmp    $0xffffffff,%eax
je     11d2 <main+0x62>
mov    %eax,%edi
xor    %esi,%esi
mov    $0x1b6,%edx
call   1060 <shmget@plt>
cmp    $0xffffffff,%eax
je     11db <main+0x6b>
xor    %ebx,%ebx
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     11e4 <main+0x74>
mov    (%rax),%edx
lea    0xe8b(%rip),%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
mov    %ebx,%eax
pop    %rbx
ret
lea    0xe27(%rip),%rdi
jmp    11eb <main+0x7b>
lea    0xe39(%rip),%rdi
jmp    11eb <main+0x7b>
lea    0xe44(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [27 x i8] c"could not generate shm key\00"
@.str.1 = [20 x i8] c"could not get shmid\00"
@.str.2 = [34 x i8] c"could not attach to shared memory\00"
@.str.3 = [25 x i8] c"%p: contains %d entries\0A\00"
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 = load i32, i32* %4
%10 = icmp ne i32 %9, 3
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 1, i32* %3
br label %45
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 2
%18 = load i8*, i8** %17
%19 = call i32 @atoi(i8* %18)
%20 = call i32 @ftok(i8* %15, i32 %19)
store i32 %20, i32* %6
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %12
%23 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %12
%26 = load i32, i32* %6
%27 = call i32 @shmget(i32 %26, i32 0, i32 438)
store i32 %27, i32* %7
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %25
%30 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %25
%33 = load i32, i32* %7
%34 = call i8* @shmat(i32 %33, i32* null, i32 0)
store i8* %34, i8** %8
%35 = icmp eq i8* %34, inttoptr (i64 -1 to i8*)
br i1 %35, label %36, label %39
36: ; preds = %32
%37 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %32
%40 = load i8*, i8** %8
%41 = load i8*, i8** %8
%42 = bitcast i8* %41 to i32*
%43 = load i32, i32* %42
%44 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %40, i32 %43)
store i32 0, i32* %3
br label %45
45: ; preds = %39, %11
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
@.str = [27 x i8] c"could not generate shm key\00"
@.str.1 = [20 x i8] c"could not get shmid\00"
@.str.2 = [34 x i8] c"could not attach to shared memory\00"
@.str.3 = [25 x i8] c"%p: contains %d entries\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %4, label %31
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = getelementptr inbounds i8*, i8** %1, i64 2
%8 = load i8*, i8** %7
%9 = tail call i32 @atoi(i8* %8)
%10 = tail call i32 @ftok(i8* %6, i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %4
%13 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %4
%16 = tail call i32 @shmget(i32 %10, i32 0, i32 438)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %15
%22 = tail call i8* @shmat(i32 %16, i32* null, i32 0)
%23 = icmp eq i8* %22, inttoptr (i64 -1 to i8*)
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = tail call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
%26 = tail call i32 @exit(i32 1)
unreachable
27: ; preds = %21
%28 = bitcast i8* %22 to i32*
%29 = load i32, i32* %28
%30 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* %22, i32 %29)
br label %31
31: ; preds = %2, %27
%32 = phi i32 [ 0, %27 ], [ 1, %2 ]
ret i32 %32
}