#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 *s6_addr32;
};
struct sockaddr_in6 {
  TYPE_2__ sin6_addr;
};
struct TYPE_3__ {
  int s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
};
struct addrinfo {
  int ai_family;
  int ai_flags;
  scalar_t__ ai_addr;
  struct addrinfo *ai_next;
};
typedef int in_addr_t;

/* Variables and functions */
#define AF_INET 129
#define AF_INET6 128
int AF_UNSPEC;
int AI_ALL;
int AI_V4MAPPED;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char *, void *, struct addrinfo *, struct addrinfo **);
in_addr_t host_resolv(char *_name) {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
  struct addrinfo hints = {.ai_family = AF_UNSPEC,
                           .ai_flags = AI_ALL | AI_V4MAPPED},
                  *res, *res0 = ((void *)0);
#pragma GCC diagnostic pop
  in_addr_t addr = 0;
  int err;
  if (!_name)
    return -1;
  if ((err = getaddrinfo(_name, ((void *)0), &hints, &res0)) != 0) {
    if (res0)
      freeaddrinfo(res0);
    return -1;
  }
  for (res = res0; res; res = res->ai_next) {
    switch (res->ai_family) {
    case AF_INET:
      addr = ((struct sockaddr_in *)res->ai_addr)->sin_addr.s_addr;
      goto end_loop;
    case AF_INET6:
      if (!addr)
        addr = ((struct sockaddr_in6 *)res->ai_addr)->sin6_addr.s6_addr32[3];
    }
  }
end_loop:
  if (res0)
    freeaddrinfo(res0);
  return addr;
}


x86 assembly (clang, -O0):
<host_resolv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2ead(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e91(%rip),%rax
mov    (%rax),%eax
mov    0x2ea0(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x24(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1183 <host_resolv+0x63>
movl   $0xffffffff,-0x4(%rbp)
jmp    126a <host_resolv+0x14a>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x28(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1030 <getaddrinfo@plt>
mov    %eax,-0x40(%rbp)
cmp    $0x0,%eax
je     11c4 <host_resolv+0xa4>
cmpq   $0x0,-0x38(%rbp)
je     11b8 <host_resolv+0x98>
mov    -0x38(%rbp),%rdi
call   1040 <freeaddrinfo@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126a <host_resolv+0x14a>
mov    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     124b <host_resolv+0x12b>
mov    -0x30(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
sub    $0x80,%eax
je     1215 <host_resolv+0xf5>
jmp    11f0 <host_resolv+0xd0>
mov    -0x44(%rbp),%eax
sub    $0x81,%eax
jne    1235 <host_resolv+0x115>
jmp    1203 <host_resolv+0xe3>
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
jmp    1250 <host_resolv+0x130>
cmpl   $0x0,-0x3c(%rbp)
jne    1230 <host_resolv+0x110>
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%rax
mov    0xc(%rax),%eax
mov    %eax,-0x3c(%rbp)
jmp    1235 <host_resolv+0x115>
jmp    123a <host_resolv+0x11a>
mov    -0x30(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    11cc <host_resolv+0xac>
jmp    1250 <host_resolv+0x130>
cmpq   $0x0,-0x38(%rbp)
je     1264 <host_resolv+0x144>
mov    -0x38(%rbp),%rdi
call   1040 <freeaddrinfo@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<host_resolv>:
push   %rbx
sub    $0x20,%rsp
mov    0x2eb4(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2e97(%rip),%rax
mov    0x2ea8(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    %ecx,0xc(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0x10(%rsp)
movq   $0x0,(%rsp)
test   %rdi,%rdi
je     1187 <host_resolv+0x67>
xor    %ebx,%ebx
lea    0x8(%rsp),%rdx
mov    %rsp,%rcx
xor    %esi,%esi
call   1030 <getaddrinfo@plt>
mov    (%rsp),%rdi
test   %eax,%eax
je     1194 <host_resolv+0x74>
mov    $0xffffffff,%ebx
test   %rdi,%rdi
je     118c <host_resolv+0x6c>
call   1040 <freeaddrinfo@plt>
jmp    118c <host_resolv+0x6c>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
ret
test   %rdi,%rdi
je     118c <host_resolv+0x6c>
xor    %ebx,%ebx
mov    %rdi,%rax
jmp    11b1 <host_resolv+0x91>
cmp    $0x81,%ecx
je     11cb <host_resolv+0xab>
mov    0x10(%rax),%rax
test   %rax,%rax
je     117b <host_resolv+0x5b>
mov    (%rax),%ecx
cmp    $0x80,%ecx
jne    11a0 <host_resolv+0x80>
test   %ebx,%ebx
jne    11a8 <host_resolv+0x88>
mov    0x8(%rax),%rcx
mov    (%rcx),%rcx
mov    0xc(%rcx),%ebx
jmp    11a8 <host_resolv+0x88>
mov    0x8(%rax),%rax
mov    (%rax),%ebx
jmp    117b <host_resolv+0x5b>

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i64, %struct.addrinfo* }
%struct.sockaddr_in = type { %struct.TYPE_3__ }
%struct.TYPE_3__ = type { i32 }
%struct.sockaddr_in6 = type { %struct.TYPE_4__ }
%struct.TYPE_4__ = type { i32* }
@AF_UNSPEC = global i32 0
@AI_ALL = global i32 0
@AI_V4MAPPED = global i32 0
define i32 @host_resolv(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.addrinfo
%5 = alloca %struct.addrinfo*
%6 = alloca %struct.addrinfo*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
%9 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i32 0, i32 0
%10 = load i32, i32* @AF_UNSPEC
store i32 %10, i32* %9
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i32 0, i32 1
%12 = load i32, i32* @AI_ALL
%13 = load i32, i32* @AI_V4MAPPED
%14 = or i32 %12, %13
store i32 %14, i32* %11
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i32 0, i32 2
store i64 0, i64* %15
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i32 0, i32 3
store %struct.addrinfo* null, %struct.addrinfo** %16
store %struct.addrinfo* null, %struct.addrinfo** %6
store i32 0, i32* %7
%17 = load i8*, i8** %3
%18 = icmp ne i8* %17, null
br i1 %18, label %20, label %19
19: ; preds = %1
store i32 -1, i32* %2
br label %76
20: ; preds = %1
%21 = load i8*, i8** %3
%22 = call i32 @getaddrinfo(i8* %21, i8* null, %struct.addrinfo* %4, %struct.addrinfo** %6)
store i32 %22, i32* %8
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %31
24: ; preds = %20
%25 = load %struct.addrinfo*, %struct.addrinfo** %6
%26 = icmp ne %struct.addrinfo* %25, null
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load %struct.addrinfo*, %struct.addrinfo** %6
%29 = call i32 @freeaddrinfo(%struct.addrinfo* %28)
br label %30
30: ; preds = %27, %24
store i32 -1, i32* %2
br label %76
31: ; preds = %20
%32 = load %struct.addrinfo*, %struct.addrinfo** %6
store %struct.addrinfo* %32, %struct.addrinfo** %5
br label %33
33: ; preds = %63, %31
%34 = load %struct.addrinfo*, %struct.addrinfo** %5
%35 = icmp ne %struct.addrinfo* %34, null
br i1 %35, label %36, label %67
36: ; preds = %33
%37 = load %struct.addrinfo*, %struct.addrinfo** %5
%38 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %37, i32 0, i32 0
%39 = load i32, i32* %38
switch i32 %39, label %62 [
i32 129, label %40
i32 128, label %48
]
40: ; preds = %36
%41 = load %struct.addrinfo*, %struct.addrinfo** %5
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %41, i32 0, i32 2
%43 = load i64, i64* %42
%44 = inttoptr i64 %43 to %struct.sockaddr_in*
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %44, i32 0, i32 0
%46 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %45, i32 0, i32 0
%47 = load i32, i32* %46
store i32 %47, i32* %7
br label %68
48: ; preds = %36
%49 = load i32, i32* %7
%50 = icmp ne i32 %49, 0
br i1 %50, label %61, label %51
51: ; preds = %48
%52 = load %struct.addrinfo*, %struct.addrinfo** %5
%53 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %52, i32 0, i32 2
%54 = load i64, i64* %53
%55 = inttoptr i64 %54 to %struct.sockaddr_in6*
%56 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %55, i32 0, i32 0
%57 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %56, i32 0, i32 0
%58 = load i32*, i32** %57
%59 = getelementptr inbounds i32, i32* %58, i64 3
%60 = load i32, i32* %59
store i32 %60, i32* %7
br label %61
61: ; preds = %51, %48
br label %62
62: ; preds = %61, %36
br label %63
63: ; preds = %62
%64 = load %struct.addrinfo*, %struct.addrinfo** %5
%65 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %64, i32 0, i32 3
%66 = load %struct.addrinfo*, %struct.addrinfo** %65
store %struct.addrinfo* %66, %struct.addrinfo** %5
br label %33
67: ; preds = %33
br label %68
68: ; preds = %67, %40
%69 = load %struct.addrinfo*, %struct.addrinfo** %6
%70 = icmp ne %struct.addrinfo* %69, null
br i1 %70, label %71, label %74
71: ; preds = %68
%72 = load %struct.addrinfo*, %struct.addrinfo** %6
%73 = call i32 @freeaddrinfo(%struct.addrinfo* %72)
br label %74
74: ; preds = %71, %68
%75 = load i32, i32* %7
store i32 %75, i32* %2
br label %76
76: ; preds = %74, %30, %19
%77 = load i32, i32* %2
ret i32 %77
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i64, %struct.addrinfo* }
%struct.sockaddr_in = type { %struct.TYPE_3__ }
%struct.TYPE_3__ = type { i32 }
%struct.sockaddr_in6 = type { %struct.TYPE_4__ }
%struct.TYPE_4__ = type { i32* }
@AF_UNSPEC = global i32 0
@AI_ALL = global i32 0
@AI_V4MAPPED = global i32 0
define i32 @host_resolv(i8* %0) {
%2 = alloca %struct.addrinfo
%3 = alloca %struct.addrinfo*
%4 = bitcast %struct.addrinfo* %2 to i8*
%5 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 0
%6 = load i32, i32* @AF_UNSPEC
store i32 %6, i32* %5
%7 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 1
%8 = load i32, i32* @AI_ALL
%9 = load i32, i32* @AI_V4MAPPED
%10 = or i32 %9, %8
store i32 %10, i32* %7
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %2, i64 0, i32 2
%12 = bitcast %struct.addrinfo** %3 to i8*
%13 = bitcast i64* %11 to i8*
store %struct.addrinfo* null, %struct.addrinfo** %3
%14 = icmp eq i8* %0, null
br i1 %14, label %53, label %15
15: ; preds = %1
%16 = call i32 @getaddrinfo(i8* %0, i8* null, %struct.addrinfo* %2, %struct.addrinfo** %3)
%17 = icmp eq i32 %16, 0
%18 = load %struct.addrinfo*, %struct.addrinfo** %3
%19 = icmp eq %struct.addrinfo* %18, null
br i1 %17, label %20, label %21
20: ; preds = %15
br i1 %19, label %53, label %22
21: ; preds = %15
br i1 %19, label %53, label %50
22: ; preds = %20, %43
%23 = phi %struct.addrinfo* [ %46, %43 ], [ %18, %20 ]
%24 = phi i32 [ %44, %43 ], [ 0, %20 ]
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 0
%26 = load i32, i32* %25
switch i32 %26, label %43 [
i32 129, label %27
i32 128, label %33
]
27: ; preds = %22
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 2
%29 = load i64, i64* %28
%30 = inttoptr i64 %29 to %struct.sockaddr_in*
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %30, i64 0, i32 0, i32 0
%32 = load i32, i32* %31
br label %48
33: ; preds = %22
%34 = icmp eq i32 %24, 0
br i1 %34, label %35, label %43
35: ; preds = %33
%36 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 2
%37 = load i64, i64* %36
%38 = inttoptr i64 %37 to %struct.sockaddr_in6*
%39 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %38, i64 0, i32 0, i32 0
%40 = load i32*, i32** %39
%41 = getelementptr inbounds i32, i32* %40, i64 3
%42 = load i32, i32* %41
br label %43
43: ; preds = %22, %35, %33
%44 = phi i32 [ %24, %22 ], [ %24, %33 ], [ %42, %35 ]
%45 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %23, i64 0, i32 3
%46 = load %struct.addrinfo*, %struct.addrinfo** %45
%47 = icmp eq %struct.addrinfo* %46, null
br i1 %47, label %48, label %22
48: ; preds = %43, %27
%49 = phi i32 [ %32, %27 ], [ %44, %43 ]
br i1 %19, label %53, label %50
50: ; preds = %48, %21
%51 = phi i32 [ -1, %21 ], [ %49, %48 ]
%52 = call i32 @freeaddrinfo(%struct.addrinfo* %18)
br label %53
53: ; preds = %50, %20, %48, %21, %1
%54 = phi i32 [ -1, %1 ], [ -1, %21 ], [ %49, %48 ], [ 0, %20 ], [ %51, %50 ]
ret i32 %54
}