#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;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ *h_addr_list;
  int /*<<< orphan*/ h_addrtype;
};
typedef int /*<<< orphan*/ sAddr;
typedef int /*<<< orphan*/ optval;

/* Variables and functions */
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, size_t);
int /*<<< orphan*/ close(int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int listenTCP(int port) {
  printf("Starting TCP server on port %d...\n", port);
  if (port < 1024) {
    printf("Warning! Port %d in superuser port range...", port);
  }
  int sock;
  int optval = 1;
  struct hostent *host;
  struct sockaddr_in sAddr;
  size_t sAddrSize = sizeof(sAddr);
  host = gethostbyname("localhost");
  if (!host) {
    perror("gethostbyname");
    return -1;
  }
  if ((sock = socket(host->h_addrtype, SOCK_STREAM, 0)) <= 0) {
    perror("socket");
    return -1;
  }
  if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&optval,
                 sizeof(optval)) < 0) {
    perror("setsockopt");
    close(sock);
    return -1;
  }
  sAddr.sin_family = host->h_addrtype;
  sAddr.sin_port = htons(port);
  memcpy(&sAddr.sin_addr.s_addr, host->h_addr_list[0], host->h_length);
  if (bind(sock, (struct sockaddr *)&sAddr, sAddrSize) < 0) {
    perror("bind");
    close(sock);
    return -1;
  }
  if (listen(sock, 5) < 0) {
    perror("listen");
    close(sock);
    return -1;
  }
  printf("Server listening on port %d\n", port);
  return sock;
}


x86 assembly (clang, -O0):
<listenTCP>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xe4b(%rip),%rdi
call   1050 <printf@plt>
cmpl   $0x400,-0x8(%rbp)
jge    11d6 <listenTCP+0x36>
mov    -0x8(%rbp),%esi
lea    0xe52(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x10(%rbp)
movq   $0xc,-0x30(%rbp)
lea    0xe63(%rip),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1218 <listenTCP+0x78>
lea    0xe52(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1355 <listenTCP+0x1b5>
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%edi
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jg     1253 <listenTCP+0xb3>
lea    0xe25(%rip),%rdi
call   10b0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1355 <listenTCP+0x1b5>
mov    -0xc(%rbp),%edi
mov    0x2d8b(%rip),%rax
mov    (%rax),%esi
mov    0x2d6a(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    12a1 <listenTCP+0x101>
lea    0xde6(%rip),%rdi
call   10b0 <perror@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1355 <listenTCP+0x1b5>
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x24(%rbp)
lea    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
call   1080 <memcpy@plt>
mov    -0xc(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    1309 <listenTCP+0x169>
lea    0xd89(%rip),%rdi
call   10b0 <perror@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1355 <listenTCP+0x1b5>
mov    -0xc(%rbp),%edi
mov    $0x5,%esi
call   1090 <listen@plt>
cmp    $0x0,%rax
jge    1340 <listenTCP+0x1a0>
lea    0xd57(%rip),%rdi
call   10b0 <perror@plt>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1355 <listenTCP+0x1b5>
mov    -0x8(%rbp),%esi
lea    0xd3b(%rip),%rdi
call   1050 <printf@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listenTCP>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edi,%r14d
lea    0xe4e(%rip),%rdi
mov    %r14d,%esi
call   1050 <printf@plt>
cmp    $0x3ff,%r14d
jg     11d2 <listenTCP+0x32>
lea    0xe59(%rip),%rdi
mov    %r14d,%esi
call   1050 <printf@plt>
movl   $0x1,0xc(%rsp)
lea    0xe6e(%rip),%rdi
call   1070 <gethostbyname@plt>
test   %rax,%rax
je     12a4 <listenTCP+0x104>
mov    %rax,%rbp
mov    0x10(%rax),%edi
mov    0x2ddc(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
jle    12ad <listenTCP+0x10d>
mov    %eax,%ebx
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
mov    0x2db1(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     12c0 <listenTCP+0x120>
mov    0x10(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x8(%rbp),%rax
mov    (%rax),%esi
mov    0x0(%rbp),%edx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
call   1080 <memcpy@plt>
mov    $0xc,%edx
mov    %ebx,%edi
mov    %rbp,%rsi
call   10a0 <bind@plt>
test   %rax,%rax
js     12c9 <listenTCP+0x129>
mov    %ebx,%edi
mov    $0x5,%esi
call   1090 <listen@plt>
test   %rax,%rax
js     12d2 <listenTCP+0x132>
lea    0xdf4(%rip),%rdi
mov    %r14d,%esi
call   1050 <printf@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdae(%rip),%rdi
jmp    12b4 <listenTCP+0x114>
lea    0xdb3(%rip),%rdi
call   10b0 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1299 <listenTCP+0xf9>
lea    0xda7(%rip),%rdi
jmp    12d9 <listenTCP+0x139>
lea    0xda9(%rip),%rdi
jmp    12d9 <listenTCP+0x139>
lea    0xda5(%rip),%rdi
call   10b0 <perror@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebx
jmp    1299 <listenTCP+0xf9>

LLVM IR (-O0):
%struct.hostent = type { i32, i32*, i32 }
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [35 x i8] c"Starting TCP server on port %d...\0A\00"
@.str.1 = [44 x i8] c"Warning! Port %d in superuser port range...\00"
@.str.2 = [10 x i8] c"localhost\00"
@.str.3 = [14 x i8] c"gethostbyname\00"
@SOCK_STREAM = global i32 0
@.str.4 = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.5 = [11 x i8] c"setsockopt\00"
@.str.6 = [5 x i8] c"bind\00"
@.str.7 = [7 x i8] c"listen\00"
@.str.8 = [29 x i8] c"Server listening on port %d\0A\00"
define i32 @listenTCP(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.hostent*
%7 = alloca %struct.sockaddr_in
%8 = alloca i64
store i32 %0, i32* %3
%9 = load i32, i32* %3
%10 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = load i32, i32* %3
%12 = icmp slt i32 %11, 1024
br i1 %12, label %13, label %16
13: ; preds = %1
%14 = load i32, i32* %3
%15 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i32 %14)
br label %16
16: ; preds = %13, %1
store i32 1, i32* %5
store i64 12, i64* %8
%17 = call %struct.hostent* @gethostbyname(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
store %struct.hostent* %17, %struct.hostent** %6
%18 = load %struct.hostent*, %struct.hostent** %6
%19 = icmp ne %struct.hostent* %18, null
br i1 %19, label %22, label %20
20: ; preds = %16
%21 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %2
br label %82
22: ; preds = %16
%23 = load %struct.hostent*, %struct.hostent** %6
%24 = getelementptr inbounds %struct.hostent, %struct.hostent* %23, i32 0, i32 2
%25 = load i32, i32* %24
%26 = load i32, i32* @SOCK_STREAM
%27 = call i32 @socket(i32 %25, i32 %26, i32 0)
store i32 %27, i32* %4
%28 = icmp sle i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %22
%30 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
store i32 -1, i32* %2
br label %82
31: ; preds = %22
%32 = load i32, i32* %4
%33 = load i32, i32* @SOL_SOCKET
%34 = load i32, i32* @SO_REUSEADDR
%35 = bitcast i32* %5 to i8*
%36 = call i64 @setsockopt(i32 %32, i32 %33, i32 %34, i8* %35, i32 4)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %31
%39 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%40 = load i32, i32* %4
%41 = call i32 @close(i32 %40)
store i32 -1, i32* %2
br label %82
42: ; preds = %31
%43 = load %struct.hostent*, %struct.hostent** %6
%44 = getelementptr inbounds %struct.hostent, %struct.hostent* %43, i32 0, i32 2
%45 = load i32, i32* %44
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %45, i32* %46
%47 = load i32, i32* %3
%48 = call i32 @htons(i32 %47)
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %48, i32* %49
%50 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%51 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %50, i32 0, i32 0
%52 = load %struct.hostent*, %struct.hostent** %6
%53 = getelementptr inbounds %struct.hostent, %struct.hostent* %52, i32 0, i32 1
%54 = load i32*, i32** %53
%55 = getelementptr inbounds i32, i32* %54, i64 0
%56 = load i32, i32* %55
%57 = load %struct.hostent*, %struct.hostent** %6
%58 = getelementptr inbounds %struct.hostent, %struct.hostent* %57, i32 0, i32 0
%59 = load i32, i32* %58
%60 = call i32 @memcpy(i32* %51, i32 %56, i32 %59)
%61 = load i32, i32* %4
%62 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%63 = load i64, i64* %8
%64 = call i64 @bind(i32 %61, %struct.sockaddr* %62, i64 %63)
%65 = icmp slt i64 %64, 0
br i1 %65, label %66, label %70
66: ; preds = %42
%67 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%68 = load i32, i32* %4
%69 = call i32 @close(i32 %68)
store i32 -1, i32* %2
br label %82
70: ; preds = %42
%71 = load i32, i32* %4
%72 = call i64 @listen(i32 %71, i32 5)
%73 = icmp slt i64 %72, 0
br i1 %73, label %74, label %78
74: ; preds = %70
%75 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
%76 = load i32, i32* %4
%77 = call i32 @close(i32 %76)
store i32 -1, i32* %2
br label %82
78: ; preds = %70
%79 = load i32, i32* %3
%80 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.8, i64 0, i64 0), i32 %79)
%81 = load i32, i32* %4
store i32 %81, i32* %2
br label %82
82: ; preds = %78, %74, %66, %38, %29, %20
%83 = load i32, i32* %2
ret i32 %83
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i32*, i32 }
%struct.sockaddr = type { i32 }
@.str = [35 x i8] c"Starting TCP server on port %d...\0A\00"
@.str.1 = [44 x i8] c"Warning! Port %d in superuser port range...\00"
@.str.2 = [10 x i8] c"localhost\00"
@.str.3 = [14 x i8] c"gethostbyname\00"
@SOCK_STREAM = global i32 0
@.str.4 = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.5 = [11 x i8] c"setsockopt\00"
@.str.6 = [5 x i8] c"bind\00"
@.str.7 = [7 x i8] c"listen\00"
@.str.8 = [29 x i8] c"Server listening on port %d\0A\00"
define i32 @listenTCP(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = tail call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = icmp slt i32 %0, 1024
br i1 %5, label %6, label %8
6: ; preds = %1
%7 = tail call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i32 %0)
br label %8
8: ; preds = %6, %1
%9 = bitcast i32* %2 to i8*
store i32 1, i32* %2
%10 = bitcast %struct.sockaddr_in* %3 to i8*
%11 = tail call %struct.hostent* @gethostbyname(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%12 = icmp eq %struct.hostent* %11, null
br i1 %12, label %13, label %15
13: ; preds = %8
%14 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %57
15: ; preds = %8
%16 = getelementptr inbounds %struct.hostent, %struct.hostent* %11, i64 0, i32 2
%17 = load i32, i32* %16
%18 = load i32, i32* @SOCK_STREAM
%19 = tail call i32 @socket(i32 %17, i32 %18, i32 0)
%20 = icmp slt i32 %19, 1
br i1 %20, label %21, label %23
21: ; preds = %15
%22 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
br label %57
23: ; preds = %15
%24 = load i32, i32* @SOL_SOCKET
%25 = load i32, i32* @SO_REUSEADDR
%26 = call i64 @setsockopt(i32 %19, i32 %24, i32 %25, i8* %9, i32 4)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %23
%29 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%30 = call i32 @close(i32 %19)
br label %57
31: ; preds = %23
%32 = load i32, i32* %16
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %32, i32* %33
%34 = call i32 @htons(i32 %0)
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %34, i32* %35
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
%37 = getelementptr inbounds %struct.hostent, %struct.hostent* %11, i64 0, i32 1
%38 = load i32*, i32** %37
%39 = load i32, i32* %38
%40 = getelementptr inbounds %struct.hostent, %struct.hostent* %11, i64 0, i32 0
%41 = load i32, i32* %40
%42 = call i32 @memcpy(i32* %36, i32 %39, i32 %41)
%43 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%44 = call i64 @bind(i32 %19, %struct.sockaddr* %43, i64 12)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %31
%47 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%48 = call i32 @close(i32 %19)
br label %57
49: ; preds = %31
%50 = call i64 @listen(i32 %19, i32 5)
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %49
%53 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0))
%54 = call i32 @close(i32 %19)
br label %57
55: ; preds = %49
%56 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.8, i64 0, i64 0), i32 %0)
br label %57
57: ; preds = %55, %52, %46, %28, %21, %13
%58 = phi i32 [ -1, %21 ], [ -1, %28 ], [ -1, %46 ], [ -1, %52 ], [ %19, %55 ], [ -1, %13 ]
ret i32 %58
}