#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 /*<<< orphan*/ abort();
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
void *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stdout;
void parent(int shmid) {
  void *addr = shmat(shmid, NULL, 0);
  if (addr == (void *)-1) {
    perror("Parent: shmat");
    abort();
  }
  int *ptr = (int *)addr;
  int i;
  for (i = 1; i < 100000; i++) {
    printf("Server: %d\n", i);
    fflush(stdout);
    *ptr = i;
    while (*ptr != -i) {
      sleep(1);
    }
  }
  *ptr = 0;
  exit(0);
}


x86 assembly (clang, -O0):
<parent>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1070 <shmat@plt>
mov    %rax,-0x10(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x10(%rbp)
jne    11b1 <parent+0x41>
lea    0xe5b(%rip),%rdi
call   1060 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
cmpl   $0x186a0,-0x1c(%rbp)
jge    1228 <parent+0xb8>
mov    -0x1c(%rbp),%esi
lea    0xe37(%rip),%rdi
call   1040 <printf@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
xor    %ecx,%ecx
sub    -0x1c(%rbp),%ecx
cmp    %ecx,%eax
je     1215 <parent+0xa5>
mov    $0x1,%edi
call   1090 <sleep@plt>
jmp    11f3 <parent+0x83>
jmp    121a <parent+0xaa>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11c0 <parent+0x50>
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<parent>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
xor    %esi,%esi
xor    %edx,%edx
call   1070 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
jne    119a <parent+0x2a>
lea    0xe72(%rip),%rdi
call   1060 <perror@plt>
xor    %eax,%eax
call   1030 <abort@plt>
mov    %rax,%rbx
mov    $0x1,%r12d
lea    0xe64(%rip),%r14
mov    0x2e2f(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %r12d,%esi
call   1040 <printf@plt>
mov    (%r15),%edi
call   1050 <fflush@plt>
mov    %r12d,(%rbx)
mov    %r12d,%ebp
neg    %ebp
nopl   0x0(%rax,%rax,1)
mov    $0x1,%edi
call   1090 <sleep@plt>
cmp    %ebp,(%rbx)
jne    11e0 <parent+0x70>
add    $0x1,%r12d
cmp    $0x186a0,%r12d
jne    11c0 <parent+0x50>
movl   $0x0,(%rbx)
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"Parent: shmat\00"
@.str.1 = [12 x i8] c"Server: %d\0A\00"
@stdout = global i32 0
define void @parent(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i32
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = call i8* @shmat(i32 %6, i32* null, i32 0)
store i8* %7, i8** %3
%8 = load i8*, i8** %3
%9 = icmp eq i8* %8, inttoptr (i64 -1 to i8*)
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%12 = call i32 (...) @abort()
unreachable
13: ; preds = %1
%14 = load i8*, i8** %3
%15 = bitcast i8* %14 to i32*
store i32* %15, i32** %4
store i32 1, i32* %5
br label %16
16: ; preds = %35, %13
%17 = load i32, i32* %5
%18 = icmp slt i32 %17, 100000
br i1 %18, label %19, label %38
19: ; preds = %16
%20 = load i32, i32* %5
%21 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %20)
%22 = load i32, i32* @stdout
%23 = call i32 @fflush(i32 %22)
%24 = load i32, i32* %5
%25 = load i32*, i32** %4
store i32 %24, i32* %25
br label %26
26: ; preds = %32, %19
%27 = load i32*, i32** %4
%28 = load i32, i32* %27
%29 = load i32, i32* %5
%30 = sub nsw i32 0, %29
%31 = icmp ne i32 %28, %30
br i1 %31, label %32, label %34
32: ; preds = %26
%33 = call i32 @sleep(i32 1)
br label %26
34: ; preds = %26
br label %35
35: ; preds = %34
%36 = load i32, i32* %5
%37 = add nsw i32 %36, 1
store i32 %37, i32* %5
br label %16
38: ; preds = %16
%39 = load i32*, i32** %4
store i32 0, i32* %39
%40 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [14 x i8] c"Parent: shmat\00"
@.str.1 = [12 x i8] c"Server: %d\0A\00"
@stdout = global i32 0
define void @parent(i32 %0) {
%2 = tail call i8* @shmat(i32 %0, i32* null, i32 0)
%3 = icmp eq i8* %2, inttoptr (i64 -1 to i8*)
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 (...) @abort()
unreachable
7: ; preds = %1
%8 = bitcast i8* %2 to i32*
br label %9
9: ; preds = %19, %7
%10 = phi i32 [ 1, %7 ], [ %20, %19 ]
%11 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = load i32, i32* @stdout
%13 = tail call i32 @fflush(i32 %12)
store i32 %10, i32* %8
%14 = sub nsw i32 0, %10
br label %15
15: ; preds = %9, %15
%16 = tail call i32 @sleep(i32 1)
%17 = load i32, i32* %8
%18 = icmp eq i32 %17, %14
br i1 %18, label %19, label %15
19: ; preds = %15
%20 = add nuw nsw i32 %10, 1
%21 = icmp eq i32 %20, 100000
br i1 %21, label %22, label %9
22: ; preds = %19
store i32 0, i32* %8
%23 = tail call i32 @exit(i32 0)
unreachable
}