#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_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int /*<<< orphan*/ AF_UNSPEC;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
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 main(void) {
  struct addrinfo hints;
  struct addrinfo *res;
  int ret;
  ret = getaddrinfo("www.android.com", "9999", NULL, &res);
  if (ret != 0) {
    printf("first getaddrinfo returned error: %s\n", gai_strerror(ret));
    return 1;
  }
  freeaddrinfo(res);
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_protocol = IPPROTO_TCP;
  ret = getaddrinfo("www.android.com", "9999", &hints, &res);
  if (ret != 0) {
    printf("second getaddrinfo returned error: %s\n", gai_strerror(ret));
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe9a(%rip),%rdi
lea    0xea3(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x18(%rbp),%rcx
call   1060 <getaddrinfo@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11aa <main+0x5a>
mov    -0x1c(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    %rax,%rsi
lea    0xe7c(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1239 <main+0xe9>
mov    -0x18(%rbp),%rdi
call   1070 <freeaddrinfo@plt>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    0x2dfa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
lea    0xe12(%rip),%rdi
lea    0xe1b(%rip),%rsi
lea    -0x10(%rbp),%rdx
lea    -0x18(%rbp),%rcx
call   1060 <getaddrinfo@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1232 <main+0xe2>
mov    -0x1c(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    %rax,%rsi
lea    0xe1a(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1239 <main+0xe9>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
lea    0xea2(%rip),%rdi
lea    0xeab(%rip),%rsi
xor    %ebx,%ebx
mov    %rsp,%rcx
xor    %edx,%edx
call   1060 <getaddrinfo@plt>
test   %eax,%eax
je     117e <main+0x2e>
lea    0xe99(%rip),%rbx
jmp    11e6 <main+0x96>
mov    (%rsp),%rdi
call   1070 <freeaddrinfo@plt>
lea    0x8(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x2e24(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
lea    0xe37(%rip),%rdi
lea    0xe40(%rip),%rsi
mov    %rsp,%rcx
mov    %r14,%rdx
call   1060 <getaddrinfo@plt>
test   %eax,%eax
je     11fd <main+0xad>
lea    0xe55(%rip),%rbx
mov    %eax,%edi
call   1040 <gai_strerror@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32 }
@.str = [16 x i8] c"www.android.com\00"
@.str.1 = [5 x i8] c"9999\00"
@.str.2 = [38 x i8] c"first getaddrinfo returned error: %s\0A\00"
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.3 = [39 x i8] c"second getaddrinfo returned error: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.addrinfo
%3 = alloca %struct.addrinfo*
%4 = alloca i32
store i32 0, i32* %1
%5 = call i32 @getaddrinfo(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %struct.addrinfo* null, %struct.addrinfo** %3)
store i32 %5, i32* %4
%6 = load i32, i32* %4
%7 = icmp ne i32 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %0
%9 = load i32, i32* %4
%10 = call i8* @gai_strerror(i32 %9)
%11 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %10)
store i32 1, i32* %1
br label %30
12: ; preds = %0
%13 = load %struct.addrinfo*, %struct.addrinfo** %3
%14 = call i32 @freeaddrinfo(%struct.addrinfo* %13)
%15 = call i32 @memset(%struct.addrinfo* %2, i32 0, i32 12)
%16 = load i32, i32* @AF_UNSPEC
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i32 0, i32 2
store i32 %16, i32* %17
%18 = load i32, i32* @SOCK_STREAM
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i32 0, i32 1
store i32 %18, i32* %19
%20 = load i32, i32* @IPPROTO_TCP
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i32 0, i32 0
store i32 %20, i32* %21
%22 = call i32 @getaddrinfo(i8* getelementptr inbounds ([16 x i8], [16 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** %3)
store i32 %22, i32* %4
%23 = load i32, i32* %4
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %12
%26 = load i32, i32* %4
%27 = call i8* @gai_strerror(i32 %26)
%28 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), i8* %27)
store i32 1, i32* %1
br label %30
29: ; preds = %12
store i32 0, i32* %1
br label %30
30: ; preds = %29, %25, %8
%31 = load i32, i32* %1
ret i32 %31
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32 }
@.str = [16 x i8] c"www.android.com\00"
@.str.1 = [5 x i8] c"9999\00"
@.str.2 = [38 x i8] c"first getaddrinfo returned error: %s\0A\00"
@AF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.3 = [39 x i8] c"second getaddrinfo returned error: %s\0A\00"
define i32 @main() {
%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 @getaddrinfo(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %struct.addrinfo* null, %struct.addrinfo** %2)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %19
7: ; preds = %0
%8 = load %struct.addrinfo*, %struct.addrinfo** %2
%9 = call i32 @freeaddrinfo(%struct.addrinfo* %8)
%10 = call i32 @memset(%struct.addrinfo* %1, i32 0, i32 12)
%11 = load i32, i32* @AF_UNSPEC
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 2
store i32 %11, i32* %12
%13 = load i32, i32* @SOCK_STREAM
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 1
store i32 %13, i32* %14
%15 = load i32, i32* @IPPROTO_TCP
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %1, i64 0, i32 0
store i32 %15, i32* %16
%17 = call i32 @getaddrinfo(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), %struct.addrinfo* %1, %struct.addrinfo** %2)
%18 = icmp eq i32 %17, 0
br i1 %18, label %24, label %19
19: ; preds = %7, %0
%20 = phi i32 [ %5, %0 ], [ %17, %7 ]
%21 = phi i8* [ getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), %0 ], [ getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0), %7 ]
%22 = call i8* @gai_strerror(i32 %20)
%23 = call i32 @printf(i8* %21, i8* %22)
br label %24
24: ; preds = %19, %7
%25 = phi i32 [ 0, %7 ], [ 1, %19 ]
ret i32 %25
}