#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_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
};
struct in_addr {
  int /*<<< orphan*/ s_addr;
};
struct addrinfo {
  scalar_t__ ai_addr;
};

/* Variables and functions */
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char const *, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                struct addrinfo **);
int /*<<< orphan*/ printf(char *, char const *, char *);
int cli_resolveip(const char *val, struct in_addr *ip) {
  int code;
  struct addrinfo *res;
  code = getaddrinfo(val, NULL, NULL, &res);
  if (code) {
    printf("Failed to resolve address '%s': %s\n", val, gai_strerror(code));
    return 0;
  }
  ip->s_addr = ((struct sockaddr_in *)(res->ai_addr))->sin_addr.s_addr;
  freeaddrinfo(res);
  return 1;
}


x86 assembly (clang, -O0):
<cli_resolveip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%edx
lea    -0x28(%rbp),%rcx
mov    %rdx,%rsi
call   1050 <getaddrinfo@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11a0 <cli_resolveip+0x60>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x1c(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x30(%rbp),%rsi
mov    %rax,%rdx
lea    0xe71(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11bf <cli_resolveip+0x7f>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rdi
call   1060 <freeaddrinfo@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cli_resolveip>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r15
mov    %rdi,%rbx
xor    %r14d,%r14d
lea    0x8(%rsp),%rcx
xor    %esi,%esi
xor    %edx,%edx
call   1050 <getaddrinfo@plt>
test   %eax,%eax
je     117f <cli_resolveip+0x3f>
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xe8e(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1030 <printf@plt>
jmp    1197 <cli_resolveip+0x57>
mov    0x8(%rsp),%rdi
mov    (%rdi),%rax
mov    (%rax),%eax
mov    %eax,(%r15)
call   1060 <freeaddrinfo@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.in_addr = type { i32 }
%struct.addrinfo = type { i64 }
%struct.sockaddr_in = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@.str = [36 x i8] c"Failed to resolve address '%s': %s\0A\00"
define i32 @cli_resolveip(i8* %0, %struct.in_addr* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.in_addr*
%6 = alloca i32
%7 = alloca %struct.addrinfo*
store i8* %0, i8** %4
store %struct.in_addr* %1, %struct.in_addr** %5
%8 = load i8*, i8** %4
%9 = call i32 @getaddrinfo(i8* %8, i32* null, i32* null, %struct.addrinfo** %7)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = load i32, i32* %6
%15 = call i8* @gai_strerror(i32 %14)
%16 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %13, i8* %15)
store i32 0, i32* %3
br label %29
17: ; preds = %2
%18 = load %struct.addrinfo*, %struct.addrinfo** %7
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i32 0, i32 0
%20 = load i64, i64* %19
%21 = inttoptr i64 %20 to %struct.sockaddr_in*
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %21, i32 0, i32 0
%23 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %22, i32 0, i32 0
%24 = load i32, i32* %23
%25 = load %struct.in_addr*, %struct.in_addr** %5
%26 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %25, i32 0, i32 0
store i32 %24, i32* %26
%27 = load %struct.addrinfo*, %struct.addrinfo** %7
%28 = call i32 @freeaddrinfo(%struct.addrinfo* %27)
store i32 1, i32* %3
br label %29
29: ; preds = %17, %12
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
%struct.in_addr = type { i32 }
%struct.addrinfo = type { i64 }
%struct.sockaddr_in = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@.str = [36 x i8] c"Failed to resolve address '%s': %s\0A\00"
define i32 @cli_resolveip(i8* %0, %struct.in_addr* nocapture writeonly %1) {
%3 = alloca %struct.addrinfo*
%4 = bitcast %struct.addrinfo** %3 to i8*
%5 = call i32 @getaddrinfo(i8* %0, i32* null, i32* null, %struct.addrinfo** %3)
%6 = icmp eq i32 %5, 0
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = call i8* @gai_strerror(i32 %5)
%9 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %8)
br label %19
10: ; preds = %2
%11 = load %struct.addrinfo*, %struct.addrinfo** %3
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %11, i64 0, i32 0
%13 = load i64, i64* %12
%14 = inttoptr i64 %13 to %struct.sockaddr_in*
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i64 0, i32 0, i32 0
%16 = load i32, i32* %15
%17 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %1, i64 0, i32 0
store i32 %16, i32* %17
%18 = call i32 @freeaddrinfo(%struct.addrinfo* %11)
br label %19
19: ; preds = %10, %7
%20 = phi i32 [ 0, %7 ], [ 1, %10 ]
ret i32 %20
}