#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);
char *calloc(int, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int sockfd, newsockfd, portno;
  socklen_t clilen;
  char *buffer = calloc(256, sizeof(char));
  struct sockaddr_in serv_addr, cli_addr;
  int n;
  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);
    printf("Waiting for a new connection\n");
    if (newsockfd < 0) {
      error("ERROR on accept");
    }
    n = write(newsockfd, "Hello World!", 12);
    if (n < 0) {
      error("ERROR writing to socket");
    }
    close(newsockfd);
  }
  close(sockfd);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x100,%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,-0x28(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1232 <main+0x52>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
lea    0xddd(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10f0 <exit@plt>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
mov    0x2d86(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1264 <main+0x84>
lea    0xdba(%rip),%rdi
call   10b0 <error@plt>
lea    -0x38(%rbp),%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10e0 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d5f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d4b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x38(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    12cc <main+0xec>
lea    0xd67(%rip),%rdi
call   10b0 <error@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   1090 <listen@plt>
movl   $0xc,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x48(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   10d0 <accept@plt>
mov    %eax,-0x18(%rbp)
lea    0xd45(%rip),%rdi
call   1050 <printf@plt>
cmpl   $0x0,-0x18(%rbp)
jge    1315 <main+0x135>
lea    0xd4d(%rip),%rdi
call   10b0 <error@plt>
mov    -0x18(%rbp),%edi
lea    0xd4e(%rip),%rsi
mov    $0xc,%edx
call   1030 <write@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jge    1342 <main+0x162>
lea    0xd3d(%rip),%rdi
call   10b0 <error@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
jmp    12e0 <main+0x100>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
cmp    $0x1,%edi
jle    12f3 <main+0x123>
mov    %rsi,%rbp
mov    0x2df7(%rip),%r15
mov    (%r15),%edi
mov    0x2dcd(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1216 <main+0x46>
lea    0xe08(%rip),%rdi
call   10a0 <error@plt>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x8(%rbp),%rdi
call   10d0 <atoi@plt>
mov    (%r15),%ecx
mov    %ecx,0x10(%rsp)
mov    0x2da1(%rip),%rcx
mov    (%rcx),%ecx
mov    %ecx,0xc(%rsp)
mov    %eax,%edi
call   1050 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %rax,%rax
jns    1270 <main+0xa0>
lea    0xdc3(%rip),%rdi
call   10a0 <error@plt>
mov    %ebx,%edi
mov    $0x5,%esi
call   1080 <listen@plt>
movl   $0xc,0x4(%rsp)
lea    0x18(%rsp),%r14
lea    0x4(%rsp),%r15
lea    0xddf(%rip),%r12
lea    0xdb3(%rip),%rbp
jmp    12a8 <main+0xd8>
xchg   %ax,%ax
mov    %r13d,%edi
call   1060 <close@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10c0 <accept@plt>
mov    %eax,%r13d
mov    %r12,%rdi
call   1030 <puts@plt>
test   %r13d,%r13d
jns    12d1 <main+0x101>
lea    0xd73(%rip),%rdi
call   10a0 <error@plt>
mov    %r13d,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   1040 <write@plt>
test   %eax,%eax
jns    12a0 <main+0xd0>
lea    0xd70(%rip),%rdi
call   10a0 <error@plt>
jmp    12a0 <main+0xd0>
mov    0x2cd6(%rip),%rax
mov    (%rax),%edi
lea    0xcfd(%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 = [30 x i8] c"Waiting for a new connection\0A\00"
@.str.4 = [16 x i8] c"ERROR on accept\00"
@.str.5 = [13 x i8] c"Hello World!\00"
@.str.6 = [24 x i8] c"ERROR writing to socket\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 i8*
%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 = call i8* @calloc(i32 256, i32 1)
store i8* %14, i8** %10
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 2
br i1 %16, label %17, label %21
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = load i32, i32* @AF_INET
%23 = load i32, i32* @SOCK_STREAM
%24 = call i32 @socket(i32 %22, i32 %23, i32 0)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %27, %21
%30 = bitcast %struct.sockaddr_in* %11 to i8*
%31 = call i32 @bzero(i8* %30, i32 12)
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %33
%35 = call i32 @atoi(i8* %34)
store i32 %35, i32* %8
%36 = load i32, i32* @AF_INET
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %36, i32* %37
%38 = load i32, i32* @INADDR_ANY
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
%40 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %39, i32 0, i32 0
store i32 %38, i32* %40
%41 = load i32, i32* %8
%42 = call i32 @htons(i32 %41)
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
store i32 %42, i32* %43
%44 = load i32, i32* %6
%45 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%46 = call i64 @bind(i32 %44, %struct.sockaddr* %45, i32 12)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %29
%49 = call i32 @error(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %50
50: ; preds = %48, %29
%51 = load i32, i32* %6
%52 = call i32 @listen(i32 %51, i32 5)
store i32 12, i32* %9
br label %53
53: ; preds = %50, %69
%54 = load i32, i32* %6
%55 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%56 = call i32 @accept(i32 %54, %struct.sockaddr* %55, i32* %9)
store i32 %56, i32* %7
%57 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0))
%58 = load i32, i32* %7
%59 = icmp slt i32 %58, 0
br i1 %59, label %60, label %62
60: ; preds = %53
%61 = call i32 @error(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
br label %62
62: ; preds = %60, %53
%63 = load i32, i32* %7
%64 = call i32 @write(i32 %63, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i32 12)
store i32 %64, i32* %13
%65 = load i32, i32* %13
%66 = icmp slt i32 %65, 0
br i1 %66, label %67, label %69
67: ; preds = %62
%68 = call i32 @error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
br label %69
69: ; preds = %67, %62
%70 = load i32, i32* %7
%71 = call i32 @close(i32 %70)
br label %53
}

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 = [13 x i8] c"Hello World!\00"
@.str.6 = [24 x i8] c"ERROR writing to socket\00"
@str = [29 x i8] c"Waiting for a new connection\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 = %51, %37
%41 = call i32 @accept(i32 %17, %struct.sockaddr* %39, i32* %3)
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
%43 = icmp slt i32 %41, 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 @write(i32 %41, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0), i32 12)
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %46
%50 = call i32 @error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0))
br label %51
51: ; preds = %49, %46
%52 = call i32 @close(i32 %41)
br label %40
}