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

/* Variables and functions */
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
scalar_t__ getaddrinfo(char const *, char *, struct addrinfo *,
                       struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int try_connect(struct addrinfo *, int *, int);
int /*<<< orphan*/ uint2str(int /*<<< orphan*/, char *);
int socket_connect(const char *hostname, uint16_t port, int flags) {
  int sock = -1, deferred;
  struct addrinfo hints, *result, *r;
  memset(&hints, 0, sizeof(hints));
  hints.ai_socktype = SOCK_STREAM;
  char portstr[8];
  uint2str(port, portstr);
  if (getaddrinfo(hostname, portstr, &hints, &result))
    return -1;
  for (r = result; r; r = r->ai_next) {
    sock = try_connect(r, &deferred, flags);
    if (sock >= 0)
      break;
  }
  freeaddrinfo(result);
  return sock;
}


x86 assembly (clang, -O0):
<socket_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2e60(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x48(%rbp),%rsi
call   1030 <uint2str@plt>
mov    -0x10(%rbp),%rdi
lea    -0x48(%rbp),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1060 <getaddrinfo@plt>
cmp    $0x0,%rax
je     11bc <socket_connect+0x6c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1215 <socket_connect+0xc5>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     1206 <socket_connect+0xb6>
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%edx
lea    -0x20(%rbp),%rsi
call   1050 <try_connect@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jl     11f1 <socket_connect+0xa1>
jmp    1206 <socket_connect+0xb6>
jmp    11f6 <socket_connect+0xa6>
mov    -0x40(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
jmp    11c4 <socket_connect+0x74>
mov    -0x38(%rbp),%rdi
call   1070 <freeaddrinfo@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_connect>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %edx,%r14d
mov    %esi,%ebx
mov    %rdi,%r12
lea    0x18(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2e61(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
lea    0x28(%rsp),%rbp
mov    %ebx,%edi
mov    %rbp,%rsi
call   1030 <uint2str@plt>
lea    0x8(%rsp),%rcx
mov    %r12,%rdi
mov    %rbp,%rsi
mov    %r15,%rdx
call   1060 <getaddrinfo@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    11ef <socket_connect+0x9f>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     11e3 <socket_connect+0x93>
lea    0x14(%rsp),%r15
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14d,%edx
call   1050 <try_connect@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11dc <socket_connect+0x8c>
mov    (%rbx),%rbx
test   %rbx,%rbx
jne    11c0 <socket_connect+0x70>
mov    0x8(%rsp),%rdi
jmp    11ea <socket_connect+0x9a>
mov    $0xffffffff,%ebp
xor    %edi,%edi
call   1070 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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