#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 {
  char *ai_canonname;
  int /*<<< orphan*/ ai_flags;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
};
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ AI_CANONNAME;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
char *gai_strerror(int);
int getaddrinfo(char *, int /*<<< orphan*/ *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ gethostname(char *, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ snprintf(char *, int, char *, ...);
void kvp_get_domain_name(char *buffer, int length) {
  struct addrinfo hints, *info;
  int error = 0;
  gethostname(buffer, length);
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_CANONNAME;
  error = getaddrinfo(buffer, NULL, &hints, &info);
  if (error != 0) {
    snprintf(buffer, length, "getaddrinfo failed: 0x%x %s", error,
             gai_strerror(error));
    return;
  }
  snprintf(buffer, length, "%s", info->ai_canonname);
  freeaddrinfo(info);
}


x86 assembly (clang, -O0):
<kvp_get_domain_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1060 <gethostname@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x2e4f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x28(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   1070 <getaddrinfo@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     1214 <kvp_get_domain_name+0xb4>
mov    -0x8(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x34(%rbp),%edi
call   1040 <gai_strerror@plt>
mov    -0x48(%rbp),%rdi
mov    -0x3c(%rbp),%esi
mov    -0x38(%rbp),%ecx
mov    %rax,%r8
lea    0xdf8(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
jmp    1239 <kvp_get_domain_name+0xd9>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%rcx
lea    0xdf0(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
mov    -0x30(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kvp_get_domain_name>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
call   1060 <gethostname@plt>
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    0x2e5a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e35(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2e38(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %rsp,%rcx
mov    %rbx,%rdi
xor    %esi,%esi
mov    %rbp,%rdx
call   1070 <getaddrinfo@plt>
test   %eax,%eax
je     11e6 <kvp_get_domain_name+0x86>
mov    %eax,%ebp
mov    %eax,%edi
call   1040 <gai_strerror@plt>
lea    0xe2e(%rip),%rdx
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %ebp,%ecx
mov    %rax,%r8
xor    %eax,%eax
call   1030 <snprintf@plt>
jmp    120a <kvp_get_domain_name+0xaa>
mov    (%rsp),%rax
mov    (%rax),%rcx
lea    0xe25(%rip),%rdx
mov    %rbx,%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1030 <snprintf@plt>
mov    (%rsp),%rdi
call   1080 <freeaddrinfo@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.addrinfo = type { i8*, i32, i32, i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@AI_CANONNAME = global i32 0
@.str = [28 x i8] c"getaddrinfo failed: 0x%x %s\00"
@.str.1 = [3 x i8] c"%s\00"
define void @kvp_get_domain_name(i8* %0, i32 %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = bitcast %struct.addrinfo* %3 to i8*
%6 = bitcast %struct.addrinfo** %4 to i8*
%7 = tail call i32 @gethostname(i8* %0, i32 %1)
%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_STREAM
%12 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 2
store i32 %11, i32* %12
%13 = load i32, i32* @AI_CANONNAME
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 1
store i32 %13, i32* %14
%15 = call i32 @getaddrinfo(i8* %0, i32* null, %struct.addrinfo* %3, %struct.addrinfo** %4)
%16 = icmp eq i32 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %2
%18 = call i8* @gai_strerror(i32 %15)
%19 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %0, i32 %1, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %15, i8* %18)
br label %27
20: ; preds = %2
%21 = load %struct.addrinfo*, %struct.addrinfo** %4
%22 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %21, i64 0, i32 0
%23 = load i8*, i8** %22
%24 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %0, i32 %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %23)
%25 = load %struct.addrinfo*, %struct.addrinfo** %4
%26 = call i32 @freeaddrinfo(%struct.addrinfo* %25)
br label %27
27: ; preds = %20, %17
ret void
}