#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_socktype;
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ *ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_family;
  struct addrinfo *ai_next;
  int /*<<< orphan*/ *ai_canonname;
};

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
scalar_t__ getaddrinfo(char const *, char const *, struct addrinfo *,
                       struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int inet_connect(const char *host, const char *service, int type) {
  struct addrinfo hints;
  struct addrinfo *result, *rp;
  int socket_fd;
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_canonname = NULL;
  hints.ai_addr = NULL;
  hints.ai_next = NULL;
  hints.ai_socktype = type;
  hints.ai_family = AF_UNSPEC;
  if (getaddrinfo(host, service, &hints, &result) != 0) {
    fprintf(stderr, "getaddrinfo() failed: %s\n", strerror(errno));
    return -1;
  }
  for (rp = result; rp != NULL; rp = rp->ai_next) {
    socket_fd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
    if (socket_fd == -1)
      continue;
    if (connect(socket_fd, rp->ai_addr, rp->ai_addrlen) != -1)
      break;
    close(socket_fd);
  }
  freeaddrinfo(result);
  return (rp == NULL) ? -1 : socket_fd;
}


x86 assembly (clang, -O0):
<inet_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1030 <memset@plt>
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x48(%rbp),%rdx
lea    -0x50(%rbp),%rcx
call   1070 <getaddrinfo@plt>
cmp    $0x0,%rax
je     1224 <inet_connect+0xa4>
mov    0x2de5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x60(%rbp)
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x60(%rbp),%edi
mov    %rax,%rdx
lea    0xded(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12cd <inet_connect+0x14d>
mov    -0x50(%rbp),%rax
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
je     12a0 <inet_connect+0x120>
mov    -0x58(%rbp),%rax
mov    0x14(%rax),%edi
mov    -0x58(%rbp),%rax
mov    (%rax),%esi
mov    -0x58(%rbp),%rax
mov    0x10(%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0xffffffff,-0x5c(%rbp)
jne    1262 <inet_connect+0xe2>
jmp    128f <inet_connect+0x10f>
mov    -0x5c(%rbp),%edi
mov    -0x58(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x58(%rbp),%rax
mov    0x4(%rax),%edx
call   1060 <connect@plt>
cmp    $0xffffffff,%eax
je     1287 <inet_connect+0x107>
jmp    12a0 <inet_connect+0x120>
mov    -0x5c(%rbp),%edi
call   1040 <close@plt>
mov    -0x58(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x58(%rbp)
jmp    122c <inet_connect+0xac>
mov    -0x50(%rbp),%rdi
call   1090 <freeaddrinfo@plt>
cmpq   $0x0,-0x58(%rbp)
jne    12c1 <inet_connect+0x141>
mov    $0xffffffff,%eax
mov    %eax,-0x64(%rbp)
jmp    12c7 <inet_connect+0x147>
mov    -0x5c(%rbp),%eax
mov    %eax,-0x64(%rbp)
mov    -0x64(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<inet_connect>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %edx,%ebx
mov    %rsi,%r14
mov    %rdi,%r15
lea    0x8(%rsp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1030 <memset@plt>
movq   $0x0,0x10(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x20(%rsp)
mov    %ebx,0x8(%rsp)
mov    0x2e24(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    %rsp,%rcx
mov    %r15,%rdi
mov    %r14,%rsi
mov    %r12,%rdx
call   1070 <getaddrinfo@plt>
test   %rax,%rax
je     120f <inet_connect+0x8f>
mov    0x2df1(%rip),%rax
mov    (%rax),%ebx
mov    0x2de0(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe02(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    $0xffffffff,%ebp
jmp    1277 <inet_connect+0xf7>
mov    (%rsp),%rbx
test   %rbx,%rbx
jne    1239 <inet_connect+0xb9>
mov    $0xffffffff,%ebp
xor    %edi,%edi
jmp    1272 <inet_connect+0xf2>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
je     1269 <inet_connect+0xe9>
mov    0x14(%rbx),%edi
mov    (%rbx),%esi
mov    0x10(%rbx),%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     1230 <inet_connect+0xb0>
mov    %eax,%ebp
mov    0x8(%rbx),%rsi
mov    0x4(%rbx),%edx
mov    %eax,%edi
call   1060 <connect@plt>
cmp    $0xffffffff,%eax
jne    126e <inet_connect+0xee>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    1230 <inet_connect+0xb0>
mov    $0xffffffff,%ebp
mov    (%rsp),%rdi
call   1090 <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 { i32, i32, i32*, i32, i32, %struct.addrinfo*, i32* }
@AF_UNSPEC = global i32 0
@stderr = global i32 0
@.str = [26 x i8] c"getaddrinfo() failed: %s\0A\00"
@errno = global i32 0
define i32 @inet_connect(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.addrinfo
%9 = alloca %struct.addrinfo*
%10 = alloca %struct.addrinfo*
%11 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%12 = call i32 @memset(%struct.addrinfo* %8, i32 0, i32 40)
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 6
store i32* null, i32** %13
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 2
store i32* null, i32** %14
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 5
store %struct.addrinfo* null, %struct.addrinfo** %15
%16 = load i32, i32* %7
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 0
store i32 %16, i32* %17
%18 = load i32, i32* @AF_UNSPEC
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 4
store i32 %18, i32* %19
%20 = load i8*, i8** %5
%21 = load i8*, i8** %6
%22 = call i64 @getaddrinfo(i8* %20, i8* %21, %struct.addrinfo* %8, %struct.addrinfo** %9)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %3
%25 = load i32, i32* @stderr
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %27)
store i32 -1, i32* %4
br label %76
29: ; preds = %3
%30 = load %struct.addrinfo*, %struct.addrinfo** %9
store %struct.addrinfo* %30, %struct.addrinfo** %10
br label %31
31: ; preds = %62, %29
%32 = load %struct.addrinfo*, %struct.addrinfo** %10
%33 = icmp ne %struct.addrinfo* %32, null
br i1 %33, label %34, label %66
34: ; preds = %31
%35 = load %struct.addrinfo*, %struct.addrinfo** %10
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %35, i32 0, i32 4
%37 = load i32, i32* %36
%38 = load %struct.addrinfo*, %struct.addrinfo** %10
%39 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %38, i32 0, i32 0
%40 = load i32, i32* %39
%41 = load %struct.addrinfo*, %struct.addrinfo** %10
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %41, i32 0, i32 3
%43 = load i32, i32* %42
%44 = call i32 @socket(i32 %37, i32 %40, i32 %43)
store i32 %44, i32* %11
%45 = load i32, i32* %11
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %48
47: ; preds = %34
br label %62
48: ; preds = %34
%49 = load i32, i32* %11
%50 = load %struct.addrinfo*, %struct.addrinfo** %10
%51 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %50, i32 0, i32 2
%52 = load i32*, i32** %51
%53 = load %struct.addrinfo*, %struct.addrinfo** %10
%54 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %53, i32 0, i32 1
%55 = load i32, i32* %54
%56 = call i32 @connect(i32 %49, i32* %52, i32 %55)
%57 = icmp ne i32 %56, -1
br i1 %57, label %58, label %59
58: ; preds = %48
br label %66
59: ; preds = %48
%60 = load i32, i32* %11
%61 = call i32 @close(i32 %60)
br label %62
62: ; preds = %59, %47
%63 = load %struct.addrinfo*, %struct.addrinfo** %10
%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** %10
br label %31
66: ; preds = %58, %31
%67 = load %struct.addrinfo*, %struct.addrinfo** %9
%68 = call i32 @freeaddrinfo(%struct.addrinfo* %67)
%69 = load %struct.addrinfo*, %struct.addrinfo** %10
%70 = icmp eq %struct.addrinfo* %69, null
br i1 %70, label %71, label %72
71: ; preds = %66
br label %74
72: ; preds = %66
%73 = load i32, i32* %11
br label %74
74: ; preds = %72, %71
%75 = phi i32 [ -1, %71 ], [ %73, %72 ]
store i32 %75, i32* %4
br label %76
76: ; preds = %74, %24
%77 = load i32, i32* %4
ret i32 %77
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32*, i32, i32, %struct.addrinfo*, i32* }
@AF_UNSPEC = global i32 0
@stderr = global i32 0
@.str = [26 x i8] c"getaddrinfo() failed: %s\0A\00"
@errno = global i32 0
define i32 @inet_connect(i8* %0, i8* %1, i32 %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 2
store i32* null, i32** %9
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 5
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 0
%12 = bitcast %struct.addrinfo** %10 to i8*
store i32 %2, i32* %11
%13 = load i32, i32* @AF_UNSPEC
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 4
store i32 %13, i32* %14
%15 = call i64 @getaddrinfo(i8* %0, i8* %1, %struct.addrinfo* %4, %struct.addrinfo** %5)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %3
%18 = load %struct.addrinfo*, %struct.addrinfo** %5
%19 = icmp eq %struct.addrinfo* %18, null
br i1 %19, label %51, label %25
20: ; preds = %3
%21 = load i32, i32* @stderr
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %23)
br label %55
25: ; preds = %17, %44
%26 = phi %struct.addrinfo* [ %46, %44 ], [ %18, %17 ]
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 4
%28 = load i32, i32* %27
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 0
%30 = load i32, i32* %29
%31 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 3
%32 = load i32, i32* %31
%33 = call i32 @socket(i32 %28, i32 %30, i32 %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %44, label %35
35: ; preds = %25
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 2
%37 = load i32*, i32** %36
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 1
%39 = load i32, i32* %38
%40 = call i32 @connect(i32 %33, i32* %37, i32 %39)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %48
42: ; preds = %35
%43 = call i32 @close(i32 %33)
br label %44
44: ; preds = %25, %42
%45 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %26, i64 0, i32 5
%46 = load %struct.addrinfo*, %struct.addrinfo** %45
%47 = icmp eq %struct.addrinfo* %46, null
br i1 %47, label %48, label %25
48: ; preds = %35, %44
%49 = phi i32 [ -1, %44 ], [ %33, %35 ]
%50 = load %struct.addrinfo*, %struct.addrinfo** %5
br label %51
51: ; preds = %48, %17
%52 = phi %struct.addrinfo* [ null, %17 ], [ %50, %48 ]
%53 = phi i32 [ -1, %17 ], [ %49, %48 ]
%54 = call i32 @freeaddrinfo(%struct.addrinfo* %52)
br label %55
55: ; preds = %51, %20
%56 = phi i32 [ -1, %20 ], [ %53, %51 ]
ret i32 %56
}