#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 (hostname == 0) {
    return 0;
  }
  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)
cmpq   $0x0,-0x10(%rbp)
jne    1154 <getip+0x24>
movq   $0x0,-0x8(%rbp)
jmp    11e4 <getip+0xb4>
mov    -0x10(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1178 <getip+0x48>
movq   $0x0,-0x8(%rbp)
jmp    11e4 <getip+0xb4>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
cmpq   $0x0,(%rax)
je     11dc <getip+0xac>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rsi
mov    -0x18(%rbp),%rdx
mov    0x2e1f(%rip),%rax
mov    (%rax),%ecx
call   1030 <inet_ntop@plt>
cmp    $0x0,%rax
je     11d7 <getip+0xa7>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11e4 <getip+0xb4>
jmp    11dc <getip+0xac>
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
test   %rdi,%rdi
je     1181 <getip+0x51>
call   1040 <gethostbyname@plt>
test   %rax,%rax
je     1181 <getip+0x51>
mov    %rax,%rbx
mov    (%rax),%rax
cmpq   $0x0,(%rax)
je     1181 <getip+0x51>
mov    0x2e81(%rip),%r15
mov    (%r15),%edi
call   1050 <malloc@plt>
mov    %rax,%r14
mov    0x2e7f(%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    1184 <getip+0x54>
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 = icmp eq i8* %6, null
br i1 %7, label %8, label %9
8: ; preds = %1
store i8* null, i8** %2
br label %39
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = call %struct.hostent* @gethostbyname(i8* %10)
store %struct.hostent* %11, %struct.hostent** %5
%12 = icmp eq %struct.hostent* %11, null
br i1 %12, label %13, label %14
13: ; preds = %9
store i8* null, i8** %2
br label %39
14: ; preds = %9
%15 = load %struct.hostent*, %struct.hostent** %5
%16 = getelementptr inbounds %struct.hostent, %struct.hostent* %15, i32 0, i32 0
%17 = load i64*, i64** %16
%18 = getelementptr inbounds i64, i64* %17, i64 0
%19 = load i64, i64* %18
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %38
21: ; preds = %14
%22 = load i32, i32* @INET_ADDRSTRLEN
%23 = call i64 @malloc(i32 %22)
%24 = inttoptr i64 %23 to i8*
store i8* %24, i8** %4
%25 = load i32, i32* @AF_INET
%26 = load %struct.hostent*, %struct.hostent** %5
%27 = getelementptr inbounds %struct.hostent, %struct.hostent* %26, i32 0, i32 0
%28 = load i64*, i64** %27
%29 = getelementptr inbounds i64, i64* %28, i64 0
%30 = load i64, i64* %29
%31 = load i8*, i8** %4
%32 = load i32, i32* @INET_ADDRSTRLEN
%33 = call i64 @inet_ntop(i32 %25, i64 %30, i8* %31, i32 %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %21
%36 = load i8*, i8** %4
store i8* %36, i8** %2
br label %39
37: ; preds = %21
br label %38
38: ; preds = %37, %14
store i8* null, i8** %2
br label %39
39: ; preds = %38, %35, %13, %8
%40 = load i8*, i8** %2
ret i8* %40
}

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