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

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int AI_NUMERICSERV;
int AI_PASSIVE;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, scalar_t__);
int /*<<< orphan*/ stderr;
int sock_connect(char *ip, char *port) {
  int res;
  int skfd;
  struct addrinfo hints, *result, *rp;
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_canonname = NULL;
  hints.ai_addr = NULL;
  hints.ai_next = NULL;
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_protocol = 0;
  hints.ai_flags = AI_PASSIVE | AI_NUMERICSERV;
  res = getaddrinfo(ip, port, &hints, &result);
  if (res != 0) {
    fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(res));
    exit(EXIT_FAILURE);
  }
  for (rp = result; rp != NULL; rp = rp->ai_next) {
    skfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
    if (skfd == -1) {
      continue;
    }
    if (connect(skfd, rp->ai_addr, rp->ai_addrlen) != -1) {
      break;
    }
    close(skfd);
  }
  if (rp == NULL) {
    printf("Could not connect to %s!\n", ip);
    return -1;
  }
  freeaddrinfo(result);
  return skfd;
}


x86 assembly (clang, -O0):
<sock_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1050 <memset@plt>
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x48(%rbp)
movq   $0x0,-0x30(%rbp)
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    0x2de1(%rip),%rax
mov    (%rax),%eax
mov    0x2df0(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x50(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x50(%rbp),%rdx
lea    -0x58(%rbp),%rcx
call   10a0 <getaddrinfo@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1263 <sock_connect+0xc3>
mov    0x2d92(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x64(%rbp),%edi
mov    %rax,%rdx
lea    0xdb0(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2d5c(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x58(%rbp),%rax
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
je     12e1 <sock_connect+0x141>
mov    -0x60(%rbp),%rax
mov    0x1c(%rax),%edi
mov    -0x60(%rbp),%rax
mov    0x18(%rax),%esi
mov    -0x60(%rbp),%rax
mov    0x10(%rax),%rdx
call   10c0 <socket@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    12a3 <sock_connect+0x103>
jmp    12d0 <sock_connect+0x130>
mov    -0x20(%rbp),%edi
mov    -0x60(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x60(%rbp),%rax
mov    0x4(%rax),%edx
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
je     12c8 <sock_connect+0x128>
jmp    12e1 <sock_connect+0x141>
mov    -0x20(%rbp),%edi
call   1060 <close@plt>
mov    -0x60(%rbp),%rax
mov    0x20(%rax),%rax
mov    %rax,-0x60(%rbp)
jmp    126b <sock_connect+0xcb>
cmpq   $0x0,-0x60(%rbp)
jne    1308 <sock_connect+0x168>
mov    -0x10(%rbp),%rsi
lea    0xd1a(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1317 <sock_connect+0x177>
mov    -0x58(%rbp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_connect>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %rsi,%rbp
mov    %rdi,%r14
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1050 <memset@plt>
xorps  %xmm0,%xmm0
movups %xmm0,0x18(%rsp)
movups %xmm0,0x30(%rsp)
mov    0x2e02(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x2c(%rsp)
mov    0x2ddd(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x28(%rsp)
mov    0x2df0(%rip),%rax
mov    0x2e01(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,0x10(%rsp)
lea    0x8(%rsp),%rcx
mov    %r14,%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   10a0 <getaddrinfo@plt>
test   %eax,%eax
jne    1284 <sock_connect+0xe4>
mov    0x8(%rsp),%rbx
jmp    122b <sock_connect+0x8b>
nopl   (%rax)
mov    %ebp,%edi
call   1060 <close@plt>
mov    0x20(%rbx),%rbx
test   %rbx,%rbx
je     1265 <sock_connect+0xc5>
mov    0x1c(%rbx),%edi
mov    0x18(%rbx),%esi
mov    0x10(%rbx),%rdx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     1227 <sock_connect+0x87>
mov    %eax,%ebp
mov    0x8(%rbx),%rsi
mov    0x4(%rbx),%edx
mov    %eax,%edi
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
je     1220 <sock_connect+0x80>
mov    0x8(%rsp),%rdi
call   10b0 <freeaddrinfo@plt>
jmp    1279 <sock_connect+0xd9>
lea    0xda5(%rip),%rdi
mov    %r14,%rsi
call   1030 <printf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d3d(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xd65(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1070 <fprintf@plt>
mov    0x2d0c(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32*, i64, i32, i32, %struct.addrinfo*, i32* }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@AI_PASSIVE = global i32 0
@AI_NUMERICSERV = global i32 0
@stderr = global i32 0
@.str = [17 x i8] c"getaddrinfo: %s\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [26 x i8] c"Could not connect to %s!\0A\00"
define i32 @sock_connect(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.addrinfo
%9 = alloca %struct.addrinfo*
%10 = alloca %struct.addrinfo*
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = call i32 @memset(%struct.addrinfo* %8, i32 0, i32 48)
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 7
store i32* null, i32** %12
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 2
store i32* null, i32** %13
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 6
store %struct.addrinfo* null, %struct.addrinfo** %14
%15 = load i32, i32* @AF_UNSPEC
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 5
store i32 %15, i32* %16
%17 = load i32, i32* @SOCK_STREAM
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 4
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 3
store i64 0, i64* %19
%20 = load i32, i32* @AI_PASSIVE
%21 = load i32, i32* @AI_NUMERICSERV
%22 = or i32 %20, %21
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %8, i32 0, i32 0
store i32 %22, i32* %23
%24 = load i8*, i8** %4
%25 = load i8*, i8** %5
%26 = call i32 @getaddrinfo(i8* %24, i8* %25, %struct.addrinfo* %8, %struct.addrinfo** %9)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %36
29: ; preds = %2
%30 = load i32, i32* @stderr
%31 = load i32, i32* %6
%32 = call i8* @gai_strerror(i32 %31)
%33 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %32)
%34 = load i32, i32* @EXIT_FAILURE
%35 = call i32 @exit(i32 %34)
unreachable
36: ; preds = %2
%37 = load %struct.addrinfo*, %struct.addrinfo** %9
store %struct.addrinfo* %37, %struct.addrinfo** %10
br label %38
38: ; preds = %69, %36
%39 = load %struct.addrinfo*, %struct.addrinfo** %10
%40 = icmp ne %struct.addrinfo* %39, null
br i1 %40, label %41, label %73
41: ; preds = %38
%42 = load %struct.addrinfo*, %struct.addrinfo** %10
%43 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %42, i32 0, i32 5
%44 = load i32, i32* %43
%45 = load %struct.addrinfo*, %struct.addrinfo** %10
%46 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %45, i32 0, i32 4
%47 = load i32, i32* %46
%48 = load %struct.addrinfo*, %struct.addrinfo** %10
%49 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %48, i32 0, i32 3
%50 = load i64, i64* %49
%51 = call i32 @socket(i32 %44, i32 %47, i64 %50)
store i32 %51, i32* %7
%52 = load i32, i32* %7
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %55
54: ; preds = %41
br label %69
55: ; preds = %41
%56 = load i32, i32* %7
%57 = load %struct.addrinfo*, %struct.addrinfo** %10
%58 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %57, i32 0, i32 2
%59 = load i32*, i32** %58
%60 = load %struct.addrinfo*, %struct.addrinfo** %10
%61 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %60, i32 0, i32 1
%62 = load i32, i32* %61
%63 = call i32 @connect(i32 %56, i32* %59, i32 %62)
%64 = icmp ne i32 %63, -1
br i1 %64, label %65, label %66
65: ; preds = %55
br label %73
66: ; preds = %55
%67 = load i32, i32* %7
%68 = call i32 @close(i32 %67)
br label %69
69: ; preds = %66, %54
%70 = load %struct.addrinfo*, %struct.addrinfo** %10
%71 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %70, i32 0, i32 6
%72 = load %struct.addrinfo*, %struct.addrinfo** %71
store %struct.addrinfo* %72, %struct.addrinfo** %10
br label %38
73: ; preds = %65, %38
%74 = load %struct.addrinfo*, %struct.addrinfo** %10
%75 = icmp eq %struct.addrinfo* %74, null
br i1 %75, label %76, label %79
76: ; preds = %73
%77 = load i8*, i8** %4
%78 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %77)
store i32 -1, i32* %3
br label %83
79: ; preds = %73
%80 = load %struct.addrinfo*, %struct.addrinfo** %9
%81 = call i32 @freeaddrinfo(%struct.addrinfo* %80)
%82 = load i32, i32* %7
store i32 %82, i32* %3
br label %83
83: ; preds = %79, %76
%84 = load i32, i32* %3
ret i32 %84
}

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