#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*/ sin;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SIGCLD;
int /*<<< orphan*/ SIGHUP;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ SOCK_STREAM;
int atoi(char *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
scalar_t__ fork();
int /*<<< orphan*/ free(char *);
char *get_root();
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ log_error(char *);
int /*<<< orphan*/ log_info(char *, int);
int loop(int, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  if (argc < 2) {
    printf("Usage: %s PORT\n", argv[0]);
    printf("\tPORT - a number which represents the port (1-65535)\n");
    printf("\t\tusually 80 is for web\n");
    return 1;
  }
  if (fork() != 0)
    return 1;
  signal(SIGHUP, SIG_IGN);
  signal(SIGCLD, SIG_IGN);
  int port = atoi(argv[1]);
  int sock;
  struct sockaddr_in sin;
  sock = socket(AF_INET, SOCK_STREAM, 0);
  sin.sin_family = AF_INET;
  sin.sin_addr.s_addr = INADDR_ANY;
  sin.sin_port = htons(port);
  if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) != 0) {
    log_error("Failed to bind to socket");
    return 1;
  }
  listen(sock, 5);
  log_info("HTTP server listening on port %d", port);
  char *root = get_root();
  int loop_result = loop(sock, root);
  while (loop_result != 1) {
    loop_result = loop(sock, root);
  }
  free(root);
  close(sock);
  return loop_result;
}


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)
cmpl   $0x2,-0x8(%rbp)
jge    123d <main+0x5d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdf2(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
lea    0xdf4(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
lea    0xe1c(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1375 <main+0x195>
mov    $0x0,%al
call   10f0 <fork@plt>
cmp    $0x0,%rax
je     125a <main+0x7a>
movl   $0x1,-0x4(%rbp)
jmp    1375 <main+0x195>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
mov    0x2d5e(%rip),%rax
mov    (%rax),%esi
call   10b0 <signal@plt>
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
mov    0x2d47(%rip),%rax
mov    (%rax),%esi
call   10b0 <signal@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10e0 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
mov    0x2d10(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x18(%rbp)
mov    0x2d25(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d11(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%edi
call   1070 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <bind@plt>
cmp    $0x0,%rax
je     130a <main+0x12a>
lea    0xd66(%rip),%rdi
call   1060 <log_error@plt>
movl   $0x1,-0x4(%rbp)
jmp    1375 <main+0x195>
mov    -0x18(%rbp),%edi
mov    $0x5,%esi
call   10c0 <listen@plt>
mov    -0x14(%rbp),%esi
lea    0xd57(%rip),%rdi
call   1040 <log_info@plt>
mov    $0x0,%al
call   10a0 <get_root@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x30(%rbp),%rsi
call   1050 <loop@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x1,-0x34(%rbp)
je     135e <main+0x17e>
mov    -0x18(%rbp),%edi
mov    -0x30(%rbp),%rsi
call   1050 <loop@plt>
mov    %eax,-0x34(%rbp)
jmp    1340 <main+0x160>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%edi
call   1090 <close@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
cmp    $0x1,%edi
jg     122e <main+0x3e>
mov    (%rbx),%rsi
lea    0xdf6(%rip),%rdi
xor    %eax,%eax
call   1090 <printf@plt>
lea    0xe32(%rip),%rdi
call   1040 <puts@plt>
lea    0xe5b(%rip),%rdi
call   1040 <puts@plt>
jmp    1320 <main+0x130>
xor    %eax,%eax
call   1100 <fork@plt>
test   %rax,%rax
jne    1320 <main+0x130>
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
mov    0x2d7a(%rip),%rbp
mov    0x0(%rbp),%esi
call   10c0 <signal@plt>
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%esi
call   10c0 <signal@plt>
mov    0x8(%rbx),%rdi
call   10f0 <atoi@plt>
mov    %eax,%r14d
mov    0x2d66(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2d34(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,%ebx
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
mov    0x2d3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    %r14d,%edi
call   1080 <htons@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10e0 <bind@plt>
test   %rax,%rax
je     12d0 <main+0xe0>
lea    0xd47(%rip),%rdi
call   1070 <log_error@plt>
jmp    1320 <main+0x130>
mov    %ebx,%edi
mov    $0x5,%esi
call   10d0 <listen@plt>
lea    0xd46(%rip),%rdi
mov    %r14d,%esi
call   1050 <log_info@plt>
xor    %eax,%eax
call   10b0 <get_root@plt>
mov    %rax,%rbp
mov    %ebx,%edi
mov    %rax,%rsi
nopw   0x0(%rax,%rax,1)
call   1060 <loop@plt>
cmp    $0x1,%eax
je     1311 <main+0x121>
mov    %ebx,%edi
mov    %rbp,%rsi
jmp    1300 <main+0x110>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %ebx,%edi
call   10a0 <close@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [16 x i8] c"Usage: %s PORT\0A\00"
@.str.1 = [54 x i8] c"\09PORT - a number which represents the port (1-65535)\0A\00"
@.str.2 = [25 x i8] c"\09\09usually 80 is for web\0A\00"
@SIGHUP = global i32 0
@SIG_IGN = global i32 0
@SIGCLD = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@.str.3 = [25 x i8] c"Failed to bind to socket\00"
@.str.4 = [33 x i8] c"HTTP server listening on port %d\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 %struct.sockaddr_in
%9 = alloca i8*
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %20
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %74
20: ; preds = %2
%21 = call i64 (...) @fork()
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %20
store i32 1, i32* %3
br label %74
24: ; preds = %20
%25 = load i32, i32* @SIGHUP
%26 = load i32, i32* @SIG_IGN
%27 = call i32 @signal(i32 %25, i32 %26)
%28 = load i32, i32* @SIGCLD
%29 = load i32, i32* @SIG_IGN
%30 = call i32 @signal(i32 %28, i32 %29)
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = call i32 @atoi(i8* %33)
store i32 %34, i32* %6
%35 = load i32, i32* @AF_INET
%36 = load i32, i32* @SOCK_STREAM
%37 = call i32 @socket(i32 %35, i32 %36, i32 0)
store i32 %37, i32* %7
%38 = load i32, i32* @AF_INET
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %38, i32* %39
%40 = load i32, i32* @INADDR_ANY
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%42 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %41, i32 0, i32 0
store i32 %40, i32* %42
%43 = load i32, i32* %6
%44 = call i32 @htons(i32 %43)
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %44, i32* %45
%46 = load i32, i32* %7
%47 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%48 = call i64 @bind(i32 %46, %struct.sockaddr* %47, i32 12)
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %24
%51 = call i32 @log_error(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %74
52: ; preds = %24
%53 = load i32, i32* %7
%54 = call i32 @listen(i32 %53, i32 5)
%55 = load i32, i32* %6
%56 = call i32 @log_info(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 %55)
%57 = call i8* (...) @get_root()
store i8* %57, i8** %9
%58 = load i32, i32* %7
%59 = load i8*, i8** %9
%60 = call i32 @loop(i32 %58, i8* %59)
store i32 %60, i32* %10
br label %61
61: ; preds = %64, %52
%62 = load i32, i32* %10
%63 = icmp ne i32 %62, 1
br i1 %63, label %64, label %68
64: ; preds = %61
%65 = load i32, i32* %7
%66 = load i8*, i8** %9
%67 = call i32 @loop(i32 %65, i8* %66)
store i32 %67, i32* %10
br label %61
68: ; preds = %61
%69 = load i8*, i8** %9
%70 = call i32 @free(i8* %69)
%71 = load i32, i32* %7
%72 = call i32 @close(i32 %71)
%73 = load i32, i32* %10
store i32 %73, i32* %3
br label %74
74: ; preds = %68, %50, %23, %13
%75 = load i32, i32* %3
ret i32 %75
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [16 x i8] c"Usage: %s PORT\0A\00"
@SIGHUP = global i32 0
@SIG_IGN = global i32 0
@SIGCLD = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@.str.3 = [25 x i8] c"Failed to bind to socket\00"
@.str.4 = [33 x i8] c"HTTP server listening on port %d\00"
@str = [53 x i8] c"\09PORT - a number which represents the port (1-65535)\00"
@str.5 = [24 x i8] c"\09\09usually 80 is for web\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = icmp slt i32 %0, 2
br i1 %4, label %5, label %10
5: ; preds = %2
%6 = load i8*, i8** %1
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str, i64 0, i64 0))
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.5, i64 0, i64 0))
br label %51
10: ; preds = %2
%11 = tail call i64 (...) @fork()
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %51
13: ; preds = %10
%14 = load i32, i32* @SIGHUP
%15 = load i32, i32* @SIG_IGN
%16 = tail call i32 @signal(i32 %14, i32 %15)
%17 = load i32, i32* @SIGCLD
%18 = load i32, i32* @SIG_IGN
%19 = tail call i32 @signal(i32 %17, i32 %18)
%20 = getelementptr inbounds i8*, i8** %1, i64 1
%21 = load i8*, i8** %20
%22 = tail call i32 @atoi(i8* %21)
%23 = bitcast %struct.sockaddr_in* %3 to i8*
%24 = load i32, i32* @AF_INET
%25 = load i32, i32* @SOCK_STREAM
%26 = tail call i32 @socket(i32 %24, i32 %25, i32 0)
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %27, i32* %28
%29 = load i32, i32* @INADDR_ANY
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %29, i32* %30
%31 = tail call i32 @htons(i32 %22)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %31, i32* %32
%33 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%34 = call i64 @bind(i32 %26, %struct.sockaddr* %33, i32 12)
%35 = icmp eq i64 %34, 0
br i1 %35, label %38, label %36
36: ; preds = %13
%37 = call i32 @log_error(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %50
38: ; preds = %13
%39 = call i32 @listen(i32 %26, i32 5)
%40 = call i32 @log_info(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0), i32 %22)
%41 = call i8* (...) @get_root()
%42 = call i32 @loop(i32 %26, i8* %41)
%43 = icmp eq i32 %42, 1
br i1 %43, label %47, label %44
44: ; preds = %38, %44
%45 = call i32 @loop(i32 %26, i8* %41)
%46 = icmp eq i32 %45, 1
br i1 %46, label %47, label %44
47: ; preds = %44, %38
%48 = call i32 @free(i8* %41)
%49 = call i32 @close(i32 %26)
br label %50
50: ; preds = %47, %36
br label %51
51: ; preds = %10, %50, %5
ret i32 1
}