#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_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};
typedef int /*<<< orphan*/ hostname;
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int /*<<< orphan*/ HOST_NAME_MAX;
int /*<<< orphan*/ NI_NUMERICHOST;
int /*<<< orphan*/ PF_UNSPEC;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ gai_strerror(int);
int getaddrinfo(char *, int /*<<< orphan*/ *, struct addrinfo *,
                struct addrinfo **);
scalar_t__ gethostname(char *, int);
int getnameinfo(int /*<<< orphan*/, int /*<<< orphan*/, char *, int,
                int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ log_err_exit(char *);
int /*<<< orphan*/ log_msg_exit(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
void ipaddr_getprimary(char *buf, int len) {
  char hostname[HOST_NAME_MAX + 1];
  struct addrinfo hints, *res = NULL;
  int e;
  if (gethostname(hostname, sizeof(hostname)) < 0)
    log_err_exit("gethostname");
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = PF_UNSPEC;
  hints.ai_socktype = SOCK_STREAM;
  if ((e = getaddrinfo(hostname, NULL, &hints, &res)) || res == NULL)
    log_msg_exit("getaddrinfo %s: %s", hostname, gai_strerror(e));
  if ((e = getnameinfo(res->ai_addr, res->ai_addrlen, buf, len, NULL, 0,
                       NI_NUMERICHOST)))
    log_msg_exit("getnameinfo %s: %s", hostname, gai_strerror(e));
  freeaddrinfo(res);
}


x86 assembly (clang, -O0):
<ipaddr_getprimary>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x48(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x38(%rbp)
mov    %eax,%esi
call   1070 <gethostname@plt>
cmp    $0x0,%rax
jge    11e3 <ipaddr_getprimary+0x63>
lea    0xe22(%rip),%rdi
call   1060 <log_err_exit@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    -0x48(%rbp),%rdi
mov    0x2dea(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2dc6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
xor    %eax,%eax
mov    %eax,%esi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1090 <getaddrinfo@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0x0,%eax
jne    1237 <ipaddr_getprimary+0xb7>
cmpq   $0x0,-0x38(%rbp)
jne    1251 <ipaddr_getprimary+0xd1>
mov    -0x3c(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
lea    0xdc0(%rip),%rdi
call   1080 <log_msg_exit@plt>
mov    -0x38(%rbp),%rax
mov    0x4(%rax),%edi
mov    -0x38(%rbp),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%rdx
mov    -0xc(%rbp),%ecx
mov    0x2d5c(%rip),%rax
mov    (%rax),%eax
xor    %r8d,%r8d
xor    %r9d,%r9d
sub    $0x10,%rsp
mov    %eax,(%rsp)
call   1050 <getnameinfo@plt>
add    $0x10,%rsp
mov    %eax,-0x3c(%rbp)
cmp    $0x0,%eax
je     12aa <ipaddr_getprimary+0x12a>
mov    -0x3c(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
lea    0xd7a(%rip),%rdi
call   1080 <log_msg_exit@plt>
mov    -0x38(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipaddr_getprimary>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %esi,%r14d
mov    %rdi,%r15
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
add    $0x1,%esi
mov    %rsp,%rbx
lea    0xf(%rsi),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
movq   $0x0,-0x28(%rbp)
mov    %rbx,%rdi
call   1070 <gethostname@plt>
test   %rax,%rax
jns    11d3 <ipaddr_getprimary+0x53>
lea    0xe32(%rip),%rdi
call   1060 <log_err_exit@plt>
lea    -0x38(%rbp),%r12
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2dd7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
lea    -0x28(%rbp),%rcx
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r12,%rdx
call   1090 <getaddrinfo@plt>
test   %eax,%eax
jne    121c <ipaddr_getprimary+0x9c>
mov    -0x28(%rbp),%rcx
test   %rcx,%rcx
jne    1238 <ipaddr_getprimary+0xb8>
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xde2(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1080 <log_msg_exit@plt>
mov    -0x28(%rbp),%rcx
mov    (%rcx),%esi
mov    0x4(%rcx),%edi
mov    0x2d84(%rip),%rax
mov    (%rax),%eax
sub    $0x8,%rsp
mov    %r15,%rdx
mov    %r14d,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
push   %rax
call   1050 <getnameinfo@plt>
add    $0x10,%rsp
test   %eax,%eax
je     127c <ipaddr_getprimary+0xfc>
mov    %eax,%edi
call   1030 <gai_strerror@plt>
lea    0xdad(%rip),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1080 <log_msg_exit@plt>
mov    -0x28(%rbp),%rdi
call   10a0 <freeaddrinfo@plt>
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32 }
@HOST_NAME_MAX = global i32 0
@.str = [12 x i8] c"gethostname\00"
@PF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [19 x i8] c"getaddrinfo %s: %s\00"
@NI_NUMERICHOST = global i32 0
@.str.2 = [19 x i8] c"getnameinfo %s: %s\00"
define void @ipaddr_getprimary(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca %struct.addrinfo
%8 = alloca %struct.addrinfo*
%9 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%10 = load i32, i32* @HOST_NAME_MAX
%11 = add nsw i32 %10, 1
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %5
%14 = alloca i8, i64 %12
store i64 %12, i64* %6
store %struct.addrinfo* null, %struct.addrinfo** %8
%15 = trunc i64 %12 to i32
%16 = call i64 @gethostname(i8* %14, i32 %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = call i32 @log_err_exit(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %18, %2
%21 = call i32 @memset(%struct.addrinfo* %7, i32 0, i32 16)
%22 = load i32, i32* @PF_UNSPEC
%23 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 3
store i32 %22, i32* %23
%24 = load i32, i32* @SOCK_STREAM
%25 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %7, i32 0, i32 2
store i32 %24, i32* %25
%26 = call i32 @getaddrinfo(i8* %14, i32* null, %struct.addrinfo* %7, %struct.addrinfo** %8)
store i32 %26, i32* %9
%27 = icmp ne i32 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %20
%29 = load %struct.addrinfo*, %struct.addrinfo** %8
%30 = icmp eq %struct.addrinfo* %29, null
br i1 %30, label %31, label %35
31: ; preds = %28, %20
%32 = load i32, i32* %9
%33 = call i32 @gai_strerror(i32 %32)
%34 = call i32 @log_msg_exit(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %14, i32 %33)
br label %35
35: ; preds = %31, %28
%36 = load %struct.addrinfo*, %struct.addrinfo** %8
%37 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %36, i32 0, i32 1
%38 = load i32, i32* %37
%39 = load %struct.addrinfo*, %struct.addrinfo** %8
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %39, i32 0, i32 0
%41 = load i32, i32* %40
%42 = load i8*, i8** %3
%43 = load i32, i32* %4
%44 = load i32, i32* @NI_NUMERICHOST
%45 = call i32 @getnameinfo(i32 %38, i32 %41, i8* %42, i32 %43, i32* null, i32 0, i32 %44)
store i32 %45, i32* %9
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %35
%48 = load i32, i32* %9
%49 = call i32 @gai_strerror(i32 %48)
%50 = call i32 @log_msg_exit(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %14, i32 %49)
br label %51
51: ; preds = %47, %35
%52 = load %struct.addrinfo*, %struct.addrinfo** %8
%53 = call i32 @freeaddrinfo(%struct.addrinfo* %52)
%54 = load i8*, i8** %5
ret void
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32 }
@HOST_NAME_MAX = global i32 0
@.str = [12 x i8] c"gethostname\00"
@PF_UNSPEC = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [19 x i8] c"getaddrinfo %s: %s\00"
@NI_NUMERICHOST = global i32 0
@.str.2 = [19 x i8] c"getnameinfo %s: %s\00"
define void @ipaddr_getprimary(i8* %0, i32 %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = load i32, i32* @HOST_NAME_MAX
%6 = add nsw i32 %5, 1
%7 = zext i32 %6 to i64
%8 = alloca i8, i64 %7
%9 = bitcast %struct.addrinfo* %3 to i8*
%10 = bitcast %struct.addrinfo** %4 to i8*
store %struct.addrinfo* null, %struct.addrinfo** %4
%11 = call i64 @gethostname(i8* %8, i32 %6)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @log_err_exit(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %2
%16 = call i32 @memset(%struct.addrinfo* %3, i32 0, i32 16)
%17 = load i32, i32* @PF_UNSPEC
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 3
store i32 %17, i32* %18
%19 = load i32, i32* @SOCK_STREAM
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 2
store i32 %19, i32* %20
%21 = call i32 @getaddrinfo(i8* %8, i32* null, %struct.addrinfo* %3, %struct.addrinfo** %4)
%22 = icmp ne i32 %21, 0
%23 = load %struct.addrinfo*, %struct.addrinfo** %4
%24 = icmp eq %struct.addrinfo* %23, null
%25 = select i1 %22, i1 true, i1 %24
br i1 %25, label %26, label %30
26: ; preds = %15
%27 = call i32 @gai_strerror(i32 %21)
%28 = call i32 @log_msg_exit(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %8, i32 %27)
%29 = load %struct.addrinfo*, %struct.addrinfo** %4
br label %30
30: ; preds = %15, %26
%31 = phi %struct.addrinfo* [ %23, %15 ], [ %29, %26 ]
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i64 0, i32 1
%33 = load i32, i32* %32
%34 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %31, i64 0, i32 0
%35 = load i32, i32* %34
%36 = load i32, i32* @NI_NUMERICHOST
%37 = call i32 @getnameinfo(i32 %33, i32 %35, i8* %0, i32 %1, i32* null, i32 0, i32 %36)
%38 = icmp eq i32 %37, 0
br i1 %38, label %42, label %39
39: ; preds = %30
%40 = call i32 @gai_strerror(i32 %37)
%41 = call i32 @log_msg_exit(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %8, i32 %40)
br label %42
42: ; preds = %39, %30
%43 = load %struct.addrinfo*, %struct.addrinfo** %4
%44 = call i32 @freeaddrinfo(%struct.addrinfo* %43)
ret void
}