#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;
  scalar_t__ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  struct addrinfo *ai_next;
  scalar_t__ ai_flags;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ gai_strerror(int);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
scalar_t__ make_connection(int, int /*<<< orphan*/, int /*<<< orphan*/);
int make_socket(int /*<<< orphan*/, int /*<<< orphan*/, scalar_t__);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ ssh_set_error(char *, ...);
int ssh_net_connect(const char *server, const char *port) {
  struct addrinfo addr_hints;
  struct addrinfo *addr_result, *addr;
  int sock, ret;
  memset(&addr_hints, 0, sizeof(struct addrinfo));
  addr_hints.ai_family = AF_UNSPEC;
  addr_hints.ai_socktype = SOCK_STREAM;
  addr_hints.ai_flags = 0;
  addr_hints.ai_protocol = 0;
  ret = getaddrinfo(server, port, &addr_hints, &addr_result);
  if (ret != 0) {
    ssh_set_error("can't resolve server '%s', port '%s': %s", server, port,
                  gai_strerror(ret));
    return -1;
  }
  sock = -1;
  for (addr = addr_result; addr != NULL; addr = addr->ai_next) {
    sock = make_socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
    if (sock < 0)
      continue;
    if (make_connection(sock, addr->ai_addr, addr->ai_addrlen) == 0)
      break;
    close(sock);
    sock = -1;
  }
  freeaddrinfo(addr_result);
  if (sock < 0)
    ssh_set_error("can't connect to server");
  return sock;
}


x86 assembly (clang, -O0):
<ssh_net_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   1090 <getaddrinfo@plt>
mov    %eax,-0x58(%rbp)
cmpl   $0x0,-0x58(%rbp)
je     1226 <ssh_net_connect+0xa6>
mov    -0x10(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x58(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x68(%rbp),%rsi
mov    -0x60(%rbp),%rdx
mov    %eax,%ecx
lea    0xded(%rip),%rdi
mov    $0x0,%al
call   1080 <ssh_set_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d8 <ssh_net_connect+0x158>
movl   $0xffffffff,-0x54(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     12b1 <ssh_net_connect+0x131>
mov    -0x50(%rbp),%rax
mov    0x14(%rax),%edi
mov    -0x50(%rbp),%rax
mov    0x10(%rax),%esi
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%rdx
call   1070 <make_socket@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0x0,-0x54(%rbp)
jge    126d <ssh_net_connect+0xed>
jmp    12a0 <ssh_net_connect+0x120>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x50(%rbp),%rax
mov    (%rax),%edx
call   1060 <make_connection@plt>
cmp    $0x0,%rax
jne    1291 <ssh_net_connect+0x111>
jmp    12b1 <ssh_net_connect+0x131>
mov    -0x54(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x54(%rbp)
mov    -0x50(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    1235 <ssh_net_connect+0xb5>
mov    -0x48(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
cmpl   $0x0,-0x54(%rbp)
jge    12d2 <ssh_net_connect+0x152>
lea    0xd5e(%rip),%rdi
mov    $0x0,%al
call   1080 <ssh_set_error@plt>
mov    -0x54(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ssh_net_connect>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x10(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
movq   $0x0,0x30(%rsp)
movq   $0x0,0x18(%rsp)
lea    0x8(%rsp),%rcx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1090 <getaddrinfo@plt>
test   %eax,%eax
je     120b <ssh_net_connect+0x8b>
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xe0b(%rip),%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1080 <ssh_set_error@plt>
mov    $0xffffffff,%ebp
jmp    1281 <ssh_net_connect+0x101>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
jne    1229 <ssh_net_connect+0xa9>
mov    $0xffffffff,%ebp
xor    %edi,%edi
jmp    1262 <ssh_net_connect+0xe2>
xchg   %ax,%ax
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     125d <ssh_net_connect+0xdd>
mov    0x14(%rbx),%edi
mov    0x10(%rbx),%esi
mov    0x8(%rbx),%rdx
call   1070 <make_socket@plt>
mov    %eax,%ebp
test   %eax,%eax
js     1220 <ssh_net_connect+0xa0>
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %ebp,%edi
call   1060 <make_connection@plt>
test   %rax,%rax
je     1277 <ssh_net_connect+0xf7>
mov    %ebp,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebp
jmp    1220 <ssh_net_connect+0xa0>
mov    0x8(%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
lea    0xdbb(%rip),%rdi
xor    %eax,%eax
call   1080 <ssh_set_error@plt>
jmp    1281 <ssh_net_connect+0x101>
mov    0x8(%rsp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i64, i32, i32, %struct.addrinfo*, i64 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [41 x i8] c"can't resolve server '%s', port '%s': %s\00"
@.str.1 = [24 x i8] c"can't connect to server\00"
define i32 @ssh_net_connect(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.addrinfo
%7 = alloca %struct.addrinfo*
%8 = alloca %struct.addrinfo*
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = call i32 @memset(%struct.addrinfo* %6, i32 0, i32 40)
%12 = load i32, i32* @AF_UNSPEC
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 4
store i32 %12, i32* %13
%14 = load i32, i32* @SOCK_STREAM
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 3
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 6
store i64 0, i64* %16
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 2
store i64 0, i64* %17
%18 = load i8*, i8** %4
%19 = load i8*, i8** %5
%20 = call i32 @getaddrinfo(i8* %18, i8* %19, %struct.addrinfo* %6, %struct.addrinfo** %7)
store i32 %20, i32* %10
%21 = load i32, i32* %10
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %2
%24 = load i8*, i8** %4
%25 = load i8*, i8** %5
%26 = load i32, i32* %10
%27 = call i32 @gai_strerror(i32 %26)
%28 = call i32 (i8*, ...) @ssh_set_error(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %24, i8* %25, i32 %27)
store i32 -1, i32* %3
br label %75
29: ; preds = %2
store i32 -1, i32* %9
%30 = load %struct.addrinfo*, %struct.addrinfo** %7
store %struct.addrinfo* %30, %struct.addrinfo** %8
br label %31
31: ; preds = %62, %29
%32 = load %struct.addrinfo*, %struct.addrinfo** %8
%33 = icmp ne %struct.addrinfo* %32, null
br i1 %33, label %34, label %66
34: ; preds = %31
%35 = load %struct.addrinfo*, %struct.addrinfo** %8
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %35, i32 0, i32 4
%37 = load i32, i32* %36
%38 = load %struct.addrinfo*, %struct.addrinfo** %8
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i32 0, i32 3
%40 = load i32, i32* %39
%41 = load %struct.addrinfo*, %struct.addrinfo** %8
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %41, i32 0, i32 2
%43 = load i64, i64* %42
%44 = call i32 @make_socket(i32 %37, i32 %40, i64 %43)
store i32 %44, i32* %9
%45 = load i32, i32* %9
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %34
br label %62
48: ; preds = %34
%49 = load i32, i32* %9
%50 = load %struct.addrinfo*, %struct.addrinfo** %8
%51 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %50, i32 0, i32 1
%52 = load i32, i32* %51
%53 = load %struct.addrinfo*, %struct.addrinfo** %8
%54 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %53, i32 0, i32 0
%55 = load i32, i32* %54
%56 = call i64 @make_connection(i32 %49, i32 %52, i32 %55)
%57 = icmp eq i64 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %48
br label %66
59: ; preds = %48
%60 = load i32, i32* %9
%61 = call i32 @close(i32 %60)
store i32 -1, i32* %9
br label %62
62: ; preds = %59, %47
%63 = load %struct.addrinfo*, %struct.addrinfo** %8
%64 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %63, i32 0, i32 5
%65 = load %struct.addrinfo*, %struct.addrinfo** %64
store %struct.addrinfo* %65, %struct.addrinfo** %8
br label %31
66: ; preds = %58, %31
%67 = load %struct.addrinfo*, %struct.addrinfo** %7
%68 = call i32 @freeaddrinfo(%struct.addrinfo* %67)
%69 = load i32, i32* %9
%70 = icmp slt i32 %69, 0
br i1 %70, label %71, label %73
71: ; preds = %66
%72 = call i32 (i8*, ...) @ssh_set_error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %73
73: ; preds = %71, %66
%74 = load i32, i32* %9
store i32 %74, i32* %3
br label %75
75: ; preds = %73, %23
%76 = load i32, i32* %3
ret i32 %76
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i64, i32, i32, %struct.addrinfo*, i64 }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [41 x i8] c"can't resolve server '%s', port '%s': %s\00"
@.str.1 = [24 x i8] c"can't connect to server\00"
define i32 @ssh_net_connect(i8* %0, i8* %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = bitcast %struct.addrinfo* %3 to i8*
%6 = bitcast %struct.addrinfo** %4 to i8*
%7 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 40)
%8 = load i32, i32* @AF_UNSPEC
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
store i32 %8, i32* %9
%10 = load i32, i32* @SOCK_STREAM
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %10, i32* %11
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 6
store i64 0, i64* %12
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 2
store i64 0, i64* %13
%14 = call i32 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %3, %struct.addrinfo** %4)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = load %struct.addrinfo*, %struct.addrinfo** %4
%18 = icmp eq %struct.addrinfo* %17, null
br i1 %18, label %51, label %22
19: ; preds = %2
%20 = call i32 @gai_strerror(i32 %14)
%21 = call i32 (i8*, ...) @ssh_set_error(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1, i32 %20)
br label %56
22: ; preds = %16, %41
%23 = phi %struct.addrinfo* [ %44, %41 ], [ %17, %16 ]
%24 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 4
%25 = load i32, i32* %24
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 3
%27 = load i32, i32* %26
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 2
%29 = load i64, i64* %28
%30 = call i32 @make_socket(i32 %25, i32 %27, i64 %29)
%31 = icmp slt i32 %30, 0
br i1 %31, label %41, label %32
32: ; preds = %22
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 1
%34 = load i32, i32* %33
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 0
%36 = load i32, i32* %35
%37 = call i64 @make_connection(i32 %30, i32 %34, i32 %36)
%38 = icmp eq i64 %37, 0
br i1 %38, label %46, label %39
39: ; preds = %32
%40 = call i32 @close(i32 %30)
br label %41
41: ; preds = %22, %39
%42 = phi i32 [ %30, %22 ], [ -1, %39 ]
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 5
%44 = load %struct.addrinfo*, %struct.addrinfo** %43
%45 = icmp eq %struct.addrinfo* %44, null
br i1 %45, label %49, label %22
46: ; preds = %32
%47 = load %struct.addrinfo*, %struct.addrinfo** %4
%48 = call i32 @freeaddrinfo(%struct.addrinfo* %47)
br label %56
49: ; preds = %41
%50 = load %struct.addrinfo*, %struct.addrinfo** %4
br label %51
51: ; preds = %49, %16
%52 = phi %struct.addrinfo* [ null, %16 ], [ %50, %49 ]
%53 = phi i32 [ -1, %16 ], [ %42, %49 ]
%54 = call i32 @freeaddrinfo(%struct.addrinfo* %52)
%55 = call i32 (i8*, ...) @ssh_set_error(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %56
56: ; preds = %46, %51, %19
%57 = phi i32 [ -1, %19 ], [ %53, %51 ], [ %30, %46 ]
ret i32 %57
}