#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 {
  scalar_t__ *h_addr_list;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INET_ADDRSTRLEN;
struct hostent *gethostbyname(char *);
scalar_t__ inet_ntop(int /*<<< orphan*/, scalar_t__, char *,
                     int /*<<< orphan*/);
scalar_t__ malloc(int /*<<< orphan*/);
char *getip(char *hostname) {
  char *ip;
  struct hostent *host;
  if ((host = gethostbyname(hostname)) == 0) {
    return 0;
  }
  if (host->h_addr_list[0] != 0) {
    ip = (char *)malloc(INET_ADDRSTRLEN);
    if (inet_ntop(AF_INET, host->h_addr_list[0], ip, INET_ADDRSTRLEN) != 0) {
      return ip;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<getip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1160 <getip+0x30>
movq   $0x0,-0x8(%rbp)
jmp    11cc <getip+0x9c>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
cmpq   $0x0,(%rax)
je     11c4 <getip+0x94>
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rsi
mov    -0x18(%rbp),%rdx
mov    0x2e37(%rip),%rax
mov    (%rax),%ecx
call   1030 <inet_ntop@plt>
cmp    $0x0,%rax
je     11bf <getip+0x8f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11cc <getip+0x9c>
jmp    11c4 <getip+0x94>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getip>:
push   %r15
push   %r14
push   %rbx
call   1040 <gethostbyname@plt>
test   %rax,%rax
je     117c <getip+0x4c>
mov    %rax,%rbx
mov    (%rax),%rax
cmpq   $0x0,(%rax)
je     117c <getip+0x4c>
mov    0x2e86(%rip),%r15
mov    (%r15),%edi
call   1050 <malloc@plt>
mov    %rax,%r14
mov    0x2e84(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rax
mov    (%rax),%rsi
mov    (%r15),%ecx
mov    %r14,%rdx
call   1030 <inet_ntop@plt>
test   %rax,%rax
jne    117f <getip+0x4f>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.hostent = type { i64* }
@INET_ADDRSTRLEN = global i32 0
@AF_INET = global i32 0
define i8* @getip(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.hostent*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call %struct.hostent* @gethostbyname(i8* %6)
store %struct.hostent* %7, %struct.hostent** %5
%8 = icmp eq %struct.hostent* %7, null
br i1 %8, label %9, label %10
9: ; preds = %1
store i8* null, i8** %2
br label %35
10: ; preds = %1
%11 = load %struct.hostent*, %struct.hostent** %5
%12 = getelementptr inbounds %struct.hostent, %struct.hostent* %11, i32 0, i32 0
%13 = load i64*, i64** %12
%14 = getelementptr inbounds i64, i64* %13, i64 0
%15 = load i64, i64* %14
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %34
17: ; preds = %10
%18 = load i32, i32* @INET_ADDRSTRLEN
%19 = call i64 @malloc(i32 %18)
%20 = inttoptr i64 %19 to i8*
store i8* %20, i8** %4
%21 = load i32, i32* @AF_INET
%22 = load %struct.hostent*, %struct.hostent** %5
%23 = getelementptr inbounds %struct.hostent, %struct.hostent* %22, i32 0, i32 0
%24 = load i64*, i64** %23
%25 = getelementptr inbounds i64, i64* %24, i64 0
%26 = load i64, i64* %25
%27 = load i8*, i8** %4
%28 = load i32, i32* @INET_ADDRSTRLEN
%29 = call i64 @inet_ntop(i32 %21, i64 %26, i8* %27, i32 %28)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %17
%32 = load i8*, i8** %4
store i8* %32, i8** %2
br label %35
33: ; preds = %17
br label %34
34: ; preds = %33, %10
store i8* null, i8** %2
br label %35
35: ; preds = %34, %31, %9
%36 = load i8*, i8** %2
ret i8* %36
}

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