#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 socklen_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int accept(int, struct sockaddr *, int *);
int atoi(char *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
char *inet_ntoa(TYPE_1__);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char *);
int main(int argc, char *argv[]) {
  if (argc != 2) {
    fprintf(stderr, "Usage: %s <port>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  int port = atoi(argv[1]);
  struct sockaddr_in server;
  struct sockaddr_in destination;
  char response[] = "HTTP/1.1 200 OK\nContent-Type: text/html; "
                    "charset=utf-8\nContent-Length: 11\n\nHello World";
  int listen_sock;
  socklen_t socksize = sizeof(struct sockaddr_in);
  memset(&server, 0, sizeof server);
  server.sin_family = AF_INET;
  server.sin_addr.s_addr = htonl(INADDR_ANY);
  server.sin_port = htons(port);
  listen_sock = socket(AF_INET, SOCK_STREAM, 0);
  int optval = 1;
  setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval);
  if (bind(listen_sock, (struct sockaddr *)&server, sizeof(struct sockaddr))) {
    perror("Failed to bind");
    exit(EXIT_FAILURE);
  }
  if (listen(listen_sock, 1)) {
    perror("Listen");
    exit(EXIT_FAILURE);
  }
  printf("Listening on port %d...\n", port);
  for (;;) {
    int con_sock =
        accept(listen_sock, (struct sockaddr *)&destination, &socksize);
    printf("New connection from %s\n", inet_ntoa(destination.sin_addr));
    send(con_sock, response, strlen(response), 0);
    close(con_sock);
  }
  close(listen_sock);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     126d <main+0x4d>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xda6(%rip),%rsi
call   10c0 <fprintf@plt>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
call   1130 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1120 <atoi@plt>
mov    %eax,-0x14(%rbp)
lea    -0x90(%rbp),%rdi
lea    0xdd5(%rip),%rsi
mov    $0x57,%edx
call   10d0 <memcpy@plt>
movl   $0xc,-0x98(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   10a0 <memset@plt>
mov    0x2d2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d16(%rip),%rax
mov    (%rax),%edi
call   1090 <htonl@plt>
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d02(%rip),%rax
mov    (%rax),%edi
mov    0x2cd9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1140 <socket@plt>
mov    %eax,-0x94(%rbp)
movl   $0x1,-0x9c(%rbp)
mov    -0x94(%rbp),%edi
mov    0x2cdb(%rip),%rax
mov    (%rax),%esi
mov    0x2c9a(%rip),%rax
mov    (%rax),%edx
lea    -0x9c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    -0x94(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   10f0 <bind@plt>
cmp    $0x0,%rax
je     1362 <main+0x142>
lea    0xcc3(%rip),%rdi
call   1100 <perror@plt>
mov    0x2c5d(%rip),%rax
mov    (%rax),%edi
call   1130 <exit@plt>
mov    -0x94(%rbp),%edi
mov    $0x1,%esi
call   10e0 <listen@plt>
cmp    $0x0,%rax
je     1396 <main+0x176>
lea    0xc9e(%rip),%rdi
call   1100 <perror@plt>
mov    0x2c29(%rip),%rax
mov    (%rax),%edi
call   1130 <exit@plt>
mov    -0x14(%rbp),%esi
lea    0xc88(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0x94(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x98(%rbp),%rdx
call   1110 <accept@plt>
mov    %eax,-0xa0(%rbp)
mov    -0x30(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%rsi
lea    0xc6c(%rip),%rdi
mov    $0x0,%al
call   1080 <printf@plt>
mov    -0xa0(%rbp),%eax
mov    %eax,-0xac(%rbp)
lea    -0x90(%rbp),%rax
mov    %rax,-0xa8(%rbp)
lea    -0x90(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0xac(%rbp),%edi
mov    -0xa8(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
mov    -0xa0(%rbp),%edi
call   10b0 <close@plt>
jmp    13a7 <main+0x187>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
cmp    $0x2,%edi
jne    13a5 <main+0x195>
mov    0x8(%rsi),%rdi
call   1110 <atoi@plt>
mov    %eax,%ebx
movaps 0xe64(%rip),%xmm0
movaps %xmm0,0x70(%rsp)
movaps 0xe48(%rip),%xmm0
movaps %xmm0,0x60(%rsp)
movaps 0xe2c(%rip),%xmm0
movaps %xmm0,0x50(%rsp)
movaps 0xe10(%rip),%xmm0
movaps %xmm0,0x40(%rsp)
movaps 0xdf4(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movabs $0x646c726f57206f,%rax
mov    %rax,0x7f(%rsp)
movl   $0xc,0xc(%rsp)
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   10a0 <memset@plt>
mov    0x2d3d(%rip),%rbp
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    0x2d27(%rip),%rax
mov    (%rax),%edi
call   1090 <htonl@plt>
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
call   1060 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x0(%rbp),%edi
mov    0x2cef(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1130 <socket@plt>
mov    %eax,%r13d
movl   $0x1,0x8(%rsp)
mov    0x2cfc(%rip),%rax
mov    (%rax),%esi
mov    0x2cbb(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    %r13d,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %r13d,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   10e0 <bind@plt>
test   %rax,%rax
jne    13bf <main+0x1af>
mov    %r13d,%edi
mov    $0x1,%esi
call   10d0 <listen@plt>
test   %rax,%rax
jne    13c8 <main+0x1b8>
lea    0xce8(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1080 <printf@plt>
lea    0x20(%rsp),%r14
lea    0xc(%rsp),%r15
lea    0xce7(%rip),%r12
lea    0x30(%rsp),%rbx
nop
mov    %r13d,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1100 <accept@plt>
mov    %eax,%ebp
mov    0x20(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %r12,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
mov    %ebp,%edi
call   10b0 <close@plt>
jmp    1360 <main+0x150>
mov    0x2c1c(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xc48(%rip),%rsi
call   10c0 <fprintf@plt>
jmp    13d4 <main+0x1c4>
lea    0xc4c(%rip),%rdi
jmp    13cf <main+0x1bf>
lea    0xc52(%rip),%rdi
call   10f0 <perror@plt>
mov    0x2bdd(%rip),%rax
mov    (%rax),%edi
call   1120 <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 = [18 x i8] c"Usage: %s <port>\0A\00"
@EXIT_FAILURE = global i32 0
@__const.main.response = [87 x i8] c"HTTP/1.1 200 OK\0AContent-Type: text/html; charset=utf-8\0AContent-Length: 11\0A\0AHello World\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [15 x i8] c"Failed to bind\00"
@.str.2 = [7 x i8] c"Listen\00"
@.str.3 = [25 x i8] c"Listening on port %d...\0A\00"
@.str.4 = [24 x i8] c"New connection from %s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.sockaddr_in
%9 = alloca [87 x i8]
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%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 ne i32 %14, 2
br i1 %15, label %16, label %24
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %2
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i32 @atoi(i8* %27)
store i32 %28, i32* %6
%29 = bitcast [87 x i8]* %9 to i8*
store i32 12, i32* %11
%30 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 12)
%31 = load i32, i32* @AF_INET
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %31, i32* %32
%33 = load i32, i32* @INADDR_ANY
%34 = call i32 @htonl(i32 %33)
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%36 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %35, i32 0, i32 0
store i32 %34, i32* %36
%37 = load i32, i32* %6
%38 = call i32 @htons(i32 %37)
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %38, i32* %39
%40 = load i32, i32* @AF_INET
%41 = load i32, i32* @SOCK_STREAM
%42 = call i32 @socket(i32 %40, i32 %41, i32 0)
store i32 %42, i32* %10
store i32 1, i32* %12
%43 = load i32, i32* %10
%44 = load i32, i32* @SOL_SOCKET
%45 = load i32, i32* @SO_REUSEADDR
%46 = call i32 @setsockopt(i32 %43, i32 %44, i32 %45, i32* %12, i32 4)
%47 = load i32, i32* %10
%48 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%49 = call i64 @bind(i32 %47, %struct.sockaddr* %48, i32 4)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %55
51: ; preds = %24
%52 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%53 = load i32, i32* @EXIT_FAILURE
%54 = call i32 @exit(i32 %53)
unreachable
55: ; preds = %24
%56 = load i32, i32* %10
%57 = call i64 @listen(i32 %56, i32 1)
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %63
59: ; preds = %55
%60 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%61 = load i32, i32* @EXIT_FAILURE
%62 = call i32 @exit(i32 %61)
unreachable
63: ; preds = %55
%64 = load i32, i32* %6
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %64)
br label %66
66: ; preds = %66, %63
%67 = load i32, i32* %10
%68 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%69 = call i32 @accept(i32 %67, %struct.sockaddr* %68, i32* %11)
store i32 %69, i32* %13
%70 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%71 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %70, i32 0, i32 0
%72 = load i32, i32* %71
%73 = call i8* @inet_ntoa(i32 %72)
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %73)
%75 = load i32, i32* %13
%76 = getelementptr inbounds [87 x i8], [87 x i8]* %9, i64 0, i64 0
%77 = getelementptr inbounds [87 x i8], [87 x i8]* %9, i64 0, i64 0
%78 = call i32 @strlen(i8* %77)
%79 = call i32 @send(i32 %75, i8* %76, i32 %78, i32 0)
%80 = load i32, i32* %13
%81 = call i32 @close(i32 %80)
br label %66
}

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 = [18 x i8] c"Usage: %s <port>\0A\00"
@EXIT_FAILURE = global i32 0
@__const.main.response = [87 x i8] c"HTTP/1.1 200 OK\0AContent-Type: text/html; charset=utf-8\0AContent-Length: 11\0A\0AHello World\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [15 x i8] c"Failed to bind\00"
@.str.2 = [7 x i8] c"Listen\00"
@.str.3 = [25 x i8] c"Listening on port %d...\0A\00"
@.str.4 = [24 x i8] c"New connection from %s\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca [87 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = icmp eq i32 %0, 2
br i1 %8, label %15, label %9
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = load i8*, i8** %1
%12 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %11)
%13 = load i32, i32* @EXIT_FAILURE
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %2
%16 = getelementptr inbounds i8*, i8** %1, i64 1
%17 = load i8*, i8** %16
%18 = tail call i32 @atoi(i8* %17)
%19 = bitcast %struct.sockaddr_in* %3 to i8*
%20 = bitcast %struct.sockaddr_in* %4 to i8*
%21 = getelementptr inbounds [87 x i8], [87 x i8]* %5, i64 0, i64 0
%22 = bitcast i32* %6 to i8*
store i32 12, i32* %6
%23 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %24, i32* %25
%26 = load i32, i32* @INADDR_ANY
%27 = call i32 @htonl(i32 %26)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %27, i32* %28
%29 = call i32 @htons(i32 %18)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %29, i32* %30
%31 = load i32, i32* @AF_INET
%32 = load i32, i32* @SOCK_STREAM
%33 = call i32 @socket(i32 %31, i32 %32, i32 0)
%34 = bitcast i32* %7 to i8*
store i32 1, i32* %7
%35 = load i32, i32* @SOL_SOCKET
%36 = load i32, i32* @SO_REUSEADDR
%37 = call i32 @setsockopt(i32 %33, i32 %35, i32 %36, i32* %7, i32 4)
%38 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%39 = call i64 @bind(i32 %33, %struct.sockaddr* %38, i32 4)
%40 = icmp eq i64 %39, 0
br i1 %40, label %45, label %41
41: ; preds = %15
%42 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%43 = load i32, i32* @EXIT_FAILURE
%44 = call i32 @exit(i32 %43)
unreachable
45: ; preds = %15
%46 = call i64 @listen(i32 %33, i32 1)
%47 = icmp eq i64 %46, 0
br i1 %47, label %52, label %48
48: ; preds = %45
%49 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%50 = load i32, i32* @EXIT_FAILURE
%51 = call i32 @exit(i32 %50)
unreachable
52: ; preds = %45
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %18)
%54 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%55 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
br label %56
56: ; preds = %56, %52
%57 = call i32 @accept(i32 %33, %struct.sockaddr* %54, i32* %6)
%58 = load i32, i32* %55
%59 = call i8* @inet_ntoa(i32 %58)
%60 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i8* %59)
%61 = call i32 @strlen(i8* %21)
%62 = call i32 @send(i32 %57, i8* %21, i32 %61, i32 0)
%63 = call i32 @close(i32 %57)
br label %56
}