#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 h_addrtype;
  char **h_addr_list;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
#define AF_INET 129
#define AF_INET6 128
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ inet_ntop(int, char *, char *, int);
char *strdup(char *);
char *trans_host_to_ip(const char *host) {
  struct hostent *he;
  char **addr_list, addr[32], *ip = NULL;
  if ((he = gethostbyname(host)) == NULL)
    return NULL;
  switch (he->h_addrtype) {
  case AF_INET:
  case AF_INET6:
    addr_list = he->h_addr_list;
    for (; *addr_list; addr_list++) {
      if (inet_ntop(he->h_addrtype, *addr_list, addr, sizeof(addr))) {
        ip = strdup(addr);
        break;
      }
    }
  }
  return ip;
}


x86 assembly (clang, -O0):
<trans_host_to_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1168 <trans_host_to_ip+0x38>
movq   $0x0,-0x8(%rbp)
jmp    11f2 <trans_host_to_ip+0xc2>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
add    $0xffffff80,%eax
sub    $0x1,%eax
ja     11ea <trans_host_to_ip+0xba>
jmp    117f <trans_host_to_ip+0x4f>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
je     11e5 <trans_host_to_ip+0xb5>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
lea    -0x40(%rbp),%rdx
mov    $0x20,%ecx
call   1030 <inet_ntop@plt>
cmp    $0x0,%eax
je     11cf <trans_host_to_ip+0x9f>
lea    -0x40(%rbp),%rdi
call   1050 <strdup@plt>
mov    %rax,-0x48(%rbp)
jmp    11e5 <trans_host_to_ip+0xb5>
jmp    11d4 <trans_host_to_ip+0xa4>
mov    -0x20(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x20(%rbp)
jmp    118b <trans_host_to_ip+0x5b>
jmp    11ea <trans_host_to_ip+0xba>
mov    -0x48(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trans_host_to_ip>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x28,%rsp
call   1040 <gethostbyname@plt>
test   %rax,%rax
je     11ae <trans_host_to_ip+0x7e>
mov    %rax,%r12
mov    (%rax),%edi
mov    %edi,%eax
and    $0xfffffffe,%eax
cmp    $0x80,%eax
jne    11ae <trans_host_to_ip+0x7e>
mov    0x8(%r12),%rbx
mov    (%rbx),%rsi
test   %rsi,%rsi
je     11ae <trans_host_to_ip+0x7e>
mov    %rsp,%rdx
mov    $0x20,%ecx
call   1030 <inet_ntop@plt>
test   %eax,%eax
jne    11a1 <trans_host_to_ip+0x71>
add    $0x8,%rbx
xor    %r14d,%r14d
mov    %rsp,%r15
xchg   %ax,%ax
mov    (%rbx),%rsi
test   %rsi,%rsi
je     11b1 <trans_host_to_ip+0x81>
mov    (%r12),%edi
mov    %r15,%rdx
mov    $0x20,%ecx
call   1030 <inet_ntop@plt>
add    $0x8,%rbx
test   %eax,%eax
je     1180 <trans_host_to_ip+0x50>
mov    %rsp,%rdi
call   1050 <strdup@plt>
mov    %rax,%r14
jmp    11b1 <trans_host_to_ip+0x81>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.hostent = type { i32, i8** }
define i8* @trans_host_to_ip(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.hostent*
%5 = alloca i8**
%6 = alloca [32 x i8]
%7 = alloca i8*
store i8* %0, i8** %3
store i8* null, i8** %7
%8 = load i8*, i8** %3
%9 = call %struct.hostent* @gethostbyname(i8* %8)
store %struct.hostent* %9, %struct.hostent** %4
%10 = icmp eq %struct.hostent* %9, null
br i1 %10, label %11, label %12
11: ; preds = %1
store i8* null, i8** %2
br label %43
12: ; preds = %1
%13 = load %struct.hostent*, %struct.hostent** %4
%14 = getelementptr inbounds %struct.hostent, %struct.hostent* %13, i32 0, i32 0
%15 = load i32, i32* %14
switch i32 %15, label %41 [
i32 129, label %16
i32 128, label %16
]
16: ; preds = %12, %12
%17 = load %struct.hostent*, %struct.hostent** %4
%18 = getelementptr inbounds %struct.hostent, %struct.hostent* %17, i32 0, i32 1
%19 = load i8**, i8*** %18
store i8** %19, i8*** %5
br label %20
20: ; preds = %37, %16
%21 = load i8**, i8*** %5
%22 = load i8*, i8** %21
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %40
24: ; preds = %20
%25 = load %struct.hostent*, %struct.hostent** %4
%26 = getelementptr inbounds %struct.hostent, %struct.hostent* %25, i32 0, i32 0
%27 = load i32, i32* %26
%28 = load i8**, i8*** %5
%29 = load i8*, i8** %28
%30 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%31 = call i32 @inet_ntop(i32 %27, i8* %29, i8* %30, i32 32)
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %24
%34 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%35 = call i8* @strdup(i8* %34)
store i8* %35, i8** %7
br label %40
36: ; preds = %24
br label %37
37: ; preds = %36
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i32 1
store i8** %39, i8*** %5
br label %20
40: ; preds = %33, %20
br label %41
41: ; preds = %40, %12
%42 = load i8*, i8** %7
store i8* %42, i8** %2
br label %43
43: ; preds = %41, %11
%44 = load i8*, i8** %2
ret i8* %44
}

LLVM IR (-O2):
%struct.hostent = type { i32, i8** }
define noalias i8* @trans_host_to_ip(i8* %0) {
%2 = alloca [32 x i8]
%3 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%4 = tail call %struct.hostent* @gethostbyname(i8* %0)
%5 = icmp eq %struct.hostent* %4, null
br i1 %5, label %30, label %6
6: ; preds = %1
%7 = getelementptr inbounds %struct.hostent, %struct.hostent* %4, i64 0, i32 0
%8 = load i32, i32* %7
%9 = and i32 %8, -2
%10 = icmp eq i32 %9, 128
br i1 %10, label %11, label %30
11: ; preds = %6
%12 = getelementptr inbounds %struct.hostent, %struct.hostent* %4, i64 0, i32 1
%13 = load i8**, i8*** %12
%14 = load i8*, i8** %13
%15 = icmp eq i8* %14, null
br i1 %15, label %30, label %16
16: ; preds = %11
%17 = call i32 @inet_ntop(i32 %8, i8* %14, i8* %3, i32 32)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %28
19: ; preds = %16, %24
%20 = phi i8** [ %21, %24 ], [ %13, %16 ]
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = icmp eq i8* %22, null
br i1 %23, label %30, label %24
24: ; preds = %19
%25 = load i32, i32* %7
%26 = call i32 @inet_ntop(i32 %25, i8* %22, i8* %3, i32 32)
%27 = icmp eq i32 %26, 0
br i1 %27, label %19, label %28
28: ; preds = %24, %16
%29 = call i8* @strdup(i8* %3)
br label %30
30: ; preds = %19, %11, %28, %6, %1
%31 = phi i8* [ null, %1 ], [ null, %6 ], [ %29, %28 ], [ null, %11 ], [ null, %19 ]
ret i8* %31
}