#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 int /*<<< orphan*/ clientaddr;

/* Variables and functions */
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ handle_request(int, struct sockaddr_in);
scalar_t__ listen(int, int);
int /*<<< orphan*/ stderr;
void run_server(int parentfd) {
  struct sockaddr_in clientaddr;
  int childfd;
  socklen_t clientlen;
  if (listen(parentfd, 5) < 0) {
    fprintf(stderr, "ERROR on listen");
    exit(1);
  }
  clientlen = sizeof(clientaddr);
  while (1) {
    childfd = accept(parentfd, (struct sockaddr *)&clientaddr, &clientlen);
    if (childfd < 0) {
      fprintf(stderr, "ERROR on accept");
      exit(1);
    }
    handle_request(childfd, clientaddr);
    close(childfd);
  }
}


x86 assembly (clang, -O0):
<run_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
mov    $0x5,%esi
call   1060 <listen@plt>
cmp    $0x0,%rax
jge    11a1 <run_server+0x41>
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
lea    0xe6e(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
movl   $0x4,-0x10(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
lea    -0x10(%rbp),%rdx
call   1070 <accept@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11e4 <run_server+0x84>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
lea    0xe3b(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0xc(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1040 <handle_request@plt>
mov    -0xc(%rbp),%edi
call   1030 <close@plt>
jmp    11a8 <run_server+0x48>

x86 assembly (clang, -O2):
<run_server>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
mov    $0x5,%esi
call   1060 <listen@plt>
test   %rax,%rax
jns    118a <run_server+0x2a>
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
lea    0xe78(%rip),%rsi
jmp    11e4 <run_server+0x84>
movl   $0x4,0x4(%rsp)
mov    %rsp,%rsi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   1070 <accept@plt>
test   %eax,%eax
js     11d4 <run_server+0x74>
mov    %eax,%ebp
mov    %rsp,%r14
lea    0x4(%rsp),%r15
nop
mov    (%rsp),%esi
mov    %ebp,%edi
call   1040 <handle_request@plt>
mov    %ebp,%edi
call   1030 <close@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1070 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11b0 <run_server+0x50>
mov    0x2e05(%rip),%rax
mov    (%rax),%edi
lea    0xe2c(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [16 x i8] c"ERROR on listen\00"
@.str.1 = [16 x i8] c"ERROR on accept\00"
define void @run_server(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = call i64 @listen(i32 %6, i32 5)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %1
store i32 4, i32* %5
br label %14
14: ; preds = %13, %24
%15 = load i32, i32* %2
%16 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%17 = call i32 @accept(i32 %15, %struct.sockaddr* %16, i32* %5)
store i32 %17, i32* %4
%18 = load i32, i32* %4
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %14
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %14
%25 = load i32, i32* %4
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%27 = load i32, i32* %26
%28 = call i32 @handle_request(i32 %25, i32 %27)
%29 = load i32, i32* %4
%30 = call i32 @close(i32 %29)
br label %14
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [16 x i8] c"ERROR on listen\00"
@.str.1 = [16 x i8] c"ERROR on accept\00"
define void @run_server(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = bitcast i32* %3 to i8*
%6 = tail call i64 @listen(i32 %0, i32 5)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %1
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %1
store i32 4, i32* %3
%13 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%14 = call i32 @accept(i32 %0, %struct.sockaddr* %13, i32* %3)
%15 = icmp slt i32 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %12
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
br label %22
18: ; preds = %22, %12
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %16, %22
%23 = phi i32 [ %14, %16 ], [ %27, %22 ]
%24 = load i32, i32* %17
%25 = call i32 @handle_request(i32 %23, i32 %24)
%26 = call i32 @close(i32 %23)
%27 = call i32 @accept(i32 %0, %struct.sockaddr* %13, i32* %3)
%28 = icmp slt i32 %27, 0
br i1 %28, label %18, label %22
}