#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 *, char *, 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;
  gethostname(buffer, length);
  int error = 0;
  memset(&hints, 0, sizeof(hints));
  hints.ai_family = AF_INET;
  hints.ai_socktype = SOCK_STREAM;
  hints.ai_flags = AI_CANONNAME;
  error = getaddrinfo(buffer, "http", &hints, &info);
  if (error != 0) {
    strcpy(buffer, "getaddrinfo failed\n");
    error = 1;
    goto get_domain_done;
  }
  strcpy(buffer, info->ai_canonname);
get_domain_done:
  freeaddrinfo(info);
  return error;
}


x86 assembly (clang, -O0):
<kvp_get_domain_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1050 <gethostname@plt>
movl   $0x0,-0x34(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2e5f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe4f(%rip),%rsi
lea    -0x28(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   1060 <getaddrinfo@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     11e7 <kvp_get_domain_name+0x97>
mov    -0x8(%rbp),%rdi
lea    0xe2f(%rip),%rsi
call   1030 <strcpy@plt>
movl   $0x1,-0x34(%rbp)
jmp    11f7 <kvp_get_domain_name+0xa7>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%rsi
call   1030 <strcpy@plt>
mov    -0x30(%rbp),%rdi
call   1070 <freeaddrinfo@plt>
mov    -0x34(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kvp_get_domain_name>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%rbx
call   1050 <gethostname@plt>
lea    0x8(%rsp),%r14
xor    %ebp,%ebp
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1040 <memset@plt>
mov    0x2e6b(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x14(%rsp)
mov    0x2e49(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
lea    0xe5c(%rip),%rsi
mov    %rsp,%rcx
mov    %rbx,%rdi
mov    %r14,%rdx
call   1060 <getaddrinfo@plt>
test   %eax,%eax
je     11c4 <kvp_get_domain_name+0x74>
mov    $0x1,%ebp
lea    0xe43(%rip),%rsi
jmp    11cb <kvp_get_domain_name+0x7b>
mov    (%rsp),%rax
mov    (%rax),%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
mov    (%rsp),%rdi
call   1070 <freeaddrinfo@plt>
mov    %ebp,%eax
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 = [5 x i8] c"http\00"
@.str.1 = [20 x i8] c"getaddrinfo failed\0A\00"
define i32 @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
%8 = load i8*, i8** %3
%9 = load i32, i32* %4
%10 = call i32 @gethostname(i8* %8, i32 %9)
store i32 0, i32* %7
%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, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %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 %25
22: ; preds = %2
%23 = load i8*, i8** %3
%24 = call i32 @strcpy(i8* %23, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %7
br label %31
25: ; preds = %2
%26 = load i8*, i8** %3
%27 = load %struct.addrinfo*, %struct.addrinfo** %6
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i32 0, i32 0
%29 = load i8*, i8** %28
%30 = call i32 @strcpy(i8* %26, i8* %29)
br label %31
31: ; preds = %25, %22
%32 = load %struct.addrinfo*, %struct.addrinfo** %6
%33 = call i32 @freeaddrinfo(%struct.addrinfo* %32)
%34 = load i32, i32* %7
ret i32 %34
}

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