#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 */
typedef struct TYPE_4__ TYPE_2__;
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
struct TYPE_4__ {
  int /*<<< orphan*/ s6_addr;
};
struct sockaddr_in6 {
  TYPE_2__ sin6_addr;
  scalar_t__ sin6_port;
  int /*<<< orphan*/ sin6_family;
};
struct TYPE_3__ {
  scalar_t__ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  scalar_t__ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sin6;
typedef int /*<<< orphan*/ sin;
typedef scalar_t__ int32_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ AF_INET6;
int NI_NUMERICHOST;
int getnameinfo(struct sockaddr *, int, char *, int, int /*<<< orphan*/ *,
                int /*<<< orphan*/, int);
scalar_t__ htonl(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/, scalar_t__ *, int);
int /*<<< orphan*/ memset(struct sockaddr_in6 *, int /*<<< orphan*/, int);
int dns_lookup(char *result, int size, int useip, int32_t *a) {
  struct sockaddr_in sin;
  struct sockaddr_in6 sin6;
  struct sockaddr *sa;
  int salen, flags;
  int mapped = 0;
  flags = useip ? NI_NUMERICHOST : 0;
  if (a[0] == 0 && a[1] == 0 && a[2] == (int32_t)htonl(0xffff))
    mapped = 1;
  if (mapped || (a[1] == 0 && a[2] == 0 && a[3] == 0)) {
    sin.sin_family = AF_INET;
    sin.sin_port = 0;
    sin.sin_addr.s_addr = mapped ? a[3] : a[0];
    sa = (struct sockaddr *)&sin;
    salen = sizeof(sin);
  } else {
    memset(&sin6, 0, sizeof(sin6));
    sin6.sin6_family = AF_INET6;
    sin6.sin6_port = 0;
    memcpy(sin6.sin6_addr.s6_addr, a, 16);
    sa = (struct sockaddr *)&sin6;
    salen = sizeof(sin6);
  }
  return getnameinfo(sa, salen, result, size, NULL, 0, flags);
}


x86 assembly (clang, -O0):
<dns_lookup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
movl   $0x0,-0x5c(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     117b <dns_lookup+0x3b>
mov    0x2e5f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x60(%rbp)
jmp    1185 <dns_lookup+0x45>
xor    %eax,%eax
mov    %eax,-0x60(%rbp)
jmp    1185 <dns_lookup+0x45>
mov    -0x60(%rbp),%eax
mov    %eax,-0x58(%rbp)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11d5 <dns_lookup+0x95>
mov    -0x18(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    11d5 <dns_lookup+0x95>
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x68(%rbp)
mov    $0xffff,%edi
call   1030 <htonl@plt>
mov    %rax,%rcx
mov    -0x68(%rbp),%rax
cmp    %rcx,%rax
jne    11d5 <dns_lookup+0x95>
movl   $0x1,-0x5c(%rbp)
cmpl   $0x0,-0x5c(%rbp)
jne    120c <dns_lookup+0xcc>
mov    -0x18(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    1262 <dns_lookup+0x122>
mov    -0x18(%rbp),%rax
cmpq   $0x0,0x10(%rax)
jne    1262 <dns_lookup+0x122>
mov    -0x18(%rbp),%rax
cmpq   $0x0,0x18(%rax)
jne    1262 <dns_lookup+0x122>
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpl   $0x0,-0x5c(%rbp)
je     123b <dns_lookup+0xfb>
mov    -0x18(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x70(%rbp)
jmp    1246 <dns_lookup+0x106>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x70(%rbp)
mov    -0x70(%rbp),%rax
mov    %rax,-0x30(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x50(%rbp)
movl   $0x18,-0x54(%rbp)
jmp    12a6 <dns_lookup+0x166>
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2d67(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x48(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x10,%edx
call   1060 <memcpy@plt>
lea    -0x48(%rbp),%rax
mov    %rax,-0x50(%rbp)
movl   $0x18,-0x54(%rbp)
mov    -0x50(%rbp),%rdi
mov    -0x54(%rbp),%esi
mov    -0x8(%rbp),%rdx
mov    -0xc(%rbp),%ecx
mov    -0x58(%rbp),%eax
xor    %r8d,%r8d
xor    %r9d,%r9d
mov    %eax,(%rsp)
call   1050 <getnameinfo@plt>
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dns_lookup>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,%rbx
mov    %edx,%r12d
mov    %esi,%r14d
mov    %rdi,%r15
mov    0x2e6f(%rip),%rax
mov    (%rax),%r13d
cmpq   $0x0,(%rcx)
jne    11a3 <dns_lookup+0x63>
cmpq   $0x0,0x8(%rbx)
jne    11a3 <dns_lookup+0x63>
mov    0x10(%rbx),%rbp
mov    $0xffff,%edi
call   1030 <htonl@plt>
cmp    %rax,%rbp
jne    11a3 <dns_lookup+0x63>
mov    0x2e5d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
movq   $0x0,0x18(%rsp)
add    $0x18,%rbx
jmp    123c <dns_lookup+0xfc>
cmpq   $0x0,0x8(%rbx)
jne    11b8 <dns_lookup+0x78>
cmpq   $0x0,0x10(%rbx)
jne    11b8 <dns_lookup+0x78>
cmpq   $0x0,0x18(%rbx)
je     1226 <dns_lookup+0xe6>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
movq   $0x0,0x18(%rsp)
mov    0x10(%rsp),%edi
mov    %rbx,%rsi
mov    $0x10,%edx
call   1060 <memcpy@plt>
test   %r12d,%r12d
cmove  %r12d,%r13d
mov    %r13d,(%rsp)
mov    %rbp,%rdi
mov    $0x18,%esi
mov    %r15,%rdx
mov    %r14d,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1050 <getnameinfo@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dbb(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
movq   $0x0,0x18(%rsp)
mov    (%rbx),%rax
mov    %rax,0x10(%rsp)
lea    0x10(%rsp),%rbp
jmp    11f3 <dns_lookup+0xb3>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_3__, i64, i32 }
%struct.TYPE_3__ = type { i64 }
%struct.sockaddr_in6 = type { %struct.TYPE_4__, i64, i32 }
%struct.TYPE_4__ = type { i32 }
%struct.sockaddr = type { i32 }
@NI_NUMERICHOST = global i32 0
@AF_INET = global i32 0
@AF_INET6 = global i32 0
define i32 @dns_lookup(i8* %0, i32 %1, i32 %2, i64* %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64*
%9 = alloca %struct.sockaddr_in
%10 = alloca %struct.sockaddr_in6
%11 = alloca %struct.sockaddr*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i64* %3, i64** %8
store i32 0, i32* %14
%15 = load i32, i32* %7
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %4
%18 = load i32, i32* @NI_NUMERICHOST
br label %20
19: ; preds = %4
br label %20
20: ; preds = %19, %17
%21 = phi i32 [ %18, %17 ], [ 0, %19 ]
store i32 %21, i32* %13
%22 = load i64*, i64** %8
%23 = getelementptr inbounds i64, i64* %22, i64 0
%24 = load i64, i64* %23
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %38
26: ; preds = %20
%27 = load i64*, i64** %8
%28 = getelementptr inbounds i64, i64* %27, i64 1
%29 = load i64, i64* %28
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %38
31: ; preds = %26
%32 = load i64*, i64** %8
%33 = getelementptr inbounds i64, i64* %32, i64 2
%34 = load i64, i64* %33
%35 = call i64 @htonl(i32 65535)
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %38
37: ; preds = %31
store i32 1, i32* %14
br label %38
38: ; preds = %37, %31, %26, %20
%39 = load i32, i32* %14
%40 = icmp ne i32 %39, 0
br i1 %40, label %56, label %41
41: ; preds = %38
%42 = load i64*, i64** %8
%43 = getelementptr inbounds i64, i64* %42, i64 1
%44 = load i64, i64* %43
%45 = icmp eq i64 %44, 0
br i1 %45, label %46, label %75
46: ; preds = %41
%47 = load i64*, i64** %8
%48 = getelementptr inbounds i64, i64* %47, i64 2
%49 = load i64, i64* %48
%50 = icmp eq i64 %49, 0
br i1 %50, label %51, label %75
51: ; preds = %46
%52 = load i64*, i64** %8
%53 = getelementptr inbounds i64, i64* %52, i64 3
%54 = load i64, i64* %53
%55 = icmp eq i64 %54, 0
br i1 %55, label %56, label %75
56: ; preds = %51, %38
%57 = load i32, i32* @AF_INET
%58 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %57, i32* %58
%59 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
store i64 0, i64* %59
%60 = load i32, i32* %14
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %66
62: ; preds = %56
%63 = load i64*, i64** %8
%64 = getelementptr inbounds i64, i64* %63, i64 3
%65 = load i64, i64* %64
br label %70
66: ; preds = %56
%67 = load i64*, i64** %8
%68 = getelementptr inbounds i64, i64* %67, i64 0
%69 = load i64, i64* %68
br label %70
70: ; preds = %66, %62
%71 = phi i64 [ %65, %62 ], [ %69, %66 ]
%72 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%73 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %72, i32 0, i32 0
store i64 %71, i64* %73
%74 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
store %struct.sockaddr* %74, %struct.sockaddr** %11
store i32 24, i32* %12
br label %86
75: ; preds = %51, %46, %41
%76 = call i32 @memset(%struct.sockaddr_in6* %10, i32 0, i32 24)
%77 = load i32, i32* @AF_INET6
%78 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %10, i32 0, i32 2
store i32 %77, i32* %78
%79 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %10, i32 0, i32 1
store i64 0, i64* %79
%80 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %10, i32 0, i32 0
%81 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %80, i32 0, i32 0
%82 = load i32, i32* %81
%83 = load i64*, i64** %8
%84 = call i32 @memcpy(i32 %82, i64* %83, i32 16)
%85 = bitcast %struct.sockaddr_in6* %10 to %struct.sockaddr*
store %struct.sockaddr* %85, %struct.sockaddr** %11
store i32 24, i32* %12
br label %86
86: ; preds = %75, %70
%87 = load %struct.sockaddr*, %struct.sockaddr** %11
%88 = load i32, i32* %12
%89 = load i8*, i8** %5
%90 = load i32, i32* %6
%91 = load i32, i32* %13
%92 = call i32 @getnameinfo(%struct.sockaddr* %87, i32 %88, i8* %89, i32 %90, i32* null, i32 0, i32 %91)
ret i32 %92
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_3__, i64, i32 }
%struct.TYPE_3__ = type { i64 }
%struct.sockaddr_in6 = type { %struct.TYPE_4__, i64, i32 }
%struct.TYPE_4__ = type { i32 }
%struct.sockaddr = type { i32 }
@NI_NUMERICHOST = global i32 0
@AF_INET = global i32 0
@AF_INET6 = global i32 0
define i32 @dns_lookup(i8* %0, i32 %1, i32 %2, i64* %3) {
%5 = alloca %struct.sockaddr_in
%6 = alloca %struct.sockaddr_in6
%7 = bitcast %struct.sockaddr_in* %5 to i8*
%8 = bitcast %struct.sockaddr_in6* %6 to i8*
%9 = load i32, i32* @NI_NUMERICHOST
%10 = load i64, i64* %3
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %21
12: ; preds = %4
%13 = getelementptr inbounds i64, i64* %3, i64 1
%14 = load i64, i64* %13
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %12
%17 = getelementptr inbounds i64, i64* %3, i64 2
%18 = load i64, i64* %17
%19 = tail call i64 @htonl(i32 65535)
%20 = icmp eq i64 %18, %19
br i1 %20, label %37, label %21
21: ; preds = %4, %12, %16
%22 = getelementptr inbounds i64, i64* %3, i64 1
%23 = load i64, i64* %22
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %47
25: ; preds = %21
%26 = getelementptr inbounds i64, i64* %3, i64 2
%27 = load i64, i64* %26
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %47
29: ; preds = %25
%30 = getelementptr inbounds i64, i64* %3, i64 3
%31 = load i64, i64* %30
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %47
33: ; preds = %29
%34 = load i32, i32* @AF_INET
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %34, i32* %35
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
store i64 0, i64* %36
br label %42
37: ; preds = %16
%38 = load i32, i32* @AF_INET
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %38, i32* %39
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
store i64 0, i64* %40
%41 = getelementptr inbounds i64, i64* %3, i64 3
br label %42
42: ; preds = %33, %37
%43 = phi i64* [ %41, %37 ], [ %3, %33 ]
%44 = load i64, i64* %43
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0, i32 0
store i64 %44, i64* %45
%46 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
br label %56
47: ; preds = %29, %25, %21
%48 = call i32 @memset(%struct.sockaddr_in6* %6, i32 0, i32 24)
%49 = load i32, i32* @AF_INET6
%50 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %6, i64 0, i32 2
store i32 %49, i32* %50
%51 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %6, i64 0, i32 1
store i64 0, i64* %51
%52 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %6, i64 0, i32 0, i32 0
%53 = load i32, i32* %52
%54 = call i32 @memcpy(i32 %53, i64* %3, i32 16)
%55 = bitcast %struct.sockaddr_in6* %6 to %struct.sockaddr*
br label %56
56: ; preds = %47, %42
%57 = phi %struct.sockaddr* [ %46, %42 ], [ %55, %47 ]
%58 = icmp eq i32 %2, 0
%59 = select i1 %58, i32 0, i32 %9
%60 = call i32 @getnameinfo(%struct.sockaddr* %57, i32 24, i8* %0, i32 %1, i32* null, i32 0, i32 %59)
ret i32 %60
}