#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 */
typedef int /*<<< orphan*/ uint32_t;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
};
struct addrinfo {
  scalar_t__ ai_family;
  scalar_t__ ai_addr;
  int /*<<< orphan*/ ai_socktype;
};
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
scalar_t__ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char const *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
uint32_t resolve_addr(const char *address) {
  struct addrinfo *server = NULL;
  struct addrinfo hints;
  int rc;
  uint32_t addr;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_DGRAM;
  rc = getaddrinfo(address, "echo", &hints, &server);
  if (rc != 0) {
    return 0;
  }
  if (server->ai_family != AF_INET) {
    freeaddrinfo(server);
    return 0;
  }
  addr = ((struct sockaddr_in *)server->ai_addr)->sin_addr.s_addr;
  freeaddrinfo(server);
  return addr;
}


x86 assembly (clang, -O0):
<resolve_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1030 <memset@plt>
mov    0x2e8d(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e77(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe87(%rip),%rsi
lea    -0x30(%rbp),%rdx
lea    -0x18(%rbp),%rcx
call   1040 <getaddrinfo@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     119f <resolve_addr+0x6f>
movl   $0x0,-0x4(%rbp)
jmp    11e7 <resolve_addr+0xb7>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    0x2e3b(%rip),%rcx
cmp    (%rcx),%rax
je     11cb <resolve_addr+0x9b>
mov    -0x18(%rbp),%rdi
call   1050 <freeaddrinfo@plt>
movl   $0x0,-0x4(%rbp)
jmp    11e7 <resolve_addr+0xb7>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <freeaddrinfo@plt>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolve_addr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbx
movq   $0x0,0x8(%rsp)
lea    0x10(%rsp),%r14
xor    %ebp,%ebp
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1030 <memset@plt>
mov    0x2e85(%rip),%r15
mov    (%r15),%rax
mov    %rax,0x10(%rsp)
mov    0x2e6e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
lea    0xe81(%rip),%rsi
lea    0x8(%rsp),%rcx
mov    %rbx,%rdi
mov    %r14,%rdx
call   1040 <getaddrinfo@plt>
test   %eax,%eax
jne    11ad <resolve_addr+0x7d>
mov    0x8(%rsp),%rdi
mov    (%rdi),%rax
xor    %ebp,%ebp
cmp    (%r15),%rax
jne    11a8 <resolve_addr+0x78>
mov    0x8(%rdi),%rax
mov    (%rax),%ebp
call   1050 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i64, i64, i32 }
%struct.sockaddr_in = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i64 0
@SOCK_DGRAM = global i32 0
@.str = [5 x i8] c"echo\00"
define i32 @resolve_addr(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.addrinfo*
%5 = alloca %struct.addrinfo
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store %struct.addrinfo* null, %struct.addrinfo** %4
%8 = call i32 @memset(%struct.addrinfo* %5, i32 0, i32 24)
%9 = load i64, i64* @AF_INET
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 0
store i64 %9, i64* %10
%11 = load i32, i32* @SOCK_DGRAM
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %5, i32 0, i32 2
store i32 %11, i32* %12
%13 = load i8*, i8** %3
%14 = call i32 @getaddrinfo(i8* %13, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %struct.addrinfo* %5, %struct.addrinfo** %4)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %1
store i32 0, i32* %2
br label %38
18: ; preds = %1
%19 = load %struct.addrinfo*, %struct.addrinfo** %4
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %19, i32 0, i32 0
%21 = load i64, i64* %20
%22 = load i64, i64* @AF_INET
%23 = icmp ne i64 %21, %22
br i1 %23, label %24, label %27
24: ; preds = %18
%25 = load %struct.addrinfo*, %struct.addrinfo** %4
%26 = call i32 @freeaddrinfo(%struct.addrinfo* %25)
store i32 0, i32* %2
br label %38
27: ; preds = %18
%28 = load %struct.addrinfo*, %struct.addrinfo** %4
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i32 0, i32 1
%30 = load i64, i64* %29
%31 = inttoptr i64 %30 to %struct.sockaddr_in*
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %31, i32 0, i32 0
%33 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %32, i32 0, i32 0
%34 = load i32, i32* %33
store i32 %34, i32* %7
%35 = load %struct.addrinfo*, %struct.addrinfo** %4
%36 = call i32 @freeaddrinfo(%struct.addrinfo* %35)
%37 = load i32, i32* %7
store i32 %37, i32* %2
br label %38
38: ; preds = %27, %24, %17
%39 = load i32, i32* %2
ret i32 %39
}

LLVM IR (-O2):
%struct.addrinfo = type { i64, i64, i32 }
%struct.sockaddr_in = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
@AF_INET = global i64 0
@SOCK_DGRAM = global i32 0
@.str = [5 x i8] c"echo\00"
define i32 @resolve_addr(i8* %0) {
%2 = alloca %struct.addrinfo*
%3 = alloca %struct.addrinfo
%4 = bitcast %struct.addrinfo** %2 to i8*
store %struct.addrinfo* null, %struct.addrinfo** %2
%5 = bitcast %struct.addrinfo* %3 to i8*
%6 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 24)
%7 = load i64, i64* @AF_INET
%8 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 0
store i64 %7, i64* %8
%9 = load i32, i32* @SOCK_DGRAM
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 2
store i32 %9, i32* %10
%11 = call i32 @getaddrinfo(i8* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %struct.addrinfo* %3, %struct.addrinfo** %2)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %28
13: ; preds = %1
%14 = load %struct.addrinfo*, %struct.addrinfo** %2
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i64 0, i32 0
%16 = load i64, i64* %15
%17 = load i64, i64* @AF_INET
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %25
19: ; preds = %13
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %14, i64 0, i32 1
%21 = load i64, i64* %20
%22 = inttoptr i64 %21 to %struct.sockaddr_in*
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %22, i64 0, i32 0, i32 0
%24 = load i32, i32* %23
br label %25
25: ; preds = %13, %19
%26 = phi i32 [ %24, %19 ], [ 0, %13 ]
%27 = call i32 @freeaddrinfo(%struct.addrinfo* %14)
br label %28
28: ; preds = %25, %1
%29 = phi i32 [ 0, %1 ], [ %26, %25 ]
ret i32 %29
}