#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 /*<<< orphan*/ sleep(int);
int main(void) {
  int shmid;
  unsigned long i;
  char *shmaddr;
  if ((shmid = shmget(2, (25UL * 1024 * 1024),
                      04000 | IPC_CREAT | SHM_R | SHM_W)) < 0) {
    perror("shmget");
    exit(1);
  }
  printf("shmid: 0x%x\n", shmid);
  sleep(10000);
  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 < (25UL * 1024 * 1024); i++) {
    shmaddr[i] = (char)(i);
    if (!(i % (1024 * 1024)))
      printf(".");
  }
  printf("\n");
  printf("Starting the Check...");
  for (i = 0; i < (25UL * 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);
  sleep(1000);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
or     $0x800,%edx
mov    0x2e43(%rip),%rax
or     (%rax),%edx
mov    0x2e1a(%rip),%rax
or     (%rax),%edx
mov    $0x2,%edi
mov    $0x1900000,%esi
call   1080 <shmget@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11e1 <main+0x61>
lea    0xe2e(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%esi
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x2710,%edi
call   10a0 <sleep@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    124a <main+0xca>
lea    0xdee(%rip),%rdi
call   1060 <perror@plt>
mov    -0x8(%rbp),%edi
mov    0x2da3(%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    0xddc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xddb(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x10(%rbp)
cmpq   $0x1900000,-0x10(%rbp)
01
jae    12c9 <main+0x149>
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    12b3 <main+0x133>
lea    0xdbc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12b8 <main+0x138>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1272 <main+0xf2>
lea    0xdb6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xd76(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movq   $0x0,-0x10(%rbp)
cmpq   $0x1900000,-0x10(%rbp)
01
jae    133e <main+0x1be>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
mov    -0x10(%rbp),%rcx
movsbl %cl,%ecx
cmp    %ecx,%eax
je     1328 <main+0x1a8>
mov    -0x10(%rbp),%rsi
lea    0xd49(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    132d <main+0x1ad>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    12ed <main+0x16d>
lea    0xd3c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <shmdt@plt>
cmp    $0x0,%rax
je     138a <main+0x20a>
lea    0xd22(%rip),%rdi
call   1060 <perror@plt>
mov    -0x8(%rbp),%edi
mov    0x2c63(%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    0x2c44(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1050 <shmctl@plt>
mov    $0x3e8,%edi
call   10a0 <sleep@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    0x2e21(%rip),%rax
mov    (%rax),%edx
mov    0x2e30(%rip),%rax
or     (%rax),%edx
mov    0x2e07(%rip),%rax
or     (%rax),%edx
or     $0x800,%edx
mov    $0x1900000,%esi
mov    $0x2,%edi
call   10a0 <shmget@plt>
test   %eax,%eax
js     1301 <main+0x161>
mov    %eax,%r14d
lea    0xe1d(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x2710,%edi
call   10c0 <sleep@plt>
mov    %r14d,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     1317 <main+0x177>
mov    %rax,%r12
lea    0xe14(%rip),%rdi
xor    %ebp,%ebp
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe4a(%rip),%rdi
call   1040 <puts@plt>
jmp    124d <main+0xad>
nopw   0x0(%rax,%rax,1)
add    $0x1,%rbp
cmp    $0x1900000,%rbp
je     1265 <main+0xc5>
mov    %bpl,(%r12,%rbp,1)
test   $0xfffff,%ebp
jne    1240 <main+0xa0>
mov    $0x2e,%edi
call   1030 <putchar@plt>
jmp    1240 <main+0xa0>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    0xdc8(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xdd0(%rip),%r15
xor    %ebx,%ebx
xor    %ebp,%ebp
jmp    12a3 <main+0x103>
nopw   0x0(%rax,%rax,1)
add    $0x1,%rbp
add    $0x1000000,%ebx
cmp    $0x1900000,%rbp
je     12c0 <main+0x120>
movsbl (%r12,%rbp,1),%eax
mov    %ebx,%ecx
sar    $0x18,%ecx
cmp    %eax,%ecx
je     1290 <main+0xf0>
mov    %r15,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1290 <main+0xf0>
lea    0xdc8(%rip),%rdi
call   1040 <puts@plt>
mov    %r12,%rdi
call   1050 <shmdt@plt>
test   %rax,%rax
jne    1340 <main+0x1a0>
mov    0x2cf8(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
xor    %edx,%edx
call   1070 <shmctl@plt>
mov    $0x3e8,%edi
call   10c0 <sleep@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcf8(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    0xcf6(%rip),%rdi
call   1080 <perror@plt>
mov    0x2cae(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
xor    %edx,%edx
call   1070 <shmctl@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>
lea    0xd24(%rip),%rdi
call   1080 <perror@plt>
mov    0x2c85(%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 26214400, 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 = call i32 @sleep(i32 10000)
%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 = %16
%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 = %16
%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, 26214400
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 = %72, %52
%56 = load i64, i64* %3
%57 = icmp ult i64 %56, 26214400
br i1 %57, label %58, label %75
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 %71
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)
br label %71
71: ; preds = %68, %58
br label %72
72: ; preds = %71
%73 = load i64, i64* %3
%74 = add i64 %73, 1
store i64 %74, i64* %3
br label %55
75: ; preds = %55
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.9, i64 0, i64 0))
%77 = load i8*, i8** %4
%78 = call i64 @shmdt(i8* %77)
%79 = icmp ne i64 %78, 0
br i1 %79, label %80, label %86
80: ; preds = %75
%81 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.10, i64 0, i64 0))
%82 = load i32, i32* %2
%83 = load i32, i32* @IPC_RMID
%84 = call i32 @shmctl(i32 %82, i32 %83, i32* null)
%85 = call i32 @exit(i32 3)
unreachable
86: ; preds = %75
%87 = load i32, i32* %2
%88 = load i32, i32* @IPC_RMID
%89 = call i32 @shmctl(i32 %87, i32 %88, i32* null)
%90 = call i32 @sleep(i32 1000)
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 26214400, 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 i32 @sleep(i32 10000)
%15 = tail call i8* @shmat(i32 %7, i8* null, i32 0)
%16 = icmp eq i8* %15, inttoptr (i64 -1 to i8*)
br i1 %16, label %17, label %22
17: ; preds = %12
%18 = tail call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%19 = load i32, i32* @IPC_RMID
%20 = tail call i32 @shmctl(i32 %7, i32 %19, i32* null)
%21 = tail call i32 @exit(i32 2)
unreachable
22: ; preds = %12
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %15)
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
br label %25
25: ; preds = %22, %33
%26 = phi i64 [ 0, %22 ], [ %34, %33 ]
%27 = trunc i64 %26 to i8
%28 = getelementptr inbounds i8, i8* %15, i64 %26
store i8 %27, i8* %28
%29 = and i64 %26, 1048575
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %25
%32 = tail call i32 @putchar(i32 46)
br label %33
33: ; preds = %25, %31
%34 = add nuw nsw i64 %26, 1
%35 = icmp eq i64 %34, 26214400
br i1 %35, label %36, label %25
36: ; preds = %33
%37 = tail call i32 @putchar(i32 10)
%38 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i64 0, i64 0))
br label %39
39: ; preds = %36, %50
%40 = phi i64 [ 0, %36 ], [ %51, %50 ]
%41 = getelementptr inbounds i8, i8* %15, i64 %40
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = trunc i64 %40 to i32
%45 = shl i32 %44, 24
%46 = ashr exact i32 %45, 24
%47 = icmp eq i32 %46, %43
br i1 %47, label %50, label %48
48: ; preds = %39
%49 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.8, i64 0, i64 0), i64 %40)
br label %50
50: ; preds = %39, %48
%51 = add nuw nsw i64 %40, 1
%52 = icmp eq i64 %51, 26214400
br i1 %52, label %53, label %39
53: ; preds = %50
%54 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.11, i64 0, i64 0))
%55 = tail call i64 @shmdt(i8* %15)
%56 = icmp eq i64 %55, 0
br i1 %56, label %62, label %57
57: ; preds = %53
%58 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.10, i64 0, i64 0))
%59 = load i32, i32* @IPC_RMID
%60 = tail call i32 @shmctl(i32 %7, i32 %59, i32* null)
%61 = tail call i32 @exit(i32 3)
unreachable
62: ; preds = %53
%63 = load i32, i32* @IPC_RMID
%64 = tail call i32 @shmctl(i32 %7, i32 %63, i32* null)
%65 = tail call i32 @sleep(i32 1000)
ret i32 0
}