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

/* Variables and functions */
int EINVAL;
struct hostent *gethostbyname(char const *);
int /*<<< orphan*/ memcpy(unsigned int *, int /*<<< orphan*/, int);
int get_ip(unsigned int *ip_addr, const char *dns) {
  struct hostent *hent = NULL;
  if (!dns || !(hent = gethostbyname(dns)))
    return -EINVAL;
  memcpy(ip_addr, hent->h_addr, 4);
  return 0;
}


x86 assembly (clang, -O0):
<get_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     115a <get_ip+0x3a>
mov    -0x18(%rbp),%rdi
call   1030 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    116d <get_ip+0x4d>
xor    %eax,%eax
mov    0x2e7d(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1188 <get_ip+0x68>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%esi
mov    $0x4,%edx
call   1040 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_ip>:
push   %rbx
test   %rsi,%rsi
je     1149 <get_ip+0x29>
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1030 <gethostbyname@plt>
test   %rax,%rax
je     1149 <get_ip+0x29>
mov    (%rax),%esi
mov    %rbx,%rdi
mov    $0x4,%edx
call   1040 <memcpy@plt>
xor    %eax,%eax
pop    %rbx
ret
mov    0x2e90(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
pop    %rbx
ret

LLVM IR (-O0):
%struct.hostent = type { i32 }
@EINVAL = global i32 0
define i32 @get_ip(i32* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca %struct.hostent*
store i32* %0, i32** %4
store i8* %1, i8** %5
store %struct.hostent* null, %struct.hostent** %6
%7 = load i8*, i8** %5
%8 = icmp ne i8* %7, null
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = load i8*, i8** %5
%11 = call %struct.hostent* @gethostbyname(i8* %10)
store %struct.hostent* %11, %struct.hostent** %6
%12 = icmp ne %struct.hostent* %11, null
br i1 %12, label %16, label %13
13: ; preds = %9, %2
%14 = load i32, i32* @EINVAL
%15 = sub nsw i32 0, %14
store i32 %15, i32* %3
br label %22
16: ; preds = %9
%17 = load i32*, i32** %4
%18 = load %struct.hostent*, %struct.hostent** %6
%19 = getelementptr inbounds %struct.hostent, %struct.hostent* %18, i32 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 @memcpy(i32* %17, i32 %20, i32 4)
store i32 0, i32* %3
br label %22
22: ; preds = %16, %13
%23 = load i32, i32* %3
ret i32 %23
}

LLVM IR (-O2):
%struct.hostent = type { i32 }
@EINVAL = global i32 0
define i32 @get_ip(i32* %0, i8* %1) {
%3 = icmp eq i8* %1, null
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = tail call %struct.hostent* @gethostbyname(i8* %1)
%6 = icmp eq %struct.hostent* %5, null
br i1 %6, label %7, label %10
7: ; preds = %4, %2
%8 = load i32, i32* @EINVAL
%9 = sub nsw i32 0, %8
br label %14
10: ; preds = %4
%11 = getelementptr inbounds %struct.hostent, %struct.hostent* %5, i64 0, i32 0
%12 = load i32, i32* %11
%13 = tail call i32 @memcpy(i32* %0, i32 %12, i32 4)
br label %14
14: ; preds = %10, %7
%15 = phi i32 [ 0, %10 ], [ %9, %7 ]
ret i32 %15
}