#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;
  int /*<<< orphan*/ sin_family;
};
struct in_addr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_addr;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
struct hostent *gethostbyname(char *);
scalar_t__ inet_aton(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ inet_ntoa(int /*<<< orphan*/);
int /*<<< orphan*/ kr_net_set_error(char *, char *, char *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/, int);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int kr_net_resolve(char *err, char *host, char *ipbuf) {
  struct sockaddr_in sa;
  sa.sin_family = AF_INET;
  if (inet_aton(host, &sa.sin_addr) == 0) {
    struct hostent *he;
    he = gethostbyname(host);
    if (he == NULL) {
      kr_net_set_error(err, "can't resolve: %s", host);
      return -1;
    }
    memcpy(&sa.sin_addr, he->h_addr, sizeof(struct in_addr));
  }
  strcpy(ipbuf, inet_ntoa(sa.sin_addr));
  return 0;
}


x86 assembly (clang, -O0):
<kr_net_resolve>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1070 <inet_aton@plt>
cmp    $0x0,%rax
jne    11e3 <kr_net_resolve+0x83>
mov    -0x18(%rbp),%rdi
call   1050 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11cf <kr_net_resolve+0x6f>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe42(%rip),%rsi
call   1080 <kr_net_set_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1205 <kr_net_resolve+0xa5>
lea    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%esi
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kr_net_resolve>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r15
mov    0x2e6f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1070 <inet_aton@plt>
test   %rax,%rax
jne    11af <kr_net_resolve+0x4f>
mov    %rbx,%rdi
call   1050 <gethostbyname@plt>
test   %rax,%rax
je     11ce <kr_net_resolve+0x6e>
mov    (%rax),%esi
lea    0x8(%rsp),%rdi
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    0x8(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %r14,%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe2b(%rip),%rsi
mov    %r15,%rdi
mov    %rbx,%rdx
call   1080 <kr_net_set_error@plt>
mov    $0xffffffff,%eax
jmp    11c4 <kr_net_resolve+0x64>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.hostent = type { i32 }
@AF_INET = global i32 0
@.str = [18 x i8] c"can't resolve: %s\00"
define i32 @kr_net_resolve(i8* %0, i8* %1, i8* %2) {
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.sockaddr_in* %4 to i8*
%6 = load i32, i32* @AF_INET
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %6, i32* %7
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%9 = call i64 @inet_aton(i8* %1, i32* %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %20
11: ; preds = %3
%12 = call %struct.hostent* @gethostbyname(i8* %1)
%13 = icmp eq %struct.hostent* %12, null
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = call i32 @kr_net_set_error(i8* %0, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %24
16: ; preds = %11
%17 = getelementptr inbounds %struct.hostent, %struct.hostent* %12, i64 0, i32 0
%18 = load i32, i32* %17
%19 = call i32 @memcpy(i32* %8, i32 %18, i32 4)
br label %20
20: ; preds = %16, %3
%21 = load i32, i32* %8
%22 = call i32 @inet_ntoa(i32 %21)
%23 = call i32 @strcpy(i8* %2, i32 %22)
br label %24
24: ; preds = %14, %20
%25 = phi i32 [ 0, %20 ], [ -1, %14 ]
ret i32 %25
}