#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 pid_t;
typedef int key_t;

/* Variables and functions */
int IPC_CREAT;
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
char *shmat(int, int /*<<< orphan*/, int /*<<< orphan*/);
int shmdt(char *);
int shmget(int, int, int);
scalar_t__ strtol(char *, int /*<<< orphan*/ *, int);
int main(int argc, char **argv) {
  if (argc != 2) {
    perror("Should be ./shared.o <size>");
    exit(-1);
  }
  int size = (int)strtol(argv[1], NULL, 10);
  printf(">> Size: %d\n", size);
  int id, r;
  char *ap;
  key_t key = 0x6060;
  pid_t pid;
  id = shmget(key, (sizeof(char) * size), IPC_CREAT | 0666);
  if (id == -1) {
    perror("Unable to create memory space");
    exit(-1);
  }
  ap = shmat(id, 0, 0);
  if (ap == (char *)-1) {
    perror("Unable to map memory space");
    exit(-1);
  }
  pid = fork();
  if (pid == -1) {
    perror("Unable to create child");
    exit(-1);
  }
  if (pid == 0) {
    int cond = 0;
    while (!cond) {
      for (int i = 0; i < size; ++i) {
        cond = cond || (ap[i] != 'a');
      }
      cond = !cond;
    }
  } else {
    for (int i = 0; i < size; ++i) {
      ap[i] = 'a';
    }
  }
  r = shmdt(ap);
  if (r == -1) {
    perror("Unable to unmap memory space");
    exit(-1);
  }
}


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)
cmpl   $0x2,-0x8(%rbp)
je     11b6 <main+0x36>
lea    0xe59(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe43(%rip),%rdi
call   1040 <printf@plt>
movl   $0x6060,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
movslq -0x14(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%esi
mov    0x2de7(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
call   1080 <shmget@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1229 <main+0xa9>
lea    0xe0f(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1070 <shmat@plt>
mov    %rax,-0x28(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x28(%rbp)
jne    1260 <main+0xe0>
lea    0xdf6(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    128a <main+0x10a>
lea    0xde7(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
cmpl   $0x0,-0x30(%rbp)
jne    131c <main+0x19c>
movl   $0x0,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    12b1 <main+0x131>
jmp    1317 <main+0x197>
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1301 <main+0x181>
mov    $0x1,%al
cmpl   $0x0,-0x34(%rbp)
mov    %al,-0x3d(%rbp)
jne    12e8 <main+0x168>
mov    -0x28(%rbp),%rax
movslq -0x38(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x61,%eax
setne  %al
mov    %al,-0x3d(%rbp)
mov    -0x3d(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x34(%rbp)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    12b8 <main+0x138>
cmpl   $0x0,-0x34(%rbp)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x34(%rbp)
jmp    129b <main+0x11b>
jmp    134e <main+0x1ce>
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1349 <main+0x1c9>
mov    -0x28(%rbp),%rax
movslq -0x3c(%rbp),%rcx
movb   $0x61,(%rax,%rcx,1)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1323 <main+0x1a3>
jmp    134e <main+0x1ce>
mov    -0x28(%rbp),%rdi
call   1030 <shmdt@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    137a <main+0x1fa>
lea    0xd0e(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    122f <main+0x9f>
mov    0x8(%rsi),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %rax,%r14
lea    0xe65(%rip),%rdi
mov    %r14d,%esi
call   1040 <printf@plt>
mov    0x2e1a(%rip),%rax
mov    $0x1b6,%edx
or     (%rax),%edx
mov    $0x6060,%edi
mov    %r14d,%esi
call   1090 <shmget@plt>
cmp    $0xffffffff,%eax
je     1238 <main+0xa8>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     1241 <main+0xb1>
mov    %rax,%rbx
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     1218 <main+0x88>
cmp    $0xffffffff,%eax
je     1253 <main+0xc3>
test   %r14d,%r14d
jle    1218 <main+0x88>
mov    %r14d,%edx
mov    %rbx,%rdi
mov    $0x61,%esi
call   1050 <memset@plt>
mov    %rbx,%rdi
call   1030 <shmdt@plt>
cmp    $0xffffffff,%eax
je     124a <main+0xba>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdca(%rip),%rdi
jmp    125a <main+0xca>
lea    0xdea(%rip),%rdi
jmp    125a <main+0xca>
lea    0xdff(%rip),%rdi
jmp    125a <main+0xca>
lea    0xe28(%rip),%rdi
jmp    125a <main+0xca>
lea    0xe08(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [28 x i8] c"Should be ./shared.o <size>\00"
@.str.1 = [13 x i8] c">> Size: %d\0A\00"
@IPC_CREAT = global i32 0
@.str.2 = [30 x i8] c"Unable to create memory space\00"
@.str.3 = [27 x i8] c"Unable to map memory space\00"
@.str.4 = [23 x i8] c"Unable to create child\00"
@.str.5 = [29 x i8] c"Unable to unmap memory space\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 i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 2
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 -1)
unreachable
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i64 @strtol(i8* %23, i32* null, i32 10)
%25 = trunc i64 %24 to i32
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %26)
store i32 24672, i32* %10
%28 = load i32, i32* %10
%29 = load i32, i32* %6
%30 = sext i32 %29 to i64
%31 = mul i64 1, %30
%32 = trunc i64 %31 to i32
%33 = load i32, i32* @IPC_CREAT
%34 = or i32 %33, 438
%35 = call i32 @shmget(i32 %28, i32 %32, i32 %34)
store i32 %35, i32* %7
%36 = load i32, i32* %7
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %20
%39 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%40 = call i32 @exit(i32 -1)
unreachable
41: ; preds = %20
%42 = load i32, i32* %7
%43 = call i8* @shmat(i32 %42, i32 0, i32 0)
store i8* %43, i8** %9
%44 = load i8*, i8** %9
%45 = icmp eq i8* %44, inttoptr (i64 -1 to i8*)
br i1 %45, label %46, label %49
46: ; preds = %41
%47 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%48 = call i32 @exit(i32 -1)
unreachable
49: ; preds = %41
%50 = call i32 (...) @fork()
store i32 %50, i32* %11
%51 = load i32, i32* %11
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %56
53: ; preds = %49
%54 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%55 = call i32 @exit(i32 -1)
unreachable
56: ; preds = %49
%57 = load i32, i32* %11
%58 = icmp eq i32 %57, 0
br i1 %58, label %59, label %92
59: ; preds = %56
store i32 0, i32* %12
br label %60
60: ; preds = %86, %59
%61 = load i32, i32* %12
%62 = icmp ne i32 %61, 0
%63 = xor i1 %62, true
br i1 %63, label %64, label %91
64: ; preds = %60
store i32 0, i32* %13
br label %65
65: ; preds = %83, %64
%66 = load i32, i32* %13
%67 = load i32, i32* %6
%68 = icmp slt i32 %66, %67
br i1 %68, label %69, label %86
69: ; preds = %65
%70 = load i32, i32* %12
%71 = icmp ne i32 %70, 0
br i1 %71, label %80, label %72
72: ; preds = %69
%73 = load i8*, i8** %9
%74 = load i32, i32* %13
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds i8, i8* %73, i64 %75
%77 = load i8, i8* %76
%78 = sext i8 %77 to i32
%79 = icmp ne i32 %78, 97
br label %80
80: ; preds = %72, %69
%81 = phi i1 [ true, %69 ], [ %79, %72 ]
%82 = zext i1 %81 to i32
store i32 %82, i32* %12
br label %83
83: ; preds = %80
%84 = load i32, i32* %13
%85 = add nsw i32 %84, 1
store i32 %85, i32* %13
br label %65
86: ; preds = %65
%87 = load i32, i32* %12
%88 = icmp ne i32 %87, 0
%89 = xor i1 %88, true
%90 = zext i1 %89 to i32
store i32 %90, i32* %12
br label %60
91: ; preds = %60
br label %106
92: ; preds = %56
store i32 0, i32* %14
br label %93
93: ; preds = %102, %92
%94 = load i32, i32* %14
%95 = load i32, i32* %6
%96 = icmp slt i32 %94, %95
br i1 %96, label %97, label %105
97: ; preds = %93
%98 = load i8*, i8** %9
%99 = load i32, i32* %14
%100 = sext i32 %99 to i64
%101 = getelementptr inbounds i8, i8* %98, i64 %100
store i8 97, i8* %101
br label %102
102: ; preds = %97
%103 = load i32, i32* %14
%104 = add nsw i32 %103, 1
store i32 %104, i32* %14
br label %93
105: ; preds = %93
br label %106
106: ; preds = %105, %91
%107 = load i8*, i8** %9
%108 = call i32 @shmdt(i8* %107)
store i32 %108, i32* %8
%109 = load i32, i32* %8
%110 = icmp eq i32 %109, -1
br i1 %110, label %111, label %114
111: ; preds = %106
%112 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0))
%113 = call i32 @exit(i32 -1)
unreachable
114: ; preds = %106
%115 = load i32, i32* %3
ret i32 %115
}

LLVM IR (-O2):
@.str = [28 x i8] c"Should be ./shared.o <size>\00"
@.str.1 = [13 x i8] c">> Size: %d\0A\00"
@IPC_CREAT = global i32 0
@.str.2 = [30 x i8] c"Unable to create memory space\00"
@.str.3 = [27 x i8] c"Unable to map memory space\00"
@.str.4 = [23 x i8] c"Unable to create child\00"
@.str.5 = [29 x i8] c"Unable to unmap memory space\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 -1)
unreachable
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i64 @strtol(i8* nocapture %9, i32* null, i32 10)
%11 = trunc i64 %10 to i32
%12 = tail call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = load i32, i32* @IPC_CREAT
%14 = or i32 %13, 438
%15 = tail call i32 @shmget(i32 24672, i32 %11, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %7
%18 = tail call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%19 = tail call i32 @exit(i32 -1)
unreachable
20: ; preds = %7
%21 = tail call i8* @shmat(i32 %15, i32 0, i32 0)
%22 = icmp eq i8* %21, inttoptr (i64 -1 to i8*)
br i1 %22, label %23, label %26
23: ; preds = %20
%24 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%25 = tail call i32 @exit(i32 -1)
unreachable
26: ; preds = %20
%27 = tail call i32 (...) @fork()
switch i32 %27, label %28 [
i32 -1, label %32
i32 0, label %35
]
28: ; preds = %26
%29 = icmp sgt i32 %11, 0
br i1 %29, label %30, label %35
30: ; preds = %28
%31 = and i64 %10, 4294967295
br label %35
32: ; preds = %26
%33 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%34 = tail call i32 @exit(i32 -1)
unreachable
35: ; preds = %26, %30, %28
%36 = tail call i32 @shmdt(i8* %21)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = tail call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0))
%40 = tail call i32 @exit(i32 -1)
unreachable
41: ; preds = %35
ret i32 0
}