#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_family;
  struct addrinfo *ai_next;
};

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
char *gai_strerror(int);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ stderr;
int sneaky_lookup(void) {
  struct addrinfo hints, *res, *res0;
  int error;
  error = getaddrinfo("www.kame.net", "http", &hints, &res0);
  if (error) {
    (void)fprintf(stderr, "dns lookup: %s\n", gai_strerror(error));
    return (-1);
  }
  for (res = res0; res; res = res->ai_next) {
    printf("got sock for family %d\n", res->ai_family);
  }
  return (0);
}


x86 assembly (clang, -O0):
<sneaky_lookup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xeb1(%rip),%rdi
lea    0xeb7(%rip),%rsi
lea    -0x18(%rbp),%rdx
lea    -0x28(%rbp),%rcx
call   1060 <getaddrinfo@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     11a2 <sneaky_lookup+0x62>
mov    0x2e69(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x30(%rbp),%edi
mov    %rax,%rdx
lea    0xe81(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11df <sneaky_lookup+0x9f>
mov    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11d8 <sneaky_lookup+0x98>
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
lea    0xe60(%rip),%rdi
call   1030 <printf@plt>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    11aa <sneaky_lookup+0x6a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sneaky_lookup>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
lea    0xeb1(%rip),%rdi
lea    0xeb7(%rip),%rsi
lea    0x10(%rsp),%rdx
lea    0x8(%rsp),%rcx
call   1060 <getaddrinfo@plt>
test   %eax,%eax
je     1192 <sneaky_lookup+0x52>
mov    0x2e70(%rip),%rcx
mov    (%rcx),%ebx
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xe92(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    $0xffffffff,%r14d
jmp    11c4 <sneaky_lookup+0x84>
mov    0x8(%rsp),%rbp
xor    %r14d,%r14d
test   %rbp,%rbp
je     11c4 <sneaky_lookup+0x84>
lea    0xe7c(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
mov    0x0(%rbp),%esi
mov    %rbx,%rdi
call   1030 <printf@plt>
mov    0x8(%rbp),%rbp
test   %rbp,%rbp
jne    11b0 <sneaky_lookup+0x70>
mov    %r14d,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, %struct.addrinfo* }
@.str = [13 x i8] c"www.kame.net\00"
@.str.1 = [5 x i8] c"http\00"
@stderr = global i32 0
@.str.2 = [16 x i8] c"dns lookup: %s\0A\00"
@.str.3 = [24 x i8] c"got sock for family %d\0A\00"
define i32 @sneaky_lookup() {
%1 = alloca i32
%2 = alloca %struct.addrinfo
%3 = alloca %struct.addrinfo*
%4 = alloca %struct.addrinfo*
%5 = alloca i32
%6 = call i32 @getaddrinfo(i8* getelementptr inbounds ([13 x i8], [13 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** %4)
store i32 %6, i32* %5
%7 = load i32, i32* %5
%8 = icmp ne i32 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %0
%10 = load i32, i32* @stderr
%11 = load i32, i32* %5
%12 = call i8* @gai_strerror(i32 %11)
%13 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %12)
store i32 -1, i32* %1
br label %29
14: ; preds = %0
%15 = load %struct.addrinfo*, %struct.addrinfo** %4
store %struct.addrinfo* %15, %struct.addrinfo** %3
br label %16
16: ; preds = %24, %14
%17 = load %struct.addrinfo*, %struct.addrinfo** %3
%18 = icmp ne %struct.addrinfo* %17, null
br i1 %18, label %19, label %28
19: ; preds = %16
%20 = load %struct.addrinfo*, %struct.addrinfo** %3
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i32 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %22)
br label %24
24: ; preds = %19
%25 = load %struct.addrinfo*, %struct.addrinfo** %3
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %25, i32 0, i32 1
%27 = load %struct.addrinfo*, %struct.addrinfo** %26
store %struct.addrinfo* %27, %struct.addrinfo** %3
br label %16
28: ; preds = %16
store i32 0, i32* %1
br label %29
29: ; preds = %28, %9
%30 = load i32, i32* %1
ret i32 %30
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, %struct.addrinfo* }
@.str = [13 x i8] c"www.kame.net\00"
@.str.1 = [5 x i8] c"http\00"
@stderr = global i32 0
@.str.2 = [16 x i8] c"dns lookup: %s\0A\00"
@.str.3 = [24 x i8] c"got sock for family %d\0A\00"
define i32 @sneaky_lookup() {
%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 ([13 x i8], [13 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)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = load %struct.addrinfo*, %struct.addrinfo** %2
%9 = icmp eq %struct.addrinfo* %8, null
br i1 %9, label %22, label %14
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = call i8* @gai_strerror(i32 %5)
%13 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i8* %12)
br label %22
14: ; preds = %7, %14
%15 = phi %struct.addrinfo* [ %20, %14 ], [ %8, %7 ]
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i64 0, i32 0
%17 = load i32, i32* %16
%18 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %17)
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %15, i64 0, i32 1
%20 = load %struct.addrinfo*, %struct.addrinfo** %19
%21 = icmp eq %struct.addrinfo* %20, null
br i1 %21, label %22, label %14
22: ; preds = %14, %7, %10
%23 = phi i32 [ -1, %10 ], [ 0, %7 ], [ 0, %14 ]
ret i32 %23
}