#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 *);
int getaddrinfo(char *, int /*<<< orphan*/ *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ gethostname(char *, int);
int /*<<< orphan*/ memset(struct addrinfo *, int /*<<< orphan*/, int);
int /*<<< orphan*/ strcpy(char *, char *);
int 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) {
    strcpy(buffer, "getaddrinfo failed\n");
    return error;
  }
  strcpy(buffer, info->ai_canonname);
  freeaddrinfo(info);
  return error;
}


x86 assembly (clang, -O0):
<kvp_get_domain_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1050 <gethostname@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2e5f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x30(%rbp),%rdx
lea    -0x38(%rbp),%rcx
call   1060 <getaddrinfo@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
je     11e3 <kvp_get_domain_name+0x93>
mov    -0x10(%rbp),%rdi
lea    0xe2d(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1202 <kvp_get_domain_name+0xb2>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rax
mov    (%rax),%rsi
call   1030 <strcpy@plt>
mov    -0x38(%rbp),%rdi
call   1070 <freeaddrinfo@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kvp_get_domain_name>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbx
call   1050 <gethostname@plt>
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2e6f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x1c(%rsp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
lea    0x8(%rsp),%rcx
mov    %rbx,%rdi
xor    %esi,%esi
mov    %rbp,%rdx
call   1060 <getaddrinfo@plt>
mov    %eax,%ebp
test   %eax,%eax
je     11c2 <kvp_get_domain_name+0x72>
lea    0xe48(%rip),%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
jmp    11dc <kvp_get_domain_name+0x8c>
mov    0x8(%rsp),%rax
mov    (%rax),%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
mov    0x8(%rsp),%rdi
call   1070 <freeaddrinfo@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
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 = [20 x i8] c"getaddrinfo failed\0A\00"
define i32 @kvp_get_domain_name(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.addrinfo
%7 = alloca %struct.addrinfo*
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %8
%9 = load i8*, i8** %4
%10 = load i32, i32* %5
%11 = call i32 @gethostname(i8* %9, i32 %10)
%12 = call i32 @memset(%struct.addrinfo* %6, i32 0, i32 24)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 3
store i32 %13, i32* %14
%15 = load i32, i32* @SOCK_STREAM
%16 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 2
store i32 %15, i32* %16
%17 = load i32, i32* @AI_CANONNAME
%18 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 1
store i32 %17, i32* %18
%19 = load i8*, i8** %4
%20 = call i32 @getaddrinfo(i8* %19, i32* null, %struct.addrinfo* %6, %struct.addrinfo** %7)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp ne i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %2
%24 = load i8*, i8** %4
%25 = call i32 @strcpy(i8* %24, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%26 = load i32, i32* %8
store i32 %26, i32* %3
br label %36
27: ; preds = %2
%28 = load i8*, i8** %4
%29 = load %struct.addrinfo*, %struct.addrinfo** %7
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %29, i32 0, i32 0
%31 = load i8*, i8** %30
%32 = call i32 @strcpy(i8* %28, i8* %31)
%33 = load %struct.addrinfo*, %struct.addrinfo** %7
%34 = call i32 @freeaddrinfo(%struct.addrinfo* %33)
%35 = load i32, i32* %8
store i32 %35, i32* %3
br label %36
36: ; preds = %27, %23
%37 = load i32, i32* %3
ret i32 %37
}

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 = [20 x i8] c"getaddrinfo failed\0A\00"
define i32 @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 %19, label %17
17: ; preds = %2
%18 = call i32 @strcpy(i8* %0, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %26
19: ; preds = %2
%20 = load %struct.addrinfo*, %struct.addrinfo** %4
%21 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %20, i64 0, i32 0
%22 = load i8*, i8** %21
%23 = call i32 @strcpy(i8* %0, i8* %22)
%24 = load %struct.addrinfo*, %struct.addrinfo** %4
%25 = call i32 @freeaddrinfo(%struct.addrinfo* %24)
br label %26
26: ; preds = %19, %17
ret i32 %15
}