#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*/ *);
size_t strlen(int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(char *, int /*<<< orphan*/, int);
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 ((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_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   1040 <get_host_by_addr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11be <host_addr4_to_name+0x7e>
movq   $0x0,-0x8(%rbp)
jmp    122a <host_addr4_to_name+0xea>
cmpl   $0x0,-0x1c(%rbp)
jle    11e0 <host_addr4_to_name+0xa0>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1050 <strlen@plt>
movslq -0x1c(%rbp),%rcx
cmp    %rcx,%rax
jb     11ff <host_addr4_to_name+0xbf>
mov    0x2df9(%rip),%rax
mov    (%rax),%ecx
mov    0x2de0(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    122a <host_addr4_to_name+0xea>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    (%rax),%esi
mov    -0x1c(%rbp),%edx
call   1030 <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    $0x2030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<host_addr4_to_name>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x2008,%rsp
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1060 <assert@plt>
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1060 <assert@plt>
mov    0x2e6e(%rip),%rax
mov    (%rax),%edx
xor    %r12d,%r12d
mov    %rsp,%rcx
mov    %rbx,%rdi
mov    $0x4,%esi
mov    $0x2000,%r8d
xor    %r9d,%r9d
call   1040 <get_host_by_addr@plt>
test   %rax,%rax
je     11e4 <host_addr4_to_name+0xa4>
test   %r15d,%r15d
jle    11cf <host_addr4_to_name+0x8f>
mov    %rax,%rbx
mov    (%rax),%edi
call   1050 <strlen@plt>
mov    %r15d,%ecx
cmp    %rcx,%rax
jae    11cf <host_addr4_to_name+0x8f>
mov    (%rbx),%esi
mov    %r14,%rdi
mov    %r15d,%edx
call   1030 <strncpy@plt>
add    $0xffffffff,%r15d
movb   $0x0,(%r14,%r15,1)
mov    %r14,%r12
jmp    11e4 <host_addr4_to_name+0xa4>
mov    0x2e0a(%rip),%rax
mov    (%rax),%eax
mov    0x2df1(%rip),%rcx
mov    %eax,(%rcx)
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x2008,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
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 %51
25: ; preds = %3
%26 = load i32, i32* %7
%27 = icmp sle i32 %26, 0
br i1 %27, label %36, label %28
28: ; preds = %25
%29 = load %struct.hostent*, %struct.hostent** %8
%30 = getelementptr inbounds %struct.hostent, %struct.hostent* %29, i32 0, i32 0
%31 = load i32, i32* %30
%32 = call i64 @strlen(i32 %31)
%33 = load i32, i32* %7
%34 = sext i32 %33 to i64
%35 = icmp uge i64 %32, %34
br i1 %35, label %36, label %38
36: ; preds = %28, %25
%37 = load i32, i32* @ERANGE
store i32 %37, i32* @errno
store i8* null, i8** %4
br label %51
38: ; preds = %28
%39 = load i8*, i8** %6
%40 = load %struct.hostent*, %struct.hostent** %8
%41 = getelementptr inbounds %struct.hostent, %struct.hostent* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = load i32, i32* %7
%44 = call i32 @strncpy(i8* %39, i32 %42, i32 %43)
%45 = load i8*, i8** %6
%46 = load i32, i32* %7
%47 = sub nsw i32 %46, 1
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %45, i64 %48
store i8 0, i8* %49
%50 = load i8*, i8** %6
store i8* %50, i8** %4
br label %51
51: ; preds = %38, %36, %24
%52 = load i8*, i8** %4
ret i8* %52
}

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 %32, label %16
16: ; preds = %3
%17 = icmp slt i32 %2, 1
br i1 %17, label %24, label %18
18: ; preds = %16
%19 = getelementptr inbounds %struct.hostent, %struct.hostent* %14, i64 0, i32 0
%20 = load i32, i32* %19
%21 = call i64 @strlen(i32 %20)
%22 = zext i32 %2 to i64
%23 = icmp ult i64 %21, %22
br i1 %23, label %26, label %24
24: ; preds = %18, %16
%25 = load i32, i32* @ERANGE
store i32 %25, i32* @errno
br label %32
26: ; preds = %18
%27 = load i32, i32* %19
%28 = call i32 @strncpy(i8* %1, i32 %27, i32 %2)
%29 = add nsw i32 %2, -1
%30 = zext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %1, i64 %30
store i8 0, i8* %31
br label %32
32: ; preds = %3, %26, %24
%33 = phi i8* [ null, %24 ], [ %1, %26 ], [ null, %3 ]
ret i8* %33
}