#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 pid_t;

/* Variables and functions */
int WEXITSTATUS(int);
scalar_t__ WIFEXITED(int);
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int wait(int *);
int /*<<< orphan*/ write_memory(int *, int *, int *, int);
void manage_parent(int *buffer, int *in, int *out, int semid) {
  pid_t child;
  int status;
  printf("Parent process (PID %d)\n", getpid());
  write_memory(buffer, in, out, semid);
  printf("Parent: end of production.\n");
  child = wait(&status);
  if (WIFEXITED(status)) {
    printf("Parent: child %d has finished (code %d)\n", child,
           WEXITSTATUS(status));
  }
}


x86 assembly (clang, -O0):
<manage_parent>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe79(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1050 <write_memory@plt>
lea    0xe70(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x24(%rbp),%rdi
call   1080 <wait@plt>
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
call   1070 <WIFEXITED@plt>
cmp    $0x0,%rax
je     11ef <manage_parent+0x8f>
mov    -0x20(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
mov    -0x28(%rbp),%esi
mov    %eax,%edx
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<manage_parent>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %rsi,%rbp
mov    %rdi,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe6f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r15,%rdx
mov    %r14d,%ecx
call   1060 <write_memory@plt>
lea    0xe90(%rip),%rdi
call   1030 <puts@plt>
lea    0x4(%rsp),%rdi
call   1090 <wait@plt>
mov    %eax,%ebx
mov    0x4(%rsp),%edi
call   1080 <WIFEXITED@plt>
test   %rax,%rax
je     11ec <manage_parent+0x7c>
mov    0x4(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
lea    0xe38(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [25 x i8] c"Parent process (PID %d)\0A\00"
@.str.1 = [28 x i8] c"Parent: end of production.\0A\00"
@.str.2 = [41 x i8] c"Parent: child %d has finished (code %d)\0A\00"
define void @manage_parent(i32* %0, i32* %1, i32* %2, i32 %3) {
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32* %0, i32** %5
store i32* %1, i32** %6
store i32* %2, i32** %7
store i32 %3, i32* %8
%11 = call i32 (...) @getpid()
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = load i32*, i32** %5
%14 = load i32*, i32** %6
%15 = load i32*, i32** %7
%16 = load i32, i32* %8
%17 = call i32 @write_memory(i32* %13, i32* %14, i32* %15, i32 %16)
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @wait(i32* %10)
store i32 %19, i32* %9
%20 = load i32, i32* %10
%21 = call i64 @WIFEXITED(i32 %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %4
%24 = load i32, i32* %9
%25 = load i32, i32* %10
%26 = call i32 @WEXITSTATUS(i32 %25)
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %24, i32 %26)
br label %28
28: ; preds = %23, %4
ret void
}

LLVM IR (-O2):
@.str = [25 x i8] c"Parent process (PID %d)\0A\00"
@.str.2 = [41 x i8] c"Parent: child %d has finished (code %d)\0A\00"
@str = [27 x i8] c"Parent: end of production.\00"
define void @manage_parent(i32* %0, i32* %1, i32* %2, i32 %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
%7 = tail call i32 (...) @getpid()
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = tail call i32 @write_memory(i32* %0, i32* %1, i32* %2, i32 %3)
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%11 = call i32 @wait(i32* %5)
%12 = load i32, i32* %5
%13 = call i64 @WIFEXITED(i32 %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %4
%16 = load i32, i32* %5
%17 = call i32 @WEXITSTATUS(i32 %16)
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %11, i32 %17)
br label %19
19: ; preds = %15, %4
ret void
}