#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 {
  int /*<<< orphan*/ **h_addr_list;
  int /*<<< orphan*/ h_name;
};
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 *, int /*<<< orphan*/, 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];
      strlcpy(lookup, hp->h_name, sizeof(lookup));
      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     133c <realhostname+0x1bc>
mov    -0x60(%rbp),%rdi
mov    -0x68(%rbp),%rax
mov    -0x40(%rbp),%rcx
mov    0x8(%rcx),%esi
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     1337 <realhostname+0x1b7>
mov    0x2da7(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x70(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x50(%rbp)
mov    -0x40(%rbp),%rcx
mov    0x8(%rcx),%esi
mov    %eax,%edx
call   1090 <strlcpy@plt>
mov    -0x70(%rbp),%rdi
call   1080 <gethostbyname@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1290 <realhostname+0x110>
movl   $0x3,-0x34(%rbp)
jmp    1314 <realhostname+0x194>
jmp    1295 <realhostname+0x115>
mov    -0x40(%rbp),%rax
mov    (%rax),%rax
cmpq   $0x0,(%rax)
jne    12b2 <realhostname+0x132>
movl   $0x1,-0x34(%rbp)
jmp    130f <realhostname+0x18f>
mov    -0x40(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1060 <memcmp@plt>
cmp    $0x0,%eax
jne    12f7 <realhostname+0x177>
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    131b <realhostname+0x19b>
jmp    12fc <realhostname+0x17c>
mov    -0x40(%rbp),%rax
mov    (%rax),%rcx
add    $0x8,%rcx
mov    %rcx,(%rax)
jmp    1295 <realhostname+0x115>
jmp    1314 <realhostname+0x194>
movl   $0x0,-0x54(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,%rsp
mov    -0x54(%rbp),%eax
test   %eax,%eax
jne    136c <realhostname+0x1ec>
jmp    1332 <realhostname+0x1b2>
jmp    1337 <realhostname+0x1b7>
jmp    133c <realhostname+0x1bc>
mov    -0x10(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    -0x78(%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    $0x18,%rsp
mov    %rdx,%r12
mov    %rsi,%r13
mov    %rdi,-0x40(%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     12a1 <realhostname+0x121>
mov    %rax,%r14
mov    0x8(%rax),%esi
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     12a1 <realhostname+0x121>
mov    %r13,-0x38(%rbp)
mov    %rsp,%r13
mov    0x2dc3(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rbx
mov    %rdx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x8(%r14),%esi
mov    %rbx,%rdi
call   1090 <strlcpy@plt>
mov    %rbx,%rdi
call   1080 <gethostbyname@plt>
test   %rax,%rax
je     1290 <realhostname+0x110>
mov    %rax,%rbx
mov    (%rax),%rax
mov    (%rax),%rdi
test   %rdi,%rdi
je     1284 <realhostname+0x104>
nopw   0x0(%rax,%rax,1)
mov    %r12,%rsi
mov    $0x4,%edx
call   1060 <memcmp@plt>
test   %eax,%eax
je     12cb <realhostname+0x14b>
mov    (%rbx),%rax
lea    0x8(%rax),%rcx
mov    %rcx,(%rbx)
mov    0x8(%rax),%rdi
test   %rdi,%rdi
jne    1260 <realhostname+0xe0>
mov    %r13,%rax
movl   $0x1,-0x2c(%rbp)
jmp    129a <realhostname+0x11a>
mov    %r13,%rax
movl   $0x3,-0x2c(%rbp)
mov    -0x38(%rbp),%r13
mov    %rax,%rsp
mov    (%r12),%edi
call   1040 <inet_ntoa@plt>
mov    -0x40(%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    -0x40(%rbp),%rdi
mov    %r15,%rsi
mov    -0x38(%rbp),%rdx
call   1030 <strncpy@plt>
xor    %eax,%eax
mov    %r13,%rsp
jmp    12bc <realhostname+0x13c>

LLVM IR (-O0):
%struct.in_addr = type { i32 }
%struct.hostent = type { i32**, 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 %83
25: ; preds = %3
%26 = load %struct.hostent*, %struct.hostent** %11
%27 = getelementptr inbounds %struct.hostent, %struct.hostent* %26, i32 0, i32 1
%28 = load i32, i32* %27
%29 = trunc i64 %16 to i32
%30 = call i32 @strlcpy(i8* %18, i32 %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 %82
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 1
%43 = load i32, i32* %42
%44 = trunc i64 %38 to i32
%45 = call i32 @strlcpy(i8* %40, i32 %43, i32 %44)
%46 = call %struct.hostent* @gethostbyname(i8* %40)
store %struct.hostent* %46, %struct.hostent** %11
%47 = load %struct.hostent*, %struct.hostent** %11
%48 = icmp eq %struct.hostent* %47, null
br i1 %48, label %49, label %50
49: ; preds = %36
store i32 3, i32* %10
br label %77
50: ; preds = %36
br label %51
51: ; preds = %71, %50
%52 = load %struct.hostent*, %struct.hostent** %11
%53 = getelementptr inbounds %struct.hostent, %struct.hostent* %52, i32 0, i32 0
%54 = load i32**, i32*** %53
%55 = load i32*, i32** %54
%56 = icmp eq i32* %55, null
br i1 %56, label %57, label %58
57: ; preds = %51
store i32 1, i32* %10
br label %76
58: ; preds = %51
%59 = load %struct.hostent*, %struct.hostent** %11
%60 = getelementptr inbounds %struct.hostent, %struct.hostent* %59, i32 0, i32 0
%61 = load i32**, i32*** %60
%62 = load i32*, i32** %61
%63 = load %struct.in_addr*, %struct.in_addr** %7
%64 = call i32 @memcmp(i32* %62, %struct.in_addr* %63, i32 4)
%65 = icmp ne i32 %64, 0
br i1 %65, label %70, label %66
66: ; preds = %58
%67 = load i8*, i8** %5
%68 = load i64, i64* %6
%69 = call i32 @strncpy(i8* %67, i8* %18, i64 %68)
store i32 0, i32* %4
store i32 1, i32* %14
br label %78
70: ; preds = %58
br label %71
71: ; preds = %70
%72 = load %struct.hostent*, %struct.hostent** %11
%73 = getelementptr inbounds %struct.hostent, %struct.hostent* %72, i32 0, i32 0
%74 = load i32**, i32*** %73
%75 = getelementptr inbounds i32*, i32** %74, i32 1
store i32** %75, i32*** %73
br label %51
76: ; preds = %57
br label %77
77: ; preds = %76, %49
store i32 0, i32* %14
br label %78
78: ; preds = %77, %66
%79 = load i8*, i8** %12
%80 = load i32, i32* %14
switch i32 %80, label %92 [
i32 0, label %81
]
81: ; preds = %78
br label %82
82: ; preds = %81, %25
br label %83
83: ; preds = %82, %3
%84 = load i8*, i8** %5
%85 = load %struct.in_addr*, %struct.in_addr** %7
%86 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %85, i32 0, i32 0
%87 = load i32, i32* %86
%88 = call i8* @inet_ntoa(i32 %87)
%89 = load i64, i64* %6
%90 = call i32 @strncpy(i8* %84, i8* %88, i64 %89)
%91 = load i32, i32* %10
store i32 %91, i32* %4
store i32 1, i32* %14
br label %92
92: ; preds = %83, %78
%93 = load i8*, i8** %8
%94 = load i32, i32* %4
ret i32 %94
}

LLVM IR (-O2):
%struct.in_addr = type { i32 }
%struct.hostent = type { i32**, 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 %46, label %11
11: ; preds = %3
%12 = getelementptr inbounds %struct.hostent, %struct.hostent* %9, i64 0, i32 1
%13 = load i32, i32* %12
%14 = call i32 @strlcpy(i8* %6, i32 %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 %46, 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 i32, i32* %12
%25 = call i32 @strlcpy(i8* %23, i32 %24, i32 %20)
%26 = call %struct.hostent* @gethostbyname(i8* %23)
%27 = icmp eq %struct.hostent* %26, null
br i1 %27, label %42, label %28
28: ; preds = %19
%29 = getelementptr inbounds %struct.hostent, %struct.hostent* %26, i64 0, i32 0
%30 = load i32**, i32*** %29
%31 = load i32*, i32** %30
%32 = icmp eq i32* %31, null
br i1 %32, label %42, label %33
33: ; preds = %28, %37
%34 = phi i32* [ %40, %37 ], [ %31, %28 ]
%35 = call i32 @memcmp(i32* %34, %struct.in_addr* %2, i32 4)
%36 = icmp eq i32 %35, 0
br i1 %36, label %44, label %37
37: ; preds = %33
%38 = load i32**, i32*** %29
%39 = getelementptr inbounds i32*, i32** %38, i64 1
store i32** %39, i32*** %29
%40 = load i32*, i32** %39
%41 = icmp eq i32* %40, null
br i1 %41, label %42, label %33
42: ; preds = %37, %28, %19
%43 = phi i32 [ 3, %19 ], [ 1, %28 ], [ 1, %37 ]
br label %46
44: ; preds = %33
%45 = call i32 @strncpy(i8* %0, i8* %6, i64 %1)
br label %52
46: ; preds = %42, %11, %3
%47 = phi i32 [ 2, %11 ], [ 2, %3 ], [ %43, %42 ]
%48 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %2, i64 0, i32 0
%49 = load i32, i32* %48
%50 = call i8* @inet_ntoa(i32 %49)
%51 = call i32 @strncpy(i8* %0, i8* %50, i64 %1)
br label %52
52: ; preds = %44, %46
%53 = phi i32 [ %47, %46 ], [ 0, %44 ]
ret i32 %53
}