#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 *);
void manage_parent(int *buffer, int *begin, int *end) {
  pid_t child;
  int status;
  printf("Parent process (PID %d)\n", getpid());
  write_memory(buffer, begin, end);
  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    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe7c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1050 <write_memory@plt>
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x20(%rbp),%rdi
call   1080 <wait@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%edi
call   1070 <WIFEXITED@plt>
cmp    $0x0,%rax
je     11e9 <manage_parent+0x89>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
mov    -0x24(%rbp),%esi
mov    %eax,%edx
lea    0xe53(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<manage_parent>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe70(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1060 <write_memory@plt>
lea    0xe94(%rip),%rdi
call   1030 <puts@plt>
lea    0xc(%rsp),%rdi
call   1090 <wait@plt>
mov    %eax,%ebx
mov    0xc(%rsp),%edi
call   1080 <WIFEXITED@plt>
test   %rax,%rax
je     11e8 <manage_parent+0x78>
mov    0xc(%rsp),%edi
call   1070 <WEXITSTATUS@plt>
lea    0xe3c(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
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) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
store i32* %2, i32** %6
%9 = call i32 (...) @getpid()
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = load i32*, i32** %4
%12 = load i32*, i32** %5
%13 = load i32*, i32** %6
%14 = call i32 @write_memory(i32* %11, i32* %12, i32* %13)
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @wait(i32* %8)
store i32 %16, i32* %7
%17 = load i32, i32* %8
%18 = call i64 @WIFEXITED(i32 %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %3
%21 = load i32, i32* %7
%22 = load i32, i32* %8
%23 = call i32 @WEXITSTATUS(i32 %22)
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %21, i32 %23)
br label %25
25: ; preds = %20, %3
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) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = tail call i32 (...) @getpid()
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = tail call i32 @write_memory(i32* %0, i32* %1, i32* %2)
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%10 = call i32 @wait(i32* %4)
%11 = load i32, i32* %4
%12 = call i64 @WIFEXITED(i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %3
%15 = load i32, i32* %4
%16 = call i32 @WEXITSTATUS(i32 %15)
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0), i32 %10, i32 %16)
br label %18
18: ; preds = %14, %3
ret void
}