#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*/ server_addr;
typedef int /*<<< orphan*/ client_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PANIC(char *);
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, void *);
int atoi(char *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int const);
int /*<<< orphan*/ dup2(int const, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ http(struct sockaddr_in *);
char *inet_ntoa(TYPE_1__);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int const, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ wait(int *);
int main(int argc, char *argv[]) {
  int sd;
  int port;
  int new_fd;
  int len;
  int state;
  struct sockaddr_in server_addr, client_addr;
  if (argc > 1)
    port = atoi(argv[1]);
  else
    port = 80;
  sd = socket(PF_INET, SOCK_STREAM, 0);
  memset(&server_addr, 0, sizeof(server_addr));
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(port);
  server_addr.sin_addr.s_addr = INADDR_ANY;
  if (bind(sd, (struct sockaddr *)&server_addr, sizeof(server_addr)) != 0)
    PANIC("Bind");
  if (listen(sd, 20) != 0)
    PANIC("Listen");
  while (1) {
    len = sizeof(client_addr);
    new_fd = accept(sd, (struct sockaddr *)&client_addr, (void *)&len);
    const int nfd = new_fd;
    printf("%d,Client from %s:%d\n", nfd, inet_ntoa(client_addr.sin_addr),
           ntohs(client_addr.sin_port));
    if (new_fd < 0)
      perror("Accept");
    else {
      if (!fork()) {
        close(sd);
        dup2(nfd, 0);
        dup2(nfd, 1);
        dup2(nfd, 2);
        close(nfd);
        http(&client_addr);
        wait(&state);
        exit(0);
      }
      close(nfd);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    1255 <main+0x35>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1100 <atoi@plt>
mov    %eax,-0x18(%rbp)
jmp    125c <main+0x3c>
movl   $0x50,-0x18(%rbp)
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
mov    0x2d64(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1140 <socket@plt>
mov    %eax,-0x14(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1090 <memset@plt>
mov    0x2d59(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    0x2d3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <bind@plt>
cmp    $0x0,%rax
je     12d2 <main+0xb2>
lea    0xd33(%rip),%rdi
call   1080 <PANIC@plt>
mov    -0x14(%rbp),%edi
mov    $0x14,%esi
call   10b0 <listen@plt>
cmp    $0x0,%rax
je     12f5 <main+0xd5>
lea    0xd15(%rip),%rdi
call   1080 <PANIC@plt>
jmp    12fa <main+0xda>
movl   $0xc,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   10f0 <accept@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x3c(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,-0x50(%rbp)
mov    -0x40(%rbp),%edi
call   10c0 <ntohs@plt>
mov    -0x54(%rbp),%esi
mov    -0x50(%rbp),%rdx
mov    %eax,%ecx
lea    0xcc8(%rip),%rdi
call   1070 <printf@plt>
cmpl   $0x0,-0x1c(%rbp)
jge    1364 <main+0x144>
lea    0xcc8(%rip),%rdi
call   10e0 <perror@plt>
jmp    13c9 <main+0x1a9>
mov    $0x0,%al
call   1130 <fork@plt>
cmp    $0x0,%eax
jne    13c1 <main+0x1a1>
mov    -0x14(%rbp),%edi
call   10a0 <close@plt>
mov    -0x44(%rbp),%edi
xor    %esi,%esi
call   1060 <dup2@plt>
mov    -0x44(%rbp),%edi
mov    $0x1,%esi
call   1060 <dup2@plt>
mov    -0x44(%rbp),%edi
mov    $0x2,%esi
call   1060 <dup2@plt>
mov    -0x44(%rbp),%edi
call   10a0 <close@plt>
lea    -0x40(%rbp),%rdi
call   1040 <http@plt>
lea    -0x24(%rbp),%rdi
call   1120 <wait@plt>
xor    %edi,%edi
call   1110 <exit@plt>
mov    -0x44(%rbp),%edi
call   10a0 <close@plt>
jmp    12fa <main+0xda>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    $0x50,%ebp
cmp    $0x2,%edi
jl     1243 <main+0x23>
mov    0x8(%rsi),%rdi
call   1100 <atoi@plt>
mov    %eax,%ebp
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
mov    0x2d7d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1140 <socket@plt>
mov    %eax,%ebx
lea    0x18(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1090 <memset@plt>
mov    0x2d6f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    %ebp,%edi
call   1050 <htons@plt>
mov    %eax,0x18(%rsp)
mov    0x2d4f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10d0 <bind@plt>
test   %rax,%rax
je     12b7 <main+0x97>
lea    0xd4e(%rip),%rdi
call   1080 <PANIC@plt>
mov    %ebx,%edi
mov    $0x14,%esi
call   10b0 <listen@plt>
test   %rax,%rax
je     12d4 <main+0xb4>
lea    0xd36(%rip),%rdi
call   1080 <PANIC@plt>
lea    0x8(%rsp),%r14
lea    0x4(%rsp),%r15
lea    0xd27(%rip),%r12
jmp    1300 <main+0xe0>
lea    0xd34(%rip),%rdi
call   10e0 <perror@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movl   $0xc,0x4(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10f0 <accept@plt>
mov    %eax,%ebp
mov    0xc(%rsp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%r13
mov    0x8(%rsp),%edi
call   10c0 <ntohs@plt>
mov    %r12,%rdi
mov    %ebp,%esi
mov    %r13,%rdx
mov    %eax,%ecx
call   1070 <printf@plt>
test   %ebp,%ebp
js     12e7 <main+0xc7>
xor    %eax,%eax
call   1130 <fork@plt>
test   %eax,%eax
je     1353 <main+0x133>
mov    %ebp,%edi
call   10a0 <close@plt>
jmp    1300 <main+0xe0>
mov    %ebx,%edi
call   10a0 <close@plt>
mov    %ebp,%edi
xor    %esi,%esi
call   1060 <dup2@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   1060 <dup2@plt>
mov    %ebp,%edi
mov    $0x2,%esi
call   1060 <dup2@plt>
mov    %ebp,%edi
call   10a0 <close@plt>
lea    0x8(%rsp),%rdi
call   1040 <http@plt>
lea    0x24(%rsp),%rdi
call   1120 <wait@plt>
xor    %edi,%edi
call   1110 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str = [5 x i8] c"Bind\00"
@.str.1 = [7 x i8] c"Listen\00"
@.str.2 = [22 x i8] c"%d,Client from %s:%d\0A\00"
@.str.3 = [7 x i8] c"Accept\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 i32
%11 = alloca %struct.sockaddr_in
%12 = alloca %struct.sockaddr_in
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp sgt i32 %14, 1
br i1 %15, label %16, label %21
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32 @atoi(i8* %19)
store i32 %20, i32* %7
br label %22
21: ; preds = %2
store i32 80, i32* %7
br label %22
22: ; preds = %21, %16
%23 = load i32, i32* @PF_INET
%24 = load i32, i32* @SOCK_STREAM
%25 = call i32 @socket(i32 %23, i32 %24, i32 0)
store i32 %25, i32* %6
%26 = call i32 @memset(%struct.sockaddr_in* %11, i32 0, i32 12)
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %27, i32* %28
%29 = load i32, i32* %7
%30 = call i32 @htons(i32 %29)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
store i32 %30, i32* %31
%32 = load i32, i32* @INADDR_ANY
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
%34 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %33, i32 0, i32 0
store i32 %32, i32* %34
%35 = load i32, i32* %6
%36 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%37 = call i64 @bind(i32 %35, %struct.sockaddr* %36, i32 12)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %22
%40 = call i32 @PANIC(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %41
41: ; preds = %39, %22
%42 = load i32, i32* %6
%43 = call i64 @listen(i32 %42, i32 20)
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %41
%46 = call i32 @PANIC(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %47
47: ; preds = %45, %41
br label %48
48: ; preds = %47, %87
store i32 12, i32* %9
%49 = load i32, i32* %6
%50 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%51 = bitcast i32* %9 to i8*
%52 = call i32 @accept(i32 %49, %struct.sockaddr* %50, i8* %51)
store i32 %52, i32* %8
%53 = load i32, i32* %8
store i32 %53, i32* %13
%54 = load i32, i32* %13
%55 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 1
%56 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %55, i32 0, i32 0
%57 = load i32, i32* %56
%58 = call i8* @inet_ntoa(i32 %57)
%59 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 0
%60 = load i32, i32* %59
%61 = call i32 @ntohs(i32 %60)
%62 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %54, i8* %58, i32 %61)
%63 = load i32, i32* %8
%64 = icmp slt i32 %63, 0
br i1 %64, label %65, label %67
65: ; preds = %48
%66 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %87
67: ; preds = %48
%68 = call i32 (...) @fork()
%69 = icmp ne i32 %68, 0
br i1 %69, label %84, label %70
70: ; preds = %67
%71 = load i32, i32* %6
%72 = call i32 @close(i32 %71)
%73 = load i32, i32* %13
%74 = call i32 @dup2(i32 %73, i32 0)
%75 = load i32, i32* %13
%76 = call i32 @dup2(i32 %75, i32 1)
%77 = load i32, i32* %13
%78 = call i32 @dup2(i32 %77, i32 2)
%79 = load i32, i32* %13
%80 = call i32 @close(i32 %79)
%81 = call i32 @http(%struct.sockaddr_in* %12)
%82 = call i32 @wait(i32* %10)
%83 = call i32 @exit(i32 0)
unreachable
84: ; preds = %67
%85 = load i32, i32* %13
%86 = call i32 @close(i32 %85)
br label %87
87: ; preds = %84, %65
br label %48
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str = [5 x i8] c"Bind\00"
@.str.1 = [7 x i8] c"Listen\00"
@.str.2 = [22 x i8] c"%d,Client from %s:%d\0A\00"
@.str.3 = [7 x i8] c"Accept\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca %struct.sockaddr_in
%7 = bitcast i32* %3 to i8*
%8 = bitcast i32* %4 to i8*
%9 = bitcast %struct.sockaddr_in* %5 to i8*
%10 = bitcast %struct.sockaddr_in* %6 to i8*
%11 = icmp sgt i32 %0, 1
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = tail call i32 @atoi(i8* %14)
br label %16
16: ; preds = %2, %12
%17 = phi i32 [ %15, %12 ], [ 80, %2 ]
%18 = load i32, i32* @PF_INET
%19 = load i32, i32* @SOCK_STREAM
%20 = tail call i32 @socket(i32 %18, i32 %19, i32 0)
%21 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 12)
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %22, i32* %23
%24 = call i32 @htons(i32 %17)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i32 %24, i32* %25
%26 = load i32, i32* @INADDR_ANY
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1, i32 0
store i32 %26, i32* %27
%28 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%29 = call i64 @bind(i32 %20, %struct.sockaddr* %28, i32 12)
%30 = icmp eq i64 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %16
%32 = call i32 @PANIC(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %33
33: ; preds = %31, %16
%34 = call i64 @listen(i32 %20, i32 20)
%35 = icmp eq i64 %34, 0
br i1 %35, label %38, label %36
36: ; preds = %33
%37 = call i32 @PANIC(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %38
38: ; preds = %36, %33
%39 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1, i32 0
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0
br label %42
42: ; preds = %66, %38
store i32 12, i32* %3
%43 = call i32 @accept(i32 %20, %struct.sockaddr* %39, i8* %7)
%44 = load i32, i32* %40
%45 = call i8* @inet_ntoa(i32 %44)
%46 = load i32, i32* %41
%47 = call i32 @ntohs(i32 %46)
%48 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i32 %43, i8* %45, i32 %47)
%49 = icmp slt i32 %43, 0
br i1 %49, label %50, label %52
50: ; preds = %42
%51 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %66
52: ; preds = %42
%53 = call i32 (...) @fork()
%54 = icmp eq i32 %53, 0
br i1 %54, label %55, label %64
55: ; preds = %52
%56 = call i32 @close(i32 %20)
%57 = call i32 @dup2(i32 %43, i32 0)
%58 = call i32 @dup2(i32 %43, i32 1)
%59 = call i32 @dup2(i32 %43, i32 2)
%60 = call i32 @close(i32 %43)
%61 = call i32 @http(%struct.sockaddr_in* %6)
%62 = call i32 @wait(i32* %4)
%63 = call i32 @exit(i32 0)
unreachable
64: ; preds = %52
%65 = call i32 @close(i32 %43)
br label %66
66: ; preds = %64, %50
br label %42
}