#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 connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int idrnet_getaddrinfo(struct addrinfo **, char *, int, int, int);
int idrnet_connect(int sockfd, int family, int socket_type, char *host,
                   int port) {
  struct addrinfo *remote_host;
  int addr_res =
      idrnet_getaddrinfo(&remote_host, host, port, family, socket_type);
  if (addr_res == -1) {
    return -1;
  }
  int connect_res =
      connect(sockfd, remote_host->ai_addr, remote_host->ai_addrlen);
  if (connect_res == -1) {
    freeaddrinfo(remote_host);
    return -1;
  }
  freeaddrinfo(remote_host);
  return 0;
}


x86 assembly (clang, -O0):
<idrnet_connect>:
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    1179 <idrnet_connect+0x49>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c0 <idrnet_connect+0x90>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
call   1030 <connect@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    11b0 <idrnet_connect+0x80>
mov    -0x28(%rbp),%rdi
call   1050 <freeaddrinfo@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c0 <idrnet_connect+0x90>
mov    -0x28(%rbp),%rdi
call   1050 <freeaddrinfo@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<idrnet_connect>:
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     1176 <idrnet_connect+0x46>
mov    (%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebp,%edi
call   1030 <connect@plt>
xor    %ebx,%ebx
cmp    $0xffffffff,%eax
sete   %bl
mov    (%rsp),%rdi
call   1050 <freeaddrinfo@plt>
neg    %ebx
jmp    117b <idrnet_connect+0x4b>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32 }
define i32 @idrnet_connect(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 %40
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 @connect(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 %37
34: ; preds = %23
%35 = load %struct.addrinfo*, %struct.addrinfo** %12
%36 = call i32 @freeaddrinfo(%struct.addrinfo* %35)
store i32 -1, i32* %6
br label %40
37: ; preds = %23
%38 = load %struct.addrinfo*, %struct.addrinfo** %12
%39 = call i32 @freeaddrinfo(%struct.addrinfo* %38)
store i32 0, i32* %6
br label %40
40: ; preds = %37, %34, %22
%41 = load i32, i32* %6
ret i32 %41
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32 }
define i32 @idrnet_connect(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 %21, 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 @connect(i32 %0, i32 %13, i32 %15)
%17 = icmp eq i32 %16, -1
%18 = load %struct.addrinfo*, %struct.addrinfo** %6
%19 = call i32 @freeaddrinfo(%struct.addrinfo* %18)
%20 = sext i1 %17 to i32
br label %21
21: ; preds = %10, %5
%22 = phi i32 [ -1, %5 ], [ %20, %10 ]
ret i32 %22
}