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

/* 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 errno;
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char const *);
scalar_t__ 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*/ zclose(int);
scalar_t__ zempty(char const *);
int zinet_listen(const char *sip, int port, int backlog) {
  int sock;
  int on = 1;
  struct sockaddr_in addr;
  int errno2;
  memset(&addr, 0, sizeof(struct sockaddr_in));
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  addr.sin_addr.s_addr = (zempty(sip) ? INADDR_ANY : inet_addr(sip));
  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    return -1;
  }
  if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0) {
    goto err;
  }
  if (bind(sock, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) {
    goto err;
  }
  if (listen(sock, backlog) < 0) {
    goto err;
  }
  return (sock);
err:
  errno2 = errno;
  zclose(sock);
  errno = errno2;
  return -1;
}


x86 assembly (clang, -O0):
<zinet_listen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0x1,-0x20(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   10a0 <zempty@plt>
cmp    $0x0,%rax
je     11f4 <zinet_listen+0x64>
mov    0x2dee(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
jmp    1200 <zinet_listen+0x70>
mov    -0x10(%rbp),%rdi
call   1070 <inet_addr@plt>
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1237 <zinet_listen+0xa7>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d6 <zinet_listen+0x146>
mov    -0x1c(%rbp),%edi
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
mov    0x2d6e(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    126a <zinet_listen+0xda>
jmp    12af <zinet_listen+0x11f>
mov    -0x1c(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
jge    128a <zinet_listen+0xfa>
jmp    12af <zinet_listen+0x11f>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1080 <listen@plt>
cmp    $0x0,%rax
jge    12a4 <zinet_listen+0x114>
jmp    12af <zinet_listen+0x11f>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12d6 <zinet_listen+0x146>
mov    0x2d0a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <zclose@plt>
mov    -0x34(%rbp),%ecx
mov    0x2cf3(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<zinet_listen>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r14d
mov    %esi,%ebp
mov    %rdi,%rbx
movl   $0x1,0x4(%rsp)
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e1e(%rip),%r15
mov    (%r15),%eax
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    %rbx,%rdi
call   10a0 <zempty@plt>
test   %rax,%rax
je     11ec <zinet_listen+0x5c>
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
jmp    11f4 <zinet_listen+0x64>
mov    %rbx,%rdi
call   1070 <inet_addr@plt>
mov    %eax,0x8(%rsp)
mov    (%r15),%edi
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    $0xffffffff,%r15d
test   %eax,%eax
js     127e <zinet_listen+0xee>
mov    %eax,%ebp
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
mov    0x2d91(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     126a <zinet_listen+0xda>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %rax,%rax
js     126a <zinet_listen+0xda>
mov    %ebp,%edi
mov    %r14d,%esi
call   1080 <listen@plt>
test   %rax,%rax
js     126a <zinet_listen+0xda>
mov    %ebp,%r15d
jmp    127e <zinet_listen+0xee>
mov    0x2d4f(%rip),%r14
mov    (%r14),%ebx
mov    %ebp,%edi
call   1050 <zclose@plt>
mov    %ebx,(%r14)
mov    %r15d,%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
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@errno = global i32 0
define i32 @zinet_listen(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
%11 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 1, i32* %9
%12 = call i32 @memset(%struct.sockaddr_in* %10, i32 0, i32 12)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %13, i32* %14
%15 = load i32, i32* %6
%16 = call i32 @htons(i32 %15)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i32 %16, i32* %17
%18 = load i8*, i8** %5
%19 = call i64 @zempty(i8* %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %3
%22 = load i32, i32* @INADDR_ANY
br label %26
23: ; preds = %3
%24 = load i8*, i8** %5
%25 = call i32 @inet_addr(i8* %24)
br label %26
26: ; preds = %23, %21
%27 = phi i32 [ %22, %21 ], [ %25, %23 ]
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, 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* %8
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %26
store i32 -1, i32* %4
br label %62
35: ; preds = %26
%36 = load i32, i32* %8
%37 = load i32, i32* @SOL_SOCKET
%38 = load i32, i32* @SO_REUSEADDR
%39 = bitcast i32* %9 to i8*
%40 = call i64 @setsockopt(i32 %36, i32 %37, i32 %38, i8* %39, i32 4)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %35
br label %57
43: ; preds = %35
%44 = load i32, i32* %8
%45 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%46 = call i64 @bind(i32 %44, %struct.sockaddr* %45, i32 12)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %43
br label %57
49: ; preds = %43
%50 = load i32, i32* %8
%51 = load i32, i32* %7
%52 = call i64 @listen(i32 %50, i32 %51)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %49
br label %57
55: ; preds = %49
%56 = load i32, i32* %8
store i32 %56, i32* %4
br label %62
57: ; preds = %54, %48, %42
%58 = load i32, i32* @errno
store i32 %58, i32* %11
%59 = load i32, i32* %8
%60 = call i32 @zclose(i32 %59)
%61 = load i32, i32* %11
store i32 %61, i32* @errno
store i32 -1, i32* %4
br label %62
62: ; preds = %57, %55, %34
%63 = load i32, i32* %4
ret i32 %63
}

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
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@errno = global i32 0
define i32 @zinet_listen(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%7 = bitcast %struct.sockaddr_in* %5 to i8*
%8 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 12)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %9, i32* %10
%11 = call i32 @htons(i32 %1)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
store i32 %11, i32* %12
%13 = call i64 @zempty(i8* %0)
%14 = icmp eq i64 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %3
%16 = load i32, i32* @INADDR_ANY
br label %19
17: ; preds = %3
%18 = call i32 @inet_addr(i8* %0)
br label %19
19: ; preds = %17, %15
%20 = phi i32 [ %16, %15 ], [ %18, %17 ]
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0, i32 0
store i32 %20, i32* %21
%22 = load i32, i32* @AF_INET
%23 = load i32, i32* @SOCK_STREAM
%24 = call i32 @socket(i32 %22, i32 %23, i32 0)
%25 = icmp slt i32 %24, 0
br i1 %25, label %41, label %26
26: ; preds = %19
%27 = load i32, i32* @SOL_SOCKET
%28 = load i32, i32* @SO_REUSEADDR
%29 = call i64 @setsockopt(i32 %24, i32 %27, i32 %28, i8* %6, i32 4)
%30 = icmp slt i64 %29, 0
br i1 %30, label %38, label %31
31: ; preds = %26
%32 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%33 = call i64 @bind(i32 %24, %struct.sockaddr* %32, i32 12)
%34 = icmp slt i64 %33, 0
br i1 %34, label %38, label %35
35: ; preds = %31
%36 = call i64 @listen(i32 %24, i32 %2)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %35, %31, %26
%39 = load i32, i32* @errno
%40 = call i32 @zclose(i32 %24)
store i32 %39, i32* @errno
br label %41
41: ; preds = %35, %19, %38
%42 = phi i32 [ -1, %38 ], [ -1, %19 ], [ %24, %35 ]
ret i32 %42
}