#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 */

/* Type definitions */
typedef int /*<<< orphan*/ uint16_t;
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, struct sockaddr *, int);
scalar_t__ listen(int, int /*<<< orphan*/);
scalar_t__ sockaddrfill(struct sockaddr_in *, char const *, char const *,
                        int /*<<< orphan*/, int /*<<< orphan*/, int);
int tcpstrlisten(int sock, const char *hostname, const char *service,
                 uint16_t queue) {
  struct sockaddr_in sa;
  if (sockaddrfill(&sa, hostname, service, AF_INET, SOCK_STREAM, 1) < 0) {
    return -1;
  }
  if (bind(sock, (struct sockaddr *)&sa, sizeof(struct sockaddr_in)) < 0) {
    return -1;
  }
  if (listen(sock, queue) < 0) {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<tcpstrlisten>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    0x2e93(%rip),%rax
mov    (%rax),%ecx
mov    0x2e7a(%rip),%rax
mov    (%rax),%r8d
lea    -0x20(%rbp),%rdi
mov    $0x1,%r9d
call   1030 <sockaddrfill@plt>
cmp    $0x0,%rax
jge    1186 <tcpstrlisten+0x56>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d5 <tcpstrlisten+0xa5>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1050 <bind@plt>
cmp    $0x0,%rax
jge    11ad <tcpstrlisten+0x7d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d5 <tcpstrlisten+0xa5>
mov    -0x8(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1040 <listen@plt>
cmp    $0x0,%rax
jge    11ce <tcpstrlisten+0x9e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d5 <tcpstrlisten+0xa5>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcpstrlisten>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %ecx,%r14d
mov    %edi,%ebp
mov    0x2ea4(%rip),%rax
mov    (%rax),%ecx
mov    0x2e8b(%rip),%rax
mov    (%rax),%r8d
lea    0x8(%rsp),%rdi
mov    $0x1,%r9d
call   1030 <sockaddrfill@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
js     1191 <tcpstrlisten+0x61>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0x4,%edx
call   1050 <bind@plt>
test   %rax,%rax
js     1191 <tcpstrlisten+0x61>
mov    %ebp,%edi
mov    %r14d,%esi
call   1040 <listen@plt>
mov    %rax,%rbx
sar    $0x3f,%rbx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @tcpstrlisten(i32 %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
store i32 %0, i32* %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%11 = load i8*, i8** %7
%12 = load i8*, i8** %8
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = call i64 @sockaddrfill(%struct.sockaddr_in* %10, i8* %11, i8* %12, i32 %13, i32 %14, i32 1)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %4
store i32 -1, i32* %5
br label %31
18: ; preds = %4
%19 = load i32, i32* %6
%20 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%21 = call i64 @bind(i32 %19, %struct.sockaddr* %20, i32 4)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %18
store i32 -1, i32* %5
br label %31
24: ; preds = %18
%25 = load i32, i32* %6
%26 = load i32, i32* %9
%27 = call i64 @listen(i32 %25, i32 %26)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %24
store i32 -1, i32* %5
br label %31
30: ; preds = %24
store i32 0, i32* %5
br label %31
31: ; preds = %30, %29, %23, %17
%32 = load i32, i32* %5
ret i32 %32
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
define i32 @tcpstrlisten(i32 %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca %struct.sockaddr_in
%6 = bitcast %struct.sockaddr_in* %5 to i8*
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = call i64 @sockaddrfill(%struct.sockaddr_in* %5, i8* %1, i8* %2, i32 %7, i32 %8, i32 1)
%10 = icmp slt i64 %9, 0
br i1 %10, label %19, label %11
11: ; preds = %4
%12 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%13 = call i64 @bind(i32 %0, %struct.sockaddr* %12, i32 4)
%14 = icmp slt i64 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %11
%16 = call i64 @listen(i32 %0, i32 %3)
%17 = ashr i64 %16, 63
%18 = trunc i64 %17 to i32
br label %19
19: ; preds = %15, %11, %4
%20 = phi i32 [ -1, %4 ], [ -1, %11 ], [ %18, %15 ]
ret i32 %20
}