#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;
};
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*/ *);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int strlen(int /*<<< orphan*/);
char *host_addr4_to_name(const struct in_addr *addr, char *dst, int dstlen) {
  struct hostent *hptr;
  unsigned char buf[8192];
  assert(addr != NULL);
  assert(dst != NULL);
  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_addr4_to_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2030,%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   1060 <assert@plt>
cmpq   $0x0,-0x18(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e63(%rip),%rax
mov    (%rax),%edx
lea    -0x2030(%rbp),%rcx
mov    $0x4,%esi
mov    $0x2000,%r8d
xor    %eax,%eax
mov    %eax,%r9d
call   1030 <get_host_by_addr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11be <host_addr4_to_name+0x7e>
movq   $0x0,-0x8(%rbp)
jmp    1208 <host_addr4_to_name+0xc8>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1050 <strlen@plt>
cmp    -0x1c(%rbp),%eax
jl     11f1 <host_addr4_to_name+0xb1>
mov    0x2e07(%rip),%rax
mov    (%rax),%ecx
mov    0x2dee(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1208 <host_addr4_to_name+0xc8>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%esi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x2030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<host_addr4_to_name>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x2008,%rsp
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbp
xor    %edi,%edi
test   %rbp,%rbp
setne  %dil
call   1060 <assert@plt>
xor    %edi,%edi
test   %r15,%r15
setne  %dil
call   1060 <assert@plt>
mov    0x2e6f(%rip),%rax
mov    (%rax),%edx
xor    %ebx,%ebx
mov    %rsp,%rcx
mov    %rbp,%rdi
mov    $0x4,%esi
mov    $0x2000,%r8d
xor    %r9d,%r9d
call   1030 <get_host_by_addr@plt>
test   %rax,%rax
je     11ce <host_addr4_to_name+0x8e>
mov    %rax,%rbp
mov    (%rax),%edi
call   1050 <strlen@plt>
cmp    %r14d,%eax
jge    11ba <host_addr4_to_name+0x7a>
mov    0x0(%rbp),%esi
mov    %r15,%rdi
call   1040 <strcpy@plt>
mov    %r15,%rbx
jmp    11ce <host_addr4_to_name+0x8e>
mov    0x2e1f(%rip),%rax
mov    (%rax),%eax
mov    0x2e06(%rip),%rcx
mov    %eax,(%rcx)
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x2008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.in_addr = type { i32 }
%struct.hostent = type { i32 }
@AF_INET = global i32 0
@ERANGE = global i32 0
@errno = global i32 0
define i8* @host_addr4_to_name(%struct.in_addr* %0, i8* %1, i32 %2) {
%4 = alloca [8192 x i8]
%5 = getelementptr inbounds [8192 x i8], [8192 x i8]* %4, i64 0, i64 0
%6 = icmp ne %struct.in_addr* %0, null
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = icmp ne i8* %1, null
%10 = zext i1 %9 to i32
%11 = tail call i32 @assert(i32 %10)
%12 = bitcast %struct.in_addr* %0 to i8*
%13 = load i32, i32* @AF_INET
%14 = call %struct.hostent* @get_host_by_addr(i8* %12, i32 4, i32 %13, i8* %5, i32 8192, i32* null)
%15 = icmp eq %struct.hostent* %14, null
br i1 %15, label %26, label %16
16: ; preds = %3
%17 = getelementptr inbounds %struct.hostent, %struct.hostent* %14, i64 0, i32 0
%18 = load i32, i32* %17
%19 = call i32 @strlen(i32 %18)
%20 = icmp slt i32 %19, %2
br i1 %20, label %23, label %21
21: ; preds = %16
%22 = load i32, i32* @ERANGE
store i32 %22, i32* @errno
br label %26
23: ; preds = %16
%24 = load i32, i32* %17
%25 = call i32 @strcpy(i8* %1, i32 %24)
br label %26
26: ; preds = %3, %23, %21
%27 = phi i8* [ null, %21 ], [ %1, %23 ], [ null, %3 ]
ret i8* %27
}