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

/* Variables and functions */
int /*<<< orphan*/ exit(int);
struct hostent *gethostbyname(char *);
long inet_addr(char *);
int /*<<< orphan*/ memcpy(long *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
long getip(char *hostname) {
  struct hostent *he;
  long ipaddr;
  if ((ipaddr = inet_addr(hostname)) < 0) {
    if ((he = gethostbyname(hostname)) == NULL) {
      perror("gethostbyname()");
      exit(-1);
    }
    memcpy(&ipaddr, he->h_addr, he->h_length);
  }
  return ipaddr;
}


x86 assembly (clang, -O0):
<getip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <inet_addr@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jge    11b6 <getip+0x66>
mov    -0x8(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11a0 <getip+0x50>
lea    0xe6f(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rdi
call   1050 <memcpy@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getip>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
call   1030 <inet_addr@plt>
mov    %rax,0x8(%rsp)
test   %rax,%rax
jns    1188 <getip+0x38>
mov    %rbx,%rdi
call   1040 <gethostbyname@plt>
test   %rax,%rax
je     118e <getip+0x3e>
mov    (%rax),%edx
mov    0x4(%rax),%esi
lea    0x8(%rsp),%rdi
call   1050 <memcpy@plt>
mov    0x8(%rsp),%rax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe6b(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.hostent = type { i32, i32 }
@.str = [16 x i8] c"gethostbyname()\00"
define i64 @getip(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.hostent*
%4 = alloca i64
store i8* %0, i8** %2
%5 = load i8*, i8** %2
%6 = call i64 @inet_addr(i8* %5)
store i64 %6, i64* %4
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %23
8: ; preds = %1
%9 = load i8*, i8** %2
%10 = call %struct.hostent* @gethostbyname(i8* %9)
store %struct.hostent* %10, %struct.hostent** %3
%11 = icmp eq %struct.hostent* %10, null
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 -1)
unreachable
15: ; preds = %8
%16 = load %struct.hostent*, %struct.hostent** %3
%17 = getelementptr inbounds %struct.hostent, %struct.hostent* %16, i32 0, i32 1
%18 = load i32, i32* %17
%19 = load %struct.hostent*, %struct.hostent** %3
%20 = getelementptr inbounds %struct.hostent, %struct.hostent* %19, i32 0, i32 0
%21 = load i32, i32* %20
%22 = call i32 @memcpy(i64* %4, i32 %18, i32 %21)
br label %23
23: ; preds = %15, %1
%24 = load i64, i64* %4
ret i64 %24
}

LLVM IR (-O2):
%struct.hostent = type { i32, i32 }
@.str = [16 x i8] c"gethostbyname()\00"
define i64 @getip(i8* %0) {
%2 = alloca i64
%3 = bitcast i64* %2 to i8*
%4 = tail call i64 @inet_addr(i8* %0)
store i64 %4, i64* %2
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %19
6: ; preds = %1
%7 = tail call %struct.hostent* @gethostbyname(i8* %0)
%8 = icmp eq %struct.hostent* %7, null
br i1 %8, label %9, label %12
9: ; preds = %6
%10 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 -1)
unreachable
12: ; preds = %6
%13 = getelementptr inbounds %struct.hostent, %struct.hostent* %7, i64 0, i32 1
%14 = load i32, i32* %13
%15 = getelementptr inbounds %struct.hostent, %struct.hostent* %7, i64 0, i32 0
%16 = load i32, i32* %15
%17 = call i32 @memcpy(i64* %2, i32 %14, i32 %16)
%18 = load i64, i64* %2
br label %19
19: ; preds = %12, %1
%20 = phi i64 [ %18, %12 ], [ %4, %1 ]
ret i64 %20
}