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

/* Variables and functions */
struct hostent *gethostbyname(char *);
scalar_t__ inet_aton(char *, struct in_addr *);
char *strdup(char *);
int getHostname(struct in_addr *addr, char **asName, char *tok) {
  struct hostent *host;
  if (asName)
    *asName = strdup(tok);
  if (inet_aton(tok, addr))
    return 0;
  host = gethostbyname(tok);
  if (!host)
    return 9;
  *addr = **((struct in_addr **)host->h_addr_list);
  return 0;
}


x86 assembly (clang, -O0):
<getHostname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1162 <getHostname+0x32>
mov    -0x20(%rbp),%rdi
call   1050 <strdup@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <inet_aton@plt>
cmp    $0x0,%rax
je     1185 <getHostname+0x55>
movl   $0x0,-0x4(%rbp)
jmp    11c2 <getHostname+0x92>
mov    -0x20(%rbp),%rdi
call   1030 <gethostbyname@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11a9 <getHostname+0x79>
movl   $0x9,-0x4(%rbp)
jmp    11c2 <getHostname+0x92>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    (%rcx),%rcx
mov    (%rcx),%rcx
mov    (%rcx),%ecx
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getHostname>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %rdi,%r14
test   %rsi,%rsi
je     114e <getHostname+0x1e>
mov    %rsi,%rbp
mov    %rbx,%rdi
call   1050 <strdup@plt>
mov    %rax,0x0(%rbp)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1040 <inet_aton@plt>
xor    %ebp,%ebp
test   %rax,%rax
jne    117f <getHostname+0x4f>
mov    %rbx,%rdi
call   1030 <gethostbyname@plt>
test   %rax,%rax
je     117a <getHostname+0x4a>
mov    (%rax),%rax
mov    (%rax),%rax
mov    (%rax),%eax
mov    %eax,(%r14)
jmp    117f <getHostname+0x4f>
mov    $0x9,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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