#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 */
typedef int /*<<< orphan*/ uint32_t;
typedef int /*<<< orphan*/ uint16_t;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
  TYPE_1__ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ src;
typedef int /*<<< orphan*/ opt;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SOMAXCONN;
int /*<<< orphan*/ SO_REUSEADDR;
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
scalar_t__ listen(int, int /*<<< orphan*/);
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 stsmon_listentoport(uint32_t bindaddr, uint16_t port) {
  struct sockaddr_in src;
  int fd, flags, opt;
  if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    fprintf(stderr, "socket() failed: %s\n", strerror(errno));
    return -1;
  }
  flags = fcntl(fd, F_GETFL);
  if (flags >= 0) {
    flags |= O_NONBLOCK;
    fcntl(fd, F_SETFL, flags);
  }
  opt = 1;
  if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof(opt))) {
    fprintf(stderr, "setsockopt() failed on socket %d: %s\n", fd,
            strerror(errno));
  }
  src.sin_addr.s_addr = bindaddr;
  src.sin_family = AF_INET;
  src.sin_port = htons(port);
  if (bind(fd, (struct sockaddr *)&src, sizeof(src)) < 0) {
    fprintf(stderr, "bind() failed on socket %d: %s\n", fd, strerror(errno));
    close(fd);
    return -1;
  }
  if (listen(fd, SOMAXCONN) < 0) {
    fprintf(stderr, "listen() failed on socket %d: %s\n", fd, strerror(errno));
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<stsmon_listentoport>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11fd <stsmon_listentoport+0x6d>
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x28(%rbp),%edi
mov    %rax,%rdx
lea    0xe16(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    139d <stsmon_listentoport+0x20d>
mov    -0x1c(%rbp),%edi
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1040 <fcntl@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jl     1242 <stsmon_listentoport+0xb2>
mov    0x2db4(%rip),%rax
mov    (%rax),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2d82(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%edx
mov    $0x0,%al
call   1040 <fcntl@plt>
movl   $0x1,-0x24(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2d95(%rip),%rax
mov    (%rax),%esi
mov    0x2d3c(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
je     12ae <stsmon_listentoport+0x11e>
mov    0x2d4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x30(%rbp),%edi
mov    -0x2c(%rbp),%edx
mov    %rax,%rcx
lea    0xd6e(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2d25(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    1331 <stsmon_listentoport+0x1a1>
mov    0x2cdb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    0x2ca9(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x38(%rbp),%edi
mov    -0x34(%rbp),%edx
mov    %rax,%rcx
lea    0xd25(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    139d <stsmon_listentoport+0x20d>
mov    -0x1c(%rbp),%edi
mov    0x2c85(%rip),%rax
mov    (%rax),%esi
call   1080 <listen@plt>
cmp    $0x0,%rax
jge    1397 <stsmon_listentoport+0x207>
mov    0x2c75(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2c43(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x40(%rbp),%edi
mov    -0x3c(%rbp),%edx
mov    %rax,%rcx
lea    0xcdf(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    139d <stsmon_listentoport+0x20d>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stsmon_listentoport>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %esi,%r14d
mov    %edi,%r15d
mov    0x2e37(%rip),%r12
mov    (%r12),%edi
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     12bf <stsmon_listentoport+0x12f>
mov    %eax,%ebx
mov    0x2dd2(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1040 <fcntl@plt>
test   %eax,%eax
js     11fa <stsmon_listentoport+0x6a>
mov    0x2df4(%rip),%rcx
or     (%rcx),%eax
mov    0x2dcb(%rip),%rcx
mov    (%rcx),%esi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fcntl@plt>
movl   $0x1,0xc(%rsp)
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
mov    0x2d86(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
je     1257 <stsmon_listentoport+0xc7>
mov    0x2d96(%rip),%rax
mov    (%rax),%ebp
mov    0x2d6d(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdcc(%rip),%rsi
mov    %ebp,%edi
mov    %ebx,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r15d,0x18(%rsp)
mov    (%r12),%eax
mov    %eax,0x14(%rsp)
mov    %r14d,%edi
call   1050 <htons@plt>
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %rax,%rax
js     12eb <stsmon_listentoport+0x15b>
mov    0x2d33(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1080 <listen@plt>
test   %rax,%rax
jns    1323 <stsmon_listentoport+0x193>
mov    0x2d22(%rip),%rax
mov    (%rax),%ebp
mov    0x2cf9(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd9e(%rip),%rsi
jmp    1309 <stsmon_listentoport+0x179>
mov    0x2d02(%rip),%rax
mov    (%rax),%ebx
mov    0x2cd9(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd23(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    131e <stsmon_listentoport+0x18e>
mov    0x2cd6(%rip),%rax
mov    (%rax),%ebp
mov    0x2cad(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd32(%rip),%rsi
mov    %ebp,%edi
mov    %ebx,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"socket() failed: %s\0A\00"
@errno = global i32 0
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [38 x i8] c"setsockopt() failed on socket %d: %s\0A\00"
@.str.2 = [32 x i8] c"bind() failed on socket %d: %s\0A\00"
@SOMAXCONN = global i32 0
@.str.3 = [34 x i8] c"listen() failed on socket %d: %s\0A\00"
define i32 @stsmon_listentoport(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %7
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %17)
store i32 -1, i32* %3
br label %82
19: ; preds = %2
%20 = load i32, i32* %7
%21 = load i32, i32* @F_GETFL
%22 = call i32 (i32, i32, ...) @fcntl(i32 %20, i32 %21)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp sge i32 %23, 0
br i1 %24, label %25, label %33
25: ; preds = %19
%26 = load i32, i32* @O_NONBLOCK
%27 = load i32, i32* %8
%28 = or i32 %27, %26
store i32 %28, i32* %8
%29 = load i32, i32* %7
%30 = load i32, i32* @F_SETFL
%31 = load i32, i32* %8
%32 = call i32 (i32, i32, ...) @fcntl(i32 %29, i32 %30, i32 %31)
br label %33
33: ; preds = %25, %19
store i32 1, i32* %9
%34 = load i32, i32* %7
%35 = load i32, i32* @SOL_SOCKET
%36 = load i32, i32* @SO_REUSEADDR
%37 = bitcast i32* %9 to i8*
%38 = call i64 @setsockopt(i32 %34, i32 %35, i32 %36, i8* %37, i32 4)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %46
40: ; preds = %33
%41 = load i32, i32* @stderr
%42 = load i32, i32* %7
%43 = load i32, i32* @errno
%44 = call i8* @strerror(i32 %43)
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %42, i8* %44)
br label %46
46: ; preds = %40, %33
%47 = load i32, i32* %4
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
%49 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %48, i32 0, i32 0
store i32 %47, i32* %49
%50 = load i32, i32* @AF_INET
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %50, i32* %51
%52 = load i32, i32* %5
%53 = call i32 @htons(i32 %52)
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %53, i32* %54
%55 = load i32, i32* %7
%56 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%57 = call i64 @bind(i32 %55, %struct.sockaddr* %56, i32 12)
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %67
59: ; preds = %46
%60 = load i32, i32* @stderr
%61 = load i32, i32* %7
%62 = load i32, i32* @errno
%63 = call i8* @strerror(i32 %62)
%64 = call i32 (i32, i8*, ...) @fprintf(i32 %60, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %61, i8* %63)
%65 = load i32, i32* %7
%66 = call i32 @close(i32 %65)
store i32 -1, i32* %3
br label %82
67: ; preds = %46
%68 = load i32, i32* %7
%69 = load i32, i32* @SOMAXCONN
%70 = call i64 @listen(i32 %68, i32 %69)
%71 = icmp slt i64 %70, 0
br i1 %71, label %72, label %80
72: ; preds = %67
%73 = load i32, i32* @stderr
%74 = load i32, i32* %7
%75 = load i32, i32* @errno
%76 = call i8* @strerror(i32 %75)
%77 = call i32 (i32, i8*, ...) @fprintf(i32 %73, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i32 %74, i8* %76)
%78 = load i32, i32* %7
%79 = call i32 @close(i32 %78)
store i32 -1, i32* %3
br label %82
80: ; preds = %67
%81 = load i32, i32* %7
store i32 %81, i32* %3
br label %82
82: ; preds = %80, %72, %59, %14
%83 = load i32, i32* %3
ret i32 %83
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str = [21 x i8] c"socket() failed: %s\0A\00"
@errno = global i32 0
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [38 x i8] c"setsockopt() failed on socket %d: %s\0A\00"
@.str.2 = [32 x i8] c"bind() failed on socket %d: %s\0A\00"
@SOMAXCONN = global i32 0
@.str.3 = [34 x i8] c"listen() failed on socket %d: %s\0A\00"
define i32 @stsmon_listentoport(i32 %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = bitcast i32* %4 to i8*
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i32, i32* @errno
%14 = tail call i8* @strerror(i32 %13)
%15 = tail call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %14)
br label %60
16: ; preds = %2
%17 = load i32, i32* @F_GETFL
%18 = tail call i32 (i32, i32, ...) @fcntl(i32 %9, i32 %17)
%19 = icmp sgt i32 %18, -1
br i1 %19, label %20, label %25
20: ; preds = %16
%21 = load i32, i32* @O_NONBLOCK
%22 = or i32 %21, %18
%23 = load i32, i32* @F_SETFL
%24 = tail call i32 (i32, i32, ...) @fcntl(i32 %9, i32 %23, i32 %22)
br label %25
25: ; preds = %20, %16
store i32 1, i32* %4
%26 = load i32, i32* @SOL_SOCKET
%27 = load i32, i32* @SO_REUSEADDR
%28 = call i64 @setsockopt(i32 %9, i32 %26, i32 %27, i8* %6, i32 4)
%29 = icmp eq i64 %28, 0
br i1 %29, label %35, label %30
30: ; preds = %25
%31 = load i32, i32* @stderr
%32 = load i32, i32* @errno
%33 = call i8* @strerror(i32 %32)
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0), i32 %9, i8* %33)
br label %35
35: ; preds = %30, %25
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2, i32 0
store i32 %0, i32* %36
%37 = load i32, i32* @AF_INET
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %37, i32* %38
%39 = call i32 @htons(i32 %1)
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %39, i32* %40
%41 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%42 = call i64 @bind(i32 %9, %struct.sockaddr* %41, i32 12)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %50
44: ; preds = %35
%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 ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %9, i8* %47)
%49 = call i32 @close(i32 %9)
br label %60
50: ; preds = %35
%51 = load i32, i32* @SOMAXCONN
%52 = call i64 @listen(i32 %9, i32 %51)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %60
54: ; preds = %50
%55 = load i32, i32* @stderr
%56 = load i32, i32* @errno
%57 = call i8* @strerror(i32 %56)
%58 = call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i32 %9, i8* %57)
%59 = call i32 @close(i32 %9)
br label %60
60: ; preds = %50, %54, %44, %11
%61 = phi i32 [ -1, %11 ], [ -1, %44 ], [ -1, %54 ], [ %9, %50 ]
ret i32 %61
}