#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;
  shmid = shmget(2, (256UL * 1024 * 1024), 04000 | IPC_CREAT | SHM_R | SHM_W);
  if (shmid < 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);
      exit(3);
    }
  printf("Done.\n");
  if (shmdt((const void *)shmaddr) != 0) {
    perror("Detach failure");
    shmctl(shmid, IPC_RMID, NULL);
    exit(4);
  }
  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)
cmpl   $0x0,-0x8(%rbp)
jge    11d2 <main+0x62>
lea    0xe3d(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%esi
lea    0xe2b(%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    1231 <main+0xc1>
lea    0xe07(%rip),%rdi
call   1060 <perror@plt>
mov    -0x8(%rbp),%edi
mov    0x2dbc(%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    0xdf5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xdf4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x10(%rbp)
cmpq   $0x10000000,-0x10(%rbp)
10
jae    12b0 <main+0x140>
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    129a <main+0x12a>
lea    0xdd5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    129f <main+0x12f>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1259 <main+0xe9>
lea    0xdcf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xd8f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x10(%rbp)
cmpq   $0x10000000,-0x10(%rbp)
10
jae    132f <main+0x1bf>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
mov    -0x10(%rbp),%rcx
movsbl %cl,%ecx
cmp    %ecx,%eax
je     1319 <main+0x1a9>
mov    -0x10(%rbp),%rsi
lea    0xd62(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x3,%edi
call   1090 <exit@plt>
jmp    131e <main+0x1ae>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    12d4 <main+0x164>
lea    0xd4b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <shmdt@plt>
cmp    $0x0,%rax
je     137b <main+0x20b>
lea    0xd31(%rip),%rdi
call   1060 <perror@plt>
mov    -0x8(%rbp),%edi
mov    0x2c72(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <shmctl@plt>
mov    $0x4,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%edi
mov    0x2c53(%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   %r14
push   %rbx
mov    0x2e35(%rip),%rax
mov    (%rax),%edx
mov    0x2e44(%rip),%rax
or     (%rax),%edx
mov    0x2e1b(%rip),%rax
or     (%rax),%edx
or     $0x800,%edx
mov    $0x10000000,%esi
mov    $0x2,%edi
call   10a0 <shmget@plt>
test   %eax,%eax
js     1324 <main+0x194>
mov    %eax,%r14d
lea    0xe31(%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     133a <main+0x1aa>
mov    %rax,%rbx
lea    0xe32(%rip),%rdi
xor    %ebp,%ebp
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe68(%rip),%rdi
call   1040 <puts@plt>
jmp    122d <main+0x9d>
nopl   0x0(%rax)
add    $0x1,%rbp
cmp    $0x10000000,%rbp
je     1245 <main+0xb5>
mov    %bpl,(%rbx,%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>
mov    $0x3,%esi
mov    $0x2000000,%eax
nopw   0x0(%rax,%rax,1)
movsbl -0x3(%rbx,%rsi,1),%ecx
lea    -0x2000000(%rax),%edx
sar    $0x18,%edx
cmp    %ecx,%edx
jne    12fc <main+0x16c>
movsbl -0x2(%rbx,%rsi,1),%ecx
lea    -0x1000000(%rax),%edx
sar    $0x18,%edx
cmp    %ecx,%edx
jne    1302 <main+0x172>
movsbl -0x1(%rbx,%rsi,1),%ecx
mov    %eax,%edx
sar    $0x18,%edx
cmp    %ecx,%edx
jne    1308 <main+0x178>
movsbl (%rbx,%rsi,1),%ecx
lea    0x1000000(%rax),%edx
sar    $0x18,%edx
cmp    %ecx,%edx
jne    130c <main+0x17c>
add    $0x4,%rsi
add    $0x4000000,%eax
cmp    $0x10000003,%rsi
jne    1270 <main+0xe0>
lea    0xdc3(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1050 <shmdt@plt>
test   %rax,%rax
jne    1363 <main+0x1d3>
mov    0x2cef(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
xor    %edx,%edx
call   1070 <shmctl@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
add    $0xfffffffffffffffd,%rsi
jmp    130c <main+0x17c>
add    $0xfffffffffffffffe,%rsi
jmp    130c <main+0x17c>
add    $0xffffffffffffffff,%rsi
lea    0xd41(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x3,%edi
call   10b0 <exit@plt>
lea    0xcd5(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xcd3(%rip),%rdi
call   1080 <perror@plt>
mov    0x2c8b(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
xor    %edx,%edx
call   1070 <shmctl@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>
lea    0xd01(%rip),%rdi
call   1080 <perror@plt>
mov    0x2c62(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
xor    %edx,%edx
call   1070 <shmctl@plt>
mov    $0x4,%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 = load i32, i32* %2
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %0
%18 = load i32, i32* %2
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = load i32, i32* %2
%21 = call i8* @shmat(i32 %20, i8* null, i32 0)
store i8* %21, i8** %4
%22 = load i8*, i8** %4
%23 = icmp eq i8* %22, inttoptr (i64 -1 to i8*)
br i1 %23, label %24, label %30
24: ; preds = %17
%25 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%26 = load i32, i32* %2
%27 = load i32, i32* @IPC_RMID
%28 = call i32 @shmctl(i32 %26, i32 %27, i32* null)
%29 = call i32 @exit(i32 2)
unreachable
30: ; preds = %17
%31 = load i8*, i8** %4
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %31)
%33 = 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 %34
34: ; preds = %49, %30
%35 = load i64, i64* %3
%36 = icmp ult i64 %35, 268435456
br i1 %36, label %37, label %52
37: ; preds = %34
%38 = load i64, i64* %3
%39 = trunc i64 %38 to i8
%40 = load i8*, i8** %4
%41 = load i64, i64* %3
%42 = getelementptr inbounds i8, i8* %40, i64 %41
store i8 %39, i8* %42
%43 = load i64, i64* %3
%44 = urem i64 %43, 1048576
%45 = icmp ne i64 %44, 0
br i1 %45, label %48, label %46
46: ; preds = %37
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
br label %48
48: ; preds = %46, %37
br label %49
49: ; preds = %48
%50 = load i64, i64* %3
%51 = add i64 %50, 1
store i64 %51, i64* %3
br label %34
52: ; preds = %34
%53 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%54 = 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 %55
55: ; preds = %73, %52
%56 = load i64, i64* %3
%57 = icmp ult i64 %56, 268435456
br i1 %57, label %58, label %76
58: ; preds = %55
%59 = load i8*, i8** %4
%60 = load i64, i64* %3
%61 = getelementptr inbounds i8, i8* %59, i64 %60
%62 = load i8, i8* %61
%63 = sext i8 %62 to i32
%64 = load i64, i64* %3
%65 = trunc i64 %64 to i8
%66 = sext i8 %65 to i32
%67 = icmp ne i32 %63, %66
br i1 %67, label %68, label %72
68: ; preds = %58
%69 = load i64, i64* %3
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.8, i64 0, i64 0), i64 %69)
%71 = call i32 @exit(i32 3)
unreachable
72: ; preds = %58
br label %73
73: ; preds = %72
%74 = load i64, i64* %3
%75 = add i64 %74, 1
store i64 %75, i64* %3
br label %55
76: ; preds = %55
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.9, i64 0, i64 0))
%78 = load i8*, i8** %4
%79 = call i64 @shmdt(i8* %78)
%80 = icmp ne i64 %79, 0
br i1 %80, label %81, label %87
81: ; preds = %76
%82 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.10, i64 0, i64 0))
%83 = load i32, i32* %2
%84 = load i32, i32* @IPC_RMID
%85 = call i32 @shmctl(i32 %83, i32 %84, i32* null)
%86 = call i32 @exit(i32 4)
unreachable
87: ; preds = %76
%88 = load i32, i32* %2
%89 = load i32, i32* @IPC_RMID
%90 = call i32 @shmctl(i32 %88, i32 %89, 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 = %78, %35
%39 = phi i64 [ 0, %35 ], [ %79, %78 ]
%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 %51, label %47
47: ; preds = %69, %60, %51, %38
%48 = phi i64 [ %39, %38 ], [ %52, %51 ], [ %61, %60 ], [ %70, %69 ]
%49 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.8, i64 0, i64 0), i64 %48)
%50 = tail call i32 @exit(i32 3)
unreachable
51: ; preds = %38
%52 = or i64 %39, 1
%53 = getelementptr inbounds i8, i8* %14, i64 %52
%54 = load i8, i8* %53
%55 = sext i8 %54 to i32
%56 = trunc i64 %52 to i32
%57 = shl i32 %56, 24
%58 = ashr exact i32 %57, 24
%59 = icmp eq i32 %58, %55
br i1 %59, label %60, label %47
60: ; preds = %51
%61 = or i64 %39, 2
%62 = getelementptr inbounds i8, i8* %14, i64 %61
%63 = load i8, i8* %62
%64 = sext i8 %63 to i32
%65 = trunc i64 %61 to i32
%66 = shl i32 %65, 24
%67 = ashr exact i32 %66, 24
%68 = icmp eq i32 %67, %64
br i1 %68, label %69, label %47
69: ; preds = %60
%70 = or i64 %39, 3
%71 = getelementptr inbounds i8, i8* %14, i64 %70
%72 = load i8, i8* %71
%73 = sext i8 %72 to i32
%74 = trunc i64 %70 to i32
%75 = shl i32 %74, 24
%76 = ashr exact i32 %75, 24
%77 = icmp eq i32 %76, %73
br i1 %77, label %78, label %47
78: ; preds = %69
%79 = add nuw nsw i64 %39, 4
%80 = icmp eq i64 %79, 268435456
br i1 %80, label %81, label %38
81: ; preds = %78
%82 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.11, i64 0, i64 0))
%83 = tail call i64 @shmdt(i8* %14)
%84 = icmp eq i64 %83, 0
br i1 %84, label %90, label %85
85: ; preds = %81
%86 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.10, i64 0, i64 0))
%87 = load i32, i32* @IPC_RMID
%88 = tail call i32 @shmctl(i32 %7, i32 %87, i32* null)
%89 = tail call i32 @exit(i32 4)
unreachable
90: ; preds = %81
%91 = load i32, i32* @IPC_RMID
%92 = tail call i32 @shmctl(i32 %7, i32 %91, i32* null)
ret i32 0
}