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


x86 assembly (clang, -O0):
<parent>:
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    11da <parent+0x4a>
lea    0xe35(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
movl   $0x1,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x1,-0x1c(%rbp)
cmpl   $0x7d00,-0x1c(%rbp)
jge    1265 <parent+0xd5>
mov    -0x1c(%rbp),%esi
lea    0xe07(%rip),%rdi
call   1050 <printf@plt>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    -0x1c(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0xc(%rbp),%edi
call   1040 <sem_up@plt>
mov    -0x8(%rbp),%edi
call   1030 <sem_down@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
xor    %ecx,%ecx
sub    -0x1c(%rbp),%ecx
cmp    %ecx,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11f0 <parent+0x60>
xor    %edi,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<parent>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %esi,%r15d
xor    %esi,%esi
xor    %edx,%edx
call   1090 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
jne    11c6 <parent+0x36>
lea    0xe49(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    %rax,%r13
mov    $0x1,%ebp
mov    $0xffffffff,%ebx
mov    0x2e06(%rip),%r12
nopw   0x0(%rax,%rax,1)
lea    0xe27(%rip),%rdi
mov    %ebp,%esi
call   1050 <printf@plt>
mov    (%r12),%edi
call   1070 <fflush@plt>
mov    %ebp,0x0(%r13)
mov    %r14d,%edi
call   1040 <sem_up@plt>
mov    %r15d,%edi
call   1030 <sem_down@plt>
xor    %edi,%edi
cmp    0x0(%r13),%ebx
sete   %dil
call   1060 <assert@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
add    $0x1,%ebp
add    $0xffffffff,%ebx
cmp    $0xffff8300,%ebx
jne    11e0 <parent+0x50>
xor    %edi,%edi
call   10a0 <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, 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 ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %3
store i32 1, i32* %8
%18 = load i8*, i8** %7
%19 = bitcast i8* %18 to i32*
store i32* %19, i32** %9
store i32 1, i32* %8
br label %20
20: ; preds = %42, %17
%21 = load i32, i32* %8
%22 = icmp slt i32 %21, 32000
br i1 %22, label %23, label %45
23: ; preds = %20
%24 = load i32, i32* %8
%25 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %24)
%26 = load i32, i32* @stdout
%27 = call i32 @fflush(i32 %26)
%28 = load i32, i32* %8
%29 = load i32*, i32** %9
store i32 %28, i32* %29
%30 = load i32, i32* %6
%31 = call i32 @sem_up(i32 %30)
%32 = load i32, i32* %5
%33 = call i32 @sem_down(i32 %32)
%34 = load i32*, i32** %9
%35 = load i32, i32* %34
%36 = load i32, i32* %8
%37 = sub nsw i32 0, %36
%38 = icmp eq i32 %35, %37
%39 = zext i1 %38 to i32
%40 = call i32 @assert(i32 %39)
%41 = call i32 @sleep(i32 1)
br label %42
42: ; preds = %23
%43 = load i32, i32* %8
%44 = add nsw i32 %43, 1
store i32 %44, i32* %8
br label %20
45: ; preds = %20
%46 = 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, 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 ([14 x i8], [14 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 = %9, %11
%12 = phi i32 [ 1, %9 ], [ %24, %11 ]
%13 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%14 = load i32, i32* @stdout
%15 = tail call i32 @fflush(i32 %14)
store i32 %12, i32* %10
%16 = tail call i32 @sem_up(i32 %2)
%17 = tail call i32 @sem_down(i32 %1)
%18 = load i32, i32* %10
%19 = sub nsw i32 0, %12
%20 = icmp eq i32 %18, %19
%21 = zext i1 %20 to i32
%22 = tail call i32 @assert(i32 %21)
%23 = tail call i32 @sleep(i32 1)
%24 = add nuw nsw i32 %12, 1
%25 = icmp eq i32 %24, 32000
br i1 %25, label %26, label %11
26: ; preds = %11
%27 = tail call i32 @exit(i32 0)
unreachable
}