#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 /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
};

/* Variables and functions */
int bind(int, int /*<<< orphan*/, int /*<<< orphan*/);
int idrnet_getaddrinfo(struct addrinfo **, char *, int, int, int);
int idrnet_bind(int sockfd, int family, int socket_type, char *host, int port) {
  struct addrinfo *address_res;
  int addr_res =
      idrnet_getaddrinfo(&address_res, host, port, family, socket_type);
  if (addr_res == -1) {
    return -1;
  }
  int bind_res = bind(sockfd, address_res->ai_addr, address_res->ai_addrlen);
  if (bind_res == -1) {
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<idrnet_bind>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0xc(%rbp),%ecx
mov    -0x10(%rbp),%r8d
lea    -0x28(%rbp),%rdi
call   1040 <idrnet_getaddrinfo@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1169 <idrnet_bind+0x49>
movl   $0xffffffff,-0x4(%rbp)
jmp    119e <idrnet_bind+0x7e>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   1030 <bind@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    1197 <idrnet_bind+0x77>
movl   $0xffffffff,-0x4(%rbp)
jmp    119e <idrnet_bind+0x7e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<idrnet_bind>:
push   %rbp
push   %rbx
push   %rax
mov    %edx,%eax
mov    %esi,%ebx
mov    %edi,%ebp
mov    %rsp,%rdi
mov    %rcx,%rsi
mov    %r8d,%edx
mov    %ebx,%ecx
mov    %eax,%r8d
call   1040 <idrnet_getaddrinfo@plt>
cmp    $0xffffffff,%eax
je     115f <idrnet_bind+0x3f>
mov    (%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebp,%edi
call   1030 <bind@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
sete   %al
neg    %eax
jmp    1164 <idrnet_bind+0x44>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32 }
define i32 @idrnet_bind(i32 %0, i32 %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca %struct.addrinfo*
%7 = bitcast %struct.addrinfo** %6 to i8*
%8 = call i32 @idrnet_getaddrinfo(%struct.addrinfo** %6, i8* %3, i32 %4, i32 %1, i32 %2)
%9 = icmp eq i32 %8, -1
br i1 %9, label %19, label %10
10: ; preds = %5
%11 = load %struct.addrinfo*, %struct.addrinfo** %6
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %11, i64 0, i32 1
%13 = load i32, i32* %12
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %11, i64 0, i32 0
%15 = load i32, i32* %14
%16 = call i32 @bind(i32 %0, i32 %13, i32 %15)
%17 = icmp eq i32 %16, -1
%18 = sext i1 %17 to i32
br label %19
19: ; preds = %5, %10
%20 = phi i32 [ %18, %10 ], [ -1, %5 ]
ret i32 %20
}