#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 in_addr {
  int dummy;
};
struct sockaddr_in {
  struct in_addr sin_addr;
};
struct addrinfo {
  scalar_t__ ai_family;
  scalar_t__ ai_addr;
  struct addrinfo *ai_next;
};
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
scalar_t__ AF_INET;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
scalar_t__ getaddrinfo(char const *, int /*<<< orphan*/ *, struct addrinfo *,
                       struct addrinfo **);
int /*<<< orphan*/ inet_aton(char const *, struct in_addr *);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int resolve_host(struct in_addr *sin_addr, const char *hostname) {
  if (!inet_aton(hostname, sin_addr)) {
    struct addrinfo *ai, *cur;
    struct addrinfo hints;
    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_INET;
    if (getaddrinfo(hostname, NULL, &hints, &ai))
      return -1;
    for (cur = ai; cur; cur = cur->ai_next) {
      if (cur->ai_family == AF_INET) {
        *sin_addr = ((struct sockaddr_in *)cur->ai_addr)->sin_addr;
        freeaddrinfo(ai);
        return 0;
      }
    }
    freeaddrinfo(ai);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<resolve_host>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <inet_aton@plt>
cmp    $0x0,%eax
jne    1229 <resolve_host+0xe9>
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1030 <memset@plt>
mov    0x2e6b(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x40(%rbp),%rdx
lea    -0x20(%rbp),%rcx
call   1050 <getaddrinfo@plt>
cmp    $0x0,%rax
je     11af <resolve_host+0x6f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1230 <resolve_host+0xf0>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1214 <resolve_host+0xd4>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    0x2e18(%rip),%rcx
cmp    (%rcx),%rax
jne    11fe <resolve_host+0xbe>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    0x8(%rcx),%rcx
mov    (%rcx),%ecx
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rdi
call   1060 <freeaddrinfo@plt>
movl   $0x0,-0x4(%rbp)
jmp    1230 <resolve_host+0xf0>
jmp    1203 <resolve_host+0xc3>
mov    -0x28(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    11b7 <resolve_host+0x77>
mov    -0x20(%rbp),%rdi
call   1060 <freeaddrinfo@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1230 <resolve_host+0xf0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolve_host>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
mov    %r14,%rsi
call   1040 <inet_aton@plt>
xor    %ebp,%ebp
test   %eax,%eax
jne    11d5 <resolve_host+0x95>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1030 <memset@plt>
mov    0x2e6c(%rip),%r15
mov    (%r15),%rax
mov    %rax,0x10(%rsp)
lea    0x8(%rsp),%rcx
mov    %rbx,%rdi
xor    %esi,%esi
mov    %rbp,%rdx
call   1050 <getaddrinfo@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
jne    11d5 <resolve_host+0x95>
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
je     11be <resolve_host+0x7e>
mov    (%r15),%rcx
mov    %rdi,%rax
cmp    %rcx,(%rax)
je     11c5 <resolve_host+0x85>
mov    0x10(%rax),%rax
test   %rax,%rax
jne    11b0 <resolve_host+0x70>
call   1060 <freeaddrinfo@plt>
jmp    11d5 <resolve_host+0x95>
mov    0x8(%rax),%rax
mov    (%rax),%eax
mov    %eax,(%r14)
call   1060 <freeaddrinfo@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.in_addr = type { i32 }
%struct.addrinfo = type { i64, i64, %struct.addrinfo* }
@AF_INET = global i64 0
define i32 @resolve_host(%struct.in_addr* %0, i8* %1) {
%3 = alloca %struct.addrinfo*
%4 = alloca %struct.addrinfo
%5 = tail call i32 @inet_aton(i8* %1, %struct.in_addr* %0)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %40
7: ; preds = %2
%8 = bitcast %struct.addrinfo** %3 to i8*
%9 = bitcast %struct.addrinfo* %4 to i8*
%10 = call i32 @memset(%struct.addrinfo* %4, i32 0, i32 24)
%11 = load i64, i64* @AF_INET
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %4, i64 0, i32 0
store i64 %11, i64* %12
%13 = call i64 @getaddrinfo(i8* %1, i32* null, %struct.addrinfo* %4, %struct.addrinfo** %3)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %38
15: ; preds = %7
%16 = load %struct.addrinfo*, %struct.addrinfo** %3
%17 = icmp eq %struct.addrinfo* %16, null
br i1 %17, label %36, label %18
18: ; preds = %15
%19 = load i64, i64* @AF_INET
br label %20
20: ; preds = %18, %32
%21 = phi %struct.addrinfo* [ %16, %18 ], [ %34, %32 ]
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i64 0, i32 0
%23 = load i64, i64* %22
%24 = icmp eq i64 %23, %19
br i1 %24, label %25, label %32
25: ; preds = %20
%26 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i64 0, i32 1
%27 = load i64, i64* %26
%28 = inttoptr i64 %27 to i32*
%29 = getelementptr %struct.in_addr, %struct.in_addr* %0, i64 0, i32 0
%30 = load i32, i32* %28
store i32 %30, i32* %29
%31 = call i32 @freeaddrinfo(%struct.addrinfo* %16)
br label %38
32: ; preds = %20
%33 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i64 0, i32 2
%34 = load %struct.addrinfo*, %struct.addrinfo** %33
%35 = icmp eq %struct.addrinfo* %34, null
br i1 %35, label %36, label %20
36: ; preds = %32, %15
%37 = call i32 @freeaddrinfo(%struct.addrinfo* %16)
br label %38
38: ; preds = %7, %36, %25
%39 = phi i32 [ 0, %25 ], [ -1, %36 ], [ -1, %7 ]
br label %40
40: ; preds = %2, %38
%41 = phi i32 [ %39, %38 ], [ 0, %2 ]
ret i32 %41
}