#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_addrtype;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ gethostname(char *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int listenOnPort(int port) {
  char myname[64 + 1];
  int s;
  struct sockaddr_in sa;
  struct hostent *hp;
  int so_reuseaddr = 1;
  memset(&sa, 0, sizeof(struct sockaddr_in));
  gethostname(myname, 64);
  hp = gethostbyname(myname);
  if (hp == NULL) {
    fprintf(stderr, "hp == NULL\n");
    return -1;
  }
  sa.sin_family = hp->h_addrtype;
  sa.sin_port = htons(port);
  sa.sin_addr.s_addr = INADDR_ANY;
  if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    fprintf(stderr, "s < 0\n");
    return -1;
  }
  if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&so_reuseaddr,
                 sizeof(int)) < 0) {
    fprintf(stderr, "setsockopt < 0 errno: %s\n", strerror(errno));
    close(s);
    return -1;
  }
  if (bind(s, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) < 0) {
    fprintf(stderr, "bind < 0 errno: %s\n", strerror(errno));
    close(s);
    return -1;
  }
  listen(s, 1);
  return s;
}


x86 assembly (clang, -O0):
<listenOnPort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x1,-0x6c(%rbp)
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
lea    -0x50(%rbp),%rdi
mov    $0x40,%esi
call   10b0 <gethostname@plt>
lea    -0x50(%rbp),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x68(%rbp)
cmpq   $0x0,-0x68(%rbp)
jne    121e <listenOnPort+0x6e>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
lea    0xdf5(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1366 <listenOnPort+0x1b6>
mov    -0x68(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x58(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x5c(%rbp)
mov    0x2d9f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x60(%rbp)
mov    0x2d9b(%rip),%rax
mov    (%rax),%edi
mov    0x2d72(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x54(%rbp)
cmp    $0x0,%eax
jge    1286 <listenOnPort+0xd6>
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
lea    0xd99(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1366 <listenOnPort+0x1b6>
mov    -0x54(%rbp),%edi
mov    0x2d58(%rip),%rax
mov    (%rax),%esi
mov    0x2d17(%rip),%rax
mov    (%rax),%edx
lea    -0x6c(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    12f6 <listenOnPort+0x146>
mov    0x2d0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x70(%rbp)
mov    0x2cf1(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x70(%rbp),%edi
mov    %rax,%rdx
lea    0xd38(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x54(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1366 <listenOnPort+0x1b6>
mov    -0x54(%rbp),%edi
lea    -0x60(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    1353 <listenOnPort+0x1a3>
mov    0x2cb0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x74(%rbp)
mov    0x2c94(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x74(%rbp),%edi
mov    %rax,%rdx
lea    0xcf5(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x54(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1366 <listenOnPort+0x1b6>
mov    -0x54(%rbp),%edi
mov    $0x1,%esi
call   1090 <listen@plt>
mov    -0x54(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listenOnPort>:
push   %rbp
push   %rbx
sub    $0x68,%rsp
mov    %edi,%ebp
movl   $0x1,0xc(%rsp)
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
call   10b0 <gethostname@plt>
mov    %rbx,%rdi
call   1070 <gethostbyname@plt>
test   %rax,%rax
je     1281 <listenOnPort+0xd1>
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2dcc(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
mov    0x2d9e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
test   %eax,%eax
js     1293 <listenOnPort+0xe3>
mov    %eax,%ebx
mov    0x2db0(%rip),%rax
mov    (%rax),%esi
mov    0x2d6f(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     12ac <listenOnPort+0xfc>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10a0 <bind@plt>
test   %rax,%rax
js     12cc <listenOnPort+0x11c>
mov    %ebx,%edi
mov    $0x1,%esi
call   1090 <listen@plt>
jmp    1302 <listenOnPort+0x152>
mov    0x2d40(%rip),%rax
mov    (%rax),%edi
lea    0xd6f(%rip),%rsi
jmp    12a3 <listenOnPort+0xf3>
mov    0x2d2e(%rip),%rax
mov    (%rax),%edi
lea    0xd69(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    12fd <listenOnPort+0x14d>
mov    0x2d15(%rip),%rax
mov    (%rax),%ebp
mov    0x2cfc(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd49(%rip),%rsi
jmp    12ea <listenOnPort+0x13a>
mov    0x2cf5(%rip),%rax
mov    (%rax),%ebp
mov    0x2cdc(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd43(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x68,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [12 x i8] c"hp == NULL\0A\00"
@INADDR_ANY = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [7 x i8] c"s < 0\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [26 x i8] c"setsockopt < 0 errno: %s\0A\00"
@errno = global i32 0
@.str.3 = [20 x i8] c"bind < 0 errno: %s\0A\00"
define i32 @listenOnPort(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [65 x i8]
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.hostent*
%8 = alloca i32
store i32 %0, i32* %3
store i32 1, i32* %8
%9 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 12)
%10 = getelementptr inbounds [65 x i8], [65 x i8]* %4, i64 0, i64 0
%11 = call i32 @gethostname(i8* %10, i32 64)
%12 = getelementptr inbounds [65 x i8], [65 x i8]* %4, i64 0, i64 0
%13 = call %struct.hostent* @gethostbyname(i8* %12)
store %struct.hostent* %13, %struct.hostent** %7
%14 = load %struct.hostent*, %struct.hostent** %7
%15 = icmp eq %struct.hostent* %14, null
br i1 %15, label %16, label %19
16: ; preds = %1
%17 = load i32, i32* @stderr
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %67
19: ; preds = %1
%20 = load %struct.hostent*, %struct.hostent** %7
%21 = getelementptr inbounds %struct.hostent, %struct.hostent* %20, i32 0, i32 0
%22 = load i32, i32* %21
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %22, i32* %23
%24 = load i32, i32* %3
%25 = call i32 @htons(i32 %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %25, i32* %26
%27 = load i32, i32* @INADDR_ANY
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%29 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %28, i32 0, i32 0
store i32 %27, i32* %29
%30 = load i32, i32* @AF_INET
%31 = load i32, i32* @SOCK_STREAM
%32 = call i32 @socket(i32 %30, i32 %31, i32 0)
store i32 %32, i32* %5
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %19
%35 = load i32, i32* @stderr
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %67
37: ; preds = %19
%38 = load i32, i32* %5
%39 = load i32, i32* @SOL_SOCKET
%40 = load i32, i32* @SO_REUSEADDR
%41 = bitcast i32* %8 to i8*
%42 = call i64 @setsockopt(i32 %38, i32 %39, i32 %40, i8* %41, i32 4)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %51
44: ; preds = %37
%45 = load i32, i32* @stderr
%46 = load i32, i32* @errno
%47 = call i8* @strerror(i32 %46)
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %47)
%49 = load i32, i32* %5
%50 = call i32 @close(i32 %49)
store i32 -1, i32* %2
br label %67
51: ; preds = %37
%52 = load i32, i32* %5
%53 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%54 = call i64 @bind(i32 %52, %struct.sockaddr* %53, i32 12)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %63
56: ; preds = %51
%57 = load i32, i32* @stderr
%58 = load i32, i32* @errno
%59 = call i8* @strerror(i32 %58)
%60 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %59)
%61 = load i32, i32* %5
%62 = call i32 @close(i32 %61)
store i32 -1, i32* %2
br label %67
63: ; preds = %51
%64 = load i32, i32* %5
%65 = call i32 @listen(i32 %64, i32 1)
%66 = load i32, i32* %5
store i32 %66, i32* %2
br label %67
67: ; preds = %63, %56, %44, %34, %16
%68 = load i32, i32* %2
ret i32 %68
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [12 x i8] c"hp == NULL\0A\00"
@INADDR_ANY = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [7 x i8] c"s < 0\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.2 = [26 x i8] c"setsockopt < 0 errno: %s\0A\00"
@errno = global i32 0
@.str.3 = [20 x i8] c"bind < 0 errno: %s\0A\00"
define i32 @listenOnPort(i32 %0) {
%2 = alloca [65 x i8]
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = getelementptr inbounds [65 x i8], [65 x i8]* %2, i64 0, i64 0
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%8 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%9 = call i32 @gethostname(i8* %5, i32 64)
%10 = call %struct.hostent* @gethostbyname(i8* %5)
%11 = icmp eq %struct.hostent* %10, null
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = load i32, i32* @stderr
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %53
15: ; preds = %1
%16 = getelementptr inbounds %struct.hostent, %struct.hostent* %10, i64 0, i32 0
%17 = load i32, i32* %16
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %17, i32* %18
%19 = call i32 @htons(i32 %0)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %19, i32* %20
%21 = load i32, i32* @INADDR_ANY
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %21, i32* %22
%23 = load i32, i32* @AF_INET
%24 = load i32, i32* @SOCK_STREAM
%25 = call i32 @socket(i32 %23, i32 %24, i32 0)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %15
%28 = load i32, i32* @stderr
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %53
30: ; preds = %15
%31 = load i32, i32* @SOL_SOCKET
%32 = load i32, i32* @SO_REUSEADDR
%33 = call i64 @setsockopt(i32 %25, i32 %31, i32 %32, i8* %7, i32 4)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %30
%36 = load i32, i32* @stderr
%37 = load i32, i32* @errno
%38 = call i8* @strerror(i32 %37)
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %38)
%40 = call i32 @close(i32 %25)
br label %53
41: ; preds = %30
%42 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%43 = call i64 @bind(i32 %25, %struct.sockaddr* %42, i32 12)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %51
45: ; preds = %41
%46 = load i32, i32* @stderr
%47 = load i32, i32* @errno
%48 = call i8* @strerror(i32 %47)
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %48)
%50 = call i32 @close(i32 %25)
br label %53
51: ; preds = %41
%52 = call i32 @listen(i32 %25, i32 1)
br label %53
53: ; preds = %51, %45, %35, %27, %12
%54 = phi i32 [ -1, %12 ], [ -1, %27 ], [ -1, %35 ], [ -1, %45 ], [ %25, %51 ]
ret i32 %54
}