#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 addrinfo {
  int /*<<< orphan*/ ai_flags;
  scalar_t__ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};
typedef int /*<<< orphan*/ info;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ AI_ADDRCONFIG;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ cctools_debug(long long, char *, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
int getaddrinfo(char *, char *, struct addrinfo *, struct addrinfo **);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
char *string_format(char *, int);
int find_localhost_addr(int port, struct addrinfo **addr) {
  char *hostname = NULL;
  char *portname = string_format("%d", port);
  struct addrinfo info, *res;
  memset(&info, 0, sizeof(info));
  info.ai_family = AF_INET;
  info.ai_socktype = SOCK_DGRAM;
  info.ai_protocol = 0;
  info.ai_flags = AI_ADDRCONFIG;
  int status = getaddrinfo(hostname, portname, &info, &res);
  if (status != 0)
    cctools_debug((1LL << 39), "couldn't resolve socket address: %s\n",
                  strerror(errno));
  free(portname);
  *addr = res;
  return status;
}


x86 assembly (clang, -O0):
<find_localhost_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x4(%rbp),%esi
lea    0xe7f(%rip),%rdi
call   1050 <string_format@plt>
mov    %rax,-0x20(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2e47(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e33(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    0x2dff(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
call   1070 <getaddrinfo@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
je     120e <find_localhost_addr+0xae>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %eax,%edx
movabs $0x8000000000,%rdi
lea    0xdfa(%rip),%rsi
call   1060 <cctools_debug@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x44(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_localhost_addr>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
mov    %edi,%esi
lea    0xe8c(%rip),%rdi
call   1050 <string_format@plt>
mov    %rax,%rbx
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2e51(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
movq   $0x0,0x10(%rsp)
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %rsp,%rcx
xor    %edi,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1070 <getaddrinfo@plt>
mov    %eax,%ebp
test   %eax,%eax
je     11fc <find_localhost_addr+0x9c>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe18(%rip),%rsi
movabs $0x8000000000,%rdi
mov    %eax,%edx
call   1060 <cctools_debug@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    (%rsp),%rax
mov    %rax,(%r14)
mov    %ebp,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i64, i32, i32 }
@.str = [3 x i8] c"%d\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@AI_ADDRCONFIG = global i32 0
@.str.1 = [37 x i8] c"couldn't resolve socket address: %s\0A\00"
@errno = global i32 0
define i32 @find_localhost_addr(i32 %0, %struct.addrinfo** %1) {
%3 = alloca i32
%4 = alloca %struct.addrinfo**
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca %struct.addrinfo
%8 = alloca %struct.addrinfo*
%9 = alloca i32
store i32 %0, i32* %3
store %struct.addrinfo** %1, %struct.addrinfo*** %4
store i8* null, i8** %5
%10 = load i32, i32* %3
%11 = call i8* @string_format(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %10)
store i8* %11, i8** %6
%12 = call i32 @memset(%struct.addrinfo* %7, i32 0, i32 24)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 3
store i32 %13, i32* %14
%15 = load i32, i32* @SOCK_DGRAM
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 2
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 1
store i64 0, i64* %17
%18 = load i32, i32* @AI_ADDRCONFIG
%19 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 0
store i32 %18, i32* %19
%20 = load i8*, i8** %5
%21 = load i8*, i8** %6
%22 = call i32 @getaddrinfo(i8* %20, i8* %21, %struct.addrinfo* %7, %struct.addrinfo** %8)
store i32 %22, i32* %9
%23 = load i32, i32* %9
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %2
%26 = load i32, i32* @errno
%27 = call i32 @strerror(i32 %26)
%28 = call i32 @cctools_debug(i64 549755813888, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %27)
br label %29
29: ; preds = %25, %2
%30 = load i8*, i8** %6
%31 = call i32 @free(i8* %30)
%32 = load %struct.addrinfo*, %struct.addrinfo** %8
%33 = load %struct.addrinfo**, %struct.addrinfo*** %4
store %struct.addrinfo* %32, %struct.addrinfo** %33
%34 = load i32, i32* %9
ret i32 %34
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i64, i32, i32 }
@.str = [3 x i8] c"%d\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@AI_ADDRCONFIG = global i32 0
@.str.1 = [37 x i8] c"couldn't resolve socket address: %s\0A\00"
@errno = global i32 0
define i32 @find_localhost_addr(i32 %0, %struct.addrinfo** nocapture writeonly %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = tail call i8* @string_format(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = bitcast %struct.addrinfo* %3 to i8*
%7 = bitcast %struct.addrinfo** %4 to i8*
%8 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 24)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %9, i32* %10
%11 = load i32, i32* @SOCK_DGRAM
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 2
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 1
store i64 0, i64* %13
%14 = load i32, i32* @AI_ADDRCONFIG
%15 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 0
store i32 %14, i32* %15
%16 = call i32 @getaddrinfo(i8* null, i8* %5, %struct.addrinfo* %3, %struct.addrinfo** %4)
%17 = icmp eq i32 %16, 0
br i1 %17, label %22, label %18
18: ; preds = %2
%19 = load i32, i32* @errno
%20 = call i32 @strerror(i32 %19)
%21 = call i32 @cctools_debug(i64 549755813888, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %20)
br label %22
22: ; preds = %18, %2
%23 = call i32 @free(i8* %5)
%24 = load %struct.addrinfo*, %struct.addrinfo** %4
store %struct.addrinfo* %24, %struct.addrinfo** %1
ret i32 %16
}