#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;
};

/* Variables and functions */
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
scalar_t__ getaddrinfo(char const *, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                       struct addrinfo **);
scalar_t__ getnameinfo(int /*<<< orphan*/, int /*<<< orphan*/, char *, size_t,
                       int /*<<< orphan*/ *, int /*<<< orphan*/,
                       int /*<<< orphan*/);
int lookup_hostname(const char *addr, char *hostname, size_t buflen) {
  struct addrinfo *addrinfo;
  int rc;
  if (getaddrinfo(addr, NULL, NULL, &addrinfo)) {
    rc = 1;
  } else {
    if (getnameinfo(addrinfo->ai_addr, addrinfo->ai_addrlen, hostname, buflen,
                    NULL, 0, 0)) {
      rc = 1;
    } else {
      rc = 0;
    }
    freeaddrinfo(addrinfo);
  }
  return rc;
}


x86 assembly (clang, -O0):
<lookup_hostname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x20(%rbp),%rcx
mov    %rdx,%rsi
call   1040 <getaddrinfo@plt>
cmp    $0x0,%rax
je     116e <lookup_hostname+0x3e>
movl   $0x1,-0x24(%rbp)
jmp    11bd <lookup_hostname+0x8d>
mov    -0x20(%rbp),%rax
mov    0x4(%rax),%edi
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
xor    %r9d,%r9d
movl   $0x0,(%rsp)
call   1030 <getnameinfo@plt>
cmp    $0x0,%rax
je     11ad <lookup_hostname+0x7d>
movl   $0x1,-0x24(%rbp)
jmp    11b4 <lookup_hostname+0x84>
movl   $0x0,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <freeaddrinfo@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lookup_hostname>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
lea    0x8(%rsp),%rcx
xor    %esi,%esi
xor    %edx,%edx
call   1040 <getaddrinfo@plt>
mov    $0x1,%ebp
test   %rax,%rax
jne    118b <lookup_hostname+0x5b>
mov    0x8(%rsp),%rax
mov    (%rax),%esi
mov    0x4(%rax),%edi
movl   $0x0,(%rsp)
mov    %rbx,%rdx
mov    %r14,%rcx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1030 <getnameinfo@plt>
xor    %ebp,%ebp
test   %rax,%rax
setne  %bpl
mov    0x8(%rsp),%rdi
call   1050 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32 }
define i32 @lookup_hostname(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca %struct.addrinfo*
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%9 = load i8*, i8** %4
%10 = call i64 @getaddrinfo(i8* %9, i32* null, i32* null, %struct.addrinfo** %7)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %3
store i32 1, i32* %8
br label %29
13: ; preds = %3
%14 = load %struct.addrinfo*, %struct.addrinfo** %7
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i32 0, i32 1
%16 = load i32, i32* %15
%17 = load %struct.addrinfo*, %struct.addrinfo** %7
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %17, i32 0, i32 0
%19 = load i32, i32* %18
%20 = load i8*, i8** %5
%21 = load i64, i64* %6
%22 = call i64 @getnameinfo(i32 %16, i32 %19, i8* %20, i64 %21, i32* null, i32 0, i32 0)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %13
store i32 1, i32* %8
br label %26
25: ; preds = %13
store i32 0, i32* %8
br label %26
26: ; preds = %25, %24
%27 = load %struct.addrinfo*, %struct.addrinfo** %7
%28 = call i32 @freeaddrinfo(%struct.addrinfo* %27)
br label %29
29: ; preds = %26, %12
%30 = load i32, i32* %8
ret i32 %30
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32 }
define i32 @lookup_hostname(i8* %0, i8* %1, i64 %2) {
%4 = alloca %struct.addrinfo*
%5 = bitcast %struct.addrinfo** %4 to i8*
%6 = call i64 @getaddrinfo(i8* %0, i32* null, i32* null, %struct.addrinfo** %4)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %19
8: ; preds = %3
%9 = load %struct.addrinfo*, %struct.addrinfo** %4
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i64 0, i32 1
%11 = load i32, i32* %10
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %9, i64 0, i32 0
%13 = load i32, i32* %12
%14 = call i64 @getnameinfo(i32 %11, i32 %13, i8* %1, i64 %2, i32* null, i32 0, i32 0)
%15 = icmp ne i64 %14, 0
%16 = zext i1 %15 to i32
%17 = load %struct.addrinfo*, %struct.addrinfo** %4
%18 = call i32 @freeaddrinfo(%struct.addrinfo* %17)
br label %19
19: ; preds = %3, %8
%20 = phi i32 [ %16, %8 ], [ 1, %3 ]
ret i32 %20
}