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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ AI_NUMERICSERV;
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);
int /*<<< orphan*/ profile_end(char *);
int /*<<< orphan*/ profile_start();
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int resolve(const char *hostname, int port, struct sockaddr_in *connect_addr) {
  struct addrinfo hints;
  struct addrinfo *res;
  char service[16];
  int s;
  snprintf(service, sizeof(service), "%u", port);
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_DGRAM;
  hints.ai_flags = AI_NUMERICSERV;
  profile_start();
  if ((s = getaddrinfo(hostname, service, &hints, &res)))
    return (0);
  profile_end("resolve");
  *connect_addr = *(struct sockaddr_in *)res->ai_addr;
  freeaddrinfo(res);
  return (1);
}


x86 assembly (clang, -O0):
<resolve>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0x10,%esi
lea    0xe7a(%rip),%rdx
call   1030 <snprintf@plt>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    $0x0,%al
call   1050 <profile_start@plt>
mov    -0x10(%rbp),%rdi
lea    -0x50(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1060 <getaddrinfo@plt>
mov    %eax,-0x54(%rbp)
cmp    $0x0,%eax
je     11f3 <resolve+0x93>
movl   $0x0,-0x4(%rbp)
jmp    121e <resolve+0xbe>
lea    0xe09(%rip),%rdi
call   1070 <profile_end@plt>
mov    -0x20(%rbp),%rax
mov    -0x40(%rbp),%rcx
mov    (%rcx),%rcx
mov    (%rcx),%ecx
mov    %ecx,(%rax)
mov    -0x40(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolve>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %rdx,%r14
mov    %esi,%ecx
mov    %rdi,%rbx
lea    0xe85(%rip),%rdx
lea    0x20(%rsp),%r15
mov    %r15,%rdi
mov    $0x10,%esi
call   1030 <snprintf@plt>
lea    0x8(%rsp),%r12
xor    %ebp,%ebp
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e21(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2e34(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
xor    %eax,%eax
call   1050 <profile_start@plt>
mov    %rsp,%rcx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1060 <getaddrinfo@plt>
test   %eax,%eax
jne    1208 <resolve+0xa8>
lea    0xe16(%rip),%rdi
call   1070 <profile_end@plt>
mov    (%rsp),%rdi
mov    (%rdi),%rax
mov    (%rax),%eax
mov    %eax,(%r14)
call   1080 <freeaddrinfo@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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