#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*/ assert(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ sem_down(int);
int /*<<< orphan*/ sem_up(int);
void *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
void child(int shmid, int parent_semid, int child_semid) {
  void *addr = shmat(shmid, NULL, 0);
  if (addr == (void *)-1) {
    perror("Child: shmat");
    exit(1);
  }
  int i;
  int *ptr = (int *)addr;
  while (1) {
    sem_down(child_semid);
    i = *ptr;
    assert(i > 0);
    printf("Client: %d\n", i);
    fflush(stdout);
    *ptr = -i;
    sem_up(parent_semid);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    -0x4(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1090 <shmat@plt>
mov    %rax,-0x18(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x18(%rbp)
jne    11ca <child+0x4a>
lea    0xe45(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <sem_down@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
setg   %al
and    $0x1,%al
mov    %al,-0x2d(%rbp)
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x1c(%rbp),%esi
lea    0xe0c(%rip),%rdi
call   1050 <printf@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
xor    %ecx,%ecx
sub    -0x1c(%rbp),%ecx
mov    %ecx,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%edi
call   1040 <sem_up@plt>
jmp    11d2 <child+0x52>

x86 assembly (clang, -O2):
<child>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %esi,%r12d
xor    %esi,%esi
xor    %edx,%edx
call   1090 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     11f8 <child+0x78>
mov    %rax,%rbx
lea    0xe63(%rip),%r15
mov    0x2e2f(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14d,%edi
call   1030 <sem_down@plt>
mov    (%rbx),%ebp
xor    %edi,%edi
test   %ebp,%ebp
setg   %dil
call   1060 <assert@plt>
mov    %r15,%rdi
mov    %ebp,%esi
call   1050 <printf@plt>
mov    0x0(%r13),%edi
call   1070 <fflush@plt>
neg    %ebp
mov    %ebp,(%rbx)
mov    %r12d,%edi
call   1040 <sem_up@plt>
jmp    11c0 <child+0x40>
lea    0xe01(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <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, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32*
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%10 = load i32, i32* %4
%11 = call i8* @shmat(i32 %10, i32* null, i32 0)
store i8* %11, i8** %7
%12 = load i8*, i8** %7
%13 = icmp eq i8* %12, inttoptr (i64 -1 to i8*)
br i1 %13, label %14, label %17
14: ; preds = %3
%15 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %3
%18 = load i8*, i8** %7
%19 = bitcast i8* %18 to i32*
store i32* %19, i32** %9
br label %20
20: ; preds = %17, %20
%21 = load i32, i32* %6
%22 = call i32 @sem_down(i32 %21)
%23 = load i32*, i32** %9
%24 = load i32, i32* %23
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = icmp sgt i32 %25, 0
%27 = zext i1 %26 to i32
%28 = call i32 @assert(i32 %27)
%29 = load i32, i32* %8
%30 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %29)
%31 = load i32, i32* @stdout
%32 = call i32 @fflush(i32 %31)
%33 = load i32, i32* %8
%34 = sub nsw i32 0, %33
%35 = load i32*, i32** %9
store i32 %34, i32* %35
%36 = load i32, i32* %5
%37 = call i32 @sem_up(i32 %36)
br label %20
}

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, i32 %1, i32 %2) {
%4 = tail call i8* @shmat(i32 %0, i32* null, i32 0)
%5 = icmp eq i8* %4, inttoptr (i64 -1 to i8*)
br i1 %5, label %6, label %9
6: ; preds = %3
%7 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %3
%10 = bitcast i8* %4 to i32*
br label %11
11: ; preds = %11, %9
%12 = tail call i32 @sem_down(i32 %2)
%13 = load i32, i32* %10
%14 = icmp sgt i32 %13, 0
%15 = zext i1 %14 to i32
%16 = tail call i32 @assert(i32 %15)
%17 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%18 = load i32, i32* @stdout
%19 = tail call i32 @fflush(i32 %18)
%20 = sub nsw i32 0, %13
store i32 %20, i32* %10
%21 = tail call i32 @sem_up(i32 %1)
br label %11
}