#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_list;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ LOGGER_ERR(char *);
int /*<<< orphan*/ exit(int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ inet_ntop(int /*<<< orphan*/, int /*<<< orphan*/ *, char *,
                             int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, int, char *);
int netutils_dns_resolver(char *domain, char *ip_list, int max_ret_ip,
                          int max_len_ip) {
  struct hostent *answer;
  int i;
  char *ipstr = ip_list;
  memset(ip_list, 0, max_ret_ip * max_len_ip);
  answer = gethostbyname(domain);
  if (answer == NULL) {
    LOGGER_ERR("gethostbyname error \n");
    exit(1);
  }
  max_ret_ip--;
  for (i = 0; ((answer->h_addr_list)[i] != NULL) || (i == max_ret_ip); i++) {
    inet_ntop(AF_INET, (answer->h_addr_list)[i], ipstr, 16);
    printf("dns-> %d ->%s\n", i, ipstr);
    ipstr += max_len_ip;
  }
  return i;
}


x86 assembly (clang, -O0):
<netutils_dns_resolver>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
imul   -0x18(%rbp),%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    -0x8(%rbp),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11be <netutils_dns_resolver+0x5e>
lea    0xe51(%rip),%rdi
call   1060 <LOGGER_ERR@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rcx
movslq -0x24(%rbp),%rdx
mov    $0x1,%al
cmpq   $0x0,(%rcx,%rdx,8)
mov    %al,-0x31(%rbp)
jne    11f5 <netutils_dns_resolver+0x95>
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
sete   %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    1205 <netutils_dns_resolver+0xa5>
jmp    125d <netutils_dns_resolver+0xfd>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
mov    -0x30(%rbp),%rdx
mov    $0x10,%ecx
call   1030 <inet_ntop@plt>
mov    -0x24(%rbp),%esi
mov    -0x30(%rbp),%rdx
lea    0xddd(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11ce <netutils_dns_resolver+0x6e>
mov    -0x24(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<netutils_dns_resolver>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r12d
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %ecx,%edx
imul   %ebp,%edx
xor    %r15d,%r15d
mov    %rsi,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
mov    %r14,%rdi
call   1070 <gethostbyname@plt>
test   %rax,%rax
je     1215 <netutils_dns_resolver+0xb5>
mov    %rax,%r14
add    $0xffffffff,%ebp
mov    (%rax),%rax
mov    (%rax),%rsi
test   %rsi,%rsi
jne    11ae <netutils_dns_resolver+0x4e>
test   %ebp,%ebp
jne    1203 <netutils_dns_resolver+0xa3>
movslq %r12d,%r13
mov    %ebp,%r12d
add    $0xffffffffffffffff,%r12
xor    %r15d,%r15d
nopl   0x0(%rax,%rax,1)
mov    %r15,%rbp
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rdx
mov    $0x10,%ecx
call   1030 <inet_ntop@plt>
lea    0xe36(%rip),%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
call   1040 <printf@plt>
add    %r13,%rbx
add    $0x1,%r15
mov    (%r14),%rax
mov    0x8(%rax,%rbp,8),%rsi
test   %rsi,%rsi
jne    11c0 <netutils_dns_resolver+0x60>
cmp    %rbp,%r12
je     11c0 <netutils_dns_resolver+0x60>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xde4(%rip),%rdi
call   1060 <LOGGER_ERR@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.hostent = type { i32** }
@.str = [22 x i8] c"gethostbyname error \0A\00"
@AF_INET = global i32 0
@.str.1 = [15 x i8] c"dns-> %d ->%s\0A\00"
define i32 @netutils_dns_resolver(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.hostent*
%10 = alloca i32
%11 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%12 = load i8*, i8** %6
store i8* %12, i8** %11
%13 = load i8*, i8** %6
%14 = load i32, i32* %7
%15 = load i32, i32* %8
%16 = mul nsw i32 %14, %15
%17 = call i32 @memset(i8* %13, i32 0, i32 %16)
%18 = load i8*, i8** %5
%19 = call %struct.hostent* @gethostbyname(i8* %18)
store %struct.hostent* %19, %struct.hostent** %9
%20 = load %struct.hostent*, %struct.hostent** %9
%21 = icmp eq %struct.hostent* %20, null
br i1 %21, label %22, label %25
22: ; preds = %4
%23 = call i32 @LOGGER_ERR(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %4
%26 = load i32, i32* %7
%27 = add nsw i32 %26, -1
store i32 %27, i32* %7
store i32 0, i32* %10
br label %28
28: ; preds = %61, %25
%29 = load %struct.hostent*, %struct.hostent** %9
%30 = getelementptr inbounds %struct.hostent, %struct.hostent* %29, i32 0, i32 0
%31 = load i32**, i32*** %30
%32 = load i32, i32* %10
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i32*, i32** %31, i64 %33
%35 = load i32*, i32** %34
%36 = icmp ne i32* %35, null
br i1 %36, label %41, label %37
37: ; preds = %28
%38 = load i32, i32* %10
%39 = load i32, i32* %7
%40 = icmp eq i32 %38, %39
br label %41
41: ; preds = %37, %28
%42 = phi i1 [ true, %28 ], [ %40, %37 ]
br i1 %42, label %43, label %64
43: ; preds = %41
%44 = load i32, i32* @AF_INET
%45 = load %struct.hostent*, %struct.hostent** %9
%46 = getelementptr inbounds %struct.hostent, %struct.hostent* %45, i32 0, i32 0
%47 = load i32**, i32*** %46
%48 = load i32, i32* %10
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i32*, i32** %47, i64 %49
%51 = load i32*, i32** %50
%52 = load i8*, i8** %11
%53 = call i32 @inet_ntop(i32 %44, i32* %51, i8* %52, i32 16)
%54 = load i32, i32* %10
%55 = load i8*, i8** %11
%56 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %54, i8* %55)
%57 = load i32, i32* %8
%58 = load i8*, i8** %11
%59 = sext i32 %57 to i64
%60 = getelementptr inbounds i8, i8* %58, i64 %59
store i8* %60, i8** %11
br label %61
61: ; preds = %43
%62 = load i32, i32* %10
%63 = add nsw i32 %62, 1
store i32 %63, i32* %10
br label %28
64: ; preds = %41
%65 = load i32, i32* %10
ret i32 %65
}

LLVM IR (-O2):
%struct.hostent = type { i32** }
@.str = [22 x i8] c"gethostbyname error \0A\00"
@AF_INET = global i32 0
@.str.1 = [15 x i8] c"dns-> %d ->%s\0A\00"
define i32 @netutils_dns_resolver(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = mul nsw i32 %3, %2
%6 = tail call i32 @memset(i8* %1, i32 0, i32 %5)
%7 = tail call %struct.hostent* @gethostbyname(i8* %0)
%8 = icmp eq %struct.hostent* %7, null
br i1 %8, label %9, label %12
9: ; preds = %4
%10 = tail call i32 @LOGGER_ERR(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %4
%13 = add nsw i32 %2, -1
%14 = getelementptr inbounds %struct.hostent, %struct.hostent* %7, i64 0, i32 0
%15 = load i32**, i32*** %14
%16 = load i32*, i32** %15
%17 = icmp ne i32* %16, null
%18 = icmp eq i32 %13, 0
%19 = select i1 %17, i1 true, i1 %18
br i1 %19, label %20, label %41
20: ; preds = %12
%21 = sext i32 %3 to i64
%22 = zext i32 %13 to i64
br label %23
23: ; preds = %20, %23
%24 = phi i64 [ 0, %20 ], [ %32, %23 ]
%25 = phi i32* [ %16, %20 ], [ %35, %23 ]
%26 = phi i8* [ %1, %20 ], [ %31, %23 ]
%27 = load i32, i32* @AF_INET
%28 = tail call i32 @inet_ntop(i32 %27, i32* %25, i8* %26, i32 16)
%29 = trunc i64 %24 to i32
%30 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %29, i8* %26)
%31 = getelementptr inbounds i8, i8* %26, i64 %21
%32 = add nuw i64 %24, 1
%33 = load i32**, i32*** %14
%34 = getelementptr inbounds i32*, i32** %33, i64 %32
%35 = load i32*, i32** %34
%36 = icmp ne i32* %35, null
%37 = icmp eq i64 %32, %22
%38 = select i1 %36, i1 true, i1 %37
br i1 %38, label %23, label %39
39: ; preds = %23
%40 = trunc i64 %32 to i32
br label %41
41: ; preds = %39, %12
%42 = phi i32 [ 0, %12 ], [ %40, %39 ]
ret i32 %42
}