#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 /*<<< orphan*/ on;
typedef int /*<<< orphan*/ local_addr;
typedef int /*<<< orphan*/ client_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_LOOPBACK;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ process_http_request(int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int listen_sock, connect_sock;
  int on = 1;
  struct sockaddr_in local_addr, client_addr;
  int client_addr_len = sizeof(client_addr);
  if ((listen_sock = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
    perror("Unable to create listening socket");
    exit(0);
  }
  if (setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) ==
      -1) {
    perror("Setting socket option");
    exit(0);
  }
  local_addr.sin_family = AF_INET;
  local_addr.sin_port = htons(80);
  local_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  if (bind(listen_sock, (struct sockaddr *)&local_addr, sizeof(local_addr)) ==
      -1) {
    perror("Unable to bind to local address");
    exit(0);
  }
  if (listen(listen_sock, 5) == -1) {
    perror("Unable to set socket backlog");
    exit(0);
  }
  while ((connect_sock = accept(listen_sock, (struct sockaddr *)&client_addr,
                                &client_addr_len)) != -1) {
    process_http_request(connect_sock);
  }
  if (connect_sock == -1) {
    perror("Unable to accept socket");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x1c(%rbp)
movl   $0xc,-0x3c(%rbp)
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11fc <main+0x5c>
lea    0xe10(%rip),%rdi
call   1080 <perror@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2de2(%rip),%rax
mov    (%rax),%esi
mov    0x2da9(%rip),%rax
mov    (%rax),%edx
lea    -0x1c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    123c <main+0x9c>
lea    0xdf2(%rip),%rdi
call   1080 <perror@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    0x2d9d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    $0x50,%edi
call   1040 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
jne    1293 <main+0xf3>
lea    0xdb1(%rip),%rdi
call   1080 <perror@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
jne    12bc <main+0x11c>
lea    0xda8(%rip),%rdi
call   1080 <perror@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
jmp    12c1 <main+0x121>
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
call   1090 <accept@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
je     12ea <main+0x14a>
mov    -0x18(%rbp),%edi
call   10b0 <process_http_request@plt>
jmp    12c1 <main+0x121>
cmpl   $0xffffffff,-0x18(%rbp)
jne    1300 <main+0x160>
lea    0xd7a(%rip),%rdi
call   1080 <perror@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
movl   $0x1,0xc(%rsp)
movl   $0xc,0x8(%rsp)
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     12c1 <main+0x121>
mov    %eax,%r14d
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
mov    0x2dcb(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %r14d,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     12ca <main+0x12a>
mov    0x2dce(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    $0x50,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %r14d,%edi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0xffffffff,%eax
je     12d3 <main+0x133>
mov    %r14d,%edi
mov    $0x5,%esi
call   1060 <listen@plt>
cmp    $0xffffffff,%eax
je     12dc <main+0x13c>
lea    0x20(%rsp),%rsi
lea    0x8(%rsp),%rdx
mov    %r14d,%edi
call   1090 <accept@plt>
cmp    $0xffffffff,%eax
je     12aa <main+0x10a>
lea    0x20(%rsp),%rbx
lea    0x8(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
mov    %eax,%edi
call   10b0 <process_http_request@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1090 <accept@plt>
cmp    $0xffffffff,%eax
jne    1290 <main+0xf0>
lea    0xdc4(%rip),%rdi
call   1080 <perror@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd38(%rip),%rdi
jmp    12e3 <main+0x143>
lea    0xd51(%rip),%rdi
jmp    12e3 <main+0x143>
lea    0xd5e(%rip),%rdi
jmp    12e3 <main+0x143>
lea    0xd75(%rip),%rdi
call   1080 <perror@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [34 x i8] c"Unable to create listening socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [22 x i8] c"Setting socket option\00"
@AF_INET = global i32 0
@INADDR_LOOPBACK = global i32 0
@.str.2 = [32 x i8] c"Unable to bind to local address\00"
@.str.3 = [29 x i8] c"Unable to set socket backlog\00"
@.str.4 = [24 x i8] c"Unable to accept socket\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
%10 = alloca %struct.sockaddr_in
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %8
store i32 12, i32* %11
%12 = load i32, i32* @PF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %6
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 0)
unreachable
19: ; preds = %2
%20 = load i32, i32* %6
%21 = load i32, i32* @SOL_SOCKET
%22 = load i32, i32* @SO_REUSEADDR
%23 = call i32 @setsockopt(i32 %20, i32 %21, i32 %22, i32* %8, i32 4)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %19
%26 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @exit(i32 0)
unreachable
28: ; preds = %19
%29 = load i32, i32* @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %29, i32* %30
%31 = call i32 @htons(i32 80)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
store i32 %31, i32* %32
%33 = load i32, i32* @INADDR_LOOPBACK
%34 = call i32 @htonl(i32 %33)
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%36 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %35, i32 0, i32 0
store i32 %34, i32* %36
%37 = load i32, i32* %6
%38 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%39 = call i32 @bind(i32 %37, %struct.sockaddr* %38, i32 12)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %44
41: ; preds = %28
%42 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%43 = call i32 @exit(i32 0)
unreachable
44: ; preds = %28
%45 = load i32, i32* %6
%46 = call i32 @listen(i32 %45, i32 5)
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %51
48: ; preds = %44
%49 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
%50 = call i32 @exit(i32 0)
unreachable
51: ; preds = %44
br label %52
52: ; preds = %57, %51
%53 = load i32, i32* %6
%54 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%55 = call i32 @accept(i32 %53, %struct.sockaddr* %54, i32* %11)
store i32 %55, i32* %7
%56 = icmp ne i32 %55, -1
br i1 %56, label %57, label %60
57: ; preds = %52
%58 = load i32, i32* %7
%59 = call i32 @process_http_request(i32 %58)
br label %52
60: ; preds = %52
%61 = load i32, i32* %7
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %65
63: ; preds = %60
%64 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %65
65: ; preds = %63, %60
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [34 x i8] c"Unable to create listening socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [22 x i8] c"Setting socket option\00"
@AF_INET = global i32 0
@INADDR_LOOPBACK = global i32 0
@.str.2 = [32 x i8] c"Unable to bind to local address\00"
@.str.3 = [29 x i8] c"Unable to set socket backlog\00"
@.str.4 = [24 x i8] c"Unable to accept socket\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%8 = bitcast %struct.sockaddr_in* %4 to i8*
%9 = bitcast %struct.sockaddr_in* %5 to i8*
%10 = bitcast i32* %6 to i8*
store i32 12, i32* %6
%11 = load i32, i32* @PF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = tail call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = tail call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%17 = tail call i32 @exit(i32 0)
unreachable
18: ; preds = %2
%19 = load i32, i32* @SOL_SOCKET
%20 = load i32, i32* @SO_REUSEADDR
%21 = call i32 @setsockopt(i32 %13, i32 %19, i32 %20, i32* %3, i32 4)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 0)
unreachable
26: ; preds = %18
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %27, i32* %28
%29 = call i32 @htons(i32 80)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %29, i32* %30
%31 = load i32, i32* @INADDR_LOOPBACK
%32 = call i32 @htonl(i32 %31)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
store i32 %32, i32* %33
%34 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%35 = call i32 @bind(i32 %13, %struct.sockaddr* %34, i32 12)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %40
37: ; preds = %26
%38 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%39 = call i32 @exit(i32 0)
unreachable
40: ; preds = %26
%41 = call i32 @listen(i32 %13, i32 5)
%42 = icmp eq i32 %41, -1
br i1 %42, label %47, label %43
43: ; preds = %40
%44 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%45 = call i32 @accept(i32 %13, %struct.sockaddr* %44, i32* %6)
%46 = icmp eq i32 %45, -1
br i1 %46, label %55, label %50
47: ; preds = %40
%48 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
%49 = call i32 @exit(i32 0)
unreachable
50: ; preds = %43, %50
%51 = phi i32 [ %53, %50 ], [ %45, %43 ]
%52 = call i32 @process_http_request(i32 %51)
%53 = call i32 @accept(i32 %13, %struct.sockaddr* %44, i32* %6)
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %50
55: ; preds = %50, %43
%56 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
ret i32 0
}