#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 {
  int /*<<< orphan*/ **h_addr_list;
};

/* Variables and functions */
struct hostent *gethostbyname(char *);
scalar_t__ inet_ntoa(struct in_addr);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ strcpy(char *, char *);
char *dhsocket_resolvename(char *hostnm) {
  char *ip;
  int cnt;
  int rnd;
  struct hostent *host;
  char *s;
  host = gethostbyname(hostnm);
  if (host == NULL) {
    return (NULL);
  }
  ip = (char *)malloc((size_t)1024 + 1);
  if (ip == NULL) {
    return (NULL);
  }
  for (cnt = 0; host->h_addr_list[cnt] != NULL; cnt++)
    ;
  rnd = 0;
  (void)strcpy(ip,
               (char *)inet_ntoa(*(struct in_addr *)host->h_addr_list[rnd]));
  return (ip);
}


x86 assembly (clang, -O0):
<dhsocket_resolvename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1171 <dhsocket_resolvename+0x31>
movq   $0x0,-0x8(%rbp)
jmp    1200 <dhsocket_resolvename+0xc0>
mov    $0x401,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1197 <dhsocket_resolvename+0x57>
movq   $0x0,-0x8(%rbp)
jmp    1200 <dhsocket_resolvename+0xc0>
movl   $0x0,-0x1c(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
movslq -0x1c(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     11c7 <dhsocket_resolvename+0x87>
jmp    11b9 <dhsocket_resolvename+0x79>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    119e <dhsocket_resolvename+0x5e>
movl   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    -0x38(%rbp),%rdi
mov    %rax,%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dhsocket_resolvename>:
push   %r14
push   %rbx
push   %rax
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     117d <dhsocket_resolvename+0x3d>
mov    %rax,%rbx
mov    $0x401,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     117d <dhsocket_resolvename+0x3d>
mov    %rax,%r14
mov    (%rbx),%rax
mov    (%rax),%rax
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    %r14,%rdi
mov    %rax,%rsi
call   1030 <strcpy@plt>
jmp    1180 <dhsocket_resolvename+0x40>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.hostent = type { i32** }
%struct.in_addr = type { i32 }
define i8* @dhsocket_resolvename(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.hostent*
%8 = alloca i8*
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = call %struct.hostent* @gethostbyname(i8* %9)
store %struct.hostent* %10, %struct.hostent** %7
%11 = load %struct.hostent*, %struct.hostent** %7
%12 = icmp eq %struct.hostent* %11, null
br i1 %12, label %13, label %14
13: ; preds = %1
store i8* null, i8** %2
br label %50
14: ; preds = %1
%15 = call i64 @malloc(i64 1025)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %4
%17 = load i8*, i8** %4
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %20
19: ; preds = %14
store i8* null, i8** %2
br label %50
20: ; preds = %14
store i32 0, i32* %5
br label %21
21: ; preds = %31, %20
%22 = load %struct.hostent*, %struct.hostent** %7
%23 = getelementptr inbounds %struct.hostent, %struct.hostent* %22, i32 0, i32 0
%24 = load i32**, i32*** %23
%25 = load i32, i32* %5
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i32*, i32** %24, i64 %26
%28 = load i32*, i32** %27
%29 = icmp ne i32* %28, null
br i1 %29, label %30, label %34
30: ; preds = %21
br label %31
31: ; preds = %30
%32 = load i32, i32* %5
%33 = add nsw i32 %32, 1
store i32 %33, i32* %5
br label %21
34: ; preds = %21
store i32 0, i32* %6
%35 = load i8*, i8** %4
%36 = load %struct.hostent*, %struct.hostent** %7
%37 = getelementptr inbounds %struct.hostent, %struct.hostent* %36, i32 0, i32 0
%38 = load i32**, i32*** %37
%39 = load i32, i32* %6
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i32*, i32** %38, i64 %40
%42 = load i32*, i32** %41
%43 = bitcast i32* %42 to %struct.in_addr*
%44 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %43, i32 0, i32 0
%45 = load i32, i32* %44
%46 = call i64 @inet_ntoa(i32 %45)
%47 = inttoptr i64 %46 to i8*
%48 = call i32 @strcpy(i8* %35, i8* %47)
%49 = load i8*, i8** %4
store i8* %49, i8** %2
br label %50
50: ; preds = %34, %19, %13
%51 = load i8*, i8** %2
ret i8* %51
}

LLVM IR (-O2):
%struct.hostent = type { i32** }
%struct.in_addr = type { i32 }
define i8* @dhsocket_resolvename(i8* %0) {
%2 = tail call %struct.hostent* @gethostbyname(i8* %0)
%3 = icmp eq %struct.hostent* %2, null
br i1 %3, label %17, label %4
4: ; preds = %1
%5 = tail call i64 @malloc(i64 1025)
%6 = inttoptr i64 %5 to i8*
%7 = icmp eq i64 %5, 0
br i1 %7, label %17, label %8
8: ; preds = %4
%9 = bitcast %struct.hostent* %2 to %struct.in_addr***
%10 = load %struct.in_addr**, %struct.in_addr*** %9
%11 = load %struct.in_addr*, %struct.in_addr** %10
%12 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %11, i64 0, i32 0
%13 = load i32, i32* %12
%14 = tail call i64 @inet_ntoa(i32 %13)
%15 = inttoptr i64 %14 to i8*
%16 = tail call i32 @strcpy(i8* %6, i8* %15)
br label %17
17: ; preds = %4, %1, %8
%18 = phi i8* [ %6, %8 ], [ null, %1 ], [ null, %4 ]
ret i8* %18
}