#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*/ __execute_filter_hook(int, int, int);
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, ...);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ setpgid(scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int fork_normal(const char *pProgram, int uUp, int dDown, pid_t *pProcess1,
                pid_t *pProcess2, int cClientIn, int cClientOut, int fFarIn,
                int fFarOut) {
  if (!pProcess1 || !pProcess2) {
    fprintf(stderr, "%s: internal error\n", pProgram);
    return -1;
  }
  if (uUp) {
    if ((*pProcess1 = fork()) < 0) {
      fprintf(stderr, "%s: could not create downstream fork: %s\n", pProgram,
              strerror(errno));
      return -1;
    }
    if (*pProcess1 == 0) {
      close(fFarIn);
      close(cClientOut);
      close((3 + 32));
      close((4 + 32));
      _exit(__execute_filter_hook(cClientIn, fFarOut, 2));
    } else
      setpgid(*pProcess1, getpid());
  }
  if (dDown) {
    if ((*pProcess2 = fork()) < 0) {
      fprintf(stderr, "%s: could not create downstream fork: %s\n", pProgram,
              strerror(errno));
      return -1;
    }
    if (*pProcess2 == 0) {
      close(fFarOut);
      close(cClientIn);
      close((3 + 32));
      close((4 + 32));
      _exit(__execute_filter_hook(fFarIn, cClientOut, 1));
    } else
      setpgid(*pProcess2, getpid());
  }
  return 0;
}


x86 assembly (clang, -O0):
<fork_normal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    0x20(%rbp),%eax
mov    0x18(%rbp),%eax
mov    0x10(%rbp),%eax
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9d,-0x2c(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11bd <fork_normal+0x3d>
cmpq   $0x0,-0x28(%rbp)
jne    11e4 <fork_normal+0x64>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe2f(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1391 <fork_normal+0x211>
cmpl   $0x0,-0x14(%rbp)
je     12b7 <fork_normal+0x137>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    -0x20(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jge    124c <fork_normal+0xcc>
mov    0x2dd3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xddb(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1391 <fork_normal+0x211>
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1295 <fork_normal+0x115>
mov    0x18(%rbp),%edi
call   1050 <close@plt>
mov    0x10(%rbp),%edi
call   1050 <close@plt>
mov    $0x23,%edi
call   1050 <close@plt>
mov    $0x24,%edi
call   1050 <close@plt>
mov    -0x2c(%rbp),%edi
mov    0x20(%rbp),%esi
mov    $0x2,%edx
call   1070 <__execute_filter_hook@plt>
mov    %eax,%edi
call   1030 <_exit@plt>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,%esi
call   1080 <setpgid@plt>
jmp    12b7 <fork_normal+0x137>
cmpl   $0x0,-0x18(%rbp)
je     138a <fork_normal+0x20a>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    -0x28(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0x0,%rax
jge    131f <fork_normal+0x19f>
mov    0x2d00(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2ce4(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rdx
mov    %rax,%rcx
lea    0xd08(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1391 <fork_normal+0x211>
mov    -0x28(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1368 <fork_normal+0x1e8>
mov    0x20(%rbp),%edi
call   1050 <close@plt>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
mov    $0x23,%edi
call   1050 <close@plt>
mov    $0x24,%edi
call   1050 <close@plt>
mov    0x18(%rbp),%edi
mov    0x10(%rbp),%esi
mov    $0x1,%edx
call   1070 <__execute_filter_hook@plt>
mov    %eax,%edi
call   1030 <_exit@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x60(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x60(%rbp),%rdi
mov    %eax,%esi
call   1080 <setpgid@plt>
jmp    138a <fork_normal+0x20a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fork_normal>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
test   %rcx,%rcx
je     1208 <fork_normal+0x88>
mov    %r8,%rbp
test   %r8,%r8
je     1208 <fork_normal+0x88>
mov    %r9d,%r13d
mov    %edx,%r14d
test   %esi,%esi
je     11d1 <fork_normal+0x51>
mov    %rcx,%r15
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %rax,(%r15)
test   %rax,%rax
js     1229 <fork_normal+0xa9>
je     1270 <fork_normal+0xf0>
mov    %rax,%r12
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r12,%rdi
mov    %eax,%esi
call   1080 <setpgid@plt>
test   %r14d,%r14d
je     125d <fork_normal+0xdd>
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %rax,0x0(%rbp)
test   %rax,%rax
js     1229 <fork_normal+0xa9>
je     12a4 <fork_normal+0x124>
mov    %rax,%r14
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r14,%rdi
mov    %eax,%esi
call   1080 <setpgid@plt>
jmp    125f <fork_normal+0xdf>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
lea    0xde8(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    125f <fork_normal+0xdf>
mov    0x2db0(%rip),%rax
mov    (%rax),%ebp
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xdcd(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebx
jmp    125f <fork_normal+0xdf>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x48(%rsp),%edi
call   1050 <close@plt>
mov    0x40(%rsp),%edi
call   1050 <close@plt>
mov    $0x23,%edi
call   1050 <close@plt>
mov    $0x24,%edi
call   1050 <close@plt>
mov    %r13d,%edi
mov    0x50(%rsp),%esi
mov    $0x2,%edx
jmp    12d6 <fork_normal+0x156>
mov    0x50(%rsp),%edi
call   1050 <close@plt>
mov    %r13d,%edi
call   1050 <close@plt>
mov    $0x23,%edi
call   1050 <close@plt>
mov    $0x24,%edi
call   1050 <close@plt>
mov    0x48(%rsp),%edi
mov    0x40(%rsp),%esi
mov    $0x1,%edx
call   1070 <__execute_filter_hook@plt>
mov    %eax,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [20 x i8] c"%s: internal error\0A\00"
@.str.1 = [42 x i8] c"%s: could not create downstream fork: %s\0A\00"
@errno = global i32 0
define i32 @fork_normal(i8* %0, i32 %1, i32 %2, i64* %3, i64* %4, i32 %5, i32 %6, i32 %7, i32 %8) {
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i64*
%15 = alloca i64*
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
store i8* %0, i8** %11
store i32 %1, i32* %12
store i32 %2, i32* %13
store i64* %3, i64** %14
store i64* %4, i64** %15
store i32 %5, i32* %16
store i32 %6, i32* %17
store i32 %7, i32* %18
store i32 %8, i32* %19
%20 = load i64*, i64** %14
%21 = icmp ne i64* %20, null
br i1 %21, label %22, label %25
22: ; preds = %9
%23 = load i64*, i64** %15
%24 = icmp ne i64* %23, null
br i1 %24, label %29, label %25
25: ; preds = %22, %9
%26 = load i32, i32* @stderr
%27 = load i8*, i8** %11
%28 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %27)
store i32 -1, i32* %10
br label %98
29: ; preds = %22
%30 = load i32, i32* %12
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %63
32: ; preds = %29
%33 = call i64 (...) @fork()
%34 = load i64*, i64** %14
store i64 %33, i64* %34
%35 = icmp slt i64 %33, 0
br i1 %35, label %36, label %42
36: ; preds = %32
%37 = load i32, i32* @stderr
%38 = load i8*, i8** %11
%39 = load i32, i32* @errno
%40 = call i8* @strerror(i32 %39)
%41 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i8* %38, i8* %40)
store i32 -1, i32* %10
br label %98
42: ; preds = %32
%43 = load i64*, i64** %14
%44 = load i64, i64* %43
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %57
46: ; preds = %42
%47 = load i32, i32* %18
%48 = call i32 @close(i32 %47)
%49 = load i32, i32* %17
%50 = call i32 @close(i32 %49)
%51 = call i32 @close(i32 35)
%52 = call i32 @close(i32 36)
%53 = load i32, i32* %16
%54 = load i32, i32* %19
%55 = call i32 @__execute_filter_hook(i32 %53, i32 %54, i32 2)
%56 = call i32 @_exit(i32 %55)
unreachable
57: ; preds = %42
%58 = load i64*, i64** %14
%59 = load i64, i64* %58
%60 = call i32 (...) @getpid()
%61 = call i32 @setpgid(i64 %59, i32 %60)
br label %62
62: ; preds = %57
br label %63
63: ; preds = %62, %29
%64 = load i32, i32* %13
%65 = icmp ne i32 %64, 0
br i1 %65, label %66, label %97
66: ; preds = %63
%67 = call i64 (...) @fork()
%68 = load i64*, i64** %15
store i64 %67, i64* %68
%69 = icmp slt i64 %67, 0
br i1 %69, label %70, label %76
70: ; preds = %66
%71 = load i32, i32* @stderr
%72 = load i8*, i8** %11
%73 = load i32, i32* @errno
%74 = call i8* @strerror(i32 %73)
%75 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %71, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i8* %72, i8* %74)
store i32 -1, i32* %10
br label %98
76: ; preds = %66
%77 = load i64*, i64** %15
%78 = load i64, i64* %77
%79 = icmp eq i64 %78, 0
br i1 %79, label %80, label %91
80: ; preds = %76
%81 = load i32, i32* %19
%82 = call i32 @close(i32 %81)
%83 = load i32, i32* %16
%84 = call i32 @close(i32 %83)
%85 = call i32 @close(i32 35)
%86 = call i32 @close(i32 36)
%87 = load i32, i32* %18
%88 = load i32, i32* %17
%89 = call i32 @__execute_filter_hook(i32 %87, i32 %88, i32 1)
%90 = call i32 @_exit(i32 %89)
unreachable
91: ; preds = %76
%92 = load i64*, i64** %15
%93 = load i64, i64* %92
%94 = call i32 (...) @getpid()
%95 = call i32 @setpgid(i64 %93, i32 %94)
br label %96
96: ; preds = %91
br label %97
97: ; preds = %96, %63
store i32 0, i32* %10
br label %98
98: ; preds = %97, %70, %36, %25
%99 = load i32, i32* %10
ret i32 %99
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [20 x i8] c"%s: internal error\0A\00"
@.str.1 = [42 x i8] c"%s: could not create downstream fork: %s\0A\00"
@errno = global i32 0
define i32 @fork_normal(i8* %0, i32 %1, i32 %2, i64* writeonly %3, i64* writeonly %4, i32 %5, i32 %6, i32 %7, i32 %8) {
%10 = icmp ne i64* %3, null
%11 = icmp ne i64* %4, null
%12 = and i1 %10, %11
br i1 %12, label %16, label %13
13: ; preds = %9
%14 = load i32, i32* @stderr
%15 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %60
16: ; preds = %9
%17 = icmp eq i32 %1, 0
br i1 %17, label %38, label %18
18: ; preds = %16
%19 = tail call i64 (...) @fork()
store i64 %19, i64* %3
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %18
%22 = load i32, i32* @stderr
%23 = load i32, i32* @errno
%24 = tail call i8* @strerror(i32 %23)
%25 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %24)
br label %60
26: ; preds = %18
%27 = icmp eq i64 %19, 0
br i1 %27, label %28, label %35
28: ; preds = %26
%29 = tail call i32 @close(i32 %7)
%30 = tail call i32 @close(i32 %6)
%31 = tail call i32 @close(i32 35)
%32 = tail call i32 @close(i32 36)
%33 = tail call i32 @__execute_filter_hook(i32 %5, i32 %8, i32 2)
%34 = tail call i32 @_exit(i32 %33)
unreachable
35: ; preds = %26
%36 = tail call i32 (...) @getpid()
%37 = tail call i32 @setpgid(i64 %19, i32 %36)
br label %38
38: ; preds = %35, %16
%39 = icmp eq i32 %2, 0
br i1 %39, label %60, label %40
40: ; preds = %38
%41 = tail call i64 (...) @fork()
store i64 %41, i64* %4
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %48
43: ; preds = %40
%44 = load i32, i32* @stderr
%45 = load i32, i32* @errno
%46 = tail call i8* @strerror(i32 %45)
%47 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %46)
br label %60
48: ; preds = %40
%49 = icmp eq i64 %41, 0
br i1 %49, label %50, label %57
50: ; preds = %48
%51 = tail call i32 @close(i32 %8)
%52 = tail call i32 @close(i32 %5)
%53 = tail call i32 @close(i32 35)
%54 = tail call i32 @close(i32 36)
%55 = tail call i32 @__execute_filter_hook(i32 %7, i32 %6, i32 1)
%56 = tail call i32 @_exit(i32 %55)
unreachable
57: ; preds = %48
%58 = tail call i32 (...) @getpid()
%59 = tail call i32 @setpgid(i64 %41, i32 %58)
br label %60
60: ; preds = %38, %57, %43, %21, %13
%61 = phi i32 [ -1, %21 ], [ -1, %43 ], [ -1, %13 ], [ 0, %57 ], [ 0, %38 ]
ret i32 %61
}