#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 EXIT_SUCCESS;
int /*<<< orphan*/ IPC_CREAT;
int /*<<< orphan*/ IPC_RMID;
int errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ ftok(char *, int);
int /*<<< orphan*/ printf(char *, int, ...);
scalar_t__ shmat(long, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ shmctl(long, int /*<<< orphan*/, int /*<<< orphan*/ *);
long shmget(int /*<<< orphan*/, int, int /*<<< orphan*/);
char *strerror(int);
int main(int argc, char *argv[]) {
  key_t mem_key;
  long mem_id;
  void *mem_addr[15];
  int i;
  if ((mem_key = ftok("./mykeyfile", 1)) == (key_t)(-1)) {
    printf("Failed to generate shared memory access key"
           ", ERRNO = %d : %s\n",
           errno, strerror(errno));
    shmctl(mem_id, IPC_RMID, NULL);
    exit(-1);
  }
  if ((mem_id = shmget(mem_key, 256, IPC_CREAT)) == (-1)) {
    printf("Failed to obtain shared memory ID"
           ", ERRNO = %d : %s\n",
           errno, strerror(errno));
    shmctl(mem_id, IPC_RMID, NULL);
    exit(-1);
  }
  for (i = 1; i <= 15; i++) {
    if ((mem_addr[i] = (void *)shmat(mem_id, 0, 0)) == (void *)(-1)) {
      printf("Failed to attach shared memory"
             ", times [%02d], ERRNO = %d : %s\n",
             i, errno, strerror(errno));
      shmctl(mem_id, IPC_RMID, NULL);
      exit(-1);
    } else {
      printf("Successfully attached shared memory, times [%02d]\n", i);
    }
  }
  shmctl(mem_id, IPC_RMID, NULL);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe70(%rip),%rdi
mov    $0x1,%esi
call   1080 <ftok@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11fa <main+0x8a>
mov    0x2e33(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xa8(%rbp)
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0xa8(%rbp),%esi
mov    %rax,%rdx
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <shmctl@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2dc4(%rip),%rax
mov    (%rax),%edx
mov    $0x100,%esi
call   1060 <shmget@plt>
mov    %rax,-0x20(%rbp)
cmp    $0xffffffffffffffff,%rax
jne    1272 <main+0x102>
mov    0x2dbb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xac(%rbp)
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0xac(%rbp),%esi
mov    %rax,%rdx
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    0x2d7b(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <shmctl@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
movl   $0x1,-0xa4(%rbp)
cmpl   $0xf,-0xa4(%rbp)
jg     1348 <main+0x1d8>
mov    -0x20(%rbp),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1050 <shmat@plt>
movslq -0xa4(%rbp),%rcx
mov    %rax,-0xa0(%rbp,%rcx,8)
ff
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    131b <main+0x1ab>
mov    -0xa4(%rbp),%eax
mov    %eax,-0xb4(%rbp)
mov    0x2d18(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xb0(%rbp)
mov    0x2d09(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0xb4(%rbp),%esi
mov    -0xb0(%rbp),%edx
mov    %rax,%rcx
lea    0xd8a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    0x2cd2(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <shmctl@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0xa4(%rbp),%esi
lea    0xd95(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1334 <main+0x1c4>
mov    -0xa4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xa4(%rbp)
jmp    127c <main+0x10c>
mov    -0x20(%rbp),%rdi
mov    0x2c85(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <shmctl@plt>
mov    0x2c6b(%rip),%rax
mov    (%rax),%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
lea    0xe85(%rip),%rdi
mov    $0x1,%esi
call   1080 <ftok@plt>
cmp    $0xffffffff,%eax
je     124c <main+0xdc>
mov    0x2e33(%rip),%rcx
mov    (%rcx),%edx
mov    %eax,%edi
mov    $0x100,%esi
call   1060 <shmget@plt>
cmp    $0xffffffffffffffff,%rax
je     127a <main+0x10a>
mov    %rax,%rbx
mov    $0x1,%ebp
lea    0xf01(%rip),%r14
nopl   0x0(%rax)
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     1207 <main+0x97>
mov    %r14,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1030 <printf@plt>
add    $0x1,%ebp
cmp    $0x10,%ebp
jne    11c0 <main+0x50>
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
xor    %edx,%edx
call   1040 <shmctl@plt>
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2dd2(%rip),%rax
mov    (%rax),%r14d
mov    %r14d,%edi
call   1090 <strerror@plt>
lea    0xe5e(%rip),%rdi
mov    %ebp,%esi
mov    %r14d,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x2da2(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
xor    %edx,%edx
call   1040 <shmctl@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    0x2d8d(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1090 <strerror@plt>
lea    0xda9(%rip),%rdi
mov    %ebx,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x2d62(%rip),%rax
mov    (%rax),%esi
jmp    123b <main+0xcb>
mov    0x2d5f(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1090 <strerror@plt>
lea    0xdb9(%rip),%rdi
mov    %ebx,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x2d34(%rip),%rax
mov    (%rax),%esi
mov    $0xffffffffffffffff,%rdi
jmp    123b <main+0xcb>

LLVM IR (-O0):
@.str = [12 x i8] c"./mykeyfile\00"
@.str.1 = [62 x i8] c"Failed to generate shared memory access key, ERRNO = %d : %s\0A\00"
@errno = global i32 0
@IPC_RMID = global i32 0
@IPC_CREAT = global i32 0
@.str.2 = [52 x i8] c"Failed to obtain shared memory ID, ERRNO = %d : %s\0A\00"
@.str.3 = [63 x i8] c"Failed to attach shared memory, times [%02d], ERRNO = %d : %s\0A\00"
@.str.4 = [51 x i8] c"Successfully attached shared memory, times [%02d]\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
%8 = alloca [15 x i8*]
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i32 @ftok(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 1)
store i32 %10, i32* %6
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %21
12: ; preds = %2
%13 = load i32, i32* @errno
%14 = load i32, i32* @errno
%15 = call i8* @strerror(i32 %14)
%16 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i64 0, i64 0), i32 %13, i8* %15)
%17 = load i64, i64* %7
%18 = load i32, i32* @IPC_RMID
%19 = call i32 @shmctl(i64 %17, i32 %18, i32* null)
%20 = call i32 @exit(i32 -1)
unreachable
21: ; preds = %2
%22 = load i32, i32* %6
%23 = load i32, i32* @IPC_CREAT
%24 = call i64 @shmget(i32 %22, i32 256, i32 %23)
store i64 %24, i64* %7
%25 = icmp eq i64 %24, -1
br i1 %25, label %26, label %35
26: ; preds = %21
%27 = load i32, i32* @errno
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0), i32 %27, i8* %29)
%31 = load i64, i64* %7
%32 = load i32, i32* @IPC_RMID
%33 = call i32 @shmctl(i64 %31, i32 %32, i32* null)
%34 = call i32 @exit(i32 -1)
unreachable
35: ; preds = %21
store i32 1, i32* %9
br label %36
36: ; preds = %61, %35
%37 = load i32, i32* %9
%38 = icmp sle i32 %37, 15
br i1 %38, label %39, label %64
39: ; preds = %36
%40 = load i64, i64* %7
%41 = call i64 @shmat(i64 %40, i32 0, i32 0)
%42 = inttoptr i64 %41 to i8*
%43 = load i32, i32* %9
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds [15 x i8*], [15 x i8*]* %8, i64 0, i64 %44
store i8* %42, i8** %45
%46 = icmp eq i8* %42, inttoptr (i64 -1 to i8*)
br i1 %46, label %47, label %57
47: ; preds = %39
%48 = load i32, i32* %9
%49 = load i32, i32* @errno
%50 = load i32, i32* @errno
%51 = call i8* @strerror(i32 %50)
%52 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.3, i64 0, i64 0), i32 %48, i32 %49, i8* %51)
%53 = load i64, i64* %7
%54 = load i32, i32* @IPC_RMID
%55 = call i32 @shmctl(i64 %53, i32 %54, i32* null)
%56 = call i32 @exit(i32 -1)
unreachable
57: ; preds = %39
%58 = load i32, i32* %9
%59 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.4, i64 0, i64 0), i32 %58)
br label %60
60: ; preds = %57
br label %61
61: ; preds = %60
%62 = load i32, i32* %9
%63 = add nsw i32 %62, 1
store i32 %63, i32* %9
br label %36
64: ; preds = %36
%65 = load i64, i64* %7
%66 = load i32, i32* @IPC_RMID
%67 = call i32 @shmctl(i64 %65, i32 %66, i32* null)
%68 = load i32, i32* @EXIT_SUCCESS
ret i32 %68
}

LLVM IR (-O2):
@.str = [12 x i8] c"./mykeyfile\00"
@.str.1 = [62 x i8] c"Failed to generate shared memory access key, ERRNO = %d : %s\0A\00"
@errno = global i32 0
@IPC_RMID = global i32 0
@IPC_CREAT = global i32 0
@.str.2 = [52 x i8] c"Failed to obtain shared memory ID, ERRNO = %d : %s\0A\00"
@.str.3 = [63 x i8] c"Failed to attach shared memory, times [%02d], ERRNO = %d : %s\0A\00"
@.str.4 = [51 x i8] c"Successfully attached shared memory, times [%02d]\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @ftok(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 1)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %12
5: ; preds = %2
%6 = load i32, i32* @errno
%7 = tail call i8* @strerror(i32 %6)
%8 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i64 0, i64 0), i32 %6, i8* %7)
%9 = load i32, i32* @IPC_RMID
%10 = tail call i32 @shmctl(i64 undef, i32 %9, i32* null)
%11 = tail call i32 @exit(i32 -1)
unreachable
12: ; preds = %2
%13 = load i32, i32* @IPC_CREAT
%14 = tail call i64 @shmget(i32 %3, i32 256, i32 %13)
%15 = icmp eq i64 %14, -1
br i1 %15, label %16, label %23
16: ; preds = %12
%17 = load i32, i32* @errno
%18 = tail call i8* @strerror(i32 %17)
%19 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0), i32 %17, i8* %18)
%20 = load i32, i32* @IPC_RMID
%21 = tail call i32 @shmctl(i64 -1, i32 %20, i32* null)
%22 = tail call i32 @exit(i32 -1)
unreachable
23: ; preds = %12, %35
%24 = phi i32 [ %37, %35 ], [ 1, %12 ]
%25 = tail call i64 @shmat(i64 %14, i32 0, i32 0)
%26 = inttoptr i64 %25 to i8*
%27 = icmp eq i8* %26, inttoptr (i64 -1 to i8*)
br i1 %27, label %28, label %35
28: ; preds = %23
%29 = load i32, i32* @errno
%30 = tail call i8* @strerror(i32 %29)
%31 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.3, i64 0, i64 0), i32 %24, i32 %29, i8* %30)
%32 = load i32, i32* @IPC_RMID
%33 = tail call i32 @shmctl(i64 %14, i32 %32, i32* null)
%34 = tail call i32 @exit(i32 -1)
unreachable
35: ; preds = %23
%36 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.4, i64 0, i64 0), i32 %24)
%37 = add nuw nsw i32 %24, 1
%38 = icmp eq i32 %37, 16
br i1 %38, label %39, label %23
39: ; preds = %35
%40 = load i32, i32* @IPC_RMID
%41 = tail call i32 @shmctl(i64 %14, i32 %40, i32* null)
%42 = load i32, i32* @EXIT_SUCCESS
ret i32 %42
}