#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ abort();
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ handle_request(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
void http_server(int port) {
  int sockfd;
  int status;
  struct sockaddr_in addr;
  int c;
  socklen_t addrlen;
  int opt_true;
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) {
    perror("socket");
    abort();
  }
  opt_true = 1;
  setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt_true, sizeof(int));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  addr.sin_addr.s_addr = htonl(INADDR_ANY);
  status = bind(sockfd, (struct sockaddr *)&addr, sizeof(addr));
  if (status < 0) {
    perror("bind");
    abort();
  }
  status = listen(sockfd, 5);
  if (status < 0) {
    perror("listen");
    abort();
  }
  printf("Serving on 0.0.0.0:%d...\n", port);
  addrlen = sizeof(addr);
  while (1) {
    c = accept(sockfd, (struct sockaddr *)&addr, &addrlen);
    if (c < 0) {
      perror("accept");
      abort();
    }
    handle_request(c);
    close(c);
  }
  close(sockfd);
}


x86 assembly (clang, -O0):
<http_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1204 <http_server+0x44>
lea    0xe08(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
movl   $0x1,-0x24(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
mov    0x2da2(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    0x2daa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1288 <http_server+0xc8>
lea    0xd8b(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x8(%rbp),%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    12b5 <http_server+0xf5>
lea    0xd63(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x4(%rbp),%esi
lea    0xd54(%rip),%rdi
call   1060 <printf@plt>
movl   $0xc,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   10d0 <accept@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    12fb <http_server+0x13b>
lea    0xd3e(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x1c(%rbp),%edi
call   1090 <handle_request@plt>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
jmp    12cb <http_server+0x10b>

x86 assembly (clang, -O2):
<http_server>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebp
mov    0x2e0d(%rip),%rbx
mov    (%rbx),%edi
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
test   %eax,%eax
jns    11f5 <http_server+0x35>
lea    0xe10(%rip),%rdi
jmp    12e9 <http_server+0x129>
mov    %eax,%r14d
movl   $0x1,0x14(%rsp)
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
mov    0x2db0(%rip),%rax
mov    (%rax),%edx
lea    0x14(%rsp),%rcx
mov    %r14d,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
mov    (%rbx),%eax
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
call   1050 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %r14d,%edi
mov    $0xc,%edx
call   10b0 <bind@plt>
test   %eax,%eax
jns    126a <http_server+0xaa>
lea    0xda2(%rip),%rdi
jmp    12e9 <http_server+0x129>
mov    %r14d,%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
test   %eax,%eax
jns    1284 <http_server+0xc4>
lea    0xd8a(%rip),%rdi
jmp    12e9 <http_server+0x129>
lea    0xd88(%rip),%rdi
mov    %ebp,%esi
call   1060 <printf@plt>
movl   $0xc,0x4(%rsp)
lea    0x8(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %r14d,%edi
call   10d0 <accept@plt>
test   %eax,%eax
js     12e2 <http_server+0x122>
mov    %eax,%ebp
lea    0x8(%rsp),%r15
lea    0x4(%rsp),%rbx
nopl   0x0(%rax)
mov    %ebp,%edi
call   1090 <handle_request@plt>
mov    %ebp,%edi
call   1080 <close@plt>
mov    %r14d,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   10d0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    12c0 <http_server+0x100>
lea    0xd44(%rip),%rdi
call   10c0 <perror@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"listen\00"
@.str.3 = [26 x i8] c"Serving on 0.0.0.0:%d...\0A\00"
@.str.4 = [7 x i8] c"accept\00"
define void @http_server(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %2
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %3
%12 = load i32, i32* %3
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%16 = call i32 (...) @abort()
unreachable
17: ; preds = %1
store i32 1, i32* %8
%18 = load i32, i32* %3
%19 = load i32, i32* @SOL_SOCKET
%20 = load i32, i32* @SO_REUSEADDR
%21 = call i32 @setsockopt(i32 %18, i32 %19, i32 %20, i32* %8, i32 4)
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %22, i32* %23
%24 = load i32, i32* %2
%25 = call i32 @htons(i32 %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
store i32 %25, i32* %26
%27 = load i32, i32* @INADDR_ANY
%28 = call i32 @htonl(i32 %27)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%30 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %29, i32 0, i32 0
store i32 %28, i32* %30
%31 = load i32, i32* %3
%32 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%33 = call i32 @bind(i32 %31, %struct.sockaddr* %32, i32 12)
store i32 %33, i32* %4
%34 = load i32, i32* %4
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %17
%37 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%38 = call i32 (...) @abort()
unreachable
39: ; preds = %17
%40 = load i32, i32* %3
%41 = call i32 @listen(i32 %40, i32 5)
store i32 %41, i32* %4
%42 = load i32, i32* %4
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %39
%45 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%46 = call i32 (...) @abort()
unreachable
47: ; preds = %39
%48 = load i32, i32* %2
%49 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %48)
store i32 12, i32* %7
br label %50
50: ; preds = %47, %59
%51 = load i32, i32* %3
%52 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%53 = call i32 @accept(i32 %51, %struct.sockaddr* %52, i32* %7)
store i32 %53, i32* %6
%54 = load i32, i32* %6
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %50
%57 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%58 = call i32 (...) @abort()
unreachable
59: ; preds = %50
%60 = load i32, i32* %6
%61 = call i32 @handle_request(i32 %60)
%62 = load i32, i32* %6
%63 = call i32 @close(i32 %62)
br label %50
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"listen\00"
@.str.3 = [26 x i8] c"Serving on 0.0.0.0:%d...\0A\00"
@.str.4 = [7 x i8] c"accept\00"
define void @http_server(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%14 = tail call i32 (...) @abort()
unreachable
15: ; preds = %1
store i32 1, i32* %4
%16 = load i32, i32* @SOL_SOCKET
%17 = load i32, i32* @SO_REUSEADDR
%18 = call i32 @setsockopt(i32 %10, i32 %16, i32 %17, i32* %4, i32 4)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %19, i32* %20
%21 = call i32 @htons(i32 %0)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %21, i32* %22
%23 = load i32, i32* @INADDR_ANY
%24 = call i32 @htonl(i32 %23)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %24, i32* %25
%26 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%27 = call i32 @bind(i32 %10, %struct.sockaddr* %26, i32 12)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %15
%30 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%31 = call i32 (...) @abort()
unreachable
32: ; preds = %15
%33 = call i32 @listen(i32 %10, i32 5)
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 (...) @abort()
unreachable
38: ; preds = %32
%39 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i32 %0)
store i32 12, i32* %3
%40 = call i32 @accept(i32 %10, %struct.sockaddr* %26, i32* %3)
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %45, %38
%43 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%44 = call i32 (...) @abort()
unreachable
45: ; preds = %38, %45
%46 = phi i32 [ %49, %45 ], [ %40, %38 ]
%47 = call i32 @handle_request(i32 %46)
%48 = call i32 @close(i32 %46)
%49 = call i32 @accept(i32 %10, %struct.sockaddr* %26, i32* %3)
%50 = icmp slt i32 %49, 0
br i1 %50, label %42, label %45
}