#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 sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sa;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
char *gai_strerror(int);
int getnameinfo(struct sockaddr *, int, char *, int, int /*<<< orphan*/ *,
                int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ stderr;
int rdns_lookup(char *ip_addr, char *hostname, int hostname_size) {
  int ret;
  struct sockaddr_in sa;
  memset(&sa, 0, sizeof(sa));
  sa.sin_family = AF_INET;
  inet_pton(AF_INET, ip_addr, &sa.sin_addr);
  if ((ret = getnameinfo((struct sockaddr *)&sa, sizeof(sa), hostname,
                         hostname_size, NULL, 0, 0)) != 0) {
    fprintf(stderr, "getnameinfo(): %s\n", gai_strerror(ret));
  }
  return (ret);
}


x86 assembly (clang, -O0):
<rdns_lookup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e6e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1070 <inet_pton@plt>
lea    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
mov    $0x8,%esi
xor    %eax,%eax
mov    %eax,%r8d
xor    %r9d,%r9d
movl   $0x0,(%rsp)
call   1050 <getnameinfo@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
je     11eb <rdns_lookup+0x9b>
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    -0x24(%rbp),%edi
mov    %rax,%rdx
lea    0xe1a(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rdns_lookup>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbp
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    0x2e6a(%rip),%rax
mov    (%rax),%edi
mov    %edi,0x14(%rsp)
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1070 <inet_pton@plt>
movl   $0x0,(%rsp)
mov    %rbx,%rdi
mov    $0x8,%esi
mov    %r15,%rdx
mov    %r14d,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1050 <getnameinfo@plt>
mov    %eax,%ebx
test   %eax,%eax
je     11d6 <rdns_lookup+0x86>
mov    0x2e1c(%rip),%rax
mov    (%rax),%ebp
mov    %ebx,%edi
call   1030 <gai_strerror@plt>
lea    0xe34(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@stderr = global i32 0
@.str = [19 x i8] c"getnameinfo(): %s\0A\00"
define i32 @rdns_lookup(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = call i32 @memset(%struct.sockaddr_in* %8, i32 0, i32 8)
%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 i32, i32* @AF_INET
%13 = load i8*, i8** %4
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%15 = call i32 @inet_pton(i32 %12, i8* %13, i32* %14)
%16 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%17 = load i8*, i8** %5
%18 = load i32, i32* %6
%19 = call i32 @getnameinfo(%struct.sockaddr* %16, i32 8, i8* %17, i32 %18, i32* null, i32 0, i32 0)
store i32 %19, i32* %7
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %3
%22 = load i32, i32* @stderr
%23 = load i32, i32* %7
%24 = call i8* @gai_strerror(i32 %23)
%25 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %24)
br label %26
26: ; preds = %21, %3
%27 = load i32, i32* %7
ret i32 %27
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@stderr = global i32 0
@.str = [19 x i8] c"getnameinfo(): %s\0A\00"
define i32 @rdns_lookup(i8* %0, i8* %1, i32 %2) {
%4 = alloca %struct.sockaddr_in
%5 = bitcast %struct.sockaddr_in* %4 to i8*
%6 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 8)
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %7, i32* %8
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%10 = call i32 @inet_pton(i32 %7, i8* %0, i32* %9)
%11 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%12 = call i32 @getnameinfo(%struct.sockaddr* %11, i32 8, i8* %1, i32 %2, i32* null, i32 0, i32 0)
%13 = icmp eq i32 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %3
%15 = load i32, i32* @stderr
%16 = call i8* @gai_strerror(i32 %12)
%17 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %16)
br label %18
18: ; preds = %14, %3
ret i32 %12
}