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

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int ipify_connect(void) {
  struct addrinfo *result, *rp;
  struct addrinfo hints;
  int rc, sd;
  memset(&hints, 0, sizeof(struct addrinfo));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  rc = getaddrinfo("api.ipify.org", "http", &hints, &result);
  if (rc || !result)
    return -1;
  for (rp = result; rp; rp = rp->ai_next) {
    sd = socket(rp->ai_family, rp->ai_socktype, 0);
    if (sd < 0)
      continue;
    rc = connect(sd, result->ai_addr, result->ai_addrlen);
    if (rc) {
      close(sd);
      sd = -1;
      continue;
    }
    break;
  }
  freeaddrinfo(result);
  return sd;
}


x86 assembly (clang, -O0):
<ipify_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1030 <memset@plt>
mov    0x2e69(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
lea    0xe69(%rip),%rdi
lea    0xe70(%rip),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x10(%rbp),%rcx
call   1060 <getaddrinfo@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    11c3 <ipify_connect+0x63>
cmpq   $0x0,-0x10(%rbp)
jne    11cf <ipify_connect+0x6f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1264 <ipify_connect+0x104>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1255 <ipify_connect+0xf5>
mov    -0x18(%rbp),%rax
mov    0xc(%rax),%edi
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%esi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    1209 <ipify_connect+0xa9>
jmp    1244 <ipify_connect+0xe4>
mov    -0x38(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
call   1050 <connect@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     123f <ipify_connect+0xdf>
mov    -0x38(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x38(%rbp)
jmp    1244 <ipify_connect+0xe4>
jmp    1255 <ipify_connect+0xf5>
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    11d7 <ipify_connect+0x77>
mov    -0x10(%rbp),%rdi
call   1070 <freeaddrinfo@plt>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipify_connect>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1030 <memset@plt>
mov    0x2e67(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
lea    0xe65(%rip),%rdi
lea    0xe6c(%rip),%rsi
lea    0x8(%rsp),%rcx
mov    %rbx,%rdx
call   1060 <getaddrinfo@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
jne    1217 <ipify_connect+0xb7>
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
jne    11e5 <ipify_connect+0x85>
jmp    1217 <ipify_connect+0xb7>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebp,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebp
mov    0x10(%rbx),%rbx
test   %rbx,%rbx
je     120d <ipify_connect+0xad>
mov    0x8(%rbx),%esi
mov    0xc(%rbx),%edi
xor    %edx,%edx
call   1080 <socket@plt>
mov    %eax,%ebp
test   %eax,%eax
js     11dc <ipify_connect+0x7c>
mov    0x8(%rsp),%rax
mov    (%rax),%edx
mov    0x4(%rax),%esi
mov    %ebp,%edi
call   1050 <connect@plt>
test   %eax,%eax
jne    11d0 <ipify_connect+0x70>
mov    0x8(%rsp),%rdi
call   1070 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, %struct.addrinfo* }
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str = [14 x i8] c"api.ipify.org\00"
@.str.1 = [5 x i8] c"http\00"
define i32 @ipify_connect() {
%1 = alloca %struct.addrinfo*
%2 = alloca %struct.addrinfo
%3 = bitcast %struct.addrinfo** %1 to i8*
%4 = bitcast %struct.addrinfo* %2 to i8*
%5 = call i32 @memset(%struct.addrinfo* %2, i32 0, i32 24)
%6 = load i32, i32* @AF_UNSPEC
%7 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 3
store i32 %6, i32* %7
%8 = load i32, i32* @SOCK_STREAM
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 2
store i32 %8, i32* %9
%10 = call i32 @getaddrinfo(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %struct.addrinfo* %2, %struct.addrinfo** %1)
%11 = icmp eq i32 %10, 0
%12 = load %struct.addrinfo*, %struct.addrinfo** %1
%13 = icmp ne %struct.addrinfo* %12, null
%14 = select i1 %11, i1 %13, i1 false
br i1 %14, label %15, label %42
15: ; preds = %0, %33
%16 = phi %struct.addrinfo* [ %36, %33 ], [ %12, %0 ]
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i64 0, i32 3
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i64 0, i32 2
%20 = load i32, i32* %19
%21 = call i32 @socket(i32 %18, i32 %20, i32 0)
%22 = icmp slt i32 %21, 0
br i1 %22, label %33, label %23
23: ; preds = %15
%24 = load %struct.addrinfo*, %struct.addrinfo** %1
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 1
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %24, i64 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @connect(i32 %21, i32 %26, i32 %28)
%30 = icmp eq i32 %29, 0
br i1 %30, label %38, label %31
31: ; preds = %23
%32 = call i32 @close(i32 %21)
br label %33
33: ; preds = %15, %31
%34 = phi i32 [ %21, %15 ], [ -1, %31 ]
%35 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %16, i64 0, i32 4
%36 = load %struct.addrinfo*, %struct.addrinfo** %35
%37 = icmp eq %struct.addrinfo* %36, null
br i1 %37, label %38, label %15
38: ; preds = %33, %23
%39 = phi i32 [ %21, %23 ], [ %34, %33 ]
%40 = load %struct.addrinfo*, %struct.addrinfo** %1
%41 = call i32 @freeaddrinfo(%struct.addrinfo* %40)
br label %42
42: ; preds = %0, %38
%43 = phi i32 [ %39, %38 ], [ -1, %0 ]
ret i32 %43
}