#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 const *);
int /*<<< orphan*/ inet_ntoa(struct in_addr);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
void get_ip_addr(char const *domain, char *ip_addr) {
  struct hostent *host = gethostbyname(domain);
  if (!host) {
    ip_addr = NULL;
    return;
  }
  for (int i = 0; host->h_addr_list[i]; i++) {
    strcpy(ip_addr, inet_ntoa(*(struct in_addr *)host->h_addr_list[i]));
    break;
  }
}


x86 assembly (clang, -O0):
<get_ip_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1165 <get_ip_addr+0x35>
movq   $0x0,-0x10(%rbp)
jmp    11ab <get_ip_addr+0x7b>
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movslq -0x1c(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     11ab <get_ip_addr+0x7b>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    -0x28(%rbp),%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_ip_addr>:
push   %rbx
mov    %rsi,%rbx
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     115b <get_ip_addr+0x2b>
mov    (%rax),%rax
mov    (%rax),%rax
test   %rax,%rax
je     115b <get_ip_addr+0x2b>
mov    (%rax),%edi
call   1040 <inet_ntoa@plt>
mov    %rbx,%rdi
mov    %eax,%esi
pop    %rbx
jmp    1030 <strcpy@plt>
pop    %rbx
ret

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

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