#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
scalar_t__ bind(int, struct sockaddr *, int);
int close(int);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ handle_conn(int);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  int fds = socket(AF_INET, SOCK_STREAM, 0);
  struct sockaddr_in local;
  local.sin_family = AF_INET;
  local.sin_port = htons(80);
  local.sin_addr.s_addr = INADDR_ANY;
  if (bind(fds, (struct sockaddr *)&local, sizeof(struct sockaddr_in))) {
    printf("ERR: bind");
    return 1;
  }
  if (listen(fds, 128)) {
    printf("ERR: listen");
    return 2;
  }
  printf("HTTP: listen() succeeded\n");
  while (1) {
    int fd = accept(fds, NULL, 0);
    if (!fork()) {
      handle_conn(fd);
      return 0;
    }
  }
  return close(fds);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x8(%rbp)
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x50,%edi
call   1040 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
je     1203 <main+0x83>
lea    0xe0e(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1278 <main+0xf8>
mov    -0x8(%rbp),%edi
mov    $0x80,%esi
call   1060 <listen@plt>
cmp    $0x0,%rax
je     1232 <main+0xb2>
lea    0xde9(%rip),%rdi
call   1050 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1278 <main+0xf8>
lea    0xddd(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1080 <accept@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
cmp    $0x0,%eax
jne    1273 <main+0xf3>
mov    -0x1c(%rbp),%edi
call   1030 <handle_conn@plt>
movl   $0x0,-0x4(%rbp)
jmp    1278 <main+0xf8>
jmp    123e <main+0xbe>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    0x2e4b(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,%ebx
mov    0x0(%rbp),%eax
mov    %eax,0x10(%rsp)
mov    $0x50,%edi
call   1050 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2e12(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %rax,%rax
je     11fd <main+0x6d>
lea    0xe0f(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%eax
jmp    1251 <main+0xc1>
mov    %ebx,%edi
mov    $0x80,%esi
call   1070 <listen@plt>
test   %rax,%rax
je     1221 <main+0x91>
lea    0xdf5(%rip),%rdi
call   1060 <printf@plt>
mov    $0x2,%eax
jmp    1251 <main+0xc1>
lea    0xdee(%rip),%rdi
call   1030 <puts@plt>
nopl   (%rax)
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <accept@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
jne    1230 <main+0xa0>
mov    %ebp,%edi
call   1040 <handle_conn@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

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
@INADDR_ANY = global i32 0
@.str = [10 x i8] c"ERR: bind\00"
@.str.1 = [12 x i8] c"ERR: listen\00"
@.str.2 = [26 x i8] c"HTTP: listen() succeeded\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
store i32 0, i32* %1
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = call i32 @socket(i32 %5, i32 %6, i32 0)
store i32 %7, i32* %2
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %8, i32* %9
%10 = call i32 @htons(i32 80)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
store i32 %10, i32* %11
%12 = load i32, i32* @INADDR_ANY
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%14 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %13, i32 0, i32 0
store i32 %12, i32* %14
%15 = load i32, i32* %2
%16 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%17 = call i64 @bind(i32 %15, %struct.sockaddr* %16, i32 12)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %0
%20 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %38
21: ; preds = %0
%22 = load i32, i32* %2
%23 = call i64 @listen(i32 %22, i32 128)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %1
br label %38
27: ; preds = %21
%28 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %29
29: ; preds = %27, %37
%30 = load i32, i32* %2
%31 = call i32 @accept(i32 %30, i32* null, i32 0)
store i32 %31, i32* %4
%32 = call i32 (...) @fork()
%33 = icmp ne i32 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %29
%35 = load i32, i32* %4
%36 = call i32 @handle_conn(i32 %35)
store i32 0, i32* %1
br label %38
37: ; preds = %29
br label %29
38: ; preds = %34, %25, %19
%39 = load i32, i32* %1
ret i32 %39
}

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
@INADDR_ANY = global i32 0
@.str = [10 x i8] c"ERR: bind\00"
@.str.1 = [12 x i8] c"ERR: listen\00"
@str = [25 x i8] c"HTTP: listen() succeeded\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = load i32, i32* @AF_INET
%3 = load i32, i32* @SOCK_STREAM
%4 = tail call i32 @socket(i32 %2, i32 %3, i32 0)
%5 = bitcast %struct.sockaddr_in* %1 to i8*
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %6, i32* %7
%8 = tail call i32 @htons(i32 80)
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %8, i32* %9
%10 = load i32, i32* @INADDR_ANY
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %10, i32* %11
%12 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%13 = call i64 @bind(i32 %4, %struct.sockaddr* %12, i32 12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %0
%16 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %30
17: ; preds = %0
%18 = call i64 @listen(i32 %4, i32 128)
%19 = icmp eq i64 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %17
%21 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %30
22: ; preds = %17
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
br label %24
24: ; preds = %24, %22
%25 = call i32 @accept(i32 %4, i32* null, i32 0)
%26 = call i32 (...) @fork()
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %24
28: ; preds = %24
%29 = call i32 @handle_conn(i32 %25)
br label %30
30: ; preds = %28, %20, %15
%31 = phi i32 [ 1, %15 ], [ 2, %20 ], [ 0, %28 ]
ret i32 %31
}