#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 */
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef scalar_t__ pid_t;
typedef int /*<<< orphan*/ client_addr;

/* Variables and functions */
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ handle_request(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int startup();
int main() {
  int server_fd = -1;
  int client_fd = -1;
  struct sockaddr_in client_addr;
  char recv_buf[1024];
  int recv_len = 0;
  socklen_t client_addr_len = sizeof(client_addr);
  server_fd = startup();
  pid_t pid;
  while (1) {
    client_fd =
        accept(server_fd, (struct sockaddr *)&client_addr, &client_addr_len);
    if (client_fd < 0) {
      perror("accept");
      exit(-1);
    }
    pid = fork();
    if (pid < 0) {
      perror("fork");
      exit(-1);
    }
    if (pid == 0) {
      close(server_fd);
      printf("client socket fd: %d\n", client_fd);
      handle_request(client_fd);
      close(client_fd);
      exit(0);
    } else {
      close(client_fd);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xffffffff,-0x8(%rbp)
movl   $0xffffffff,-0xc(%rbp)
movl   $0x0,-0x414(%rbp)
movl   $0x4,-0x418(%rbp)
mov    $0x0,%al
call   1060 <startup@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x418(%rbp),%rdx
call   1080 <accept@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11f4 <main+0x74>
lea    0xe1b(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x420(%rbp)
cmpq   $0x0,-0x420(%rbp)
jge    1226 <main+0xa6>
lea    0xdf0(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
cmpq   $0x0,-0x420(%rbp)
jne    1262 <main+0xe2>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0xc(%rbp),%esi
lea    0xdc6(%rip),%rdi
call   1030 <printf@plt>
mov    -0xc(%rbp),%edi
call   1050 <handle_request@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
jmp    11be <main+0x3e>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
movl   $0x4,0xc(%rsp)
xor    %eax,%eax
call   1060 <startup@plt>
mov    %eax,%ebx
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %eax,%edi
call   1080 <accept@plt>
test   %eax,%eax
js     11e8 <main+0x68>
mov    %eax,%ebp
lea    0x10(%rsp),%r14
lea    0xc(%rsp),%r15
nopl   0x0(%rax)
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     11f1 <main+0x71>
je     1207 <main+0x87>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1080 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11c0 <main+0x40>
lea    0xe11(%rip),%rdi
jmp    11f8 <main+0x78>
lea    0xe0f(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    %ebx,%edi
call   1040 <close@plt>
lea    0xdf7(%rip),%rdi
mov    %ebp,%esi
call   1030 <printf@plt>
mov    %ebp,%edi
call   1050 <handle_request@plt>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [7 x i8] c"accept\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [22 x i8] c"client socket fd: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca [1024 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
store i32 0, i32* %1
store i32 -1, i32* %2
store i32 -1, i32* %3
store i32 0, i32* %6
store i32 4, i32* %7
%9 = call i32 (...) @startup()
store i32 %9, i32* %2
br label %10
10: ; preds = %0, %42
%11 = load i32, i32* %2
%12 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%13 = call i32 @accept(i32 %11, %struct.sockaddr* %12, i32* %7)
store i32 %13, i32* %3
%14 = load i32, i32* %3
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %10
%17 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 -1)
unreachable
19: ; preds = %10
%20 = call i64 (...) @fork()
store i64 %20, i64* %8
%21 = load i64, i64* %8
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %19
%24 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 -1)
unreachable
26: ; preds = %19
%27 = load i64, i64* %8
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %39
29: ; preds = %26
%30 = load i32, i32* %2
%31 = call i32 @close(i32 %30)
%32 = load i32, i32* %3
%33 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %32)
%34 = load i32, i32* %3
%35 = call i32 @handle_request(i32 %34)
%36 = load i32, i32* %3
%37 = call i32 @close(i32 %36)
%38 = call i32 @exit(i32 0)
unreachable
39: ; preds = %26
%40 = load i32, i32* %3
%41 = call i32 @close(i32 %40)
br label %42
42: ; preds = %39
br label %10
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [7 x i8] c"accept\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [22 x i8] c"client socket fd: %d\0A\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca i32
%3 = bitcast %struct.sockaddr_in* %1 to i8*
%4 = bitcast i32* %2 to i8*
store i32 4, i32* %2
%5 = tail call i32 (...) @startup()
%6 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%7 = call i32 @accept(i32 %5, %struct.sockaddr* %6, i32* %2)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %27, %0
%10 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 -1)
unreachable
12: ; preds = %0, %27
%13 = phi i32 [ %29, %27 ], [ %7, %0 ]
%14 = call i64 (...) @fork()
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @exit(i32 -1)
unreachable
19: ; preds = %12
%20 = icmp eq i64 %14, 0
br i1 %20, label %21, label %27
21: ; preds = %19
%22 = call i32 @close(i32 %5)
%23 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%24 = call i32 @handle_request(i32 %13)
%25 = call i32 @close(i32 %13)
%26 = call i32 @exit(i32 0)
unreachable
27: ; preds = %19
%28 = call i32 @close(i32 %13)
%29 = call i32 @accept(i32 %5, %struct.sockaddr* %6, i32* %2)
%30 = icmp slt i32 %29, 0
br i1 %30, label %9, label %12
}