#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 sockaddr {
  int dummy;
};
struct addrinfo {
  int ai_flags;
  int ai_family;
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_socktype;
};
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int getaddrinfo(char const *, char const *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ memcpy(struct sockaddr *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int resolve(struct sockaddr *ia, int pf, const char *host, const char *servname,
            int flags) {
  int n;
  struct addrinfo hints, *res;
  memset(&hints, 0, sizeof(hints));
  hints.ai_flags = flags;
  hints.ai_family = pf;
  hints.ai_socktype = SOCK_DGRAM;
  n = getaddrinfo(host, servname, &hints, &res);
  if (n == 0) {
    memcpy(ia, res->ai_addr, res->ai_addrlen);
    freeaddrinfo(res);
  }
  return n;
}


x86 assembly (clang, -O0):
<resolve>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1030 <memset@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   1050 <getaddrinfo@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    11c5 <resolve+0x85>
mov    -0x8(%rbp),%rdi
mov    -0x48(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x48(%rbp),%rax
mov    0x8(%rax),%edx
call   1040 <memcpy@plt>
mov    -0x48(%rbp),%rdi
call   1060 <freeaddrinfo@plt>
mov    -0x28(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolve>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8d,%ebp
mov    %rcx,%r15
mov    %rdx,%r12
mov    %esi,%ebx
mov    %rdi,%r14
lea    0x10(%rsp),%r13
mov    %r13,%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1030 <memset@plt>
mov    %ebp,0x10(%rsp)
mov    %ebx,0x14(%rsp)
mov    0x2e69(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
lea    0x8(%rsp),%rcx
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r13,%rdx
call   1050 <getaddrinfo@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    11bb <resolve+0x7b>
mov    0x8(%rsp),%rax
mov    0x8(%rax),%edx
mov    0xc(%rax),%esi
mov    %r14,%rdi
call   1040 <memcpy@plt>
mov    0x8(%rsp),%rdi
call   1060 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@SOCK_DGRAM = global i32 0
define i32 @resolve(%struct.sockaddr* %0, i32 %1, i8* %2, i8* %3, i32 %4) {
%6 = alloca %struct.sockaddr*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.addrinfo
%13 = alloca %struct.addrinfo*
store %struct.sockaddr* %0, %struct.sockaddr** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i32 %4, i32* %10
%14 = call i32 @memset(%struct.addrinfo* %12, i32 0, i32 20)
%15 = load i32, i32* %10
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %12, i32 0, i32 0
store i32 %15, i32* %16
%17 = load i32, i32* %7
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %12, i32 0, i32 1
store i32 %17, i32* %18
%19 = load i32, i32* @SOCK_DGRAM
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %12, i32 0, i32 4
store i32 %19, i32* %20
%21 = load i8*, i8** %8
%22 = load i8*, i8** %9
%23 = call i32 @getaddrinfo(i8* %21, i8* %22, %struct.addrinfo* %12, %struct.addrinfo** %13)
store i32 %23, i32* %11
%24 = load i32, i32* %11
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %37
26: ; preds = %5
%27 = load %struct.sockaddr*, %struct.sockaddr** %6
%28 = load %struct.addrinfo*, %struct.addrinfo** %13
%29 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %28, i32 0, i32 3
%30 = load i32, i32* %29
%31 = load %struct.addrinfo*, %struct.addrinfo** %13
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i32 0, i32 2
%33 = load i32, i32* %32
%34 = call i32 @memcpy(%struct.sockaddr* %27, i32 %30, i32 %33)
%35 = load %struct.addrinfo*, %struct.addrinfo** %13
%36 = call i32 @freeaddrinfo(%struct.addrinfo* %35)
br label %37
37: ; preds = %26, %5
%38 = load i32, i32* %11
ret i32 %38
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
%struct.addrinfo = type { i32, i32, i32, i32, i32 }
@SOCK_DGRAM = global i32 0
define i32 @resolve(%struct.sockaddr* %0, i32 %1, i8* %2, i8* %3, i32 %4) {
%6 = alloca %struct.addrinfo
%7 = alloca %struct.addrinfo*
%8 = bitcast %struct.addrinfo* %6 to i8*
%9 = bitcast %struct.addrinfo** %7 to i8*
%10 = call i32 @memset(%struct.addrinfo* %6, i32 0, i32 20)
%11 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i64 0, i32 0
store i32 %4, i32* %11
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i64 0, i32 1
store i32 %1, i32* %12
%13 = load i32, i32* @SOCK_DGRAM
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i64 0, i32 4
store i32 %13, i32* %14
%15 = call i32 @getaddrinfo(i8* %2, i8* %3, %struct.addrinfo* %6, %struct.addrinfo** %7)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %26
17: ; preds = %5
%18 = load %struct.addrinfo*, %struct.addrinfo** %7
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i64 0, i32 3
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %18, i64 0, i32 2
%22 = load i32, i32* %21
%23 = call i32 @memcpy(%struct.sockaddr* %0, i32 %20, i32 %22)
%24 = load %struct.addrinfo*, %struct.addrinfo** %7
%25 = call i32 @freeaddrinfo(%struct.addrinfo* %24)
br label %26
26: ; preds = %17, %5
ret i32 %15
}