#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_storage {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fdopen(int, char *);
int fork();
int /*<<< orphan*/ log_exit(char *, int /*<<< orphan*/);
int /*<<< orphan*/ service(int /*<<< orphan*/ *, int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
void server_main(int server, char *docroot) {
  for (;;) {
    struct sockaddr_storage addr;
    socklen_t addrlen = sizeof addr;
    int sock;
    int pid;
    sock = accept(server, (struct sockaddr *)&addr, &addrlen);
    if (sock < 0)
      log_exit("accept(2) failed: %s", strerror(errno));
    pid = fork();
    if (pid < 0)
      exit(3);
    if (pid == 0) {
      FILE *inf = fdopen(sock, "r");
      FILE *outf = fdopen(sock, "w");
      service(inf, outf, docroot);
      exit(0);
    }
    close(sock);
  }
}


x86 assembly (clang, -O0):
<server_main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x4,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   1060 <accept@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11cf <server_main+0x4f>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %eax,%esi
lea    0xe36(%rip),%rdi
call   1070 <log_exit@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11ed <server_main+0x6d>
mov    $0x3,%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x24(%rbp)
jne    1235 <server_main+0xb5>
mov    -0x20(%rbp),%edi
lea    0xe14(%rip),%rsi
call   1040 <fdopen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%edi
lea    0xe03(%rip),%rsi
call   1040 <fdopen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1050 <service@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%edi
call   1030 <close@plt>
jmp    118f <server_main+0xf>

x86 assembly (clang, -O2):
<server_main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,0x8(%rsp)
mov    %edi,%ebp
lea    0x10(%rsp),%r15
lea    0x4(%rsp),%r13
mov    0x2e3a(%rip),%r14
lea    0xe53(%rip),%r12
nopl   (%rax)
movl   $0x4,0x4(%rsp)
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r13,%rdx
call   1060 <accept@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11dd <server_main+0x5d>
mov    (%r14),%edi
call   1090 <strerror@plt>
mov    %r12,%rdi
mov    %eax,%esi
call   1070 <log_exit@plt>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
js     11f3 <server_main+0x73>
je     11fd <server_main+0x7d>
mov    %ebx,%edi
call   1030 <close@plt>
jmp    11b0 <server_main+0x30>
mov    $0x3,%edi
call   1080 <exit@plt>
lea    0xe11(%rip),%rsi
mov    %ebx,%edi
call   1040 <fdopen@plt>
mov    %rax,%rbp
lea    0xe02(%rip),%rsi
mov    %ebx,%edi
call   1040 <fdopen@plt>
mov    %rbp,%rdi
mov    %rax,%rsi
mov    0x8(%rsp),%rdx
call   1050 <service@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [21 x i8] c"accept(2) failed: %s\00"
@errno = global i32 0
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c"w\00"
define void @server_main(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.sockaddr_storage
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32*
store i32 %0, i32* %3
store i8* %1, i8** %4
br label %11
11: ; preds = %40, %2
store i32 4, i32* %6
%12 = load i32, i32* %3
%13 = bitcast %struct.sockaddr_storage* %5 to %struct.sockaddr*
%14 = call i32 @accept(i32 %12, %struct.sockaddr* %13, i32* %6)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %11
%18 = load i32, i32* @errno
%19 = call i32 @strerror(i32 %18)
%20 = call i32 @log_exit(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %19)
br label %21
21: ; preds = %17, %11
%22 = call i32 (...) @fork()
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = call i32 @exit(i32 3)
unreachable
27: ; preds = %21
%28 = load i32, i32* %8
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %40
30: ; preds = %27
%31 = load i32, i32* %7
%32 = call i32* @fdopen(i32 %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %32, i32** %9
%33 = load i32, i32* %7
%34 = call i32* @fdopen(i32 %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %34, i32** %10
%35 = load i32*, i32** %9
%36 = load i32*, i32** %10
%37 = load i8*, i8** %4
%38 = call i32 @service(i32* %35, i32* %36, i8* %37)
%39 = call i32 @exit(i32 0)
unreachable
40: ; preds = %27
%41 = load i32, i32* %7
%42 = call i32 @close(i32 %41)
br label %11
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [21 x i8] c"accept(2) failed: %s\00"
@errno = global i32 0
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c"w\00"
define void @server_main(i32 %0, i8* %1) {
%3 = alloca %struct.sockaddr_storage
%4 = alloca i32
%5 = bitcast %struct.sockaddr_storage* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = bitcast %struct.sockaddr_storage* %3 to %struct.sockaddr*
br label %8
8: ; preds = %27, %2
store i32 4, i32* %4
%9 = call i32 @accept(i32 %0, %struct.sockaddr* %7, i32* %4)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %8
%12 = load i32, i32* @errno
%13 = call i32 @strerror(i32 %12)
%14 = call i32 @log_exit(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %13)
br label %15
15: ; preds = %11, %8
%16 = call i32 (...) @fork()
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = call i32 @exit(i32 3)
unreachable
20: ; preds = %15
%21 = icmp eq i32 %16, 0
br i1 %21, label %22, label %27
22: ; preds = %20
%23 = call i32* @fdopen(i32 %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32* @fdopen(i32 %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%25 = call i32 @service(i32* %23, i32* %24, i8* %1)
%26 = call i32 @exit(i32 0)
unreachable
27: ; preds = %20
%28 = call i32 @close(i32 %9)
br label %8
}