#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 {
  scalar_t__ h_addrtype;
  int /*<<< orphan*/ h_name;
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ *h_addr_list;
};

/* Variables and functions */
scalar_t__ AF_INET;
int /*<<< orphan*/ errno;
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ memcpy(unsigned long *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int sscanf(char *, char *, char *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
char *svz_inet_ntoa(unsigned long);
int /*<<< orphan*/ svz_log(int, char *, ...);
char *dns_handle_request(char *inbuf) {
  unsigned long addr;
  struct hostent *host;
  static char resolved[256];
  if ((1 == sscanf(inbuf, "%s", resolved))) {
    if ((host = gethostbyname(resolved)) == NULL) {
      svz_log(1, "dns: gethostbyname: %s (%s)\n", strerror(errno), resolved);
      return NULL;
    }
    if (host->h_addrtype == AF_INET) {
      memcpy(&addr, host->h_addr_list[0], host->h_length);
      svz_log(4, "dns: %s is %s\n", host->h_name, svz_inet_ntoa(addr));
      sprintf(resolved, "%s", svz_inet_ntoa(addr));
      return resolved;
    }
  } else {
    svz_log(1, "dns: protocol error\n");
    return NULL;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<dns_handle_request>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe79(%rip),%rsi
lea    0x2ee2(%rip),%rdx
call   1060 <sscanf@plt>
mov    %eax,%ecx
mov    $0x1,%eax
cmp    %ecx,%eax
jne    1284 <dns_handle_request+0x114>
lea    0x2ec7(%rip),%rdi
call   1030 <gethostbyname@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11f3 <dns_handle_request+0x83>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %eax,%edx
mov    $0x1,%edi
lea    0xe2b(%rip),%rsi
lea    0x2e91(%rip),%rcx
mov    $0x0,%al
call   1090 <svz_log@plt>
movq   $0x0,-0x8(%rbp)
jmp    12ac <dns_handle_request+0x13c>
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    0x2de7(%rip),%rcx
cmp    (%rcx),%rax
jne    127f <dns_handle_request+0x10f>
mov    -0x20(%rbp),%rax
mov    0x10(%rax),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%rax
mov    0xc(%rax),%edx
lea    -0x18(%rbp),%rdi
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
mov    0x8(%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <svz_inet_ntoa@plt>
mov    -0x24(%rbp),%edx
mov    %rax,%rcx
mov    $0x4,%edi
lea    0xdd7(%rip),%rsi
mov    $0x0,%al
call   1090 <svz_log@plt>
mov    -0x18(%rbp),%rdi
call   1050 <svz_inet_ntoa@plt>
mov    %rax,%rdx
lea    0x2e0d(%rip),%rdi
lea    0xd96(%rip),%rsi
call   1070 <sprintf@plt>
lea    0x2dfa(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    12ac <dns_handle_request+0x13c>
jmp    12a4 <dns_handle_request+0x134>
mov    $0x1,%edi
lea    0xd9f(%rip),%rsi
mov    $0x0,%al
call   1090 <svz_log@plt>
movq   $0x0,-0x8(%rbp)
jmp    12ac <dns_handle_request+0x13c>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dns_handle_request>:
push   %r14
push   %rbx
push   %rax
lea    0xe85(%rip),%rsi
lea    0x2eee(%rip),%rdx
call   1070 <sscanf@plt>
cmp    $0x1,%eax
jne    1201 <dns_handle_request+0x91>
lea    0x2edd(%rip),%r14
mov    %r14,%rdi
call   1040 <gethostbyname@plt>
test   %rax,%rax
je     1219 <dns_handle_request+0xa9>
mov    %rax,%rbx
mov    (%rax),%rax
mov    0x2e3b(%rip),%rcx
cmp    (%rcx),%rax
jne    1248 <dns_handle_request+0xd8>
mov    0x10(%rbx),%rax
mov    (%rax),%esi
mov    0xc(%rbx),%edx
mov    %rsp,%rdi
call   1050 <memcpy@plt>
mov    0x8(%rbx),%ebx
mov    (%rsp),%rdi
call   1060 <svz_inet_ntoa@plt>
lea    0xe46(%rip),%rsi
mov    $0x4,%edi
mov    %ebx,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1090 <svz_log@plt>
mov    (%rsp),%rdi
call   1060 <svz_inet_ntoa@plt>
mov    %r14,%rdi
mov    %rax,%rsi
call   1030 <strcpy@plt>
jmp    124b <dns_handle_request+0xdb>
lea    0xe27(%rip),%rsi
xor    %r14d,%r14d
mov    $0x1,%edi
xor    %eax,%eax
call   1090 <svz_log@plt>
jmp    124b <dns_handle_request+0xdb>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdd5(%rip),%rsi
lea    0x2e3b(%rip),%rcx
xor    %r14d,%r14d
mov    $0x1,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1090 <svz_log@plt>
jmp    124b <dns_handle_request+0xdb>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.hostent = type { i64, i32, i32, i32* }
@dns_handle_request.resolved = internal global [256 x i8] zeroinitializer
@.str = [3 x i8] c"%s\00"
@.str.1 = [29 x i8] c"dns: gethostbyname: %s (%s)\0A\00"
@errno = global i32 0
@AF_INET = global i64 0
@.str.2 = [15 x i8] c"dns: %s is %s\0A\00"
@.str.3 = [21 x i8] c"dns: protocol error\0A\00"
define i8* @dns_handle_request(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca %struct.hostent*
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = call i32 @sscanf(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([256 x i8], [256 x i8]* @dns_handle_request.resolved, i64 0, i64 0))
%8 = icmp eq i32 1, %7
br i1 %8, label %9, label %42
9: ; preds = %1
%10 = call %struct.hostent* @gethostbyname(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @dns_handle_request.resolved, i64 0, i64 0))
store %struct.hostent* %10, %struct.hostent** %5
%11 = icmp eq %struct.hostent* %10, null
br i1 %11, label %12, label %16
12: ; preds = %9
%13 = load i32, i32* @errno
%14 = call i32 @strerror(i32 %13)
%15 = call i32 (i32, i8*, ...) @svz_log(i32 1, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %14, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @dns_handle_request.resolved, i64 0, i64 0))
store i8* null, i8** %2
br label %45
16: ; preds = %9
%17 = load %struct.hostent*, %struct.hostent** %5
%18 = getelementptr inbounds %struct.hostent, %struct.hostent* %17, i32 0, i32 0
%19 = load i64, i64* %18
%20 = load i64, i64* @AF_INET
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %41
22: ; preds = %16
%23 = load %struct.hostent*, %struct.hostent** %5
%24 = getelementptr inbounds %struct.hostent, %struct.hostent* %23, i32 0, i32 3
%25 = load i32*, i32** %24
%26 = getelementptr inbounds i32, i32* %25, i64 0
%27 = load i32, i32* %26
%28 = load %struct.hostent*, %struct.hostent** %5
%29 = getelementptr inbounds %struct.hostent, %struct.hostent* %28, i32 0, i32 2
%30 = load i32, i32* %29
%31 = call i32 @memcpy(i64* %4, i32 %27, i32 %30)
%32 = load %struct.hostent*, %struct.hostent** %5
%33 = getelementptr inbounds %struct.hostent, %struct.hostent* %32, i32 0, i32 1
%34 = load i32, i32* %33
%35 = load i64, i64* %4
%36 = call i8* @svz_inet_ntoa(i64 %35)
%37 = call i32 (i32, i8*, ...) @svz_log(i32 4, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %34, i8* %36)
%38 = load i64, i64* %4
%39 = call i8* @svz_inet_ntoa(i64 %38)
%40 = call i32 @sprintf(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @dns_handle_request.resolved, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %39)
store i8* getelementptr inbounds ([256 x i8], [256 x i8]* @dns_handle_request.resolved, i64 0, i64 0), i8** %2
br label %45
41: ; preds = %16
br label %44
42: ; preds = %1
%43 = call i32 (i32, i8*, ...) @svz_log(i32 1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
store i8* null, i8** %2
br label %45
44: ; preds = %41
store i8* null, i8** %2
br label %45
45: ; preds = %44, %42, %22, %12
%46 = load i8*, i8** %2
ret i8* %46
}

LLVM IR (-O2):
%struct.hostent = type { i64, i32, i32, i32* }
@dns_handle_request.resolved = internal global [256 x i8] zeroinitializer
@.str = [3 x i8] c"%s\00"
@.str.1 = [29 x i8] c"dns: gethostbyname: %s (%s)\0A\00"
@errno = global i32 0
@AF_INET = global i64 0
@.str.2 = [15 x i8] c"dns: %s is %s\0A\00"
@.str.3 = [21 x i8] c"dns: protocol error\0A\00"
define i8* @dns_handle_request(i8* nocapture %0) {
%2 = alloca i64
%3 = bitcast i64* %2 to i8*
%4 = tail call i32 @sscanf(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([256 x i8], [256 x i8]* @dns_handle_request.resolved, i64 0, i64 0))
%5 = icmp eq i32 %4, 1
br i1 %5, label %6, label %33
6: ; preds = %1
%7 = tail call %struct.hostent* @gethostbyname(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @dns_handle_request.resolved, i64 0, i64 0))
%8 = icmp eq %struct.hostent* %7, null
br i1 %8, label %9, label %13
9: ; preds = %6
%10 = load i32, i32* @errno
%11 = tail call i32 @strerror(i32 %10)
%12 = tail call i32 (i32, i8*, ...) @svz_log(i32 1, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %11, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @dns_handle_request.resolved, i64 0, i64 0))
br label %35
13: ; preds = %6
%14 = getelementptr inbounds %struct.hostent, %struct.hostent* %7, i64 0, i32 0
%15 = load i64, i64* %14
%16 = load i64, i64* @AF_INET
%17 = icmp eq i64 %15, %16
br i1 %17, label %18, label %35
18: ; preds = %13
%19 = getelementptr inbounds %struct.hostent, %struct.hostent* %7, i64 0, i32 3
%20 = load i32*, i32** %19
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.hostent, %struct.hostent* %7, i64 0, i32 2
%23 = load i32, i32* %22
%24 = call i32 @memcpy(i64* %2, i32 %21, i32 %23)
%25 = getelementptr inbounds %struct.hostent, %struct.hostent* %7, i64 0, i32 1
%26 = load i32, i32* %25
%27 = load i64, i64* %2
%28 = call i8* @svz_inet_ntoa(i64 %27)
%29 = call i32 (i32, i8*, ...) @svz_log(i32 4, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %26, i8* %28)
%30 = load i64, i64* %2
%31 = call i8* @svz_inet_ntoa(i64 %30)
%32 = call i8* @strcpy(i8* dereferenceable(1) getelementptr inbounds ([256 x i8], [256 x i8]* @dns_handle_request.resolved, i64 0, i64 0), i8* dereferenceable(1) %31)
br label %35
33: ; preds = %1
%34 = tail call i32 (i32, i8*, ...) @svz_log(i32 1, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %35
35: ; preds = %13, %33, %18, %9
%36 = phi i8* [ null, %9 ], [ getelementptr inbounds ([256 x i8], [256 x i8]* @dns_handle_request.resolved, i64 0, i64 0), %18 ], [ null, %33 ], [ null, %13 ]
ret i8* %36
}