#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 accept_client(int);
int connect_server(char *);
scalar_t__ fork();
int getpid();
int init_listen_server(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, ...);
int read(int, char *, int);
scalar_t__ strlen(char *);
int /*<<< orphan*/ write(int, char *, scalar_t__);
int main() {
  pid_t pid;
  pid = fork();
  if (pid < 0) {
    perror("fork()");
  } else if (pid > 0) {
    int lsn_fd, apt_fd;
    char buf[128];
    int ret;
    printf("parent pid %d\n", getpid());
    lsn_fd = init_listen_server("/tmp/test_request.domain");
    apt_fd = accept_client(lsn_fd);
    ret = read(apt_fd, buf, 124);
    if (ret > 0) {
      buf[ret] = '\0';
      printf("%d, %s\n", getpid(), buf);
    }
  } else {
    int fd;
    printf("child pid %d\n", getpid());
    fd = connect_server("/tmp/test_request.domain");
    if (fd > 0) {
      write(fd, "I am child", strlen("I am child.") + 1);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11d9 <main+0x39>
lea    0xe31(%rip),%rdi
call   10b0 <perror@plt>
jmp    12df <main+0x13f>
cmpq   $0x0,-0x10(%rbp)
jle    126e <main+0xce>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
lea    0xe14(%rip),%rdi
call   10a0 <init_listen_server@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <accept_client@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    $0x7c,%edx
call   1090 <read@plt>
mov    %eax,-0xa4(%rbp)
cmpl   $0x0,-0xa4(%rbp)
jle    1269 <main+0xc9>
movslq -0xa4(%rbp),%rax
movb   $0x0,-0xa0(%rbp,%rax,1)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    -0xa0(%rbp),%rdx
lea    0xdcd(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
jmp    12da <main+0x13a>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xdb9(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
lea    0xd8a(%rip),%rdi
call   1060 <connect_server@plt>
mov    %eax,-0xa8(%rbp)
cmpl   $0x0,-0xa8(%rbp)
jle    12d5 <main+0x135>
mov    -0xa8(%rbp),%eax
mov    %eax,-0xac(%rbp)
lea    0xd99(%rip),%rdi
call   1070 <strlen@plt>
mov    -0xac(%rbp),%edi
mov    %rax,%rdx
add    $0x1,%rdx
lea    0xd75(%rip),%rsi
call   1030 <write@plt>
jmp    12da <main+0x13a>
jmp    12df <main+0x13f>
xor    %eax,%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
sub    $0x88,%rsp
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
js     1244 <main+0xb4>
je     1208 <main+0x78>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe50(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xe4f(%rip),%rdi
call   1090 <init_listen_server@plt>
mov    %eax,%edi
call   1050 <accept_client@plt>
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x7c,%edx
call   1080 <read@plt>
test   %eax,%eax
jle    1250 <main+0xc0>
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe35(%rip),%rdi
mov    %rsp,%rdx
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    1250 <main+0xc0>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe21(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xdf0(%rip),%rdi
call   1060 <connect_server@plt>
test   %eax,%eax
jle    1250 <main+0xc0>
lea    0xe0f(%rip),%rsi
mov    $0xc,%edx
mov    %eax,%edi
call   1030 <write@plt>
jmp    1250 <main+0xc0>
lea    0xdb5(%rip),%rdi
call   10a0 <perror@plt>
xor    %eax,%eax
add    $0x88,%rsp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"fork()\00"
@.str.1 = [15 x i8] c"parent pid %d\0A\00"
@.str.2 = [25 x i8] c"/tmp/test_request.domain\00"
@.str.3 = [8 x i8] c"%d, %s\0A\00"
@.str.4 = [14 x i8] c"child pid %d\0A\00"
@.str.5 = [11 x i8] c"I am child\00"
@.str.6 = [12 x i8] c"I am child.\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
%5 = alloca [128 x i8]
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
%8 = call i64 (...) @fork()
store i64 %8, i64* %2
%9 = load i64, i64* %2
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %48
13: ; preds = %0
%14 = load i64, i64* %2
%15 = icmp sgt i64 %14, 0
br i1 %15, label %16, label %35
16: ; preds = %13
%17 = call i32 (...) @getpid()
%18 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = call i32 @init_listen_server(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
store i32 %19, i32* %3
%20 = load i32, i32* %3
%21 = call i32 @accept_client(i32 %20)
store i32 %21, i32* %4
%22 = load i32, i32* %4
%23 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%24 = call i32 @read(i32 %22, i8* %23, i32 124)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %34
27: ; preds = %16
%28 = load i32, i32* %6
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 %29
store i8 0, i8* %30
%31 = call i32 (...) @getpid()
%32 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%33 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %31, i8* %32)
br label %34
34: ; preds = %27, %16
br label %47
35: ; preds = %13
%36 = call i32 (...) @getpid()
%37 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %36)
%38 = call i32 @connect_server(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
store i32 %38, i32* %7
%39 = load i32, i32* %7
%40 = icmp sgt i32 %39, 0
br i1 %40, label %41, label %46
41: ; preds = %35
%42 = load i32, i32* %7
%43 = call i64 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0))
%44 = add nsw i64 %43, 1
%45 = call i32 @write(i32 %42, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), i64 %44)
br label %46
46: ; preds = %41, %35
br label %47
47: ; preds = %46, %34
br label %48
48: ; preds = %47, %11
ret i32 0
}

LLVM IR (-O2):
@.str = [7 x i8] c"fork()\00"
@.str.1 = [15 x i8] c"parent pid %d\0A\00"
@.str.2 = [25 x i8] c"/tmp/test_request.domain\00"
@.str.3 = [8 x i8] c"%d, %s\0A\00"
@.str.4 = [14 x i8] c"child pid %d\0A\00"
@.str.5 = [11 x i8] c"I am child\00"
define i32 @main() {
%1 = alloca [128 x i8]
%2 = tail call i64 (...) @fork()
%3 = icmp slt i64 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %29
6: ; preds = %0
%7 = icmp eq i64 %2, 0
br i1 %7, label %22, label %8
8: ; preds = %6
%9 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%10 = tail call i32 (...) @getpid()
%11 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = tail call i32 @init_listen_server(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%13 = tail call i32 @accept_client(i32 %12)
%14 = call i32 @read(i32 %13, i8* %9, i32 124)
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %8
%17 = zext i32 %14 to i64
%18 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 %17
store i8 0, i8* %18
%19 = tail call i32 (...) @getpid()
%20 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %19, i8* %9)
br label %21
21: ; preds = %16, %8
br label %29
22: ; preds = %6
%23 = tail call i32 (...) @getpid()
%24 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %23)
%25 = tail call i32 @connect_server(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = tail call i32 @write(i32 %25, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0), i64 12)
br label %29
29: ; preds = %22, %27, %21, %4
ret i32 0
}