#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 */
typedef int /*<<< orphan*/ ns_rr;
typedef int /*<<< orphan*/ ns_msg;

/* Variables and functions */
int /*<<< orphan*/ C_ANY;
scalar_t__ NS_INT16SZ;
int NS_PACKETSZ;
int /*<<< orphan*/ T_MX;
int /*<<< orphan*/ dn_expand(scalar_t__, scalar_t__, scalar_t__, char *, int);
char *malloc(int);
int /*<<< orphan*/ ns_initparse(char *, int, int /*<<< orphan*/ *);
scalar_t__ ns_msg_base(int /*<<< orphan*/);
scalar_t__ ns_msg_size(int /*<<< orphan*/);
int /*<<< orphan*/ ns_parserr(int /*<<< orphan*/ *, int /*<<< orphan*/,
                              int /*<<< orphan*/, int /*<<< orphan*/ *);
scalar_t__ ns_rr_rdata(int /*<<< orphan*/);
int /*<<< orphan*/ ns_s_an;
int res_init();
int res_query(char const *, int /*<<< orphan*/, int /*<<< orphan*/, char *,
              int);
char *getMailserver(const char *domain) {
  int len;
  char answer[NS_PACKETSZ];
  char *mailserver = malloc(NS_PACKETSZ);
  ns_msg msg;
  ns_rr rr;
  if (res_init() == -1) {
    return NULL;
  }
  len = res_query(domain, C_ANY, T_MX, answer, NS_PACKETSZ);
  if (len == -1) {
    return NULL;
  }
  ns_initparse(answer, len, &msg);
  ns_parserr(&msg, ns_s_an, 0, &rr);
  dn_expand(ns_msg_base(msg), ns_msg_base(msg) + ns_msg_size(msg),
            ns_rr_rdata(rr) + NS_INT16SZ, mailserver, NS_PACKETSZ);
  return mailserver;
}


x86 assembly (clang, -O0):
<getMailserver>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   1060 <res_init@plt>
cmp    $0xffffffff,%eax
jne    11fe <getMailserver+0x6e>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    12dd <getMailserver+0x14d>
mov    -0x48(%rbp),%rcx
mov    -0x10(%rbp),%rdi
mov    0x2de3(%rip),%rax
mov    (%rax),%esi
mov    0x2dca(%rip),%rax
mov    (%rax),%edx
mov    0x2da1(%rip),%rax
mov    (%rax),%r8d
call   10b0 <res_query@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1248 <getMailserver+0xb8>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    12dd <getMailserver+0x14d>
mov    -0x48(%rbp),%rdi
mov    -0x14(%rbp),%esi
lea    -0x34(%rbp),%rdx
call   1030 <ns_initparse@plt>
mov    0x2d69(%rip),%rax
mov    (%rax),%esi
lea    -0x34(%rbp),%rdi
xor    %edx,%edx
lea    -0x38(%rbp),%rcx
call   1040 <ns_parserr@plt>
mov    -0x34(%rbp),%edi
call   10a0 <ns_msg_base@plt>
mov    %rax,-0x58(%rbp)
mov    -0x34(%rbp),%edi
call   10a0 <ns_msg_base@plt>
mov    %rax,-0x60(%rbp)
mov    -0x34(%rbp),%edi
call   1050 <ns_msg_size@plt>
mov    %rax,%rcx
mov    -0x60(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x50(%rbp)
mov    -0x38(%rbp),%edi
call   1070 <ns_rr_rdata@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    %rax,%rdx
mov    0x2d18(%rip),%rax
add    (%rax),%rdx
mov    -0x30(%rbp),%rcx
mov    0x2cfa(%rip),%rax
mov    (%rax),%r8d
call   1090 <dn_expand@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getMailserver>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r15
mov    0x2e18(%rip),%r13
mov    0x0(%r13),%edi
mov    %rsp,%rbx
mov    %rdi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
call   1080 <malloc@plt>
mov    %rax,%r14
xor    %r12d,%r12d
xor    %eax,%eax
call   1060 <res_init@plt>
cmp    $0xffffffff,%eax
je     1279 <getMailserver+0xe9>
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
mov    0x2df5(%rip),%rax
mov    (%rax),%edx
mov    0x0(%r13),%r8d
mov    %r15,%rdi
mov    %rbx,%rcx
call   10b0 <res_query@plt>
cmp    $0xffffffff,%eax
je     1276 <getMailserver+0xe6>
lea    -0x2c(%rbp),%r15
mov    %rbx,%rdi
mov    %eax,%esi
mov    %r15,%rdx
call   1030 <ns_initparse@plt>
mov    0x2daf(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rcx
mov    %r15,%rdi
xor    %edx,%edx
call   1040 <ns_parserr@plt>
mov    -0x2c(%rbp),%edi
call   10a0 <ns_msg_base@plt>
mov    %rax,%r15
mov    -0x2c(%rbp),%edi
call   10a0 <ns_msg_base@plt>
mov    %rax,%rbx
mov    -0x2c(%rbp),%edi
call   1050 <ns_msg_size@plt>
add    %rax,%rbx
mov    -0x30(%rbp),%edi
call   1070 <ns_rr_rdata@plt>
mov    0x2d77(%rip),%rcx
add    (%rcx),%rax
mov    0x0(%r13),%r8d
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
mov    %r14,%rcx
call   1090 <dn_expand@plt>
mov    %r14,%r12
jmp    1279 <getMailserver+0xe9>
xor    %r12d,%r12d
mov    %r12,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@NS_PACKETSZ = global i32 0
@C_ANY = global i32 0
@T_MX = global i32 0
@ns_s_an = global i32 0
@NS_INT16SZ = global i64 0
define i8* @getMailserver(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %3
%11 = load i32, i32* @NS_PACKETSZ
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %5
%14 = alloca i8, i64 %12
store i64 %12, i64* %6
%15 = load i32, i32* @NS_PACKETSZ
%16 = call i8* @malloc(i32 %15)
store i8* %16, i8** %7
%17 = call i32 (...) @res_init()
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %20
19: ; preds = %1
store i8* null, i8** %2
store i32 1, i32* %10
br label %49
20: ; preds = %1
%21 = load i8*, i8** %3
%22 = load i32, i32* @C_ANY
%23 = load i32, i32* @T_MX
%24 = load i32, i32* @NS_PACKETSZ
%25 = call i32 @res_query(i8* %21, i32 %22, i32 %23, i8* %14, i32 %24)
store i32 %25, i32* %4
%26 = load i32, i32* %4
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %29
28: ; preds = %20
store i8* null, i8** %2
store i32 1, i32* %10
br label %49
29: ; preds = %20
%30 = load i32, i32* %4
%31 = call i32 @ns_initparse(i8* %14, i32 %30, i32* %8)
%32 = load i32, i32* @ns_s_an
%33 = call i32 @ns_parserr(i32* %8, i32 %32, i32 0, i32* %9)
%34 = load i32, i32* %8
%35 = call i64 @ns_msg_base(i32 %34)
%36 = load i32, i32* %8
%37 = call i64 @ns_msg_base(i32 %36)
%38 = load i32, i32* %8
%39 = call i64 @ns_msg_size(i32 %38)
%40 = add nsw i64 %37, %39
%41 = load i32, i32* %9
%42 = call i64 @ns_rr_rdata(i32 %41)
%43 = load i64, i64* @NS_INT16SZ
%44 = add nsw i64 %42, %43
%45 = load i8*, i8** %7
%46 = load i32, i32* @NS_PACKETSZ
%47 = call i32 @dn_expand(i64 %35, i64 %40, i64 %44, i8* %45, i32 %46)
%48 = load i8*, i8** %7
store i8* %48, i8** %2
store i32 1, i32* %10
br label %49
49: ; preds = %29, %28, %19
%50 = load i8*, i8** %5
%51 = load i8*, i8** %2
ret i8* %51
}

LLVM IR (-O2):
@NS_PACKETSZ = global i32 0
@C_ANY = global i32 0
@T_MX = global i32 0
@ns_s_an = global i32 0
@NS_INT16SZ = global i64 0
define i8* @getMailserver(i8* %0) {
%2 = alloca i32
%3 = alloca i32
%4 = load i32, i32* @NS_PACKETSZ
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = tail call i8* @malloc(i32 %4)
%8 = bitcast i32* %2 to i8*
%9 = bitcast i32* %3 to i8*
%10 = tail call i32 (...) @res_init()
%11 = icmp eq i32 %10, -1
br i1 %11, label %35, label %12
12: ; preds = %1
%13 = load i32, i32* @C_ANY
%14 = load i32, i32* @T_MX
%15 = load i32, i32* @NS_PACKETSZ
%16 = call i32 @res_query(i8* %0, i32 %13, i32 %14, i8* %6, i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %35, label %18
18: ; preds = %12
%19 = call i32 @ns_initparse(i8* %6, i32 %16, i32* %2)
%20 = load i32, i32* @ns_s_an
%21 = call i32 @ns_parserr(i32* %2, i32 %20, i32 0, i32* %3)
%22 = load i32, i32* %2
%23 = call i64 @ns_msg_base(i32 %22)
%24 = load i32, i32* %2
%25 = call i64 @ns_msg_base(i32 %24)
%26 = load i32, i32* %2
%27 = call i64 @ns_msg_size(i32 %26)
%28 = add nsw i64 %27, %25
%29 = load i32, i32* %3
%30 = call i64 @ns_rr_rdata(i32 %29)
%31 = load i64, i64* @NS_INT16SZ
%32 = add nsw i64 %31, %30
%33 = load i32, i32* @NS_PACKETSZ
%34 = call i32 @dn_expand(i64 %23, i64 %28, i64 %32, i8* %7, i32 %33)
br label %35
35: ; preds = %12, %1, %18
%36 = phi i8* [ %7, %18 ], [ null, %1 ], [ null, %12 ]
ret i8* %36
}