#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 in_addr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_name;
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ *h_addr_list;
};
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ ERANGE;
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
struct hostent *get_host_by_addr(char *, int, int /*<<< orphan*/,
                                 unsigned char *, int, int /*<<< orphan*/ *);
struct hostent *get_host_by_name(char const *, unsigned char *, int,
                                 int /*<<< orphan*/ *);
int /*<<< orphan*/ memcpy(struct in_addr *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
size_t strlen(int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, int /*<<< orphan*/, int);
char *host_name_to_cname(const char *src, char *dst, int dstlen) {
  struct hostent *hptr;
  unsigned char buf[8192];
  struct in_addr addr;
  assert(src != NULL);
  assert(dst != NULL);
  if (!(hptr = get_host_by_name(src, buf, sizeof(buf), NULL)))
    return (NULL);
  memcpy(&addr, hptr->h_addr_list[0], hptr->h_length);
  if (!(hptr = get_host_by_addr((char *)&addr, 4, AF_INET, buf, sizeof(buf),
                                NULL)))
    return (NULL);
  if ((dstlen <= 0) || (strlen(hptr->h_name) >= (size_t)dstlen)) {
    errno = ERANGE;
    return (NULL);
  }
  strncpy(dst, hptr->h_name, dstlen);
  dst[dstlen - 1] = '\0';
  return (dst);
}


x86 assembly (clang, -O0):
<host_name_to_cname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2040,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
cmpq   $0x0,-0x18(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x10(%rbp),%rdi
lea    -0x2030(%rbp),%rsi
mov    $0x2000,%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1030 <get_host_by_name@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11ce <host_name_to_cname+0x6e>
movq   $0x0,-0x8(%rbp)
jmp    129e <host_name_to_cname+0x13e>
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%esi
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%edx
lea    -0x2038(%rbp),%rdi
call   1080 <memcpy@plt>
lea    -0x2038(%rbp),%rdi
mov    0x2def(%rip),%rax
mov    (%rax),%edx
lea    -0x2030(%rbp),%rcx
mov    $0x4,%esi
mov    $0x2000,%r8d
xor    %eax,%eax
mov    %eax,%r9d
call   1050 <get_host_by_addr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1232 <host_name_to_cname+0xd2>
movq   $0x0,-0x8(%rbp)
jmp    129e <host_name_to_cname+0x13e>
cmpl   $0x0,-0x1c(%rbp)
jle    1254 <host_name_to_cname+0xf4>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1060 <strlen@plt>
movslq -0x1c(%rbp),%rcx
cmp    %rcx,%rax
jb     1273 <host_name_to_cname+0x113>
mov    0x2d85(%rip),%rax
mov    (%rax),%ecx
mov    0x2d6c(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    129e <host_name_to_cname+0x13e>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%esi
mov    -0x1c(%rbp),%edx
call   1040 <strncpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x2040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<host_name_to_cname>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2010,%rsp
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbx
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1070 <assert@plt>
xor    %edi,%edi
test   %r15,%r15
setne  %dil
call   1070 <assert@plt>
xor    %r13d,%r13d
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x2000,%edx
xor    %ecx,%ecx
call   1030 <get_host_by_name@plt>
test   %rax,%rax
je     1240 <host_name_to_cname+0xe0>
mov    0x8(%rax),%rcx
mov    (%rcx),%esi
mov    0x4(%rax),%edx
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1080 <memcpy@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%edx
xor    %r13d,%r13d
lea    0x10(%rsp),%rcx
mov    %rbx,%rdi
mov    $0x4,%esi
mov    $0x2000,%r8d
xor    %r9d,%r9d
call   1050 <get_host_by_addr@plt>
test   %rax,%rax
je     1240 <host_name_to_cname+0xe0>
test   %r14d,%r14d
jle    122b <host_name_to_cname+0xcb>
mov    %rax,%r12
mov    (%rax),%edi
call   1060 <strlen@plt>
mov    %r14d,%ecx
cmp    %rcx,%rax
jae    122b <host_name_to_cname+0xcb>
mov    (%r12),%esi
mov    %r15,%rdi
mov    %r14d,%edx
call   1040 <strncpy@plt>
add    $0xffffffff,%r14d
movb   $0x0,(%r15,%r14,1)
mov    %r15,%r13
jmp    1240 <host_name_to_cname+0xe0>
mov    0x2dae(%rip),%rax
mov    (%rax),%eax
mov    0x2d95(%rip),%rcx
mov    %eax,(%rcx)
xor    %r13d,%r13d
mov    %r13,%rax
add    $0x2010,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.hostent = type { i32, i32, i32* }
%struct.in_addr = type { i32 }
@AF_INET = global i32 0
@ERANGE = global i32 0
@errno = global i32 0
define i8* @host_name_to_cname(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.hostent*
%9 = alloca [8192 x i8]
%10 = alloca %struct.in_addr
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i8*, i8** %5
%12 = icmp ne i8* %11, null
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = load i8*, i8** %6
%16 = icmp ne i8* %15, null
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
%19 = load i8*, i8** %5
%20 = getelementptr inbounds [8192 x i8], [8192 x i8]* %9, i64 0, i64 0
%21 = call %struct.hostent* @get_host_by_name(i8* %19, i8* %20, i32 8192, i32* null)
store %struct.hostent* %21, %struct.hostent** %8
%22 = icmp ne %struct.hostent* %21, null
br i1 %22, label %24, label %23
23: ; preds = %3
store i8* null, i8** %4
br label %66
24: ; preds = %3
%25 = load %struct.hostent*, %struct.hostent** %8
%26 = getelementptr inbounds %struct.hostent, %struct.hostent* %25, i32 0, i32 2
%27 = load i32*, i32** %26
%28 = getelementptr inbounds i32, i32* %27, i64 0
%29 = load i32, i32* %28
%30 = load %struct.hostent*, %struct.hostent** %8
%31 = getelementptr inbounds %struct.hostent, %struct.hostent* %30, i32 0, i32 1
%32 = load i32, i32* %31
%33 = call i32 @memcpy(%struct.in_addr* %10, i32 %29, i32 %32)
%34 = bitcast %struct.in_addr* %10 to i8*
%35 = load i32, i32* @AF_INET
%36 = getelementptr inbounds [8192 x i8], [8192 x i8]* %9, i64 0, i64 0
%37 = call %struct.hostent* @get_host_by_addr(i8* %34, i32 4, i32 %35, i8* %36, i32 8192, i32* null)
store %struct.hostent* %37, %struct.hostent** %8
%38 = icmp ne %struct.hostent* %37, null
br i1 %38, label %40, label %39
39: ; preds = %24
store i8* null, i8** %4
br label %66
40: ; preds = %24
%41 = load i32, i32* %7
%42 = icmp sle i32 %41, 0
br i1 %42, label %51, label %43
43: ; preds = %40
%44 = load %struct.hostent*, %struct.hostent** %8
%45 = getelementptr inbounds %struct.hostent, %struct.hostent* %44, i32 0, i32 0
%46 = load i32, i32* %45
%47 = call i64 @strlen(i32 %46)
%48 = load i32, i32* %7
%49 = sext i32 %48 to i64
%50 = icmp uge i64 %47, %49
br i1 %50, label %51, label %53
51: ; preds = %43, %40
%52 = load i32, i32* @ERANGE
store i32 %52, i32* @errno
store i8* null, i8** %4
br label %66
53: ; preds = %43
%54 = load i8*, i8** %6
%55 = load %struct.hostent*, %struct.hostent** %8
%56 = getelementptr inbounds %struct.hostent, %struct.hostent* %55, i32 0, i32 0
%57 = load i32, i32* %56
%58 = load i32, i32* %7
%59 = call i32 @strncpy(i8* %54, i32 %57, i32 %58)
%60 = load i8*, i8** %6
%61 = load i32, i32* %7
%62 = sub nsw i32 %61, 1
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8, i8* %60, i64 %63
store i8 0, i8* %64
%65 = load i8*, i8** %6
store i8* %65, i8** %4
br label %66
66: ; preds = %53, %51, %39, %23
%67 = load i8*, i8** %4
ret i8* %67
}

LLVM IR (-O2):
%struct.in_addr = type { i32 }
%struct.hostent = type { i32, i32, i32* }
@AF_INET = global i32 0
@ERANGE = global i32 0
@errno = global i32 0
define i8* @host_name_to_cname(i8* %0, i8* %1, i32 %2) {
%4 = alloca [8192 x i8]
%5 = alloca %struct.in_addr
%6 = getelementptr inbounds [8192 x i8], [8192 x i8]* %4, i64 0, i64 0
%7 = bitcast %struct.in_addr* %5 to i8*
%8 = icmp ne i8* %0, null
%9 = zext i1 %8 to i32
%10 = tail call i32 @assert(i32 %9)
%11 = icmp ne i8* %1, null
%12 = zext i1 %11 to i32
%13 = tail call i32 @assert(i32 %12)
%14 = call %struct.hostent* @get_host_by_name(i8* %0, i8* %6, i32 8192, i32* null)
%15 = icmp eq %struct.hostent* %14, null
br i1 %15, label %42, label %16
16: ; preds = %3
%17 = getelementptr inbounds %struct.hostent, %struct.hostent* %14, i64 0, i32 2
%18 = load i32*, i32** %17
%19 = load i32, i32* %18
%20 = getelementptr inbounds %struct.hostent, %struct.hostent* %14, i64 0, i32 1
%21 = load i32, i32* %20
%22 = call i32 @memcpy(%struct.in_addr* %5, i32 %19, i32 %21)
%23 = load i32, i32* @AF_INET
%24 = call %struct.hostent* @get_host_by_addr(i8* %7, i32 4, i32 %23, i8* %6, i32 8192, i32* null)
%25 = icmp eq %struct.hostent* %24, null
br i1 %25, label %42, label %26
26: ; preds = %16
%27 = icmp slt i32 %2, 1
br i1 %27, label %34, label %28
28: ; preds = %26
%29 = getelementptr inbounds %struct.hostent, %struct.hostent* %24, i64 0, i32 0
%30 = load i32, i32* %29
%31 = call i64 @strlen(i32 %30)
%32 = zext i32 %2 to i64
%33 = icmp ult i64 %31, %32
br i1 %33, label %36, label %34
34: ; preds = %28, %26
%35 = load i32, i32* @ERANGE
store i32 %35, i32* @errno
br label %42
36: ; preds = %28
%37 = load i32, i32* %29
%38 = call i32 @strncpy(i8* %1, i32 %37, i32 %2)
%39 = add nsw i32 %2, -1
%40 = zext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %1, i64 %40
store i8 0, i8* %41
br label %42
42: ; preds = %16, %3, %36, %34
%43 = phi i8* [ null, %34 ], [ %1, %36 ], [ null, %3 ], [ null, %16 ]
ret i8* %43
}