#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 {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ serv_addr;
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
scalar_t__ close(int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ handle_request(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  printf("Chromoting client webserver: http://localhost:%d\n", 8080);
  signal(SIGCHLD, SIG_IGN);
  int sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock < 0) {
    error("Unable to open socket");
  }
  struct sockaddr_in serv_addr;
  bzero((char *)&serv_addr, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  serv_addr.sin_port = htons(8080);
  if (bind(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
    error("Unable to bind socket");
  }
  if (listen(sock, 5) < 0) {
    error("Unable to listen to socket");
  }
  while (1) {
    int connection = accept(sock, NULL, NULL);
    if (connection < 0) {
      error("Unable to accept connection");
    }
    pid_t pid = fork();
    if (pid == 0) {
      if (close(sock) < 0) {
        error("Unable to close socket in child");
      }
      handle_request(connection);
      exit(0);
    } else {
      if (close(connection) < 0) {
        error("Unable to close connection in parent");
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe03(%rip),%rdi
mov    $0x1f90,%esi
call   1040 <printf@plt>
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
mov    0x2d92(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1250 <main+0x70>
lea    0xde7(%rip),%rdi
call   10b0 <error@plt>
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    0x2d83(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x1f90,%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    12af <main+0xcf>
lea    0xd9e(%rip),%rdi
call   10b0 <error@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   1090 <listen@plt>
cmp    $0x0,%rax
jge    12d2 <main+0xf2>
lea    0xd91(%rip),%rdi
call   10b0 <error@plt>
jmp    12d7 <main+0xf7>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   10d0 <accept@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    12ff <main+0x11f>
lea    0xd7f(%rip),%rdi
call   10b0 <error@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1342 <main+0x162>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
cmp    $0x0,%rax
jge    1333 <main+0x153>
lea    0xd67(%rip),%rdi
call   10b0 <error@plt>
mov    -0x24(%rbp),%edi
call   1080 <handle_request@plt>
xor    %edi,%edi
call   10e0 <exit@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
cmp    $0x0,%rax
jge    1360 <main+0x180>
lea    0xd5a(%rip),%rdi
call   10b0 <error@plt>
jmp    1365 <main+0x185>
jmp    12d7 <main+0xf7>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0xe0f(%rip),%rdi
mov    $0x1f90,%esi
call   1040 <printf@plt>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
call   1070 <signal@plt>
mov    0x2dcf(%rip),%r14
mov    (%r14),%edi
mov    0x2d9d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    123e <main+0x5e>
lea    0xdf9(%rip),%rdi
call   10b0 <error@plt>
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    (%r14),%eax
mov    %eax,0x10(%rsp)
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0xc(%rsp)
mov    $0x1f90,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
test   %rax,%rax
jns    1297 <main+0xb7>
lea    0xdb6(%rip),%rdi
call   10b0 <error@plt>
mov    %ebx,%edi
mov    $0x5,%esi
call   1090 <listen@plt>
lea    0xdb4(%rip),%r14
test   %rax,%rax
jns    12b7 <main+0xd7>
mov    %r14,%rdi
call   10b0 <error@plt>
lea    0xdbb(%rip),%r15
lea    0xdf0(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10d0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    12e9 <main+0x109>
mov    %r15,%rdi
call   10b0 <error@plt>
xor    %eax,%eax
call   10f0 <fork@plt>
test   %rax,%rax
je     1303 <main+0x123>
mov    %ebp,%edi
call   1060 <close@plt>
test   %rax,%rax
jns    12d0 <main+0xf0>
jmp    12af <main+0xcf>
mov    %ebx,%edi
call   1060 <close@plt>
test   %rax,%rax
jns    131b <main+0x13b>
lea    0xd7f(%rip),%rdi
call   10b0 <error@plt>
mov    %ebp,%edi
call   1080 <handle_request@plt>
xor    %edi,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [50 x i8] c"Chromoting client webserver: http://localhost:%d\0A\00"
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [22 x i8] c"Unable to open socket\00"
@INADDR_ANY = global i32 0
@.str.2 = [22 x i8] c"Unable to bind socket\00"
@.str.3 = [27 x i8] c"Unable to listen to socket\00"
@.str.4 = [28 x i8] c"Unable to accept connection\00"
@.str.5 = [32 x i8] c"Unable to close socket in child\00"
@.str.6 = [37 x i8] c"Unable to close connection in parent\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
%9 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = call i32 @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i32 8080)
%11 = load i32, i32* @SIGCHLD
%12 = load i32, i32* @SIG_IGN
%13 = call i32 @signal(i32 %11, i32 %12)
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 @error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %19, %2
%22 = bitcast %struct.sockaddr_in* %7 to i8*
%23 = call i32 @bzero(i8* %22, i32 12)
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %24, i32* %25
%26 = load i32, i32* @INADDR_ANY
%27 = call i32 @htonl(i32 %26)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%29 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %28, i32 0, i32 0
store i32 %27, i32* %29
%30 = call i32 @htons(i32 8080)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %30, i32* %31
%32 = load i32, i32* %6
%33 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%34 = call i64 @bind(i32 %32, %struct.sockaddr* %33, i32 12)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %21
%37 = call i32 @error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %38
38: ; preds = %36, %21
%39 = load i32, i32* %6
%40 = call i64 @listen(i32 %39, i32 5)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %38
%43 = call i32 @error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
br label %44
44: ; preds = %42, %38
br label %45
45: ; preds = %44, %73
%46 = load i32, i32* %6
%47 = call i32 @accept(i32 %46, i32* null, i32* null)
store i32 %47, i32* %8
%48 = load i32, i32* %8
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %45
%51 = call i32 @error(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
br label %52
52: ; preds = %50, %45
%53 = call i64 (...) @fork()
store i64 %53, i64* %9
%54 = load i64, i64* %9
%55 = icmp eq i64 %54, 0
br i1 %55, label %56, label %66
56: ; preds = %52
%57 = load i32, i32* %6
%58 = call i64 @close(i32 %57)
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %62
60: ; preds = %56
%61 = call i32 @error(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0))
br label %62
62: ; preds = %60, %56
%63 = load i32, i32* %8
%64 = call i32 @handle_request(i32 %63)
%65 = call i32 @exit(i32 0)
unreachable
66: ; preds = %52
%67 = load i32, i32* %8
%68 = call i64 @close(i32 %67)
%69 = icmp slt i64 %68, 0
br i1 %69, label %70, label %72
70: ; preds = %66
%71 = call i32 @error(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0))
br label %72
72: ; preds = %70, %66
br label %73
73: ; preds = %72
br label %45
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [50 x i8] c"Chromoting client webserver: http://localhost:%d\0A\00"
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [22 x i8] c"Unable to open socket\00"
@INADDR_ANY = global i32 0
@.str.2 = [22 x i8] c"Unable to bind socket\00"
@.str.3 = [27 x i8] c"Unable to listen to socket\00"
@.str.4 = [28 x i8] c"Unable to accept connection\00"
@.str.5 = [32 x i8] c"Unable to close socket in child\00"
@.str.6 = [37 x i8] c"Unable to close connection in parent\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_in
%4 = tail call i32 @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i32 8080)
%5 = load i32, i32* @SIGCHLD
%6 = load i32, i32* @SIG_IGN
%7 = tail call i32 @signal(i32 %5, i32 %6)
%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 %14
12: ; preds = %2
%13 = tail call i32 @error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
br label %14
14: ; preds = %12, %2
%15 = bitcast %struct.sockaddr_in* %3 to i8*
%16 = call i32 @bzero(i8* %15, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %17, i32* %18
%19 = load i32, i32* @INADDR_ANY
%20 = tail call i32 @htonl(i32 %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %20, i32* %21
%22 = tail call i32 @htons(i32 8080)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %22, i32* %23
%24 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%25 = call i64 @bind(i32 %10, %struct.sockaddr* %24, i32 12)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %14
%28 = call i32 @error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
br label %29
29: ; preds = %27, %14
%30 = call i64 @listen(i32 %10, i32 5)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %52, %29
%33 = phi i8* [ getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), %29 ], [ getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0), %52 ]
%34 = call i32 @error(i8* %33)
br label %35
35: ; preds = %29, %32
br label %36
36: ; preds = %35, %52
%37 = call i32 @accept(i32 %10, i32* null, i32* null)
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = call i32 @error(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
br label %41
41: ; preds = %39, %36
%42 = call i64 (...) @fork()
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %52
44: ; preds = %41
%45 = call i64 @close(i32 %10)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %44
%48 = call i32 @error(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0))
br label %49
49: ; preds = %47, %44
%50 = call i32 @handle_request(i32 %37)
%51 = call i32 @exit(i32 0)
unreachable
52: ; preds = %41
%53 = call i64 @close(i32 %37)
%54 = icmp slt i64 %53, 0
br i1 %54, label %32, label %36
}