#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*/ perror(char *);
int /*<<< orphan*/ server_client(int);
void server_listen(int listenfd) {
  int connfd;
  struct sockaddr_in client_addr;
  socklen_t addr_len;
  char buffer[1024];
  pid_t child_pid;
  addr_len = sizeof(client_addr);
  if ((connfd = accept(listenfd, (struct sockaddr *)&client_addr, &addr_len)) <
      0) {
    perror("socket accept");
    exit(1);
  }
  child_pid = fork();
  if (child_pid < 0) {
    perror("fork");
    exit(1);
  } else if (child_pid == 0) {
    close(listenfd);
    server_client(connfd);
    exit(0);
  } else
    close(connfd);
}


x86 assembly (clang, -O0):
<server_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x4,-0x14(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   1060 <accept@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11a7 <server_listen+0x47>
lea    0xe68(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jge    11d9 <server_listen+0x79>
lea    0xe44(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x428(%rbp)
jne    11fe <server_listen+0x9e>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%edi
call   1030 <server_client@plt>
xor    %edi,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
jmp    120b <server_listen+0xab>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<server_listen>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
movl   $0x4,0xc(%rsp)
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   1060 <accept@plt>
test   %eax,%eax
js     11a1 <server_listen+0x41>
mov    %eax,%ebp
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
js     11aa <server_listen+0x4a>
je     11c0 <server_listen+0x60>
mov    %ebp,%edi
call   1040 <close@plt>
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe58(%rip),%rdi
jmp    11b1 <server_listen+0x51>
lea    0xe5d(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%edi
call   1030 <server_client@plt>
xor    %edi,%edi
call   1070 <exit@plt>

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

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