#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, ...);
int wait(int *);
int /*<<< orphan*/ write_memory(void *);
void manage_parent(void *shared_memory) {
  pid_t child;
  int status;
  printf("Parent process (PID %d)\n", getpid());
  write_memory(shared_memory);
  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    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xe84(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1050 <write_memory@plt>
lea    -0x10(%rbp),%rdi
call   1080 <wait@plt>
mov    %eax,-0xc(%rbp)
mov    -0x10(%rbp),%edi
call   1070 <WIFEXITED@plt>
cmp    $0x0,%rax
je     11cb <manage_parent+0x6b>
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%edi
call   1060 <WEXITSTATUS@plt>
mov    -0x14(%rbp),%esi
mov    %eax,%edx
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<manage_parent>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe8a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1050 <write_memory@plt>
lea    0xc(%rsp),%rdi
call   1080 <wait@plt>
mov    %eax,%ebx
mov    0xc(%rsp),%edi
call   1070 <WIFEXITED@plt>
test   %rax,%rax
je     11bc <manage_parent+0x5c>
mov    0xc(%rsp),%edi
call   1060 <WEXITSTATUS@plt>
lea    0xe68(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x10,%rsp
pop    %rbx
ret

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

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