#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 /*<<< orphan*/ IPC_RMID;
int SHM_R;
int SHM_W;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
char *shmat(int, void *, int /*<<< orphan*/);
int /*<<< orphan*/ shmctl(int, int /*<<< orphan*/, int /*<<< orphan*/ *);
scalar_t__ shmdt(void const *);
int shmget(int, unsigned long, int);
int main(void) {
  int shmid;
  unsigned long i;
  char *shmaddr;
  if ((shmid = shmget(2, (256UL * 1024 * 1024),
                      04000 | IPC_CREAT | SHM_R | SHM_W)) < 0) {
    perror("shmget");
    exit(1);
  }
  printf("shmid: 0x%x\n", shmid);
  shmaddr = shmat(shmid, (void *)(0x0UL), (0));
  if (shmaddr == (char *)-1) {
    perror("Shared memory attach failure");
    shmctl(shmid, IPC_RMID, NULL);
    exit(2);
  }
  printf("shmaddr: %p\n", shmaddr);
  printf("Starting the writes:\n");
  for (i = 0; i < (256UL * 1024 * 1024); i++) {
    shmaddr[i] = (char)(i);
    if (!(i % (1024 * 1024)))
      printf(".");
  }
  printf("\n");
  printf("Starting the Check...");
  for (i = 0; i < (256UL * 1024 * 1024); i++)
    if (shmaddr[i] != (char)i)
      printf("\nIndex %lu mismatched\n", i);
  printf("Done.\n");
  if (shmdt((const void *)shmaddr) != 0) {
    perror("Detach failure");
    shmctl(shmid, IPC_RMID, NULL);
    exit(3);
  }
  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    0x2e4a(%rip),%rax
mov    (%rax),%edx
or     $0x800,%edx
mov    0x2e53(%rip),%rax
or     (%rax),%edx
mov    0x2e2a(%rip),%rax
or     (%rax),%edx
mov    $0x2,%edi
mov    $0x10000000,%esi
call   1080 <shmget@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11d1 <main+0x61>
lea    0xe3e(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%esi
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1070 <shmat@plt>
mov    %rax,-0x18(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x18(%rbp)
jne    1230 <main+0xc0>
lea    0xe08(%rip),%rdi
call   1060 <perror@plt>
mov    -0x8(%rbp),%edi
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <shmctl@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%rsi
lea    0xdf6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xdf5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x10(%rbp)
cmpq   $0x10000000,-0x10(%rbp)
10
jae    12af <main+0x13f>
mov    -0x10(%rbp),%rax
mov    %al,%dl
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
and    $0xfffff,%rax
cmp    $0x0,%rax
jne    1299 <main+0x129>
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    129e <main+0x12e>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1258 <main+0xe8>
lea    0xdd0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xd90(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x10(%rbp)
cmpq   $0x10000000,-0x10(%rbp)
10
jae    1324 <main+0x1b4>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
mov    -0x10(%rbp),%rcx
movsbl %cl,%ecx
cmp    %ecx,%eax
je     130e <main+0x19e>
mov    -0x10(%rbp),%rsi
lea    0xd63(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1313 <main+0x1a3>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    12d3 <main+0x163>
lea    0xd56(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <shmdt@plt>
cmp    $0x0,%rax
je     1370 <main+0x200>
lea    0xd3c(%rip),%rdi
call   1060 <perror@plt>
mov    -0x8(%rbp),%edi
mov    0x2c7d(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <shmctl@plt>
mov    $0x3,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%edi
mov    0x2c5e(%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   %r15
push   %r14
push   %r12
push   %rbx
mov    0x2e31(%rip),%rax
mov    (%rax),%edx
mov    0x2e40(%rip),%rax
or     (%rax),%edx
mov    0x2e17(%rip),%rax
or     (%rax),%edx
or     $0x800,%edx
mov    $0x10000000,%esi
mov    $0x2,%edi
call   10a0 <shmget@plt>
test   %eax,%eax
js     12d7 <main+0x147>
mov    %eax,%r14d
lea    0xe2d(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     12ed <main+0x15d>
mov    %rax,%r12
lea    0xe2e(%rip),%rdi
xor    %ebp,%ebp
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe64(%rip),%rdi
call   1040 <puts@plt>
jmp    122d <main+0x9d>
nopl   (%rax)
add    $0x1,%rbp
cmp    $0x10000000,%rbp
je     1245 <main+0xb5>
mov    %bpl,(%r12,%rbp,1)
test   $0xfffff,%ebp
jne    1220 <main+0x90>
mov    $0x2e,%edi
call   1030 <putchar@plt>
jmp    1220 <main+0x90>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0xde8(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xdf0(%rip),%r15
xor    %ebx,%ebx
xor    %ebp,%ebp
jmp    1283 <main+0xf3>
nopw   0x0(%rax,%rax,1)
add    $0x1,%rbp
add    $0x1000000,%ebx
cmp    $0x10000000,%rbp
je     12a0 <main+0x110>
movsbl (%r12,%rbp,1),%eax
mov    %ebx,%ecx
sar    $0x18,%ecx
cmp    %eax,%ecx
je     1270 <main+0xe0>
mov    %r15,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1270 <main+0xe0>
lea    0xde8(%rip),%rdi
call   1040 <puts@plt>
mov    %r12,%rdi
call   1050 <shmdt@plt>
test   %rax,%rax
jne    1316 <main+0x186>
mov    0x2d18(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
xor    %edx,%edx
call   1070 <shmctl@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd22(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xd20(%rip),%rdi
call   1080 <perror@plt>
mov    0x2cd8(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
xor    %edx,%edx
call   1070 <shmctl@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>
lea    0xd4e(%rip),%rdi
call   1080 <perror@plt>
mov    0x2caf(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
xor    %edx,%edx
call   1070 <shmctl@plt>
mov    $0x3,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@IPC_CREAT = global i32 0
@SHM_R = global i32 0
@SHM_W = global i32 0
@.str = [7 x i8] c"shmget\00"
@.str.1 = [13 x i8] c"shmid: 0x%x\0A\00"
@.str.2 = [29 x i8] c"Shared memory attach failure\00"
@IPC_RMID = global i32 0
@.str.3 = [13 x i8] c"shmaddr: %p\0A\00"
@.str.4 = [22 x i8] c"Starting the writes:\0A\00"
@.str.5 = [2 x i8] c".\00"
@.str.6 = [2 x i8] c"\0A\00"
@.str.7 = [22 x i8] c"Starting the Check...\00"
@.str.8 = [23 x i8] c"\0AIndex %lu mismatched\0A\00"
@.str.9 = [7 x i8] c"Done.\0A\00"
@.str.10 = [15 x i8] c"Detach failure\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca i8*
store i32 0, i32* %1
%5 = load i32, i32* @IPC_CREAT
%6 = or i32 2048, %5
%7 = load i32, i32* @SHM_R
%8 = or i32 %6, %7
%9 = load i32, i32* @SHM_W
%10 = or i32 %8, %9
%11 = call i32 @shmget(i32 2, i64 268435456, i32 %10)
store i32 %11, i32* %2
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %0
%17 = load i32, i32* %2
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = load i32, i32* %2
%20 = call i8* @shmat(i32 %19, i8* null, i32 0)
store i8* %20, i8** %4
%21 = load i8*, i8** %4
%22 = icmp eq i8* %21, inttoptr (i64 -1 to i8*)
br i1 %22, label %23, label %29
23: ; preds = %16
%24 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%25 = load i32, i32* %2
%26 = load i32, i32* @IPC_RMID
%27 = call i32 @shmctl(i32 %25, i32 %26, i32* null)
%28 = call i32 @exit(i32 2)
unreachable
29: ; preds = %16
%30 = load i8*, i8** %4
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %30)
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
store i64 0, i64* %3
br label %33
33: ; preds = %48, %29
%34 = load i64, i64* %3
%35 = icmp ult i64 %34, 268435456
br i1 %35, label %36, label %51
36: ; preds = %33
%37 = load i64, i64* %3
%38 = trunc i64 %37 to i8
%39 = load i8*, i8** %4
%40 = load i64, i64* %3
%41 = getelementptr inbounds i8, i8* %39, i64 %40
store i8 %38, i8* %41
%42 = load i64, i64* %3
%43 = urem i64 %42, 1048576
%44 = icmp ne i64 %43, 0
br i1 %44, label %47, label %45
45: ; preds = %36
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
br label %47
47: ; preds = %45, %36
br label %48
48: ; preds = %47
%49 = load i64, i64* %3
%50 = add i64 %49, 1
store i64 %50, i64* %3
br label %33
51: ; preds = %33
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0))
store i64 0, i64* %3
br label %54
54: ; preds = %71, %51
%55 = load i64, i64* %3
%56 = icmp ult i64 %55, 268435456
br i1 %56, label %57, label %74
57: ; preds = %54
%58 = load i8*, i8** %4
%59 = load i64, i64* %3
%60 = getelementptr inbounds i8, i8* %58, i64 %59
%61 = load i8, i8* %60
%62 = sext i8 %61 to i32
%63 = load i64, i64* %3
%64 = trunc i64 %63 to i8
%65 = sext i8 %64 to i32
%66 = icmp ne i32 %62, %65
br i1 %66, label %67, label %70
67: ; preds = %57
%68 = load i64, i64* %3
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.8, i64 0, i64 0), i64 %68)
br label %70
70: ; preds = %67, %57
br label %71
71: ; preds = %70
%72 = load i64, i64* %3
%73 = add i64 %72, 1
store i64 %73, i64* %3
br label %54
74: ; preds = %54
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.9, i64 0, i64 0))
%76 = load i8*, i8** %4
%77 = call i64 @shmdt(i8* %76)
%78 = icmp ne i64 %77, 0
br i1 %78, label %79, label %85
79: ; preds = %74
%80 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.10, i64 0, i64 0))
%81 = load i32, i32* %2
%82 = load i32, i32* @IPC_RMID
%83 = call i32 @shmctl(i32 %81, i32 %82, i32* null)
%84 = call i32 @exit(i32 3)
unreachable
85: ; preds = %74
%86 = load i32, i32* %2
%87 = load i32, i32* @IPC_RMID
%88 = call i32 @shmctl(i32 %86, i32 %87, i32* null)
ret i32 0
}

LLVM IR (-O2):
@IPC_CREAT = global i32 0
@SHM_R = global i32 0
@SHM_W = global i32 0
@.str = [7 x i8] c"shmget\00"
@.str.1 = [13 x i8] c"shmid: 0x%x\0A\00"
@.str.2 = [29 x i8] c"Shared memory attach failure\00"
@IPC_RMID = global i32 0
@.str.3 = [13 x i8] c"shmaddr: %p\0A\00"
@.str.7 = [22 x i8] c"Starting the Check...\00"
@.str.8 = [23 x i8] c"\0AIndex %lu mismatched\0A\00"
@.str.10 = [15 x i8] c"Detach failure\00"
@str = [21 x i8] c"Starting the writes:\00"
@str.11 = [6 x i8] c"Done.\00"
define i32 @main() {
%1 = load i32, i32* @IPC_CREAT
%2 = load i32, i32* @SHM_R
%3 = load i32, i32* @SHM_W
%4 = or i32 %1, %2
%5 = or i32 %4, %3
%6 = or i32 %5, 2048
%7 = tail call i32 @shmget(i32 2, i64 268435456, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%14 = tail call i8* @shmat(i32 %7, i8* null, i32 0)
%15 = icmp eq i8* %14, inttoptr (i64 -1 to i8*)
br i1 %15, label %16, label %21
16: ; preds = %12
%17 = tail call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%18 = load i32, i32* @IPC_RMID
%19 = tail call i32 @shmctl(i32 %7, i32 %18, i32* null)
%20 = tail call i32 @exit(i32 2)
unreachable
21: ; preds = %12
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %14)
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
br label %24
24: ; preds = %21, %32
%25 = phi i64 [ 0, %21 ], [ %33, %32 ]
%26 = trunc i64 %25 to i8
%27 = getelementptr inbounds i8, i8* %14, i64 %25
store i8 %26, i8* %27
%28 = and i64 %25, 1048575
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %24
%31 = tail call i32 @putchar(i32 46)
br label %32
32: ; preds = %24, %30
%33 = add nuw nsw i64 %25, 1
%34 = icmp eq i64 %33, 268435456
br i1 %34, label %35, label %24
35: ; preds = %32
%36 = tail call i32 @putchar(i32 10)
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0))
br label %38
38: ; preds = %35, %49
%39 = phi i64 [ 0, %35 ], [ %50, %49 ]
%40 = getelementptr inbounds i8, i8* %14, i64 %39
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = trunc i64 %39 to i32
%44 = shl i32 %43, 24
%45 = ashr exact i32 %44, 24
%46 = icmp eq i32 %45, %42
br i1 %46, label %49, label %47
47: ; preds = %38
%48 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.8, i64 0, i64 0), i64 %39)
br label %49
49: ; preds = %38, %47
%50 = add nuw nsw i64 %39, 1
%51 = icmp eq i64 %50, 268435456
br i1 %51, label %52, label %38
52: ; preds = %49
%53 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.11, i64 0, i64 0))
%54 = tail call i64 @shmdt(i8* %14)
%55 = icmp eq i64 %54, 0
br i1 %55, label %61, label %56
56: ; preds = %52
%57 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.10, i64 0, i64 0))
%58 = load i32, i32* @IPC_RMID
%59 = tail call i32 @shmctl(i32 %7, i32 %58, i32* null)
%60 = tail call i32 @exit(i32 3)
unreachable
61: ; preds = %52
%62 = load i32, i32* @IPC_RMID
%63 = tail call i32 @shmctl(i32 %7, i32 %62, i32* null)
ret i32 0
}