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

/* Type definitions */
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ socklen_t;
typedef int /*<<< orphan*/ cli_addr;

/* Variables and functions */
size_t EINTR;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup(int);
int /*<<< orphan*/ errexit(char *, int /*<<< orphan*/);
size_t errno;
int /*<<< orphan*/ exit(int);
int fork();
char *inet_ntoa(int /*<<< orphan*/);
int ntohs(int /*<<< orphan*/);
int passivesock(char *, char *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ start();
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ *sys_errlist;
int main(int argc, char **argv) {
  int cpid;
  int serv_sock, cli_sock, cli_len;
  struct sockaddr_in cli_addr;
  char port[6] = "8080";
  if (argc > 2) {
    printf("Usage: ./httpd [port (optional, default 8080)]\n");
    exit(1);
  } else if (argc == 2) {
    if (atoi(argv[1]) > 65535) {
      printf("Port should be an integer less than 65536.\n");
      exit(1);
    }
    strcpy(port, argv[1]);
  }
  serv_sock = passivesock(port, "tcp", 30);
  while (1) {
    cli_len = sizeof(cli_addr);
    cli_sock =
        accept(serv_sock, (struct sockaddr *)&cli_addr, (socklen_t *)&cli_len);
    if (cli_sock < 0) {
      if (errno == EINTR) {
        continue;
      }
      errexit("accept: %s\n", sys_errlist[errno]);
    }
    cpid = fork();
    if (cpid == -1) {
      exit(1);
    } else if (cpid == 0) {
      close(serv_sock);
      close(0);
      close(1);
      dup(cli_sock);
      dup(cli_sock);
      start();
      exit(0);
    } else {
      printf("Connection from %s:%d.\n", inet_ntoa(cli_addr.sin_addr),
             ntohs(cli_addr.sin_port));
      close(cli_sock);
    }
  }
  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)
mov    0xe14(%rip),%eax
mov    %eax,-0x2e(%rbp)
mov    0xe0e(%rip),%ax
mov    %ax,-0x2a(%rbp)
cmpl   $0x2,-0x8(%rbp)
jle    121c <main+0x4c>
lea    0xdfb(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
cmpl   $0x2,-0x8(%rbp)
jne    1267 <main+0x97>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10d0 <atoi@plt>
cmp    $0xffff,%eax
jle    1256 <main+0x86>
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
lea    -0x2e(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1030 <strcpy@plt>
jmp    126c <main+0x9c>
lea    -0x2e(%rbp),%rdi
lea    0xdeb(%rip),%rsi
mov    $0x1e,%edx
call   1050 <passivesock@plt>
mov    %eax,-0x18(%rbp)
movl   $0x8,-0x20(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    12ea <main+0x11a>
mov    0x2d29(%rip),%rax
mov    (%rax),%rax
mov    0x2d17(%rip),%rcx
cmp    (%rcx),%rax
jne    12c7 <main+0xf7>
jmp    1284 <main+0xb4>
mov    0x2d2a(%rip),%rax
mov    (%rax),%rax
mov    0x2d00(%rip),%rcx
mov    (%rcx),%rcx
mov    (%rax,%rcx,4),%esi
lea    0xd81(%rip),%rdi
call   1090 <errexit@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1308 <main+0x138>
mov    $0x1,%edi
call   10e0 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    1349 <main+0x179>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
xor    %edi,%edi
call   1080 <close@plt>
mov    $0x1,%edi
call   1080 <close@plt>
mov    -0x1c(%rbp),%edi
call   1070 <dup@plt>
mov    -0x1c(%rbp),%edi
call   1070 <dup@plt>
mov    $0x0,%al
call   10a0 <start@plt>
xor    %edi,%edi
call   10e0 <exit@plt>
mov    -0x24(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%edi
call   10b0 <ntohs@plt>
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
lea    0xd08(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
jmp    137e <main+0x1ae>
jmp    1284 <main+0xb4>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
movw   $0x0,0x8(%rsp)
movl   $0x30383038,0x4(%rsp)
30
cmp    $0x3,%edi
jl     120b <main+0x2b>
lea    0xe50(%rip),%rdi
jmp    122a <main+0x4a>
cmp    $0x2,%edi
jne    1246 <main+0x66>
mov    0x8(%rsi),%rbx
mov    %rbx,%rdi
call   10e0 <atoi@plt>
cmp    $0x10000,%eax
jl     1239 <main+0x59>
lea    0xe04(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
lea    0x4(%rsp),%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
lea    0xdb9(%rip),%rsi
lea    0x4(%rsp),%rdi
mov    $0x1e,%edx
call   1060 <passivesock@plt>
mov    %eax,%ebx
lea    0x10(%rsp),%r14
lea    0xc(%rsp),%r15
lea    0xda7(%rip),%r13
nop
movl   $0x8,0xc(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10d0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    12ba <main+0xda>
mov    0x2d46(%rip),%rax
mov    (%rax),%rax
mov    0x2d34(%rip),%rcx
cmp    (%rcx),%rax
je     1270 <main+0x90>
mov    0x2d50(%rip),%rcx
mov    (%rcx),%rcx
mov    (%rcx,%rax,4),%esi
lea    0xd55(%rip),%rdi
call   10a0 <errexit@plt>
xor    %eax,%eax
call   1100 <fork@plt>
test   %eax,%eax
je     12fa <main+0x11a>
cmp    $0xffffffff,%eax
je     132e <main+0x14e>
mov    0x14(%rsp),%edi
call   1050 <inet_ntoa@plt>
mov    %rax,%r12
mov    0x10(%rsp),%edi
call   10c0 <ntohs@plt>
mov    %r13,%rdi
mov    %r12,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <printf@plt>
mov    %ebp,%edi
call   1090 <close@plt>
jmp    1270 <main+0x90>
mov    %ebx,%edi
call   1090 <close@plt>
xor    %edi,%edi
call   1090 <close@plt>
mov    $0x1,%edi
call   1090 <close@plt>
mov    %ebp,%edi
call   1080 <dup@plt>
mov    %ebp,%edi
call   1080 <dup@plt>
xor    %eax,%eax
call   10b0 <start@plt>
xor    %edi,%edi
call   10f0 <exit@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@__const.main.port = [6 x i8] c"8080\00\00"
@.str = [48 x i8] c"Usage: ./httpd [port (optional, default 8080)]\0A\00"
@.str.1 = [44 x i8] c"Port should be an integer less than 65536.\0A\00"
@.str.2 = [4 x i8] c"tcp\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.3 = [12 x i8] c"accept: %s\0A\00"
@sys_errlist = global i32* null
@.str.4 = [24 x i8] c"Connection from %s:%d.\0A\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 i32
%10 = alloca %struct.sockaddr_in
%11 = alloca [6 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = bitcast [6 x i8]* %11 to i8*
%13 = load i32, i32* %4
%14 = icmp sgt i32 %13, 2
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i32, i32* %4
%20 = icmp eq i32 %19, 2
br i1 %20, label %21, label %36
21: ; preds = %18
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32 @atoi(i8* %24)
%26 = icmp sgt i32 %25, 65535
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %21
%31 = getelementptr inbounds [6 x i8], [6 x i8]* %11, i64 0, i64 0
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %33
%35 = call i32 @strcpy(i8* %31, i8* %34)
br label %36
36: ; preds = %30, %18
br label %37
37: ; preds = %36
%38 = getelementptr inbounds [6 x i8], [6 x i8]* %11, i64 0, i64 0
%39 = call i32 @passivesock(i8* %38, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 30)
store i32 %39, i32* %7
br label %40
40: ; preds = %37, %50, %88
store i32 8, i32* %9
%41 = load i32, i32* %7
%42 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%43 = call i32 @accept(i32 %41, %struct.sockaddr* %42, i32* %9)
store i32 %43, i32* %8
%44 = load i32, i32* %8
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %57
46: ; preds = %40
%47 = load i64, i64* @errno
%48 = load i64, i64* @EINTR
%49 = icmp eq i64 %47, %48
br i1 %49, label %50, label %51
50: ; preds = %46
br label %40
51: ; preds = %46
%52 = load i32*, i32** @sys_errlist
%53 = load i64, i64* @errno
%54 = getelementptr inbounds i32, i32* %52, i64 %53
%55 = load i32, i32* %54
%56 = call i32 @errexit(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %55)
br label %57
57: ; preds = %51, %40
%58 = call i32 (...) @fork()
store i32 %58, i32* %6
%59 = load i32, i32* %6
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %63
61: ; preds = %57
%62 = call i32 @exit(i32 1)
unreachable
63: ; preds = %57
%64 = load i32, i32* %6
%65 = icmp eq i32 %64, 0
br i1 %65, label %66, label %77
66: ; preds = %63
%67 = load i32, i32* %7
%68 = call i32 @close(i32 %67)
%69 = call i32 @close(i32 0)
%70 = call i32 @close(i32 1)
%71 = load i32, i32* %8
%72 = call i32 @dup(i32 %71)
%73 = load i32, i32* %8
%74 = call i32 @dup(i32 %73)
%75 = call i32 (...) @start()
%76 = call i32 @exit(i32 0)
unreachable
77: ; preds = %63
%78 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
%79 = load i32, i32* %78
%80 = call i8* @inet_ntoa(i32 %79)
%81 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%82 = load i32, i32* %81
%83 = call i32 @ntohs(i32 %82)
%84 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %80, i32 %83)
%85 = load i32, i32* %8
%86 = call i32 @close(i32 %85)
br label %87
87: ; preds = %77
br label %88
88: ; preds = %87
br label %40
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@__const.main.port = [6 x i8] c"8080\00\00"
@.str.2 = [4 x i8] c"tcp\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.3 = [12 x i8] c"accept: %s\0A\00"
@sys_errlist = global i32* null
@.str.4 = [24 x i8] c"Connection from %s:%d.\0A\00"
@str = [43 x i8] c"Port should be an integer less than 65536.\00"
@str.5 = [47 x i8] c"Usage: ./httpd [port (optional, default 8080)]\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca [6 x i8]
%6 = bitcast i32* %3 to i8*
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = getelementptr inbounds [6 x i8], [6 x i8]* %5, i64 0, i64 0
%9 = icmp sgt i32 %0, 2
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @str.5, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = icmp eq i32 %0, 2
br i1 %14, label %15, label %25
15: ; preds = %13
%16 = getelementptr inbounds i8*, i8** %1, i64 1
%17 = load i8*, i8** %16
%18 = tail call i32 @atoi(i8* %17)
%19 = icmp sgt i32 %18, 65535
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str, i64 0, i64 0))
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %15
%24 = call i32 @strcpy(i8* %8, i8* %17)
br label %25
25: ; preds = %13, %23
%26 = call i32 @passivesock(i8* %8, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 30)
%27 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
br label %30
30: ; preds = %61, %25
store i32 8, i32* %3
%31 = call i32 @accept(i32 %26, %struct.sockaddr* %27, i32* %3)
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %42
33: ; preds = %30
%34 = load i64, i64* @errno
%35 = load i64, i64* @EINTR
%36 = icmp eq i64 %34, %35
br i1 %36, label %61, label %37
37: ; preds = %33
%38 = load i32*, i32** @sys_errlist
%39 = getelementptr inbounds i32, i32* %38, i64 %34
%40 = load i32, i32* %39
%41 = call i32 @errexit(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 %40)
br label %42
42: ; preds = %37, %30
%43 = call i32 (...) @fork()
switch i32 %43, label %54 [
i32 -1, label %44
i32 0, label %46
]
44: ; preds = %42
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %42
%47 = call i32 @close(i32 %26)
%48 = call i32 @close(i32 0)
%49 = call i32 @close(i32 1)
%50 = call i32 @dup(i32 %31)
%51 = call i32 @dup(i32 %31)
%52 = call i32 (...) @start()
%53 = call i32 @exit(i32 0)
unreachable
54: ; preds = %42
%55 = load i32, i32* %28
%56 = call i8* @inet_ntoa(i32 %55)
%57 = load i32, i32* %29
%58 = call i32 @ntohs(i32 %57)
%59 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %56, i32 %58)
%60 = call i32 @close(i32 %31)
br label %61
61: ; preds = %54, %33
br label %30
}