#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 {
  unsigned long s_addr;
};
struct hostent {
  scalar_t__ h_addr;
};

/* Variables and functions */
unsigned long INADDR_NONE;
int /*<<< orphan*/ free(char *);
scalar_t__ gethostbyname_r(char const *, struct hostent *, char *, int,
                           struct hostent **, int *);
unsigned long inet_addr(char const *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, char const *);
int /*<<< orphan*/ res_init();
unsigned long my_gethostbyname(const char *server) {
  unsigned long ip;
  struct hostent host, *lphost;
  char *dnsbuffer;
  int rc;
  if (INADDR_NONE != (ip = inet_addr(server)))
    return ip;
  if (NULL == (dnsbuffer = (char *)malloc(8192)))
    return INADDR_NONE;
  if (gethostbyname_r(server, &host, dnsbuffer, 8192, &lphost, &rc) ||
      (!lphost)) {
    res_init();
    if (gethostbyname_r(server, &host, dnsbuffer, 8192, &lphost, &rc) ||
        (!lphost)) {
      printf("can not resolve ip of %s\n", server);
      ip = INADDR_NONE;
    } else {
      ip = ((struct in_addr *)(lphost->h_addr))->s_addr;
    }
  } else {
    ip = ((struct in_addr *)(lphost->h_addr))->s_addr;
  }
  free(dnsbuffer);
  return ip;
}


x86 assembly (clang, -O0):
<my_gethostbyname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <inet_addr@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
mov    %rcx,-0x18(%rbp)
cmp    %rcx,%rax
je     11a4 <my_gethostbyname+0x44>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1295 <my_gethostbyname+0x135>
mov    $0x2000,%edi
call   1080 <malloc@plt>
mov    %rax,%rcx
mov    %rcx,-0x30(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    11d3 <my_gethostbyname+0x73>
mov    0x2e21(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
jmp    1295 <my_gethostbyname+0x135>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rdx
lea    -0x20(%rbp),%rsi
mov    $0x2000,%ecx
lea    -0x28(%rbp),%r8
lea    -0x34(%rbp),%r9
call   1040 <gethostbyname_r@plt>
cmp    $0x0,%rax
jne    1206 <my_gethostbyname+0xa6>
cmpq   $0x0,-0x28(%rbp)
jne    1276 <my_gethostbyname+0x116>
mov    $0x0,%al
call   1060 <res_init@plt>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rdx
lea    -0x20(%rbp),%rsi
mov    $0x2000,%ecx
lea    -0x28(%rbp),%r8
lea    -0x34(%rbp),%r9
call   1040 <gethostbyname_r@plt>
cmp    $0x0,%rax
jne    1240 <my_gethostbyname+0xe0>
cmpq   $0x0,-0x28(%rbp)
jne    1263 <my_gethostbyname+0x103>
mov    -0x10(%rbp),%rsi
lea    0xdb5(%rip),%rdi
call   1050 <printf@plt>
mov    0x2d91(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    1271 <my_gethostbyname+0x111>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    1284 <my_gethostbyname+0x124>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_gethostbyname>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r15
mov    0x2e73(%rip),%r12
mov    (%r12),%r14
call   1070 <inet_addr@plt>
mov    %rax,%rbx
cmp    %rax,%r14
jne    1225 <my_gethostbyname+0xc5>
mov    $0x2000,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     11fe <my_gethostbyname+0x9e>
mov    %rax,%r14
lea    0x10(%rsp),%rsi
mov    %rsp,%r8
lea    0xc(%rsp),%r9
mov    %r15,%rdi
mov    %rax,%rdx
mov    $0x2000,%ecx
call   1040 <gethostbyname_r@plt>
test   %rax,%rax
jne    11c7 <my_gethostbyname+0x67>
mov    (%rsp),%rax
test   %rax,%rax
jne    11f9 <my_gethostbyname+0x99>
xor    %eax,%eax
call   1060 <res_init@plt>
lea    0x10(%rsp),%rsi
mov    %rsp,%r8
lea    0xc(%rsp),%r9
mov    %r15,%rdi
mov    %r14,%rdx
mov    $0x2000,%ecx
call   1040 <gethostbyname_r@plt>
test   %rax,%rax
jne    120a <my_gethostbyname+0xaa>
mov    (%rsp),%rax
test   %rax,%rax
je     120a <my_gethostbyname+0xaa>
mov    (%rax),%r12
jmp    1219 <my_gethostbyname+0xb9>
mov    0x2de3(%rip),%rax
mov    (%rax),%rbx
jmp    1225 <my_gethostbyname+0xc5>
lea    0xdef(%rip),%rdi
mov    %r15,%rsi
call   1050 <printf@plt>
mov    (%r12),%rbx
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.hostent = type { i64 }
%struct.in_addr = type { i64 }
@INADDR_NONE = global i64 0
@.str = [26 x i8] c"can not resolve ip of %s\0A\00"
define i64 @my_gethostbyname(i8* %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i64
%5 = alloca %struct.hostent
%6 = alloca %struct.hostent*
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %3
%9 = load i64, i64* @INADDR_NONE
%10 = load i8*, i8** %3
%11 = call i64 @inet_addr(i8* %10)
store i64 %11, i64* %4
%12 = icmp ne i64 %9, %11
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = load i64, i64* %4
store i64 %14, i64* %2
br label %61
15: ; preds = %1
%16 = call i64 @malloc(i32 8192)
%17 = inttoptr i64 %16 to i8*
store i8* %17, i8** %7
%18 = icmp eq i8* null, %17
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = load i64, i64* @INADDR_NONE
store i64 %20, i64* %2
br label %61
21: ; preds = %15
%22 = load i8*, i8** %3
%23 = load i8*, i8** %7
%24 = call i64 @gethostbyname_r(i8* %22, %struct.hostent* %5, i8* %23, i32 8192, %struct.hostent** %6, i32* %8)
%25 = icmp ne i64 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %21
%27 = load %struct.hostent*, %struct.hostent** %6
%28 = icmp ne %struct.hostent* %27, null
br i1 %28, label %50, label %29
29: ; preds = %26, %21
%30 = call i32 (...) @res_init()
%31 = load i8*, i8** %3
%32 = load i8*, i8** %7
%33 = call i64 @gethostbyname_r(i8* %31, %struct.hostent* %5, i8* %32, i32 8192, %struct.hostent** %6, i32* %8)
%34 = icmp ne i64 %33, 0
br i1 %34, label %38, label %35
35: ; preds = %29
%36 = load %struct.hostent*, %struct.hostent** %6
%37 = icmp ne %struct.hostent* %36, null
br i1 %37, label %42, label %38
38: ; preds = %35, %29
%39 = load i8*, i8** %3
%40 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %39)
%41 = load i64, i64* @INADDR_NONE
store i64 %41, i64* %4
br label %49
42: ; preds = %35
%43 = load %struct.hostent*, %struct.hostent** %6
%44 = getelementptr inbounds %struct.hostent, %struct.hostent* %43, i32 0, i32 0
%45 = load i64, i64* %44
%46 = inttoptr i64 %45 to %struct.in_addr*
%47 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %46, i32 0, i32 0
%48 = load i64, i64* %47
store i64 %48, i64* %4
br label %49
49: ; preds = %42, %38
br label %57
50: ; preds = %26
%51 = load %struct.hostent*, %struct.hostent** %6
%52 = getelementptr inbounds %struct.hostent, %struct.hostent* %51, i32 0, i32 0
%53 = load i64, i64* %52
%54 = inttoptr i64 %53 to %struct.in_addr*
%55 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %54, i32 0, i32 0
%56 = load i64, i64* %55
store i64 %56, i64* %4
br label %57
57: ; preds = %50, %49
%58 = load i8*, i8** %7
%59 = call i32 @free(i8* %58)
%60 = load i64, i64* %4
store i64 %60, i64* %2
br label %61
61: ; preds = %57, %19, %13
%62 = load i64, i64* %2
ret i64 %62
}

LLVM IR (-O2):
%struct.hostent = type { i64 }
%struct.in_addr = type { i64 }
@INADDR_NONE = global i64 0
@.str = [26 x i8] c"can not resolve ip of %s\0A\00"
define i64 @my_gethostbyname(i8* %0) {
%2 = alloca %struct.hostent
%3 = alloca %struct.hostent*
%4 = alloca i32
%5 = bitcast %struct.hostent* %2 to i8*
%6 = bitcast %struct.hostent** %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = load i64, i64* @INADDR_NONE
%9 = tail call i64 @inet_addr(i8* %0)
%10 = icmp eq i64 %8, %9
br i1 %10, label %11, label %46
11: ; preds = %1
%12 = tail call i64 @malloc(i32 8192)
%13 = inttoptr i64 %12 to i8*
%14 = icmp eq i64 %12, 0
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = load i64, i64* @INADDR_NONE
br label %46
17: ; preds = %11
%18 = call i64 @gethostbyname_r(i8* %0, %struct.hostent* %2, i8* %13, i32 8192, %struct.hostent** %3, i32* %4)
%19 = icmp eq i64 %18, 0
%20 = load %struct.hostent*, %struct.hostent** %3
%21 = icmp ne %struct.hostent* %20, null
%22 = select i1 %19, i1 %21, i1 false
br i1 %22, label %37, label %23
23: ; preds = %17
%24 = call i32 (...) @res_init()
%25 = call i64 @gethostbyname_r(i8* %0, %struct.hostent* %2, i8* %13, i32 8192, %struct.hostent** %3, i32* %4)
%26 = icmp eq i64 %25, 0
%27 = load %struct.hostent*, %struct.hostent** %3
%28 = icmp ne %struct.hostent* %27, null
%29 = select i1 %26, i1 %28, i1 false
br i1 %29, label %32, label %30
30: ; preds = %23
%31 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %42
32: ; preds = %23
%33 = getelementptr inbounds %struct.hostent, %struct.hostent* %27, i64 0, i32 0
%34 = load i64, i64* %33
%35 = inttoptr i64 %34 to %struct.in_addr*
%36 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %35, i64 0, i32 0
br label %42
37: ; preds = %17
%38 = getelementptr inbounds %struct.hostent, %struct.hostent* %20, i64 0, i32 0
%39 = load i64, i64* %38
%40 = inttoptr i64 %39 to %struct.in_addr*
%41 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %40, i64 0, i32 0
br label %42
42: ; preds = %30, %32, %37
%43 = phi i64* [ %41, %37 ], [ %36, %32 ], [ @INADDR_NONE, %30 ]
%44 = load i64, i64* %43
%45 = call i32 @free(i8* %13)
br label %46
46: ; preds = %1, %42, %15
%47 = phi i64 [ %16, %15 ], [ %44, %42 ], [ %9, %1 ]
ret i64 %47
}