#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*/ SIGCHLD;
int /*<<< orphan*/ SIGTERM;
scalar_t__ SIG_ERR;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ err(int, char *);
scalar_t__ fork();
scalar_t__ getpid();
int /*<<< orphan*/ kill(scalar_t__, int /*<<< orphan*/);
scalar_t__ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ tcp_client(scalar_t__, int);
int /*<<< orphan*/ tcp_server(scalar_t__);
int main(int argc, char *argv[]) {
  pid_t child_pid, parent_pid;
  if (signal(SIGCHLD, SIG_IGN) == SIG_ERR)
    err(-1, "signal");
  parent_pid = getpid();
  child_pid = fork();
  if (child_pid < 0)
    err(-1, "fork");
  if (child_pid == 0) {
    child_pid = getpid();
    tcp_server(parent_pid);
    return (0);
  } else
    tcp_client(child_pid, 0);
  (void)kill(child_pid, SIGTERM);
  sleep(5);
  parent_pid = getpid();
  child_pid = fork();
  if (child_pid < 0)
    err(-1, "fork");
  if (child_pid == 0) {
    child_pid = getpid();
    tcp_server(parent_pid);
    return (0);
  } else
    tcp_client(child_pid, 1);
  (void)kill(child_pid, SIGTERM);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
call   1040 <signal@plt>
mov    0x2e14(%rip),%rcx
cmp    (%rcx),%rax
jne    11ce <main+0x4e>
mov    $0xffffffff,%edi
lea    0xe37(%rip),%rsi
call   1060 <err@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1200 <main+0x80>
mov    $0xffffffff,%edi
lea    0xe0c(%rip),%rsi
call   1060 <err@plt>
cmpq   $0x0,-0x18(%rbp)
jne    122b <main+0xab>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
call   1070 <tcp_server@plt>
movl   $0x0,-0x4(%rbp)
jmp    12d6 <main+0x156>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
call   1080 <tcp_client@plt>
mov    -0x18(%rbp),%rdi
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
call   1050 <kill@plt>
mov    $0x5,%edi
call   1090 <sleep@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1284 <main+0x104>
mov    $0xffffffff,%edi
lea    0xd88(%rip),%rsi
call   1060 <err@plt>
cmpq   $0x0,-0x18(%rbp)
jne    12af <main+0x12f>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
call   1070 <tcp_server@plt>
movl   $0x0,-0x4(%rbp)
jmp    12d6 <main+0x156>
mov    -0x18(%rbp),%rdi
mov    $0x1,%esi
call   1080 <tcp_client@plt>
mov    -0x18(%rbp),%rdi
mov    0x2d20(%rip),%rax
mov    (%rax),%esi
call   1050 <kill@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
call   1040 <signal@plt>
mov    0x2e25(%rip),%rcx
cmp    (%rcx),%rax
jne    11b9 <main+0x39>
lea    0xe51(%rip),%rsi
mov    $0xffffffff,%edi
call   1060 <err@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %rax,%r14
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %rax,%rbx
test   %rax,%rax
js     11d6 <main+0x56>
jne    11e7 <main+0x67>
jmp    1228 <main+0xa8>
lea    0xe2a(%rip),%rsi
mov    $0xffffffff,%edi
call   1060 <err@plt>
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <tcp_client@plt>
mov    0x2df0(%rip),%r15
mov    (%r15),%esi
mov    %rbx,%rdi
call   1050 <kill@plt>
mov    $0x5,%edi
call   1090 <sleep@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %rax,%r14
xor    %eax,%eax
call   10a0 <fork@plt>
mov    %rax,%rbx
test   %rax,%rax
js     1239 <main+0xb9>
jne    124a <main+0xca>
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %r14,%rdi
call   1070 <tcp_server@plt>
jmp    1262 <main+0xe2>
lea    0xdc7(%rip),%rsi
mov    $0xffffffff,%edi
call   1060 <err@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
call   1080 <tcp_client@plt>
mov    (%r15),%esi
mov    %rbx,%rdi
call   1050 <kill@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@SIG_ERR = global i64 0
@.str = [7 x i8] c"signal\00"
@.str.1 = [5 x i8] c"fork\00"
@SIGTERM = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* @SIGCHLD
%9 = load i32, i32* @SIG_IGN
%10 = call i64 @signal(i32 %8, i32 %9)
%11 = load i64, i64* @SIG_ERR
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @err(i32 -1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %2
%16 = call i64 (...) @getpid()
store i64 %16, i64* %7
%17 = call i64 (...) @fork()
store i64 %17, i64* %6
%18 = load i64, i64* %6
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 @err(i32 -1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %15
%23 = load i64, i64* %6
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = call i64 (...) @getpid()
store i64 %26, i64* %6
%27 = load i64, i64* %7
%28 = call i32 @tcp_server(i64 %27)
store i32 0, i32* %3
br label %57
29: ; preds = %22
%30 = load i64, i64* %6
%31 = call i32 @tcp_client(i64 %30, i32 0)
br label %32
32: ; preds = %29
%33 = load i64, i64* %6
%34 = load i32, i32* @SIGTERM
%35 = call i32 @kill(i64 %33, i32 %34)
%36 = call i32 @sleep(i32 5)
%37 = call i64 (...) @getpid()
store i64 %37, i64* %7
%38 = call i64 (...) @fork()
store i64 %38, i64* %6
%39 = load i64, i64* %6
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %32
%42 = call i32 @err(i32 -1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %43
43: ; preds = %41, %32
%44 = load i64, i64* %6
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %43
%47 = call i64 (...) @getpid()
store i64 %47, i64* %6
%48 = load i64, i64* %7
%49 = call i32 @tcp_server(i64 %48)
store i32 0, i32* %3
br label %57
50: ; preds = %43
%51 = load i64, i64* %6
%52 = call i32 @tcp_client(i64 %51, i32 1)
br label %53
53: ; preds = %50
%54 = load i64, i64* %6
%55 = load i32, i32* @SIGTERM
%56 = call i32 @kill(i64 %54, i32 %55)
store i32 0, i32* %3
br label %57
57: ; preds = %53, %46, %25
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@SIG_ERR = global i64 0
@.str = [7 x i8] c"signal\00"
@.str.1 = [5 x i8] c"fork\00"
@SIGTERM = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @SIGCHLD
%4 = load i32, i32* @SIG_IGN
%5 = tail call i64 @signal(i32 %3, i32 %4)
%6 = load i64, i64* @SIG_ERR
%7 = icmp eq i64 %5, %6
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @err(i32 -1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %2
%11 = tail call i64 (...) @getpid()
%12 = tail call i64 (...) @fork()
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = tail call i32 @err(i32 -1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %21
16: ; preds = %10
%17 = icmp eq i64 %12, 0
br i1 %17, label %18, label %21
18: ; preds = %16
%19 = tail call i64 (...) @getpid()
%20 = tail call i32 @tcp_server(i64 %11)
br label %40
21: ; preds = %14, %16
%22 = tail call i32 @tcp_client(i64 %12, i32 0)
%23 = load i32, i32* @SIGTERM
%24 = tail call i32 @kill(i64 %12, i32 %23)
%25 = tail call i32 @sleep(i32 5)
%26 = tail call i64 (...) @getpid()
%27 = tail call i64 (...) @fork()
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %21
%30 = tail call i32 @err(i32 -1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %36
31: ; preds = %21
%32 = icmp eq i64 %27, 0
br i1 %32, label %33, label %36
33: ; preds = %31
%34 = tail call i64 (...) @getpid()
%35 = tail call i32 @tcp_server(i64 %26)
br label %40
36: ; preds = %29, %31
%37 = tail call i32 @tcp_client(i64 %27, i32 1)
%38 = load i32, i32* @SIGTERM
%39 = tail call i32 @kill(i64 %27, i32 %38)
br label %40
40: ; preds = %36, %33, %18
ret i32 0
}