#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 */
scalar_t__ fork();
int /*<<< orphan*/ waitpid(scalar_t__, int *, int /*<<< orphan*/);
int exec_server_client2(int argc, char *argv[],
                        int (*pf_server)(int argc, char *argv[]),
                        int (*pf_client1)(int argc, char *argv[]),
                        int (*pf_client2)(int argc, char *argv[])) {
  pid_t pid_client1 = fork();
  if (pid_client1 == 0) {
    int s = pf_server(argc, argv);
    int wstatus;
    waitpid(pid_client1, &wstatus, 0);
    return s;
  } else {
    pid_t pid_client2 = fork();
    if (pid_client2 == 0) {
      int s = pf_client1(argc, argv);
      return s;
    } else {
      int s = pf_client2(argc, argv);
      int wstatus;
      waitpid(pid_client2, &wstatus, 0);
      return s;
    }
  }
}


x86 assembly (clang, -O0):
<exec_server_client2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    $0x0,%al
call   1040 <fork@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    117b <exec_server_client2+0x5b>
mov    -0x18(%rbp),%rax
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   *%rax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
lea    -0x38(%rbp),%rsi
xor    %edx,%edx
call   1030 <waitpid@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d1 <exec_server_client2+0xb1>
mov    $0x0,%al
call   1040 <fork@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    11ac <exec_server_client2+0x8c>
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   *%rax
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d1 <exec_server_client2+0xb1>
mov    -0x28(%rbp),%rax
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   *%rax
mov    %eax,-0x48(%rbp)
mov    -0x40(%rbp),%rdi
lea    -0x4c(%rbp),%rsi
xor    %edx,%edx
call   1030 <waitpid@plt>
mov    -0x48(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<exec_server_client2>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r14
mov    %rcx,%r13
mov    %rdx,%r15
mov    %rsi,%r12
mov    %edi,%ebp
xor    %eax,%eax
call   1040 <fork@plt>
test   %rax,%rax
je     116f <exec_server_client2+0x4f>
xor    %eax,%eax
call   1040 <fork@plt>
test   %rax,%rax
je     1198 <exec_server_client2+0x78>
mov    %rax,%rbx
mov    %ebp,%edi
mov    %r12,%rsi
call   *%r14
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %rbx,%rdi
xor    %edx,%edx
call   1030 <waitpid@plt>
mov    %ebp,%eax
jmp    1189 <exec_server_client2+0x69>
mov    %ebp,%edi
mov    %r12,%rsi
call   *%r15
mov    %eax,%ebx
lea    0x4(%rsp),%rsi
xor    %edi,%edi
xor    %edx,%edx
call   1030 <waitpid@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,%edi
mov    %r12,%rsi
mov    %r13,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    *%rax

LLVM IR (-O0):
define i32 @exec_server_client2(i32 %0, i8** %1, i32 (i32, i8**)* %2, i32 (i32, i8**)* %3, i32 (i32, i8**)* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8**
%9 = alloca i32 (i32, i8**)*
%10 = alloca i32 (i32, i8**)*
%11 = alloca i32 (i32, i8**)*
%12 = alloca i64
%13 = alloca i32
%14 = alloca i32
%15 = alloca i64
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
store i32 %0, i32* %7
store i8** %1, i8*** %8
store i32 (i32, i8**)* %2, i32 (i32, i8**)** %9
store i32 (i32, i8**)* %3, i32 (i32, i8**)** %10
store i32 (i32, i8**)* %4, i32 (i32, i8**)** %11
%19 = call i64 (...) @fork()
store i64 %19, i64* %12
%20 = load i64, i64* %12
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %30
22: ; preds = %5
%23 = load i32 (i32, i8**)*, i32 (i32, i8**)** %9
%24 = load i32, i32* %7
%25 = load i8**, i8*** %8
%26 = call i32 %23(i32 %24, i8** %25)
store i32 %26, i32* %13
%27 = load i64, i64* %12
%28 = call i32 @waitpid(i64 %27, i32* %14, i32 0)
%29 = load i32, i32* %13
store i32 %29, i32* %6
br label %48
30: ; preds = %5
%31 = call i64 (...) @fork()
store i64 %31, i64* %15
%32 = load i64, i64* %15
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %40
34: ; preds = %30
%35 = load i32 (i32, i8**)*, i32 (i32, i8**)** %10
%36 = load i32, i32* %7
%37 = load i8**, i8*** %8
%38 = call i32 %35(i32 %36, i8** %37)
store i32 %38, i32* %16
%39 = load i32, i32* %16
store i32 %39, i32* %6
br label %48
40: ; preds = %30
%41 = load i32 (i32, i8**)*, i32 (i32, i8**)** %11
%42 = load i32, i32* %7
%43 = load i8**, i8*** %8
%44 = call i32 %41(i32 %42, i8** %43)
store i32 %44, i32* %17
%45 = load i64, i64* %15
%46 = call i32 @waitpid(i64 %45, i32* %18, i32 0)
%47 = load i32, i32* %17
store i32 %47, i32* %6
br label %48
48: ; preds = %40, %34, %22
%49 = load i32, i32* %6
ret i32 %49
}

LLVM IR (-O2):
define i32 @exec_server_client2(i32 %0, i8** %1, i32 (i32, i8**)* nocapture %2, i32 (i32, i8**)* nocapture %3, i32 (i32, i8**)* nocapture %4) {
%6 = alloca i32
%7 = alloca i32
%8 = tail call i64 (...) @fork()
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %5
%11 = tail call i32 %2(i32 %0, i8** %1)
%12 = bitcast i32* %6 to i8*
%13 = call i32 @waitpid(i64 0, i32* %6, i32 0)
br label %23
14: ; preds = %5
%15 = tail call i64 (...) @fork()
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = tail call i32 %3(i32 %0, i8** %1)
br label %23
19: ; preds = %14
%20 = tail call i32 %4(i32 %0, i8** %1)
%21 = bitcast i32* %7 to i8*
%22 = call i32 @waitpid(i64 %15, i32* %7, i32 0)
br label %23
23: ; preds = %17, %19, %10
%24 = phi i32 [ %11, %10 ], [ %18, %17 ], [ %20, %19 ]
ret i32 %24
}