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

/* 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*/ error(char *);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ readfromclient(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  int sockfd, newsockfd, portno, clilen, pid;
  struct sockaddr_in serv_addr, cli_addr;
  if (argc < 2) {
    fprintf(stderr, "ERROR, no port provided\n");
    exit(1);
  }
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) {
    error("ERROR opening socket");
  }
  bzero((char *)&serv_addr, sizeof(serv_addr));
  portno = atoi(argv[1]);
  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");
  }
  printf("Server started\n");
  while (1) {
    listen(sockfd, 5);
    clilen = sizeof(cli_addr);
    newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
    if (newsockfd < 0) {
      error("ERROR on accept");
    }
    pid = fork();
    if (pid < 0) {
      error("ERROR on fork");
    } else if (pid == 0) {
      close(sockfd);
      readfromclient(newsockfd);
    } else {
      close(newsockfd);
    }
  }
  printf("Server closed\n");
  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)
cmpl   $0x2,-0x8(%rbp)
jge    121f <main+0x3f>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
lea    0xdf0(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1251 <main+0x71>
lea    0xdcd(%rip),%rdi
call   10a0 <error@plt>
lea    -0x30(%rbp),%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10d0 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d72(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d5e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    12b9 <main+0xd9>
lea    0xd7a(%rip),%rdi
call   10a0 <error@plt>
lea    0xd7f(%rip),%rdi
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   1080 <listen@plt>
movl   $0xc,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1302 <main+0x122>
lea    0xd52(%rip),%rdi
call   10a0 <error@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1327 <main+0x147>
lea    0xd42(%rip),%rdi
call   10a0 <error@plt>
jmp    1353 <main+0x173>
cmpl   $0x0,-0x24(%rbp)
jne    1346 <main+0x166>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%edi
call   1030 <readfromclient@plt>
jmp    134e <main+0x16e>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
jmp    1353 <main+0x173>
jmp    12c5 <main+0xe5>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
cmp    $0x1,%edi
jle    1310 <main+0x130>
mov    %rsi,%r14
mov    0x2de7(%rip),%r15
mov    (%r15),%edi
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1226 <main+0x46>
lea    0xdf8(%rip),%rdi
call   10a0 <error@plt>
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x8(%r14),%rdi
call   10d0 <atoi@plt>
mov    (%r15),%ecx
mov    %ecx,0x10(%rsp)
mov    0x2d91(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0xc(%rsp)
mov    %eax,%edi
call   1050 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %rax,%rax
jns    1280 <main+0xa0>
lea    0xdb3(%rip),%rdi
call   10a0 <error@plt>
lea    0xdd6(%rip),%rdi
call   1040 <puts@plt>
lea    0x18(%rsp),%r14
lea    0x4(%rsp),%r15
lea    0xda2(%rip),%r12
lea    0xdab(%rip),%r13
jmp    12b8 <main+0xd8>
cs nopw 0x0(%rax,%rax,1)
mov    %r13,%rdi
call   10a0 <error@plt>
mov    %ebx,%edi
mov    $0x5,%esi
call   1080 <listen@plt>
movl   $0xc,0x4(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10c0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    12e7 <main+0x107>
mov    %r12,%rdi
call   10a0 <error@plt>
xor    %eax,%eax
call   10f0 <fork@plt>
test   %eax,%eax
js     12b0 <main+0xd0>
je     1300 <main+0x120>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    12b8 <main+0xd8>
nopl   (%rax)
mov    %ebx,%edi
call   1060 <close@plt>
mov    %ebp,%edi
call   1030 <readfromclient@plt>
jmp    12b8 <main+0xd8>
mov    0x2cb9(%rip),%rax
mov    (%rax),%edi
lea    0xce0(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

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

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