#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 */
struct addrinfo {
  int ai_family;
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  struct addrinfo *ai_next;
  scalar_t__ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
};
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int getaddrinfo(char const *, int /*<<< orphan*/ *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int fetch_bind(int sd, int af, const char *addr) {
  struct addrinfo hints, *res, *res0;
  int err;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = af;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_protocol = 0;
  if ((err = getaddrinfo(addr, NULL, &hints, &res0)) != 0)
    return (-1);
  for (res = res0; res; res = res->ai_next)
    if (bind(sd, res->ai_addr, res->ai_addrlen) == 0)
      return (0);
  return (-1);
}


x86 assembly (clang, -O0):
<fetch_bind>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1030 <memset@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    0x2e81(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x40(%rbp),%rdx
lea    -0x50(%rbp),%rcx
call   1050 <getaddrinfo@plt>
mov    %eax,-0x54(%rbp)
cmp    $0x0,%eax
je     1199 <fetch_bind+0x69>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f5 <fetch_bind+0xc5>
mov    -0x50(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     11ee <fetch_bind+0xbe>
mov    -0x8(%rbp),%edi
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%esi
mov    -0x48(%rbp),%rax
mov    0x4(%rax),%edx
call   1040 <bind@plt>
cmp    $0x0,%rax
jne    11d8 <fetch_bind+0xa8>
movl   $0x0,-0x4(%rbp)
jmp    11f5 <fetch_bind+0xc5>
jmp    11dd <fetch_bind+0xad>
mov    -0x48(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x48(%rbp)
jmp    11a1 <fetch_bind+0x71>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fetch_bind>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %rdx,%r14
mov    %esi,%ebp
mov    %edi,%ebx
lea    0x10(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1030 <memset@plt>
mov    %ebp,0x10(%rsp)
mov    0x2e80(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
movq   $0x0,0x28(%rsp)
lea    0x8(%rsp),%rcx
mov    %r14,%rdi
xor    %esi,%esi
mov    %r15,%rdx
call   1050 <getaddrinfo@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
jne    11c0 <fetch_bind+0x90>
mov    0x8(%rsp),%rbp
test   %rbp,%rbp
je     11c0 <fetch_bind+0x90>
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x4(%rbp),%edx
mov    0x8(%rbp),%esi
mov    %ebx,%edi
call   1040 <bind@plt>
test   %rax,%rax
je     11bd <fetch_bind+0x8d>
mov    0x10(%rbp),%rbp
test   %rbp,%rbp
jne    11a0 <fetch_bind+0x70>
jmp    11c0 <fetch_bind+0x90>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, %struct.addrinfo*, i64, i32 }
@SOCK_STREAM = global i32 0
define i32 @fetch_bind(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.addrinfo
%9 = alloca %struct.addrinfo*
%10 = alloca %struct.addrinfo*
%11 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%12 = call i32 @memset(%struct.addrinfo* %8, i32 0, i32 40)
%13 = load i32, i32* %6
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 0
store i32 %13, i32* %14
%15 = load i32, i32* @SOCK_STREAM
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 5
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 4
store i64 0, i64* %17
%18 = load i8*, i8** %7
%19 = call i32 @getaddrinfo(i8* %18, i32* null, %struct.addrinfo* %8, %struct.addrinfo** %10)
store i32 %19, i32* %11
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %3
store i32 -1, i32* %4
br label %44
22: ; preds = %3
%23 = load %struct.addrinfo*, %struct.addrinfo** %10
store %struct.addrinfo* %23, %struct.addrinfo** %9
br label %24
24: ; preds = %39, %22
%25 = load %struct.addrinfo*, %struct.addrinfo** %9
%26 = icmp ne %struct.addrinfo* %25, null
br i1 %26, label %27, label %43
27: ; preds = %24
%28 = load i32, i32* %5
%29 = load %struct.addrinfo*, %struct.addrinfo** %9
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %29, i32 0, i32 2
%31 = load i32, i32* %30
%32 = load %struct.addrinfo*, %struct.addrinfo** %9
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %32, i32 0, i32 1
%34 = load i32, i32* %33
%35 = call i64 @bind(i32 %28, i32 %31, i32 %34)
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %27
store i32 0, i32* %4
br label %44
38: ; preds = %27
br label %39
39: ; preds = %38
%40 = load %struct.addrinfo*, %struct.addrinfo** %9
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %40, i32 0, i32 3
%42 = load %struct.addrinfo*, %struct.addrinfo** %41
store %struct.addrinfo* %42, %struct.addrinfo** %9
br label %24
43: ; preds = %24
store i32 -1, i32* %4
br label %44
44: ; preds = %43, %37, %21
%45 = load i32, i32* %4
ret i32 %45
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, %struct.addrinfo*, i64, i32 }
@SOCK_STREAM = global i32 0
define i32 @fetch_bind(i32 %0, i32 %1, i8* %2) {
%4 = alloca %struct.addrinfo
%5 = alloca %struct.addrinfo*
%6 = bitcast %struct.addrinfo* %4 to i8*
%7 = bitcast %struct.addrinfo** %5 to i8*
%8 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 40)
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 0
store i32 %1, i32* %9
%10 = load i32, i32* @SOCK_STREAM
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 5
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 4
store i64 0, i64* %12
%13 = call i32 @getaddrinfo(i8* %2, i32* null, %struct.addrinfo* %4, %struct.addrinfo** %5)
%14 = icmp ne i32 %13, 0
%15 = load %struct.addrinfo*, %struct.addrinfo** %5
%16 = icmp eq %struct.addrinfo* %15, null
%17 = select i1 %14, i1 true, i1 %16
br i1 %17, label %30, label %22
18: ; preds = %22
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 3
%20 = load %struct.addrinfo*, %struct.addrinfo** %19
%21 = icmp eq %struct.addrinfo* %20, null
br i1 %21, label %30, label %22
22: ; preds = %3, %18
%23 = phi %struct.addrinfo* [ %20, %18 ], [ %15, %3 ]
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 2
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 1
%27 = load i32, i32* %26
%28 = call i64 @bind(i32 %0, i32 %25, i32 %27)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %18
30: ; preds = %22, %18, %3
%31 = phi i32 [ -1, %3 ], [ 0, %22 ], [ -1, %18 ]
ret i32 %31
}