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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ client(int, int, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int getpid();
int /*<<< orphan*/ server(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
void client_server(int id, int signal, int number_of_communications) {
  pid_t pid = fork();
  if (pid == 0) {
    fprintf(stdout, "CLIENT: I am the child.  My PID is %i.\n", getpid());
    client(id, signal, number_of_communications);
  } else if (pid > 0) {
    fprintf(stdout, "SERVER: I am the parent. My PID is %i.\n", getpid());
    server(number_of_communications);
  } else {
    fprintf(stderr, "Error: I couldn't fork!\n");
    exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<client_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c0 <client_server+0x60>
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x1c(%rbp),%edi
mov    %eax,%edx
lea    0xe5a(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
call   1060 <client@plt>
jmp    1228 <client_server+0xc8>
cmpq   $0x0,-0x18(%rbp)
jle    11fe <client_server+0x9e>
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x20(%rbp),%edi
mov    %eax,%edx
lea    0xe3e(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0xc(%rbp),%edi
call   1050 <server@plt>
jmp    1223 <client_server+0xc3>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
lea    0xe42(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2dbc(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
jmp    1228 <client_server+0xc8>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<client_server>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %esi,%r14d
mov    %edi,%ebp
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
je     11af <client_server+0x4f>
jle    11e9 <client_server+0x89>
mov    0x2e4d(%rip),%rax
mov    (%rax),%ebp
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe95(%rip),%rsi
mov    %ebp,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %ebx,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <server@plt>
mov    0x2e1a(%rip),%rax
mov    (%rax),%r15d
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe39(%rip),%rsi
mov    %r15d,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %ebp,%edi
mov    %r14d,%esi
mov    %ebx,%edx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <client@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
lea    0xe57(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stdout = global i32 0
@.str = [40 x i8] c"CLIENT: I am the child. My PID is %i.\0A\00"
@.str.1 = [40 x i8] c"SERVER: I am the parent. My PID is %i.\0A\00"
@stderr = global i32 0
@.str.2 = [25 x i8] c"Error: I couldn't fork!\0A\00"
@EXIT_FAILURE = global i32 0
define void @client_server(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i64
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%8 = call i64 (...) @fork()
store i64 %8, i64* %7
%9 = load i64, i64* %7
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %19
11: ; preds = %3
%12 = load i32, i32* @stdout
%13 = call i32 (...) @getpid()
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = load i32, i32* %4
%16 = load i32, i32* %5
%17 = load i32, i32* %6
%18 = call i32 @client(i32 %15, i32 %16, i32 %17)
br label %34
19: ; preds = %3
%20 = load i64, i64* %7
%21 = icmp sgt i64 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %19
%23 = load i32, i32* @stdout
%24 = call i32 (...) @getpid()
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %24)
%26 = load i32, i32* %6
%27 = call i32 @server(i32 %26)
br label %33
28: ; preds = %19
%29 = load i32, i32* @stderr
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %22
br label %34
34: ; preds = %33, %11
ret void
}

LLVM IR (-O2):
@stdout = global i32 0
@.str = [40 x i8] c"CLIENT: I am the child. My PID is %i.\0A\00"
@.str.1 = [40 x i8] c"SERVER: I am the parent. My PID is %i.\0A\00"
@stderr = global i32 0
@.str.2 = [25 x i8] c"Error: I couldn't fork!\0A\00"
@EXIT_FAILURE = global i32 0
define void @client_server(i32 %0, i32 %1, i32 %2) {
%4 = tail call i64 (...) @fork()
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %11
6: ; preds = %3
%7 = load i32, i32* @stdout
%8 = tail call i32 (...) @getpid()
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = tail call i32 @client(i32 %0, i32 %1, i32 %2)
br label %23
11: ; preds = %3
%12 = icmp sgt i64 %4, 0
br i1 %12, label %13, label %18
13: ; preds = %11
%14 = load i32, i32* @stdout
%15 = tail call i32 (...) @getpid()
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = tail call i32 @server(i32 %2)
br label %23
18: ; preds = %11
%19 = load i32, i32* @stderr
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%21 = load i32, i32* @EXIT_FAILURE
%22 = tail call i32 @exit(i32 %21)
unreachable
23: ; preds = %13, %6
ret void
}