#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 sockaddr_in {
  int /*<<< orphan*/ sin_addr;
};
struct hostent {
  unsigned long *h_addr;
  int h_length;
};
typedef unsigned long in_addr_t;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
struct hostent *gethostbyname(char const *);
unsigned long inet_addr(char const *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, unsigned long *, int);
int /*<<< orphan*/ perror(char *);
int resolv_hostname(const char *hname, struct sockaddr_in *sin) {
  struct hostent *hostp;
  unsigned long addr;
  addr = inet_addr(hname);
  if (addr != (in_addr_t)-1)
    memcpy(&sin->sin_addr, &addr, sizeof(addr));
  else {
    hostp = gethostbyname(hname);
    if (hostp == NULL) {
      perror("ERROR resolving hostname");
      return -1;
    } else
      memcpy(&sin->sin_addr, hostp->h_addr, hostp->h_length);
  }
  return 0;
}


x86 assembly (clang, -O0):
<resolv_hostname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <inet_addr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
je     117f <resolv_hostname+0x3f>
mov    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
mov    $0x8,%edx
call   1050 <memcpy@plt>
jmp    11cb <resolv_hostname+0x8b>
mov    -0x10(%rbp),%rdi
call   1040 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11af <resolv_hostname+0x6f>
lea    0xe62(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d2 <resolv_hostname+0x92>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%edx
call   1050 <memcpy@plt>
jmp    11cb <resolv_hostname+0x8b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<resolv_hostname>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
call   1030 <inet_addr@plt>
mov    %rax,(%rsp)
cmp    $0xffffffffffffffff,%rax
je     1166 <resolv_hostname+0x26>
mov    %rsp,%rsi
mov    %r14,%rdi
mov    $0x8,%edx
jmp    117c <resolv_hostname+0x3c>
mov    %rbx,%rdi
call   1040 <gethostbyname@plt>
test   %rax,%rax
je     118b <resolv_hostname+0x4b>
mov    (%rax),%rsi
mov    0x8(%rax),%edx
mov    %r14,%rdi
call   1050 <memcpy@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe6e(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
jmp    1183 <resolv_hostname+0x43>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.hostent = type { i64*, i32 }
@.str = [25 x i8] c"ERROR resolving hostname\00"
define i32 @resolv_hostname(i8* %0, %struct.sockaddr_in* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.sockaddr_in*
%6 = alloca %struct.hostent*
%7 = alloca i64
store i8* %0, i8** %4
store %struct.sockaddr_in* %1, %struct.sockaddr_in** %5
%8 = load i8*, i8** %4
%9 = call i64 @inet_addr(i8* %8)
store i64 %9, i64* %7
%10 = load i64, i64* %7
%11 = icmp ne i64 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %13, i32 0, i32 0
%15 = call i32 @memcpy(i32* %14, i64* %7, i32 8)
br label %34
16: ; preds = %2
%17 = load i8*, i8** %4
%18 = call %struct.hostent* @gethostbyname(i8* %17)
store %struct.hostent* %18, %struct.hostent** %6
%19 = load %struct.hostent*, %struct.hostent** %6
%20 = icmp eq %struct.hostent* %19, null
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %35
23: ; preds = %16
%24 = load %struct.sockaddr_in*, %struct.sockaddr_in** %5
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %24, i32 0, i32 0
%26 = load %struct.hostent*, %struct.hostent** %6
%27 = getelementptr inbounds %struct.hostent, %struct.hostent* %26, i32 0, i32 0
%28 = load i64*, i64** %27
%29 = load %struct.hostent*, %struct.hostent** %6
%30 = getelementptr inbounds %struct.hostent, %struct.hostent* %29, i32 0, i32 1
%31 = load i32, i32* %30
%32 = call i32 @memcpy(i32* %25, i64* %28, i32 %31)
br label %33
33: ; preds = %23
br label %34
34: ; preds = %33, %12
store i32 0, i32* %3
br label %35
35: ; preds = %34, %21
%36 = load i32, i32* %3
ret i32 %36
}

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