#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 */

/* Variables and functions */
int IPC_CREAT;
int atoi(char *);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmget(int, int, int);
int /*<<< orphan*/ sleep(int);
char *strerror(scalar_t__);
int main(int argc, char **argv) {
  int ret = 0;
  int sval = 0;
  int memoryKey = atoi(argv[1]);
  int shmid;
  char *shm;
  errno = 0;
  if ((shmid = shmget(memoryKey, 4096, IPC_CREAT | 0777)) < 0) {
    if (errno) {
      char *x = strerror(errno);
      printf("shmget error %s\n", x);
    }
  }
  printf("shmget successful %d\n", shmid);
  if ((shm = (char *)shmat(shmid, NULL, 0)) == (char *)-1) {
    perror("shmat");
    exit(1);
  }
  while (1) {
    int i = 0;
    printf("memory at:");
    printf(" (%4.4d): %x, %x, %x, %x : ", 0, shm[0 + 0], shm[0 + 1], shm[0 + 2],
           shm[0 + 3]);
    printf(" (%4.4d): %x, %x, %x, %x, %x : ", 4, shm[4 + 0], shm[4 + 1],
           shm[4 + 2], shm[4 + 3], shm[4 + 4]);
    printf(" (%4.4d): %x, %x, %x, %x, %x\n", 2050, shm[2050 + 0], shm[2050 + 1],
           shm[2050 + 2], shm[2050 + 3], shm[2050 + 4]);
    sleep(1);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e25(%rip),%rax
movq   $0x0,(%rax)
mov    -0x1c(%rbp),%edi
mov    0x2e0c(%rip),%rax
mov    (%rax),%edx
or     $0x1ff,%edx
mov    $0x1000,%esi
call   1060 <shmget@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    1225 <main+0xa5>
mov    0x2def(%rip),%rax
cmpq   $0x0,(%rax)
je     1220 <main+0xa0>
mov    0x2dde(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rsi
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1225 <main+0xa5>
mov    -0x20(%rbp),%esi
lea    0xde2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <shmat@plt>
mov    %rax,-0x28(%rbp)
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    126e <main+0xee>
lea    0xdc8(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    1273 <main+0xf3>
movl   $0x0,-0x34(%rbp)
lea    0xdac(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x28(%rbp),%rax
movsbl (%rax),%edx
mov    -0x28(%rbp),%rax
movsbl 0x1(%rax),%ecx
mov    -0x28(%rbp),%rax
movsbl 0x2(%rax),%r8d
mov    -0x28(%rbp),%rax
movsbl 0x3(%rax),%r9d
lea    0xd88(%rip),%rdi
xor    %esi,%esi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x28(%rbp),%rax
movsbl 0x4(%rax),%edx
mov    -0x28(%rbp),%rax
movsbl 0x5(%rax),%ecx
mov    -0x28(%rbp),%rax
movsbl 0x6(%rax),%r8d
mov    -0x28(%rbp),%rax
movsbl 0x7(%rax),%r9d
mov    -0x28(%rbp),%rax
movsbl 0x8(%rax),%eax
lea    0xd6a(%rip),%rdi
mov    $0x4,%esi
mov    %eax,(%rsp)
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x28(%rbp),%rax
movsbl 0x802(%rax),%edx
mov    -0x28(%rbp),%rax
movsbl 0x803(%rax),%ecx
mov    -0x28(%rbp),%rax
movsbl 0x804(%rax),%r8d
mov    -0x28(%rbp),%rax
movsbl 0x805(%rax),%r9d
mov    -0x28(%rbp),%rax
movsbl 0x806(%rax),%eax
lea    0xd3b(%rip),%rdi
mov    $0x802,%esi
mov    %eax,(%rsp)
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
jmp    1273 <main+0xf3>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x8(%rsi),%rdi
call   1070 <atoi@plt>
mov    0x2e43(%rip),%r14
movq   $0x0,(%r14)
mov    0x2e2d(%rip),%rcx
mov    $0x1ff,%edx
or     (%rcx),%edx
mov    %eax,%edi
mov    $0x1000,%esi
call   1060 <shmget@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11e2 <main+0x62>
mov    (%r14),%rdi
test   %rdi,%rdi
je     11e2 <main+0x62>
call   1090 <strerror@plt>
lea    0xe28(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xe28(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     12c5 <main+0x145>
mov    %rax,%rbx
lea    0xe1c(%rip),%r14
lea    0xe20(%rip),%r15
lea    0xe35(%rip),%r12
lea    0xe4e(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
mov    %r14,%rdi
xor    %eax,%eax
call   1030 <printf@plt>
movsbl (%rbx),%edx
movsbl 0x1(%rbx),%ecx
movsbl 0x2(%rbx),%r8d
movsbl 0x3(%rbx),%r9d
mov    %r15,%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1030 <printf@plt>
movsbl 0x4(%rbx),%edx
movsbl 0x5(%rbx),%ecx
movsbl 0x6(%rbx),%r8d
movsbl 0x7(%rbx),%r9d
movsbl 0x8(%rbx),%eax
mov    %eax,(%rsp)
mov    %r12,%rdi
mov    $0x4,%esi
xor    %eax,%eax
call   1030 <printf@plt>
movsbl 0x802(%rbx),%edx
movsbl 0x803(%rbx),%ecx
movsbl 0x804(%rbx),%r8d
movsbl 0x805(%rbx),%r9d
movsbl 0x806(%rbx),%eax
mov    %eax,(%rsp)
mov    %r13,%rdi
mov    $0x802,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
jmp    1230 <main+0xb0>
lea    0xd5b(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@errno = global i64 0
@IPC_CREAT = global i32 0
@.str = [17 x i8] c"shmget error %s\0A\00"
@.str.1 = [22 x i8] c"shmget successful %d\0A\00"
@.str.2 = [6 x i8] c"shmat\00"
@.str.3 = [11 x i8] c"memory at:\00"
@.str.4 = [28 x i8] c" (%4.4d): %x, %x, %x, %x : \00"
@.str.5 = [32 x i8] c" (%4.4d): %x, %x, %x, %x, %x : \00"
@.str.6 = [30 x i8] c" (%4.4d): %x, %x, %x, %x, %x\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 i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 0, i32* %7
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = call i32 @atoi(i8* %15)
store i32 %16, i32* %8
store i64 0, i64* @errno
%17 = load i32, i32* %8
%18 = load i32, i32* @IPC_CREAT
%19 = or i32 %18, 511
%20 = call i32 @shmget(i32 %17, i32 4096, i32 %19)
store i32 %20, i32* %9
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %31
22: ; preds = %2
%23 = load i64, i64* @errno
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %22
%26 = load i64, i64* @errno
%27 = call i8* @strerror(i64 %26)
store i8* %27, i8** %11
%28 = load i8*, i8** %11
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %28)
br label %30
30: ; preds = %25, %22
br label %31
31: ; preds = %30, %2
%32 = load i32, i32* %9
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %32)
%34 = load i32, i32* %9
%35 = call i64 @shmat(i32 %34, i32* null, i32 0)
%36 = inttoptr i64 %35 to i8*
store i8* %36, i8** %10
%37 = icmp eq i8* %36, inttoptr (i64 -1 to i8*)
br i1 %37, label %38, label %41
38: ; preds = %31
%39 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %31
br label %42
42: ; preds = %41, %42
store i32 0, i32* %12
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%44 = load i8*, i8** %10
%45 = getelementptr inbounds i8, i8* %44, i64 0
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = load i8*, i8** %10
%49 = getelementptr inbounds i8, i8* %48, i64 1
%50 = load i8, i8* %49
%51 = sext i8 %50 to i32
%52 = load i8*, i8** %10
%53 = getelementptr inbounds i8, i8* %52, i64 2
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = load i8*, i8** %10
%57 = getelementptr inbounds i8, i8* %56, i64 3
%58 = load i8, i8* %57
%59 = sext i8 %58 to i32
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 0, i32 %47, i32 %51, i32 %55, i32 %59)
%61 = load i8*, i8** %10
%62 = getelementptr inbounds i8, i8* %61, i64 4
%63 = load i8, i8* %62
%64 = sext i8 %63 to i32
%65 = load i8*, i8** %10
%66 = getelementptr inbounds i8, i8* %65, i64 5
%67 = load i8, i8* %66
%68 = sext i8 %67 to i32
%69 = load i8*, i8** %10
%70 = getelementptr inbounds i8, i8* %69, i64 6
%71 = load i8, i8* %70
%72 = sext i8 %71 to i32
%73 = load i8*, i8** %10
%74 = getelementptr inbounds i8, i8* %73, i64 7
%75 = load i8, i8* %74
%76 = sext i8 %75 to i32
%77 = load i8*, i8** %10
%78 = getelementptr inbounds i8, i8* %77, i64 8
%79 = load i8, i8* %78
%80 = sext i8 %79 to i32
%81 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0), i32 4, i32 %64, i32 %68, i32 %72, i32 %76, i32 %80)
%82 = load i8*, i8** %10
%83 = getelementptr inbounds i8, i8* %82, i64 2050
%84 = load i8, i8* %83
%85 = sext i8 %84 to i32
%86 = load i8*, i8** %10
%87 = getelementptr inbounds i8, i8* %86, i64 2051
%88 = load i8, i8* %87
%89 = sext i8 %88 to i32
%90 = load i8*, i8** %10
%91 = getelementptr inbounds i8, i8* %90, i64 2052
%92 = load i8, i8* %91
%93 = sext i8 %92 to i32
%94 = load i8*, i8** %10
%95 = getelementptr inbounds i8, i8* %94, i64 2053
%96 = load i8, i8* %95
%97 = sext i8 %96 to i32
%98 = load i8*, i8** %10
%99 = getelementptr inbounds i8, i8* %98, i64 2054
%100 = load i8, i8* %99
%101 = sext i8 %100 to i32
%102 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0), i32 2050, i32 %85, i32 %89, i32 %93, i32 %97, i32 %101)
%103 = call i32 @sleep(i32 1)
br label %42
}

LLVM IR (-O2):
@errno = global i64 0
@IPC_CREAT = global i32 0
@.str = [17 x i8] c"shmget error %s\0A\00"
@.str.1 = [22 x i8] c"shmget successful %d\0A\00"
@.str.2 = [6 x i8] c"shmat\00"
@.str.3 = [11 x i8] c"memory at:\00"
@.str.4 = [28 x i8] c" (%4.4d): %x, %x, %x, %x : \00"
@.str.5 = [32 x i8] c" (%4.4d): %x, %x, %x, %x, %x : \00"
@.str.6 = [30 x i8] c" (%4.4d): %x, %x, %x, %x, %x\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = getelementptr inbounds i8*, i8** %1, i64 1
%4 = load i8*, i8** %3
%5 = tail call i32 @atoi(i8* %4)
store i64 0, i64* @errno
%6 = load i32, i32* @IPC_CREAT
%7 = or i32 %6, 511
%8 = tail call i32 @shmget(i32 %5, i32 4096, i32 %7)
%9 = icmp slt i32 %8, 0
%10 = load i64, i64* @errno
%11 = icmp ne i64 %10, 0
%12 = select i1 %9, i1 %11, i1 false
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = tail call i8* @strerror(i64 %10)
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %14)
br label %16
16: ; preds = %13, %2
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%18 = tail call i64 @shmat(i32 %8, i32* null, i32 0)
%19 = inttoptr i64 %18 to i8*
%20 = icmp eq i8* %19, inttoptr (i64 -1 to i8*)
br i1 %20, label %35, label %21
21: ; preds = %16
%22 = getelementptr inbounds i8, i8* %19, i64 1
%23 = getelementptr inbounds i8, i8* %19, i64 2
%24 = getelementptr inbounds i8, i8* %19, i64 3
%25 = getelementptr inbounds i8, i8* %19, i64 4
%26 = getelementptr inbounds i8, i8* %19, i64 5
%27 = getelementptr inbounds i8, i8* %19, i64 6
%28 = getelementptr inbounds i8, i8* %19, i64 7
%29 = getelementptr inbounds i8, i8* %19, i64 8
%30 = getelementptr inbounds i8, i8* %19, i64 2050
%31 = getelementptr inbounds i8, i8* %19, i64 2051
%32 = getelementptr inbounds i8, i8* %19, i64 2052
%33 = getelementptr inbounds i8, i8* %19, i64 2053
%34 = getelementptr inbounds i8, i8* %19, i64 2054
br label %38
35: ; preds = %16
%36 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%37 = tail call i32 @exit(i32 1)
unreachable
38: ; preds = %21, %38
%39 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
%40 = load i8, i8* %19
%41 = sext i8 %40 to i32
%42 = load i8, i8* %22
%43 = sext i8 %42 to i32
%44 = load i8, i8* %23
%45 = sext i8 %44 to i32
%46 = load i8, i8* %24
%47 = sext i8 %46 to i32
%48 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 0, i32 %41, i32 %43, i32 %45, i32 %47)
%49 = load i8, i8* %25
%50 = sext i8 %49 to i32
%51 = load i8, i8* %26
%52 = sext i8 %51 to i32
%53 = load i8, i8* %27
%54 = sext i8 %53 to i32
%55 = load i8, i8* %28
%56 = sext i8 %55 to i32
%57 = load i8, i8* %29
%58 = sext i8 %57 to i32
%59 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0), i32 4, i32 %50, i32 %52, i32 %54, i32 %56, i32 %58)
%60 = load i8, i8* %30
%61 = sext i8 %60 to i32
%62 = load i8, i8* %31
%63 = sext i8 %62 to i32
%64 = load i8, i8* %32
%65 = sext i8 %64 to i32
%66 = load i8, i8* %33
%67 = sext i8 %66 to i32
%68 = load i8, i8* %34
%69 = sext i8 %68 to i32
%70 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0), i32 2050, i32 %61, i32 %63, i32 %65, i32 %67, i32 %69)
%71 = tail call i32 @sleep(i32 1)
br label %38
}