#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 {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ optval;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int FD_CLOEXEC;
int /*<<< orphan*/ F_GETFD;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int errno;
int fcntl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(unsigned short);
int inet_pton(int /*<<< orphan*/, char const *, TYPE_1__ *);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int ss_listen_on_ipv4_socket_impl(const char *address, unsigned short port,
                                  int backlog) {
  struct sockaddr_in addr;
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  if (address) {
    if (!(inet_pton(AF_INET, address, &(addr.sin_addr)) == 1))
      return -1;
  } else
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
  int sock = socket(AF_INET, SOCK_STREAM, 0);
  if (sock == -1)
    return -2;
  fcntl(sock, F_SETFD, fcntl(sock, F_GETFD) | FD_CLOEXEC);
  int optval = 1;
  setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
  addr.sin_port = htons(port);
  if ((bind(sock, (struct sockaddr *)&addr, sizeof(addr))) == -1) {
    int saved_errno = errno;
    close(sock);
    errno = saved_errno;
    return -3;
  }
  if ((listen(sock, backlog)) == -1) {
    int saved_errno = errno;
    close(sock);
    errno = saved_errno;
    return -4;
  }
  return sock;
}


x86 assembly (clang, -O0):
<ss_listen_on_ipv4_socket_impl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %si,%ax
mov    %rdi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
mov    %edx,-0x18(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1211 <ss_listen_on_ipv4_socket_impl+0x71>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
lea    -0x28(%rbp),%rdx
add    $0x4,%rdx
call   1090 <inet_pton@plt>
cmp    $0x1,%eax
je     120c <ss_listen_on_ipv4_socket_impl+0x6c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1354 <ss_listen_on_ipv4_socket_impl+0x1b4>
jmp    1222 <ss_listen_on_ipv4_socket_impl+0x82>
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x24(%rbp)
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
mov    0x2d8e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1254 <ss_listen_on_ipv4_socket_impl+0xb4>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1354 <ss_listen_on_ipv4_socket_impl+0x1b4>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    0x2d67(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2d38(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1040 <fcntl@plt>
mov    -0x40(%rbp),%edi
mov    -0x3c(%rbp),%esi
mov    %eax,%edx
mov    0x2d30(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1040 <fcntl@plt>
movl   $0x1,-0x30(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2d46(%rip),%rax
mov    (%rax),%esi
mov    0x2cf5(%rip),%rax
mov    (%rax),%edx
lea    -0x30(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
movzwl -0x12(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0xffffffff,%eax
jne    130e <ss_listen_on_ipv4_socket_impl+0x16e>
mov    0x2cc7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x2c(%rbp),%edi
call   1080 <close@plt>
mov    -0x34(%rbp),%ecx
mov    0x2cb0(%rip),%rax
mov    %ecx,(%rax)
movl   $0xfffffffd,-0x4(%rbp)
jmp    1354 <ss_listen_on_ipv4_socket_impl+0x1b4>
mov    -0x2c(%rbp),%edi
mov    -0x18(%rbp),%esi
call   10a0 <listen@plt>
cmp    $0xffffffff,%eax
jne    134e <ss_listen_on_ipv4_socket_impl+0x1ae>
mov    0x2c87(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x2c(%rbp),%edi
call   1080 <close@plt>
mov    -0x38(%rbp),%ecx
mov    0x2c70(%rip),%rax
mov    %ecx,(%rax)
movl   $0xfffffffc,-0x4(%rbp)
jmp    1354 <ss_listen_on_ipv4_socket_impl+0x1b4>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ss_listen_on_ipv4_socket_impl>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r14d
mov    %esi,%r15d
mov    %rdi,%rbp
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2e15(%rip),%r12
mov    (%r12),%ebx
mov    %ebx,0x8(%rsp)
test   %rbp,%rbp
je     11fb <ss_listen_on_ipv4_socket_impl+0x5b>
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
mov    %rbp,%rsi
call   1090 <inet_pton@plt>
mov    $0xffffffff,%ebx
cmp    $0x1,%eax
jne    12e2 <ss_listen_on_ipv4_socket_impl+0x142>
mov    (%r12),%ebx
jmp    120d <ss_listen_on_ipv4_socket_impl+0x6d>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x4(%rsp)
mov    0x2dac(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     12be <ss_listen_on_ipv4_socket_impl+0x11e>
mov    %eax,%ebx
mov    0x2d97(%rip),%rax
mov    (%rax),%ebp
mov    0x2d6e(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1040 <fcntl@plt>
mov    0x2d6c(%rip),%rcx
or     (%rcx),%eax
mov    %ebx,%edi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <fcntl@plt>
movl   $0x1,0xc(%rsp)
mov    0x2d7e(%rip),%rax
mov    (%rax),%esi
mov    0x2d2d(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
movzwl %r15w,%edi
call   1050 <htons@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0xffffffff,%eax
je     12c5 <ss_listen_on_ipv4_socket_impl+0x125>
mov    %ebx,%edi
mov    %r14d,%esi
call   10a0 <listen@plt>
cmp    $0xffffffff,%eax
jne    12e2 <ss_listen_on_ipv4_socket_impl+0x142>
mov    $0xfffffffc,%r15d
jmp    12cb <ss_listen_on_ipv4_socket_impl+0x12b>
mov    $0xfffffffe,%ebx
jmp    12e2 <ss_listen_on_ipv4_socket_impl+0x142>
mov    $0xfffffffd,%r15d
mov    0x2cde(%rip),%r14
mov    (%r14),%ebp
mov    %ebx,%edi
call   1080 <close@plt>
mov    %ebp,(%r14)
mov    %r15d,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
@F_SETFD = global i32 0
@F_GETFD = global i32 0
@FD_CLOEXEC = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@errno = global i32 0
define i32 @ss_listen_on_ipv4_socket_impl(i8* %0, i16 zeroext %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i16
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %5
store i16 %1, i16* %6
store i32 %2, i32* %7
%13 = call i32 @memset(%struct.sockaddr_in* %8, i32 0, i32 12)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %14, i32* %15
%16 = load i8*, i8** %5
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %26
18: ; preds = %3
%19 = load i32, i32* @AF_INET
%20 = load i8*, i8** %5
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%22 = call i32 @inet_pton(i32 %19, i8* %20, %struct.TYPE_2__* %21)
%23 = icmp eq i32 %22, 1
br i1 %23, label %25, label %24
24: ; preds = %18
store i32 -1, i32* %4
br label %75
25: ; preds = %18
br label %31
26: ; preds = %3
%27 = load i32, i32* @INADDR_ANY
%28 = call i32 @htonl(i32 %27)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%30 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %29, i32 0, i32 0
store i32 %28, i32* %30
br label %31
31: ; preds = %26, %25
%32 = load i32, i32* @AF_INET
%33 = load i32, i32* @SOCK_STREAM
%34 = call i32 @socket(i32 %32, i32 %33, i32 0)
store i32 %34, i32* %9
%35 = load i32, i32* %9
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %38
37: ; preds = %31
store i32 -2, i32* %4
br label %75
38: ; preds = %31
%39 = load i32, i32* %9
%40 = load i32, i32* @F_SETFD
%41 = load i32, i32* %9
%42 = load i32, i32* @F_GETFD
%43 = call i32 (i32, i32, ...) @fcntl(i32 %41, i32 %42)
%44 = load i32, i32* @FD_CLOEXEC
%45 = or i32 %43, %44
%46 = call i32 (i32, i32, ...) @fcntl(i32 %39, i32 %40, i32 %45)
store i32 1, i32* %10
%47 = load i32, i32* %9
%48 = load i32, i32* @SOL_SOCKET
%49 = load i32, i32* @SO_REUSEADDR
%50 = call i32 @setsockopt(i32 %47, i32 %48, i32 %49, i32* %10, i32 4)
%51 = load i16, i16* %6
%52 = call i32 @htons(i16 zeroext %51)
%53 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %52, i32* %53
%54 = load i32, i32* %9
%55 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%56 = call i32 @bind(i32 %54, %struct.sockaddr* %55, i32 12)
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %63
58: ; preds = %38
%59 = load i32, i32* @errno
store i32 %59, i32* %11
%60 = load i32, i32* %9
%61 = call i32 @close(i32 %60)
%62 = load i32, i32* %11
store i32 %62, i32* @errno
store i32 -3, i32* %4
br label %75
63: ; preds = %38
%64 = load i32, i32* %9
%65 = load i32, i32* %7
%66 = call i32 @listen(i32 %64, i32 %65)
%67 = icmp eq i32 %66, -1
br i1 %67, label %68, label %73
68: ; preds = %63
%69 = load i32, i32* @errno
store i32 %69, i32* %12
%70 = load i32, i32* %9
%71 = call i32 @close(i32 %70)
%72 = load i32, i32* %12
store i32 %72, i32* @errno
store i32 -4, i32* %4
br label %75
73: ; preds = %63
%74 = load i32, i32* %9
store i32 %74, i32* %4
br label %75
75: ; preds = %73, %68, %58, %37, %24
%76 = load i32, i32* %4
ret i32 %76
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
@F_SETFD = global i32 0
@F_GETFD = global i32 0
@FD_CLOEXEC = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@errno = global i32 0
define i32 @ss_listen_on_ipv4_socket_impl(i8* %0, i16 zeroext %1, i32 %2) {
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %8, i32* %9
%10 = icmp eq i8* %0, null
br i1 %10, label %17, label %11
11: ; preds = %3
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
%13 = call i32 @inet_pton(i32 %8, i8* %0, %struct.TYPE_2__* %12)
%14 = icmp eq i32 %13, 1
br i1 %14, label %15, label %51
15: ; preds = %11
%16 = load i32, i32* @AF_INET
br label %21
17: ; preds = %3
%18 = load i32, i32* @INADDR_ANY
%19 = call i32 @htonl(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %19, i32* %20
br label %21
21: ; preds = %15, %17
%22 = phi i32 [ %16, %15 ], [ %8, %17 ]
%23 = load i32, i32* @SOCK_STREAM
%24 = call i32 @socket(i32 %22, i32 %23, i32 0)
%25 = icmp eq i32 %24, -1
br i1 %25, label %51, label %26
26: ; preds = %21
%27 = load i32, i32* @F_SETFD
%28 = load i32, i32* @F_GETFD
%29 = call i32 (i32, i32, ...) @fcntl(i32 %24, i32 %28)
%30 = load i32, i32* @FD_CLOEXEC
%31 = or i32 %30, %29
%32 = call i32 (i32, i32, ...) @fcntl(i32 %24, i32 %27, i32 %31)
%33 = bitcast i32* %5 to i8*
store i32 1, i32* %5
%34 = load i32, i32* @SOL_SOCKET
%35 = load i32, i32* @SO_REUSEADDR
%36 = call i32 @setsockopt(i32 %24, i32 %34, i32 %35, i32* %5, i32 4)
%37 = call i32 @htons(i16 zeroext %1)
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %37, i32* %38
%39 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%40 = call i32 @bind(i32 %24, %struct.sockaddr* %39, i32 12)
%41 = icmp eq i32 %40, -1
br i1 %41, label %45, label %42
42: ; preds = %26
%43 = call i32 @listen(i32 %24, i32 %2)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %49
45: ; preds = %42, %26
%46 = phi i32 [ -3, %26 ], [ -4, %42 ]
%47 = load i32, i32* @errno
%48 = call i32 @close(i32 %24)
store i32 %47, i32* @errno
br label %49
49: ; preds = %45, %42
%50 = phi i32 [ %24, %42 ], [ %46, %45 ]
br label %51
51: ; preds = %49, %21, %11
%52 = phi i32 [ -1, %11 ], [ %50, %49 ], [ -2, %21 ]
ret i32 %52
}