#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;
};
typedef int /*<<< orphan*/ serv_addr;
typedef int /*<<< orphan*/ cli_addr;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
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*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ proccess_request(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  int sock_fd;
  int new_sock_fd;
  FILE *client;
  int port;
  int cli_len;
  int pid;
  struct sockaddr_in serv_addr;
  struct sockaddr_in cli_addr;
  char buffer[256];
  if (argc < 2) {
    fprintf(stderr, "ERROR, no port provided. usage: %s [port]\n", argv[0]);
    exit(1);
  }
  sock_fd = socket(AF_INET, SOCK_STREAM, 0);
  if (sock_fd < 0) {
    fprintf(stderr, "ERROR, opening port\n");
    exit(1);
  }
  bzero((char *)&serv_addr, sizeof(serv_addr));
  port = atoi(argv[1]);
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_port = htons(port);
  serv_addr.sin_addr.s_addr = INADDR_ANY;
  if (bind(sock_fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
    fprintf(stderr, "ERROR, unable to bind port\n");
    exit(1);
  }
  listen(sock_fd, 1);
  cli_len = sizeof(cli_addr);
  while (1) {
    new_sock_fd = accept(sock_fd, (struct sockaddr *)&cli_addr, &cli_len);
    if (new_sock_fd < 0) {
      fprintf(stderr, "ERROR, accepting client request\n");
      exit(1);
    }
    pid = fork();
    if (pid < 0) {
      fprintf(stderr, "ERROR, cant fork\n");
      exit(1);
    }
    if (pid == 0) {
      proccess_request(new_sock_fd);
    }
    close(new_sock_fd);
  }
  close(sock_fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    120b <main+0x4b>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe06(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
mov    0x2dad(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1252 <main+0x92>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
lea    0xdea(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    -0x38(%rbp),%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x24(%rbp)
mov    0x2d71(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x24(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x34(%rbp)
mov    0x2d52(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
jge    12cf <main+0x10f>
mov    0x2d1b(%rip),%rax
mov    (%rax),%edi
lea    0xd82(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1060 <listen@plt>
movl   $0xc,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x48(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1090 <accept@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1321 <main+0x161>
mov    0x2cc9(%rip),%rax
mov    (%rax),%edi
lea    0xd4c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1356 <main+0x196>
mov    0x2c94(%rip),%rax
mov    (%rax),%edi
lea    0xd38(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
cmpl   $0x0,-0x2c(%rbp)
jne    1368 <main+0x1a8>
mov    -0x18(%rbp),%edi
call   10c0 <proccess_request@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
jmp    12e3 <main+0x123>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbp
cmp    $0x1,%edi
jg     11f7 <main+0x37>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
mov    0x0(%rbp),%rdx
lea    0xe1a(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2dea(%rip),%r15
mov    (%r15),%edi
mov    0x2dc0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
test   %eax,%eax
jns    122a <main+0x6a>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
lea    0xe06(%rip),%rsi
jmp    1320 <main+0x160>
mov    %eax,%ebx
lea    0x8(%rsp),%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    0x8(%rbp),%rdi
call   10a0 <atoi@plt>
mov    (%r15),%ecx
mov    %ecx,0x10(%rsp)
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2d80(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %rax,%rax
jns    128f <main+0xcf>
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
lea    0xdb6(%rip),%rsi
jmp    1320 <main+0x160>
mov    %ebx,%edi
mov    $0x1,%esi
call   1060 <listen@plt>
movl   $0xc,0x4(%rsp)
lea    0x18(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   1090 <accept@plt>
test   %eax,%eax
jns    12ca <main+0x10a>
mov    0x2d11(%rip),%rax
mov    (%rax),%edi
lea    0xd94(%rip),%rsi
jmp    1320 <main+0x160>
mov    %eax,%ebp
lea    0x18(%rsp),%r14
lea    0x4(%rsp),%r15
jmp    12fa <main+0x13a>
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
call   1040 <close@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1090 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
js     12b8 <main+0xf8>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
js     1310 <main+0x150>
jne    12e0 <main+0x120>
mov    %ebp,%edi
call   10c0 <proccess_request@plt>
jmp    12e0 <main+0x120>
mov    0x2cb9(%rip),%rax
mov    (%rax),%edi
lea    0xd5d(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [43 x i8] c"ERROR, no port provided. usage: %s [port]\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [21 x i8] c"ERROR, opening port\0A\00"
@INADDR_ANY = global i32 0
@.str.2 = [28 x i8] c"ERROR, unable to bind port\0A\00"
@.str.3 = [33 x i8] c"ERROR, accepting client request\0A\00"
@.str.4 = [18 x i8] c"ERROR, cant fork\0A\00"
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 %10, label %15
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i8*, i8** %1
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = load i32, i32* @AF_INET
%17 = load i32, i32* @SOCK_STREAM
%18 = tail call i32 @socket(i32 %16, i32 %17, i32 0)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %15
%21 = load i32, i32* @stderr
%22 = tail call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %15
%25 = call i32 @bzero(i8* %7, i32 12)
%26 = getelementptr inbounds i8*, i8** %1, i64 1
%27 = load i8*, i8** %26
%28 = tail call i32 @atoi(i8* %27)
%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 = tail call i32 @htons(i32 %28)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %31, i32* %32
%33 = load i32, i32* @INADDR_ANY
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
store i32 %33, i32* %34
%35 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%36 = call i64 @bind(i32 %18, %struct.sockaddr* %35, i32 12)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %24
%39 = load i32, i32* @stderr
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %24
%43 = call i32 @listen(i32 %18, i32 1)
store i32 12, i32* %3
%44 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%45 = call i32 @accept(i32 %18, %struct.sockaddr* %44, i32* %3)
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %63, %42
%48 = load i32, i32* @stderr
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %42, %63
%52 = phi i32 [ %65, %63 ], [ %45, %42 ]
%53 = call i32 (...) @fork()
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %59
55: ; preds = %51
%56 = load i32, i32* @stderr
%57 = call i32 (i32, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%58 = call i32 @exit(i32 1)
unreachable
59: ; preds = %51
%60 = icmp eq i32 %53, 0
br i1 %60, label %61, label %63
61: ; preds = %59
%62 = call i32 @proccess_request(i32 %52)
br label %63
63: ; preds = %61, %59
%64 = call i32 @close(i32 %52)
%65 = call i32 @accept(i32 %18, %struct.sockaddr* %44, i32* %3)
%66 = icmp slt i32 %65, 0
br i1 %66, label %47, label %51
}