#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*/);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int strlen(int /*<<< orphan*/);
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 (strlen(hptr->h_name) >= dstlen) {
    errno = ERANGE;
    return (NULL);
  }
  strcpy(dst, hptr->h_name);
  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    127c <host_name_to_cname+0x11c>
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   1040 <get_host_by_addr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1232 <host_name_to_cname+0xd2>
movq   $0x0,-0x8(%rbp)
jmp    127c <host_name_to_cname+0x11c>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1060 <strlen@plt>
cmp    -0x1c(%rbp),%eax
jl     1265 <host_name_to_cname+0x105>
mov    0x2d93(%rip),%rax
mov    (%rax),%ecx
mov    0x2d7a(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    127c <host_name_to_cname+0x11c>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%esi
call   1050 <strcpy@plt>
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   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x2018,%rsp
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbp
xor    %edi,%edi
test   %rbp,%rbp
setne  %dil
call   1070 <assert@plt>
xor    %edi,%edi
test   %r15,%r15
setne  %dil
call   1070 <assert@plt>
xor    %ebx,%ebx
lea    0x10(%rsp),%rsi
mov    %rbp,%rdi
mov    $0x2000,%edx
xor    %ecx,%ecx
call   1030 <get_host_by_name@plt>
test   %rax,%rax
je     1221 <host_name_to_cname+0xc1>
mov    0x8(%rax),%rcx
mov    (%rcx),%esi
mov    0x4(%rax),%edx
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
call   1080 <memcpy@plt>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edx
xor    %ebx,%ebx
lea    0x10(%rsp),%rcx
mov    %rbp,%rdi
mov    $0x4,%esi
mov    $0x2000,%r8d
xor    %r9d,%r9d
call   1040 <get_host_by_addr@plt>
test   %rax,%rax
je     1221 <host_name_to_cname+0xc1>
mov    %rax,%rbp
mov    (%rax),%edi
call   1060 <strlen@plt>
cmp    %r14d,%eax
jge    120d <host_name_to_cname+0xad>
mov    0x0(%rbp),%esi
mov    %r15,%rdi
call   1050 <strcpy@plt>
mov    %r15,%rbx
jmp    1221 <host_name_to_cname+0xc1>
mov    0x2dcc(%rip),%rax
mov    (%rax),%eax
mov    0x2db3(%rip),%rcx
mov    %eax,(%rcx)
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x2018,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
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 %56
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 %56
40: ; preds = %24
%41 = load %struct.hostent*, %struct.hostent** %8
%42 = getelementptr inbounds %struct.hostent, %struct.hostent* %41, i32 0, i32 0
%43 = load i32, i32* %42
%44 = call i32 @strlen(i32 %43)
%45 = load i32, i32* %7
%46 = icmp sge i32 %44, %45
br i1 %46, label %47, label %49
47: ; preds = %40
%48 = load i32, i32* @ERANGE
store i32 %48, i32* @errno
store i8* null, i8** %4
br label %56
49: ; preds = %40
%50 = load i8*, i8** %6
%51 = load %struct.hostent*, %struct.hostent** %8
%52 = getelementptr inbounds %struct.hostent, %struct.hostent* %51, i32 0, i32 0
%53 = load i32, i32* %52
%54 = call i32 @strcpy(i8* %50, i32 %53)
%55 = load i8*, i8** %6
store i8* %55, i8** %4
br label %56
56: ; preds = %49, %47, %39, %23
%57 = load i8*, i8** %4
ret i8* %57
}

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 %36, 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 %36, label %26
26: ; preds = %16
%27 = getelementptr inbounds %struct.hostent, %struct.hostent* %24, i64 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @strlen(i32 %28)
%30 = icmp slt i32 %29, %2
br i1 %30, label %33, label %31
31: ; preds = %26
%32 = load i32, i32* @ERANGE
store i32 %32, i32* @errno
br label %36
33: ; preds = %26
%34 = load i32, i32* %27
%35 = call i32 @strcpy(i8* %1, i32 %34)
br label %36
36: ; preds = %16, %3, %33, %31
%37 = phi i8* [ null, %31 ], [ %1, %33 ], [ null, %3 ], [ null, %16 ]
ret i8* %37
}