#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 child(int shmid) {
  void *addr = shmat(shmid, NULL, 0);
  if (addr == (void *)-1) {
    perror("Child: shmat");
    abort();
  }
  int *ptr = (int *)addr;
  sleep(2);
  int val;
  while ((val = *ptr) != 0) {
    int i = *ptr;
    if (i > 0) {
      printf("Client: %d\n", i);
      fflush(stdout);
      *ptr = -i;
    } else {
      sleep(1);
    }
  }
  exit(0);
}


x86 assembly (clang, -O0):
<child>:
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 <child+0x41>
lea    0xe5b(%rip),%rdi
call   1060 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    $0x2,%edi
call   1090 <sleep@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
je     1224 <child+0xb4>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jle    1215 <child+0xa5>
mov    -0x20(%rbp),%esi
lea    0xe1b(%rip),%rdi
call   1040 <printf@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
xor    %ecx,%ecx
sub    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
jmp    121f <child+0xaf>
mov    $0x1,%edi
call   1090 <sleep@plt>
jmp    11c3 <child+0x53>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<child>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %esi,%esi
xor    %edx,%edx
call   1070 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
jne    1199 <child+0x29>
lea    0xe73(%rip),%rdi
call   1060 <perror@plt>
xor    %eax,%eax
call   1030 <abort@plt>
mov    %rax,%rbx
mov    $0x2,%edi
call   1090 <sleep@plt>
mov    (%rbx),%ebp
test   %ebp,%ebp
je     11ee <child+0x7e>
lea    0xe5a(%rip),%r14
mov    0x2e26(%rip),%r15
jmp    11d0 <child+0x60>
nopl   0x0(%rax)
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    (%rbx),%ebp
test   %ebp,%ebp
je     11ee <child+0x7e>
test   %ebp,%ebp
jle    11c0 <child+0x50>
mov    %r14,%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
mov    (%r15),%edi
call   1050 <fflush@plt>
neg    %ebp
mov    %ebp,(%rbx)
test   %ebp,%ebp
jne    11d0 <child+0x60>
xor    %edi,%edi
call   1080 <exit@plt>

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

LLVM IR (-O2):
@.str = [13 x i8] c"Child: shmat\00"
@.str.1 = [12 x i8] c"Client: %d\0A\00"
@stdout = global i32 0
define void @child(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 ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 (...) @abort()
unreachable
7: ; preds = %1
%8 = bitcast i8* %2 to i32*
%9 = tail call i32 @sleep(i32 2)
%10 = load i32, i32* %8
%11 = icmp eq i32 %10, 0
br i1 %11, label %26, label %12
12: ; preds = %7, %23
%13 = phi i32 [ %24, %23 ], [ %10, %7 ]
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %12
%16 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%17 = load i32, i32* @stdout
%18 = tail call i32 @fflush(i32 %17)
%19 = sub nsw i32 0, %13
store i32 %19, i32* %8
br label %23
20: ; preds = %12
%21 = tail call i32 @sleep(i32 1)
%22 = load i32, i32* %8
br label %23
23: ; preds = %20, %15
%24 = phi i32 [ %22, %20 ], [ %19, %15 ]
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %12
26: ; preds = %23, %7
%27 = tail call i32 @exit(i32 0)
unreachable
}