#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 /*<<< orphan*/ on;
typedef int /*<<< orphan*/ addr;

/* 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*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_aton(char *, TYPE_1__ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ spy_log(int, char *, char *);
char *strerror(int /*<<< orphan*/);
int spy_create_listen_socket(int port, char *bind_addr) {
  int fd, on = 1;
  struct sockaddr_in addr;
  if ((fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    spy_log(3, "create socket failed, %s", strerror(errno));
    return -1;
  }
  if ((setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))) == -1) {
    spy_log(3, "set reuse addr failed, %s", strerror(errno));
    return -1;
  }
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  addr.sin_addr.s_addr = htonl(INADDR_ANY);
  if (bind_addr && inet_aton(bind_addr, &addr.sin_addr) == 0) {
    spy_log(3, "bind sockaddr %s failed, invalid ip address", bind_addr);
    close(fd);
    return -1;
  }
  if (bind(fd, (struct sockaddr *)&addr, sizeof(addr))) {
    spy_log(3, "bind socket failed, error %s", strerror(errno));
    close(fd);
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<spy_create_listen_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x18(%rbp)
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1209 <spy_create_listen_socket+0x69>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rdx
mov    $0x3,%edi
lea    0xe08(%rip),%rsi
call   1030 <spy_log@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133e <spy_create_listen_socket+0x19e>
mov    -0x14(%rbp),%edi
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
mov    0x2d9c(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1264 <spy_create_listen_socket+0xc4>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rdx
mov    $0x3,%edi
lea    0xdc6(%rip),%rsi
call   1030 <spy_log@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133e <spy_create_listen_socket+0x19e>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2d65(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    0x2d46(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     12e7 <spy_create_listen_socket+0x147>
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1090 <inet_aton@plt>
cmp    $0x0,%rax
jne    12e7 <spy_create_listen_socket+0x147>
mov    -0x10(%rbp),%rdx
mov    $0x3,%edi
lea    0xd65(%rip),%rsi
call   1030 <spy_log@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133e <spy_create_listen_socket+0x19e>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
je     1338 <spy_create_listen_socket+0x198>
mov    0x2cb7(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    %rax,%rdx
mov    $0x3,%edi
lea    0xd40(%rip),%rsi
call   1030 <spy_log@plt>
mov    -0x14(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133e <spy_create_listen_socket+0x19e>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spy_create_listen_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
movl   $0x1,0x14(%rsp)
mov    0x2e22(%rip),%r15
mov    (%r15),%edi
mov    0x2e00(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     128a <spy_create_listen_socket+0xea>
mov    %eax,%ebx
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
mov    0x2dcc(%rip),%rax
mov    (%rax),%edx
lea    0x14(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1040 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     12a1 <spy_create_listen_socket+0x101>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
call   1050 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x8(%rsp)
test   %r14,%r14
je     1255 <spy_create_listen_socket+0xb5>
lea    0x8(%rsp),%rsi
mov    %r14,%rdi
call   1090 <inet_aton@plt>
test   %rax,%rax
je     12c5 <spy_create_listen_socket+0x125>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10a0 <bind@plt>
test   %rax,%rax
je     12e5 <spy_create_listen_socket+0x145>
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xddf(%rip),%rsi
mov    $0x3,%edi
mov    %rax,%rdx
jmp    12d4 <spy_create_listen_socket+0x134>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd61(%rip),%rsi
jmp    12b6 <spy_create_listen_socket+0x116>
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xd63(%rip),%rsi
mov    $0x3,%edi
mov    %rax,%rdx
call   1030 <spy_log@plt>
jmp    12e0 <spy_create_listen_socket+0x140>
lea    0xd67(%rip),%rsi
mov    $0x3,%edi
mov    %r14,%rdx
call   1030 <spy_log@plt>
mov    %ebx,%edi
call   1080 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [25 x i8] c"create socket failed, %s\00"
@errno = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.1 = [26 x i8] c"set reuse addr failed, %s\00"
@INADDR_ANY = global i32 0
@.str.2 = [44 x i8] c"bind sockaddr %s failed, invalid ip address\00"
@.str.3 = [29 x i8] c"bind socket failed, error %s\00"
define i32 @spy_create_listen_socket(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%6 = bitcast %struct.sockaddr_in* %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 eq i32 %9, -1
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i32, i32* @errno
%13 = tail call i8* @strerror(i32 %12)
%14 = tail call i32 @spy_log(i32 3, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* %13)
br label %50
15: ; preds = %2
%16 = load i32, i32* @SOL_SOCKET
%17 = load i32, i32* @SO_REUSEADDR
%18 = call i32 @setsockopt(i32 %9, i32 %16, i32 %17, i32* %3, i32 4)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %24
20: ; preds = %15
%21 = load i32, i32* @errno
%22 = call i8* @strerror(i32 %21)
%23 = call i32 @spy_log(i32 3, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %22)
br label %50
24: ; preds = %15
%25 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%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 = call i32 @htons(i32 %0)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %28, i32* %29
%30 = load i32, i32* @INADDR_ANY
%31 = call i32 @htonl(i32 %30)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
store i32 %31, i32* %32
%33 = icmp eq i8* %1, null
br i1 %33, label %41, label %34
34: ; preds = %24
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%36 = call i64 @inet_aton(i8* %1, %struct.TYPE_2__* %35)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %34
%39 = call i32 @spy_log(i32 3, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i8* %1)
%40 = call i32 @close(i32 %9)
br label %50
41: ; preds = %34, %24
%42 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%43 = call i64 @bind(i32 %9, %struct.sockaddr* %42, i32 12)
%44 = icmp eq i64 %43, 0
br i1 %44, label %50, label %45
45: ; preds = %41
%46 = load i32, i32* @errno
%47 = call i8* @strerror(i32 %46)
%48 = call i32 @spy_log(i32 3, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %47)
%49 = call i32 @close(i32 %9)
br label %50
50: ; preds = %41, %45, %38, %20, %11
%51 = phi i32 [ -1, %11 ], [ -1, %20 ], [ -1, %38 ], [ -1, %45 ], [ %9, %41 ]
ret i32 %51
}