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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int MAXHOSTNAMELEN;
struct hostent *gethostbyaddr(char const *, int, int /*<<< orphan*/);
struct hostent *gethostbyname(char *);
char *inet_ntoa(struct in_addr const);
int /*<<< orphan*/ memcmp(int /*<<< orphan*/ *, struct in_addr const *, int);
int /*<<< orphan*/ strlcpy(char *, char *, int);
size_t strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
int /*<<< orphan*/ trimdomain(char *, size_t);
int realhostname(char *host, size_t hsize, const struct in_addr *ip) {
  char trimmed[MAXHOSTNAMELEN];
  int result;
  struct hostent *hp;
  result = (2);
  hp = gethostbyaddr((const char *)ip, sizeof(*ip), AF_INET);
  if (hp != NULL) {
    strlcpy(trimmed, hp->h_name, sizeof(trimmed));
    trimdomain(trimmed, strlen(trimmed));
    if (strlen(trimmed) <= hsize) {
      char lookup[MAXHOSTNAMELEN];
      strncpy(lookup, hp->h_name, sizeof(lookup) - 1);
      lookup[sizeof(lookup) - 1] = '\0';
      hp = gethostbyname(lookup);
      if (hp == NULL)
        result = (3);
      else
        for (;; hp->h_addr_list++) {
          if (*hp->h_addr_list == NULL) {
            result = (1);
            break;
          }
          if (!memcmp(*hp->h_addr_list, ip, sizeof(*ip))) {
            strncpy(host, trimmed, hsize);
            return (0);
          }
        }
    }
  }
  strncpy(host, inet_ntoa(*ip), hsize);
  return result;
}


x86 assembly (clang, -O0):
<realhostname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x68(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
movl   $0x2,-0x34(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2e0f(%rip),%rax
mov    (%rax),%edx
mov    $0x4,%esi
call   10a0 <gethostbyaddr@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     1354 <realhostname+0x1d4>
mov    -0x60(%rbp),%rdi
mov    -0x68(%rbp),%rax
mov    -0x40(%rbp),%rcx
mov    (%rcx),%rsi
mov    %eax,%edx
call   1090 <strlcpy@plt>
mov    -0x60(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x60(%rbp),%rdi
mov    %rax,%rsi
call   1070 <trimdomain@plt>
mov    -0x60(%rbp),%rdi
call   1050 <strlen@plt>
cmp    -0x18(%rbp),%rax
ja     134f <realhostname+0x1cf>
mov    0x2da7(%rip),%rax
mov    (%rax),%eax
mov    %eax,%edx
mov    %rdx,-0x70(%rbp)
mov    %rsp,%rax
mov    %rax,-0x48(%rbp)
mov    %rdx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%rdi
sub    %rax,%rdi
mov    %rdi,-0x78(%rbp)
mov    %rdi,%rsp
mov    %rdx,-0x50(%rbp)
mov    -0x40(%rbp),%rax
mov    (%rax),%rsi
sub    $0x1,%rdx
call   1030 <strncpy@plt>
mov    -0x78(%rbp),%rdi
mov    -0x70(%rbp),%rax
sub    $0x1,%rax
movb   $0x0,(%rdi,%rax,1)
call   1080 <gethostbyname@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    12a4 <realhostname+0x124>
movl   $0x3,-0x34(%rbp)
jmp    132c <realhostname+0x1ac>
jmp    12a9 <realhostname+0x129>
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%rax
cmpq   $0x0,(%rax)
jne    12c7 <realhostname+0x147>
movl   $0x1,-0x34(%rbp)
jmp    1327 <realhostname+0x1a7>
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1060 <memcmp@plt>
cmp    $0x0,%eax
jne    130d <realhostname+0x18d>
mov    -0x60(%rbp),%rsi
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
call   1030 <strncpy@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    1333 <realhostname+0x1b3>
jmp    1312 <realhostname+0x192>
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%rcx
add    $0x8,%rcx
mov    %rcx,0x8(%rax)
jmp    12a9 <realhostname+0x129>
jmp    132c <realhostname+0x1ac>
movl   $0x0,-0x54(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,%rsp
mov    -0x54(%rbp),%eax
test   %eax,%eax
jne    1384 <realhostname+0x204>
jmp    134a <realhostname+0x1ca>
jmp    134f <realhostname+0x1cf>
jmp    1354 <realhostname+0x1d4>
mov    -0x10(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    -0x80(%rbp),%rdi
mov    %rax,%rsi
mov    -0x18(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<realhostname>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%r12
mov    %rsi,%r13
mov    %rdi,-0x48(%rbp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%ebx
mov    %rsp,%r15
lea    0xf(%rbx),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    0x2e2c(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
mov    $0x4,%esi
call   10a0 <gethostbyaddr@plt>
movl   $0x2,-0x2c(%rbp)
test   %rax,%rax
je     12b1 <realhostname+0x131>
mov    %rax,%r14
mov    (%rax),%rsi
mov    %r15,%rdi
mov    %ebx,%edx
call   1090 <strlcpy@plt>
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %r15,%rdi
mov    %rax,%rsi
call   1070 <trimdomain@plt>
mov    %r15,%rdi
call   1050 <strlen@plt>
cmp    %r13,%rax
ja     12b1 <realhostname+0x131>
mov    %r13,-0x40(%rbp)
mov    %rsp,-0x38(%rbp)
mov    0x2dc2(%rip),%rax
mov    (%rax),%r13d
mov    %rsp,%rbx
lea    0xf(%r13),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    (%r14),%rsi
lea    -0x1(%r13),%rdx
mov    %rbx,%rdi
call   1030 <strncpy@plt>
movb   $0x0,-0x1(%r13,%rbx,1)
mov    %rbx,%rdi
call   1080 <gethostbyname@plt>
test   %rax,%rax
je     129f <realhostname+0x11f>
mov    %rax,%rbx
mov    0x8(%rax),%rax
mov    (%rax),%rdi
test   %rdi,%rdi
je     1296 <realhostname+0x116>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r12,%rsi
mov    $0x4,%edx
call   1060 <memcmp@plt>
test   %eax,%eax
je     12db <realhostname+0x15b>
mov    0x8(%rbx),%rax
lea    0x8(%rax),%rcx
mov    %rcx,0x8(%rbx)
mov    0x8(%rax),%rdi
test   %rdi,%rdi
jne    1270 <realhostname+0xf0>
movl   $0x1,-0x2c(%rbp)
jmp    12a6 <realhostname+0x126>
movl   $0x3,-0x2c(%rbp)
mov    -0x40(%rbp),%r13
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    (%r12),%edi
call   1040 <inet_ntoa@plt>
mov    -0x48(%rbp),%rdi
mov    %rax,%rsi
mov    %r13,%rdx
call   1030 <strncpy@plt>
mov    -0x2c(%rbp),%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    -0x48(%rbp),%rdi
mov    %r15,%rsi
mov    -0x40(%rbp),%rdx
call   1030 <strncpy@plt>
xor    %eax,%eax
mov    -0x38(%rbp),%rsp
jmp    12cc <realhostname+0x14c>

LLVM IR (-O0):
%struct.in_addr = type { i32 }
%struct.hostent = type { i8*, i32** }
@MAXHOSTNAMELEN = global i32 0
@AF_INET = global i32 0
define i32 @realhostname(i8* %0, i64 %1, %struct.in_addr* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca %struct.in_addr*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca %struct.hostent*
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i32
store i8* %0, i8** %5
store i64 %1, i64* %6
store %struct.in_addr* %2, %struct.in_addr** %7
%15 = load i32, i32* @MAXHOSTNAMELEN
%16 = zext i32 %15 to i64
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %8
%18 = alloca i8, i64 %16
store i64 %16, i64* %9
store i32 2, i32* %10
%19 = load %struct.in_addr*, %struct.in_addr** %7
%20 = bitcast %struct.in_addr* %19 to i8*
%21 = load i32, i32* @AF_INET
%22 = call %struct.hostent* @gethostbyaddr(i8* %20, i32 4, i32 %21)
store %struct.hostent* %22, %struct.hostent** %11
%23 = load %struct.hostent*, %struct.hostent** %11
%24 = icmp ne %struct.hostent* %23, null
br i1 %24, label %25, label %85
25: ; preds = %3
%26 = load %struct.hostent*, %struct.hostent** %11
%27 = getelementptr inbounds %struct.hostent, %struct.hostent* %26, i32 0, i32 0
%28 = load i8*, i8** %27
%29 = trunc i64 %16 to i32
%30 = call i32 @strlcpy(i8* %18, i8* %28, i32 %29)
%31 = call i64 @strlen(i8* %18)
%32 = call i32 @trimdomain(i8* %18, i64 %31)
%33 = call i64 @strlen(i8* %18)
%34 = load i64, i64* %6
%35 = icmp ule i64 %33, %34
br i1 %35, label %36, label %84
36: ; preds = %25
%37 = load i32, i32* @MAXHOSTNAMELEN
%38 = zext i32 %37 to i64
%39 = call i8* @llvm.stacksave()
store i8* %39, i8** %12
%40 = alloca i8, i64 %38
store i64 %38, i64* %13
%41 = load %struct.hostent*, %struct.hostent** %11
%42 = getelementptr inbounds %struct.hostent, %struct.hostent* %41, i32 0, i32 0
%43 = load i8*, i8** %42
%44 = sub i64 %38, 1
%45 = call i32 @strncpy(i8* %40, i8* %43, i64 %44)
%46 = sub i64 %38, 1
%47 = getelementptr inbounds i8, i8* %40, i64 %46
store i8 0, i8* %47
%48 = call %struct.hostent* @gethostbyname(i8* %40)
store %struct.hostent* %48, %struct.hostent** %11
%49 = load %struct.hostent*, %struct.hostent** %11
%50 = icmp eq %struct.hostent* %49, null
br i1 %50, label %51, label %52
51: ; preds = %36
store i32 3, i32* %10
br label %79
52: ; preds = %36
br label %53
53: ; preds = %73, %52
%54 = load %struct.hostent*, %struct.hostent** %11
%55 = getelementptr inbounds %struct.hostent, %struct.hostent* %54, i32 0, i32 1
%56 = load i32**, i32*** %55
%57 = load i32*, i32** %56
%58 = icmp eq i32* %57, null
br i1 %58, label %59, label %60
59: ; preds = %53
store i32 1, i32* %10
br label %78
60: ; preds = %53
%61 = load %struct.hostent*, %struct.hostent** %11
%62 = getelementptr inbounds %struct.hostent, %struct.hostent* %61, i32 0, i32 1
%63 = load i32**, i32*** %62
%64 = load i32*, i32** %63
%65 = load %struct.in_addr*, %struct.in_addr** %7
%66 = call i32 @memcmp(i32* %64, %struct.in_addr* %65, i32 4)
%67 = icmp ne i32 %66, 0
br i1 %67, label %72, label %68
68: ; preds = %60
%69 = load i8*, i8** %5
%70 = load i64, i64* %6
%71 = call i32 @strncpy(i8* %69, i8* %18, i64 %70)
store i32 0, i32* %4
store i32 1, i32* %14
br label %80
72: ; preds = %60
br label %73
73: ; preds = %72
%74 = load %struct.hostent*, %struct.hostent** %11
%75 = getelementptr inbounds %struct.hostent, %struct.hostent* %74, i32 0, i32 1
%76 = load i32**, i32*** %75
%77 = getelementptr inbounds i32*, i32** %76, i32 1
store i32** %77, i32*** %75
br label %53
78: ; preds = %59
br label %79
79: ; preds = %78, %51
store i32 0, i32* %14
br label %80
80: ; preds = %79, %68
%81 = load i8*, i8** %12
%82 = load i32, i32* %14
switch i32 %82, label %94 [
i32 0, label %83
]
83: ; preds = %80
br label %84
84: ; preds = %83, %25
br label %85
85: ; preds = %84, %3
%86 = load i8*, i8** %5
%87 = load %struct.in_addr*, %struct.in_addr** %7
%88 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %87, i32 0, i32 0
%89 = load i32, i32* %88
%90 = call i8* @inet_ntoa(i32 %89)
%91 = load i64, i64* %6
%92 = call i32 @strncpy(i8* %86, i8* %90, i64 %91)
%93 = load i32, i32* %10
store i32 %93, i32* %4
store i32 1, i32* %14
br label %94
94: ; preds = %85, %80
%95 = load i8*, i8** %8
%96 = load i32, i32* %4
ret i32 %96
}

LLVM IR (-O2):
%struct.in_addr = type { i32 }
%struct.hostent = type { i8*, i32** }
@MAXHOSTNAMELEN = global i32 0
@AF_INET = global i32 0
define i32 @realhostname(i8* %0, i64 %1, %struct.in_addr* %2) {
%4 = load i32, i32* @MAXHOSTNAMELEN
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = bitcast %struct.in_addr* %2 to i8*
%8 = load i32, i32* @AF_INET
%9 = tail call %struct.hostent* @gethostbyaddr(i8* %7, i32 4, i32 %8)
%10 = icmp eq %struct.hostent* %9, null
br i1 %10, label %48, label %11
11: ; preds = %3
%12 = getelementptr inbounds %struct.hostent, %struct.hostent* %9, i64 0, i32 0
%13 = load i8*, i8** %12
%14 = call i32 @strlcpy(i8* %6, i8* %13, i32 %4)
%15 = call i64 @strlen(i8* %6)
%16 = call i32 @trimdomain(i8* %6, i64 %15)
%17 = call i64 @strlen(i8* %6)
%18 = icmp ugt i64 %17, %1
br i1 %18, label %48, label %19
19: ; preds = %11
%20 = load i32, i32* @MAXHOSTNAMELEN
%21 = zext i32 %20 to i64
%22 = call i8* @llvm.stacksave()
%23 = alloca i8, i64 %21
%24 = load i8*, i8** %12
%25 = add nsw i64 %21, -1
%26 = call i32 @strncpy(i8* %23, i8* %24, i64 %25)
%27 = getelementptr inbounds i8, i8* %23, i64 %25
store i8 0, i8* %27
%28 = call %struct.hostent* @gethostbyname(i8* %23)
%29 = icmp eq %struct.hostent* %28, null
br i1 %29, label %44, label %30
30: ; preds = %19
%31 = getelementptr inbounds %struct.hostent, %struct.hostent* %28, i64 0, i32 1
%32 = load i32**, i32*** %31
%33 = load i32*, i32** %32
%34 = icmp eq i32* %33, null
br i1 %34, label %44, label %35
35: ; preds = %30, %39
%36 = phi i32* [ %42, %39 ], [ %33, %30 ]
%37 = call i32 @memcmp(i32* %36, %struct.in_addr* %2, i32 4)
%38 = icmp eq i32 %37, 0
br i1 %38, label %46, label %39
39: ; preds = %35
%40 = load i32**, i32*** %31
%41 = getelementptr inbounds i32*, i32** %40, i64 1
store i32** %41, i32*** %31
%42 = load i32*, i32** %41
%43 = icmp eq i32* %42, null
br i1 %43, label %44, label %35
44: ; preds = %39, %30, %19
%45 = phi i32 [ 3, %19 ], [ 1, %30 ], [ 1, %39 ]
br label %48
46: ; preds = %35
%47 = call i32 @strncpy(i8* %0, i8* %6, i64 %1)
br label %54
48: ; preds = %44, %11, %3
%49 = phi i32 [ 2, %11 ], [ 2, %3 ], [ %45, %44 ]
%50 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %2, i64 0, i32 0
%51 = load i32, i32* %50
%52 = call i8* @inet_ntoa(i32 %51)
%53 = call i32 @strncpy(i8* %0, i8* %52, i64 %1)
br label %54
54: ; preds = %46, %48
%55 = phi i32 [ %49, %48 ], [ 0, %46 ]
ret i32 %55
}