#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 socklen_t;
typedef int /*<<< orphan*/ serv_addr;
typedef scalar_t__ pid_t;
typedef int /*<<< orphan*/ cli_addr;

/* 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, struct sockaddr *, int *);
int atoi(char *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ process(int, char const *);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int sockfd, newsockfd, portno;
  socklen_t clilen;
  char buffer[1024];
  struct sockaddr_in serv_addr, cli_addr;
  int n;
  const char *baseDirPath = (argc < 2) ? "/" : argv[1];
  portno = (argc < 3) ? 80 : atoi(argv[2]);
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0)
    error("ERROR opening socket");
  bzero((char *)&serv_addr, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = INADDR_ANY;
  serv_addr.sin_port = htons(portno);
  if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
    error("ERROR on binding");
  signal(SIGCHLD, SIG_IGN);
  while (1) {
    listen(sockfd, 5);
    clilen = sizeof(cli_addr);
    newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
    pid_t parent;
    if ((parent = fork()) >= 0) {
      if (parent) {
        close(newsockfd);
      } else {
        close(sockfd);
        process(newsockfd, baseDirPath);
        exit(0);
      }
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x470,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1206 <main+0x36>
lea    0xe06(%rip),%rax
mov    %rax,-0x460(%rbp)
jmp    1215 <main+0x45>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x460(%rbp)
mov    -0x460(%rbp),%rax
mov    %rax,-0x450(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    123d <main+0x6d>
mov    $0x50,%eax
mov    %eax,-0x464(%rbp)
jmp    1250 <main+0x80>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x464(%rbp)
mov    -0x464(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
mov    0x2d57(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    128b <main+0xbb>
lea    0xd7c(%rip),%rdi
call   1090 <error@plt>
lea    -0x430(%rbp),%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    0x2d45(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x428(%rbp)
mov    0x2d2e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x42c(%rbp)
mov    -0x1c(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x430(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    12f2 <main+0x122>
lea    0xd2a(%rip),%rdi
call   1090 <error@plt>
mov    0x2cdf(%rip),%rax
mov    (%rax),%edi
mov    0x2cce(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   1070 <listen@plt>
movl   $0xc,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x440(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   10b0 <accept@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %rax,-0x458(%rbp)
cmp    $0x0,%rax
jl     1389 <main+0x1b9>
cmpq   $0x0,-0x458(%rbp)
je     1366 <main+0x196>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
jmp    1384 <main+0x1b4>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
mov    -0x450(%rbp),%rsi
call   1040 <process@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
jmp    1389 <main+0x1b9>
jmp    1309 <main+0x139>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    $0x50,%r15d
cmp    $0x2,%edi
jge    11f0 <main+0x20>
lea    0xe12(%rip),%r14
jmp    1202 <main+0x32>
mov    0x8(%rsi),%r14
je     1202 <main+0x32>
mov    0x10(%rsi),%rdi
call   10c0 <atoi@plt>
mov    %eax,%r15d
mov    0x2ddf(%rip),%r12
mov    (%r12),%edi
mov    0x2dac(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    122f <main+0x5f>
lea    0xdd8(%rip),%rdi
call   1090 <error@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    (%r12),%eax
mov    %eax,0x18(%rsp)
mov    0x2d90(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    %r15d,%edi
call   1030 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %rax,%rax
jns    1282 <main+0xb2>
lea    0xd9a(%rip),%rdi
call   1090 <error@plt>
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
mov    0x2d3e(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
lea    0x20(%rsp),%r15
lea    0xc(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebp,%edi
mov    $0x5,%esi
call   1070 <listen@plt>
movl   $0xc,0xc(%rsp)
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   10b0 <accept@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   10e0 <fork@plt>
test   %rax,%rax
js     12b0 <main+0xe0>
je     12ea <main+0x11a>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    12b0 <main+0xe0>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1040 <process@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [2 x i8] c"/\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [21 x i8] c"ERROR opening socket\00"
@INADDR_ANY = global i32 0
@.str.2 = [17 x i8] c"ERROR on binding\00"
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
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 [1024 x i8]
%11 = alloca %struct.sockaddr_in
%12 = alloca %struct.sockaddr_in
%13 = alloca i32
%14 = alloca i8*
%15 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = load i32, i32* %4
%17 = icmp slt i32 %16, 2
br i1 %17, label %18, label %19
18: ; preds = %2
br label %23
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
br label %23
23: ; preds = %19, %18
%24 = phi i8* [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %18 ], [ %22, %19 ]
store i8* %24, i8** %14
%25 = load i32, i32* %4
%26 = icmp slt i32 %25, 3
br i1 %26, label %27, label %28
27: ; preds = %23
br label %33
28: ; preds = %23
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 2
%31 = load i8*, i8** %30
%32 = call i32 @atoi(i8* %31)
br label %33
33: ; preds = %28, %27
%34 = phi i32 [ 80, %27 ], [ %32, %28 ]
store i32 %34, i32* %8
%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* %6
%38 = load i32, i32* %6
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %33
%41 = call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %42
42: ; preds = %40, %33
%43 = bitcast %struct.sockaddr_in* %11 to i8*
%44 = call i32 @bzero(i8* %43, i32 12)
%45 = load i32, i32* @AF_INET
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %45, i32* %46
%47 = load i32, i32* @INADDR_ANY
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
%49 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %48, i32 0, i32 0
store i32 %47, i32* %49
%50 = load i32, i32* %8
%51 = call i32 @htons(i32 %50)
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
store i32 %51, i32* %52
%53 = load i32, i32* %6
%54 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%55 = call i64 @bind(i32 %53, %struct.sockaddr* %54, i32 12)
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %42
%58 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %59
59: ; preds = %57, %42
%60 = load i32, i32* @SIGCHLD
%61 = load i32, i32* @SIG_IGN
%62 = call i32 @signal(i32 %60, i32 %61)
br label %63
63: ; preds = %59, %85
%64 = load i32, i32* %6
%65 = call i32 @listen(i32 %64, i32 5)
store i32 12, i32* %9
%66 = load i32, i32* %6
%67 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%68 = call i32 @accept(i32 %66, %struct.sockaddr* %67, i32* %9)
store i32 %68, i32* %7
%69 = call i64 (...) @fork()
store i64 %69, i64* %15
%70 = icmp sge i64 %69, 0
br i1 %70, label %71, label %85
71: ; preds = %63
%72 = load i64, i64* %15
%73 = icmp ne i64 %72, 0
br i1 %73, label %74, label %77
74: ; preds = %71
%75 = load i32, i32* %7
%76 = call i32 @close(i32 %75)
br label %84
77: ; preds = %71
%78 = load i32, i32* %6
%79 = call i32 @close(i32 %78)
%80 = load i32, i32* %7
%81 = load i8*, i8** %14
%82 = call i32 @process(i32 %80, i8* %81)
%83 = call i32 @exit(i32 0)
unreachable
84: ; preds = %74
br label %85
85: ; preds = %84, %63
br label %63
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [2 x i8] c"/\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [21 x i8] c"ERROR opening socket\00"
@INADDR_ANY = global i32 0
@.str.2 = [17 x i8] c"ERROR on binding\00"
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_in
%6 = bitcast i32* %3 to i8*
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = bitcast %struct.sockaddr_in* %5 to i8*
%9 = icmp slt i32 %0, 2
br i1 %9, label %18, label %10
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = icmp eq i32 %0, 2
br i1 %13, label %18, label %14
14: ; preds = %10
%15 = getelementptr inbounds i8*, i8** %1, i64 2
%16 = load i8*, i8** %15
%17 = tail call i32 @atoi(i8* %16)
br label %18
18: ; preds = %2, %10, %14
%19 = phi i8* [ %12, %14 ], [ %12, %10 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %2 ]
%20 = phi i32 [ %17, %14 ], [ 80, %10 ], [ 80, %2 ]
%21 = load i32, i32* @AF_INET
%22 = load i32, i32* @SOCK_STREAM
%23 = tail call i32 @socket(i32 %21, i32 %22, i32 0)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %18
%26 = tail call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %25, %18
%28 = call i32 @bzero(i8* %7, i32 12)
%29 = load i32, i32* @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %29, i32* %30
%31 = load i32, i32* @INADDR_ANY
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %31, i32* %32
%33 = tail call i32 @htons(i32 %20)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %33, i32* %34
%35 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%36 = call i64 @bind(i32 %23, %struct.sockaddr* %35, i32 12)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %27
%39 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %40
40: ; preds = %38, %27
%41 = load i32, i32* @SIGCHLD
%42 = load i32, i32* @SIG_IGN
%43 = call i32 @signal(i32 %41, i32 %42)
%44 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
br label %45
45: ; preds = %54, %40
%46 = call i32 @listen(i32 %23, i32 5)
store i32 12, i32* %3
%47 = call i32 @accept(i32 %23, %struct.sockaddr* %44, i32* %3)
%48 = call i64 (...) @fork()
%49 = icmp sgt i64 %48, -1
br i1 %49, label %50, label %54
50: ; preds = %45
%51 = icmp eq i64 %48, 0
br i1 %51, label %55, label %52
52: ; preds = %50
%53 = call i32 @close(i32 %47)
br label %54
54: ; preds = %52, %45
br label %45
55: ; preds = %50
%56 = call i32 @close(i32 %23)
%57 = call i32 @process(i32 %47, i8* %19)
%58 = call i32 @exit(i32 0)
unreachable
}