#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 hostent {
  int /*<<< orphan*/ h_name;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
struct hostent *gethostbyaddr(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, unsigned int, unsigned int,
                           unsigned int, unsigned int);
int /*<<< orphan*/ strncpy(char *, int /*<<< orphan*/, int);
char *hostname_from_ip(long unsigned ip) {
  struct hostent *hp;
  long unsigned addr = ip;
  unsigned char *p;
  static char s[121 + 1];
  hp = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET);
  if (hp == NULL) {
    p = (unsigned char *)&addr;
    sprintf(s, "%u.%u.%u.%u", (unsigned int)p[0], (unsigned int)p[1],
            (unsigned int)p[2], (unsigned int)p[3]);
    return s;
  }
  strncpy(s, hp->h_name, 121);
  s[121] = '\0';
  return s;
}


x86 assembly (clang, -O0):
<hostname_from_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
lea    -0x20(%rbp),%rdi
mov    0x2e99(%rip),%rax
mov    (%rax),%edx
mov    $0x8,%esi
call   1040 <gethostbyaddr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b6 <hostname_from_ip+0x86>
lea    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movzbl (%rax),%edx
mov    -0x28(%rbp),%rax
movzbl 0x1(%rax),%ecx
mov    -0x28(%rbp),%rax
movzbl 0x2(%rax),%r8d
mov    -0x28(%rbp),%rax
movzbl 0x3(%rax),%r9d
lea    0x2eb6(%rip),%rdi
lea    0xe5f(%rip),%rsi
call   1050 <sprintf@plt>
lea    0x2ea3(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    11df <hostname_from_ip+0xaf>
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
lea    0x2e8d(%rip),%rdi
mov    $0x79,%edx
call   1030 <strncpy@plt>
movb   $0x0,0x2ef5(%rip)
lea    0x2e75(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hostname_from_ip>:
push   %rax
mov    %rdi,(%rsp)
mov    0x2eac(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rdi
mov    $0x8,%esi
call   1040 <gethostbyaddr@plt>
test   %rax,%rax
je     116c <hostname_from_ip+0x3c>
mov    (%rax),%esi
lea    0x2ef7(%rip),%rdi
mov    $0x79,%edx
call   1030 <strncpy@plt>
movb   $0x0,0x2f5f(%rip)
jmp    1194 <hostname_from_ip+0x64>
movzbl (%rsp),%edx
movzbl 0x1(%rsp),%ecx
movzbl 0x2(%rsp),%r8d
movzbl 0x3(%rsp),%r9d
lea    0x2ec8(%rip),%rdi
lea    0xe71(%rip),%rsi
call   1050 <sprintf@plt>
lea    0x2eb5(%rip),%rax
pop    %rcx
ret

LLVM IR (-O0):
%struct.hostent = type { i32 }
@hostname_from_ip.s = internal global [122 x i8] zeroinitializer
@AF_INET = global i32 0
@.str = [12 x i8] c"%u.%u.%u.%u\00"
define i8* @hostname_from_ip(i64 %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca %struct.hostent*
%5 = alloca i64
%6 = alloca i8*
store i64 %0, i64* %3
%7 = load i64, i64* %3
store i64 %7, i64* %5
%8 = bitcast i64* %5 to i8*
%9 = load i32, i32* @AF_INET
%10 = call %struct.hostent* @gethostbyaddr(i8* %8, i32 8, i32 %9)
store %struct.hostent* %10, %struct.hostent** %4
%11 = load %struct.hostent*, %struct.hostent** %4
%12 = icmp eq %struct.hostent* %11, null
br i1 %12, label %13, label %32
13: ; preds = %1
%14 = bitcast i64* %5 to i8*
store i8* %14, i8** %6
%15 = load i8*, i8** %6
%16 = getelementptr inbounds i8, i8* %15, i64 0
%17 = load i8, i8* %16
%18 = zext i8 %17 to i32
%19 = load i8*, i8** %6
%20 = getelementptr inbounds i8, i8* %19, i64 1
%21 = load i8, i8* %20
%22 = zext i8 %21 to i32
%23 = load i8*, i8** %6
%24 = getelementptr inbounds i8, i8* %23, i64 2
%25 = load i8, i8* %24
%26 = zext i8 %25 to i32
%27 = load i8*, i8** %6
%28 = getelementptr inbounds i8, i8* %27, i64 3
%29 = load i8, i8* %28
%30 = zext i8 %29 to i32
%31 = call i32 @sprintf(i8* getelementptr inbounds ([122 x i8], [122 x i8]* @hostname_from_ip.s, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %18, i32 %22, i32 %26, i32 %30)
store i8* getelementptr inbounds ([122 x i8], [122 x i8]* @hostname_from_ip.s, i64 0, i64 0), i8** %2
br label %37
32: ; preds = %1
%33 = load %struct.hostent*, %struct.hostent** %4
%34 = getelementptr inbounds %struct.hostent, %struct.hostent* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @strncpy(i8* getelementptr inbounds ([122 x i8], [122 x i8]* @hostname_from_ip.s, i64 0, i64 0), i32 %35, i32 121)
store i8 0, i8* getelementptr inbounds ([122 x i8], [122 x i8]* @hostname_from_ip.s, i64 0, i64 121)
store i8* getelementptr inbounds ([122 x i8], [122 x i8]* @hostname_from_ip.s, i64 0, i64 0), i8** %2
br label %37
37: ; preds = %32, %13
%38 = load i8*, i8** %2
ret i8* %38
}

LLVM IR (-O2):
%struct.hostent = type { i32 }
@hostname_from_ip.s = internal global [122 x i8] zeroinitializer
@AF_INET = global i32 0
@.str = [12 x i8] c"%u.%u.%u.%u\00"
define i8* @hostname_from_ip(i64 %0) {
%2 = alloca i64
%3 = bitcast i64* %2 to i8*
store i64 %0, i64* %2
%4 = load i32, i32* @AF_INET
%5 = call %struct.hostent* @gethostbyaddr(i8* %3, i32 8, i32 %4)
%6 = icmp eq %struct.hostent* %5, null
br i1 %6, label %7, label %20
7: ; preds = %1
%8 = load i8, i8* %3
%9 = zext i8 %8 to i32
%10 = getelementptr inbounds i8, i8* %3, i64 1
%11 = load i8, i8* %10
%12 = zext i8 %11 to i32
%13 = getelementptr inbounds i8, i8* %3, i64 2
%14 = load i8, i8* %13
%15 = zext i8 %14 to i32
%16 = getelementptr inbounds i8, i8* %3, i64 3
%17 = load i8, i8* %16
%18 = zext i8 %17 to i32
%19 = call i32 @sprintf(i8* dereferenceable(1) getelementptr inbounds ([122 x i8], [122 x i8]* @hostname_from_ip.s, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %9, i32 %12, i32 %15, i32 %18)
br label %24
20: ; preds = %1
%21 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 @strncpy(i8* getelementptr inbounds ([122 x i8], [122 x i8]* @hostname_from_ip.s, i64 0, i64 0), i32 %22, i32 121)
store i8 0, i8* getelementptr inbounds ([122 x i8], [122 x i8]* @hostname_from_ip.s, i64 0, i64 121)
br label %24
24: ; preds = %20, %7
ret i8* getelementptr inbounds ([122 x i8], [122 x i8]* @hostname_from_ip.s, i64 0, i64 0)
}