#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 {
  char *h_name;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int UT_HOSTSIZE;
struct hostent *gethostbyaddr(char *, int, int /*<<< orphan*/);
char *inet_ntoa(struct in_addr);
int /*<<< orphan*/ strncpy(char *, char *, int);
int dns_lookup(char *result, int size, char *org, unsigned int ip) {
  struct hostent *h;
  if (ip == 0 || (int)ip == -1 || (ip >> 24) == 0 || (ip & 255) == 0) {
    if (size > UT_HOSTSIZE)
      size = UT_HOSTSIZE + 1;
    strncpy(result, org, size - 1);
    result[size - 1] = 0;
    return 0;
  }
  if ((h = gethostbyaddr((char *)&ip, 4, AF_INET)) == NULL) {
    strncpy(result, inet_ntoa(*(struct in_addr *)&ip), size);
    result[size - 1] = 0;
    return 0;
  }
  strncpy(result, h->h_name, size - 1);
  result[size - 1] = 0;
  return 0;
}


x86 assembly (clang, -O0):
<dns_lookup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     117a <dns_lookup+0x4a>
cmpl   $0xffffffff,-0x24(%rbp)
je     117a <dns_lookup+0x4a>
mov    -0x24(%rbp),%eax
shr    $0x18,%eax
cmp    $0x0,%eax
je     117a <dns_lookup+0x4a>
mov    -0x24(%rbp),%eax
and    $0xff,%eax
cmp    $0x0,%eax
jne    11cb <dns_lookup+0x9b>
mov    -0x14(%rbp),%eax
mov    0x2e54(%rip),%rcx
cmp    (%rcx),%eax
jle    119b <dns_lookup+0x6b>
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
jmp    125a <dns_lookup+0x12a>
lea    -0x24(%rbp),%rdi
mov    0x2e12(%rip),%rax
mov    (%rax),%edx
mov    $0x4,%esi
call   1050 <gethostbyaddr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    122c <dns_lookup+0xfc>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x24(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    -0x38(%rbp),%rdi
mov    %rax,%rsi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
jmp    125a <dns_lookup+0x12a>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%rsi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dns_lookup>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebx
mov    %rdi,%r14
mov    %ecx,0xc(%rsp)
lea    0x1(%rcx),%eax
cmp    $0x1000001,%eax
jb     1181 <dns_lookup+0x51>
movzbl %cl,%eax
test   %eax,%eax
je     1181 <dns_lookup+0x51>
mov    0x2e8f(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rdi
mov    $0x4,%esi
call   1050 <gethostbyaddr@plt>
test   %rax,%rax
je     11a4 <dns_lookup+0x74>
mov    (%rax),%rsi
add    $0xffffffff,%ebx
mov    %r14,%rdi
mov    %ebx,%edx
call   1030 <strncpy@plt>
jmp    11bd <dns_lookup+0x8d>
mov    0x2e50(%rip),%rax
mov    (%rax),%eax
lea    0x1(%rax),%ebp
cmp    %ebx,%eax
cmovge %ebx,%ebp
add    $0xffffffff,%ebp
mov    %r14,%rdi
mov    %rdx,%rsi
mov    %ebp,%edx
call   1030 <strncpy@plt>
jmp    11bf <dns_lookup+0x8f>
mov    0xc(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %r14,%rdi
mov    %rax,%rsi
mov    %ebx,%edx
call   1030 <strncpy@plt>
add    $0xffffffff,%ebx
mov    %ebx,%ebp
movslq %ebp,%rax
movb   $0x0,(%r14,%rax,1)
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.hostent = type { i8* }
%struct.in_addr = type { i32 }
@UT_HOSTSIZE = global i32 0
@AF_INET = global i32 0
define i32 @dns_lookup(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca %struct.hostent*
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%11 = load i32, i32* %9
%12 = icmp eq i32 %11, 0
br i1 %12, label %24, label %13
13: ; preds = %4
%14 = load i32, i32* %9
%15 = icmp eq i32 %14, -1
br i1 %15, label %24, label %16
16: ; preds = %13
%17 = load i32, i32* %9
%18 = lshr i32 %17, 24
%19 = icmp eq i32 %18, 0
br i1 %19, label %24, label %20
20: ; preds = %16
%21 = load i32, i32* %9
%22 = and i32 %21, 255
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %42
24: ; preds = %20, %16, %13, %4
%25 = load i32, i32* %7
%26 = load i32, i32* @UT_HOSTSIZE
%27 = icmp sgt i32 %25, %26
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = load i32, i32* @UT_HOSTSIZE
%30 = add nsw i32 %29, 1
store i32 %30, i32* %7
br label %31
31: ; preds = %28, %24
%32 = load i8*, i8** %6
%33 = load i8*, i8** %8
%34 = load i32, i32* %7
%35 = sub nsw i32 %34, 1
%36 = call i32 @strncpy(i8* %32, i8* %33, i32 %35)
%37 = load i8*, i8** %6
%38 = load i32, i32* %7
%39 = sub nsw i32 %38, 1
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %37, i64 %40
store i8 0, i8* %41
store i32 0, i32* %5
br label %73
42: ; preds = %20
%43 = bitcast i32* %9 to i8*
%44 = load i32, i32* @AF_INET
%45 = call %struct.hostent* @gethostbyaddr(i8* %43, i32 4, i32 %44)
store %struct.hostent* %45, %struct.hostent** %10
%46 = icmp eq %struct.hostent* %45, null
br i1 %46, label %47, label %60
47: ; preds = %42
%48 = load i8*, i8** %6
%49 = bitcast i32* %9 to %struct.in_addr*
%50 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %49, i32 0, i32 0
%51 = load i32, i32* %50
%52 = call i8* @inet_ntoa(i32 %51)
%53 = load i32, i32* %7
%54 = call i32 @strncpy(i8* %48, i8* %52, i32 %53)
%55 = load i8*, i8** %6
%56 = load i32, i32* %7
%57 = sub nsw i32 %56, 1
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %55, i64 %58
store i8 0, i8* %59
store i32 0, i32* %5
br label %73
60: ; preds = %42
%61 = load i8*, i8** %6
%62 = load %struct.hostent*, %struct.hostent** %10
%63 = getelementptr inbounds %struct.hostent, %struct.hostent* %62, i32 0, i32 0
%64 = load i8*, i8** %63
%65 = load i32, i32* %7
%66 = sub nsw i32 %65, 1
%67 = call i32 @strncpy(i8* %61, i8* %64, i32 %66)
%68 = load i8*, i8** %6
%69 = load i32, i32* %7
%70 = sub nsw i32 %69, 1
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds i8, i8* %68, i64 %71
store i8 0, i8* %72
store i32 0, i32* %5
br label %73
73: ; preds = %60, %47, %31
%74 = load i32, i32* %5
ret i32 %74
}

LLVM IR (-O2):
%struct.hostent = type { i8* }
@UT_HOSTSIZE = global i32 0
@AF_INET = global i32 0
define i32 @dns_lookup(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
store i32 %3, i32* %5
%6 = add i32 %3, 1
%7 = icmp ult i32 %6, 16777217
%8 = and i32 %3, 255
%9 = icmp eq i32 %8, 0
%10 = or i1 %7, %9
br i1 %10, label %11, label %18
11: ; preds = %4
%12 = load i32, i32* @UT_HOSTSIZE
%13 = icmp slt i32 %12, %1
%14 = add nsw i32 %12, 1
%15 = select i1 %13, i32 %14, i32 %1
%16 = add nsw i32 %15, -1
%17 = tail call i32 @strncpy(i8* %0, i8* %2, i32 %16)
br label %33
18: ; preds = %4
%19 = bitcast i32* %5 to i8*
%20 = load i32, i32* @AF_INET
%21 = call %struct.hostent* @gethostbyaddr(i8* %19, i32 4, i32 %20)
%22 = icmp eq %struct.hostent* %21, null
br i1 %22, label %23, label %28
23: ; preds = %18
%24 = load i32, i32* %5
%25 = call i8* @inet_ntoa(i32 %24)
%26 = call i32 @strncpy(i8* %0, i8* %25, i32 %1)
%27 = add nsw i32 %1, -1
br label %33
28: ; preds = %18
%29 = getelementptr inbounds %struct.hostent, %struct.hostent* %21, i64 0, i32 0
%30 = load i8*, i8** %29
%31 = add nsw i32 %1, -1
%32 = call i32 @strncpy(i8* %0, i8* %30, i32 %31)
br label %33
33: ; preds = %28, %23, %11
%34 = phi i32 [ %31, %28 ], [ %27, %23 ], [ %16, %11 ]
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %0, i64 %35
store i8 0, i8* %36
ret i32 0
}