#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 /*<<< orphan*/ shmid;

/* 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);
int read(int, int *, int);
void *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stdout;
void child(int fd) {
  int shmid;
  if (read(fd, &shmid, sizeof(shmid)) != sizeof(shmid)) {
    perror("write");
    abort();
  }
  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
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
cltq
cmp    $0x4,%rax
je     11bb <child+0x3b>
lea    0xe51(%rip),%rdi
call   1070 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1080 <shmat@plt>
mov    %rax,-0x10(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x10(%rbp)
jne    11f1 <child+0x71>
lea    0xe21(%rip),%rdi
call   1070 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    $0x2,%edi
call   10a0 <sleep@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
je     1264 <child+0xe4>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jle    1255 <child+0xd5>
mov    -0x20(%rbp),%esi
lea    0xde1(%rip),%rdi
call   1040 <printf@plt>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
xor    %ecx,%ecx
sub    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
jmp    125f <child+0xdf>
mov    $0x1,%edi
call   10a0 <sleep@plt>
jmp    1203 <child+0x83>
xor    %edi,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<child>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
lea    0x4(%rsp),%rsi
mov    $0x4,%edx
call   1050 <read@plt>
cmp    $0x4,%eax
jne    11b7 <child+0x37>
mov    0x4(%rsp),%edi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
jne    11ca <child+0x4a>
lea    0xe51(%rip),%rdi
jmp    11be <child+0x3e>
lea    0xe42(%rip),%rdi
call   1070 <perror@plt>
xor    %eax,%eax
call   1030 <abort@plt>
mov    %rax,%rbx
mov    $0x2,%edi
call   10a0 <sleep@plt>
mov    (%rbx),%ebp
test   %ebp,%ebp
je     121e <child+0x9e>
lea    0xe2f(%rip),%r14
mov    0x2df5(%rip),%r15
jmp    1200 <child+0x80>
nopl   (%rax)
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    (%rbx),%ebp
test   %ebp,%ebp
je     121e <child+0x9e>
test   %ebp,%ebp
jle    11f0 <child+0x70>
mov    %r14,%rdi
mov    %ebp,%esi
call   1040 <printf@plt>
mov    (%r15),%edi
call   1060 <fflush@plt>
neg    %ebp
mov    %ebp,(%rbx)
test   %ebp,%ebp
jne    1200 <child+0x80>
xor    %edi,%edi
call   1090 <exit@plt>

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

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