#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 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*/ connection(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 socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  int sockfd, newsockfd, portno, pid;
  socklen_t clilen;
  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");
  listen(sockfd, 5);
  clilen = sizeof(cli_addr);
  while (1) {
    newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
    if (newsockfd < 0)
      error("ERROR on accept");
    pid = fork();
    if (pid < 0)
      error("ERROR on fork");
    if (pid == 0) {
      close(sockfd);
      connection(newsockfd);
      exit(0);
    } else
      close(newsockfd);
  }
  close(sockfd);
  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    120f <main+0x3f>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
lea    0xe00(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1241 <main+0x71>
lea    0xddd(%rip),%rdi
call   1090 <error@plt>
lea    -0x30(%rbp),%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d82(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d6e(%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   1080 <bind@plt>
cmp    $0x0,%rax
jge    12a9 <main+0xd9>
lea    0xd8a(%rip),%rdi
call   1090 <error@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   1070 <listen@plt>
movl   $0xc,-0x24(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
lea    -0x24(%rbp),%rdx
call   10b0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12e6 <main+0x116>
lea    0xd5e(%rip),%rdi
call   1090 <error@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1306 <main+0x136>
lea    0xd4e(%rip),%rdi
call   1090 <error@plt>
cmpl   $0x0,-0x20(%rbp)
jne    1327 <main+0x157>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
call   1030 <connection@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
jmp    12bd <main+0xed>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
cmp    $0x1,%edi
jg     1202 <main+0x32>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
lea    0xe0d(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    %rsi,%rbp
mov    0x2ddc(%rip),%r15
mov    (%r15),%edi
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1231 <main+0x61>
lea    0xded(%rip),%rdi
call   1090 <error@plt>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    0x8(%rbp),%rdi
call   10c0 <atoi@plt>
mov    (%r15),%ecx
mov    %ecx,0x10(%rsp)
mov    0x2d86(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0xc(%rsp)
mov    %eax,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %rax,%rax
jns    128b <main+0xbb>
lea    0xda8(%rip),%rdi
call   1090 <error@plt>
mov    %ebx,%edi
mov    $0x5,%esi
call   1070 <listen@plt>
movl   $0xc,0x4(%rsp)
lea    0x18(%rsp),%r14
lea    0x4(%rsp),%r15
lea    0xd8f(%rip),%r12
lea    0xd98(%rip),%r13
jmp    12cf <main+0xff>
nopl   0x0(%rax)
mov    %r13,%rdi
call   1090 <error@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10b0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    12ea <main+0x11a>
mov    %r12,%rdi
call   1090 <error@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
js     12c0 <main+0xf0>
jne    12c8 <main+0xf8>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebp,%edi
call   1030 <connection@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 }
@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"ERROR on accept\00"
@.str.4 = [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 = load i32, i32* %6
%50 = call i32 @listen(i32 %49, i32 5)
store i32 12, i32* %10
br label %51
51: ; preds = %48, %77
%52 = load i32, i32* %6
%53 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%54 = call i32 @accept(i32 %52, %struct.sockaddr* %53, i32* %10)
store i32 %54, i32* %7
%55 = load i32, i32* %7
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %51
%58 = call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %59
59: ; preds = %57, %51
%60 = call i32 (...) @fork()
store i32 %60, i32* %9
%61 = load i32, i32* %9
%62 = icmp slt i32 %61, 0
br i1 %62, label %63, label %65
63: ; preds = %59
%64 = call i32 @error(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %65
65: ; preds = %63, %59
%66 = load i32, i32* %9
%67 = icmp eq i32 %66, 0
br i1 %67, label %68, label %74
68: ; preds = %65
%69 = load i32, i32* %6
%70 = call i32 @close(i32 %69)
%71 = load i32, i32* %7
%72 = call i32 @connection(i32 %71)
%73 = call i32 @exit(i32 0)
unreachable
74: ; preds = %65
%75 = load i32, i32* %7
%76 = call i32 @close(i32 %75)
br label %77
77: ; preds = %74
br label %51
}

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.3 = [16 x i8] c"ERROR on accept\00"
@.str.4 = [14 x i8] c"ERROR on fork\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 @listen(i32 %17, i32 5)
store i32 12, i32* %3
%39 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
br label %40
40: ; preds = %56, %37
%41 = call i32 @accept(i32 %17, %struct.sockaddr* %39, i32* %3)
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %40
%44 = call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %45
45: ; preds = %43, %40
%46 = call i32 (...) @fork()
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %45
%49 = call i32 @error(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %56
50: ; preds = %45
%51 = icmp eq i32 %46, 0
br i1 %51, label %52, label %56
52: ; preds = %50
%53 = call i32 @close(i32 %17)
%54 = call i32 @connection(i32 %41)
%55 = call i32 @exit(i32 0)
unreachable
56: ; preds = %48, %50
%57 = call i32 @close(i32 %41)
br label %40
}