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

/* Variables and functions */
int NI_NAMEREQD;
char *alloc_string(char *);
char *gai_strerror(int);
int getnameinfo(struct sockaddr const *, int /*<<< orphan*/, char *, int,
                int /*<<< orphan*/ *, int /*<<< orphan*/, int);
char *reverse_host(const struct sockaddr *a, socklen_t length) {
  char h[256];
  int flags, st;
  flags = NI_NAMEREQD;
  st = getnameinfo(a, length, h, 256, NULL, 0, flags);
  if (!st)
    return alloc_string(h);
  return alloc_string(gai_strerror(st));
}


x86 assembly (clang, -O0):
<reverse_host>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2ea7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x124(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
lea    -0x120(%rbp),%rdx
mov    -0x124(%rbp),%eax
mov    $0x100,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
mov    %eax,(%rsp)
call   1040 <getnameinfo@plt>
mov    %eax,-0x128(%rbp)
cmpl   $0x0,-0x128(%rbp)
jne    11a0 <reverse_host+0x70>
lea    -0x120(%rbp),%rdi
call   1050 <alloc_string@plt>
mov    %rax,-0x8(%rbp)
jmp    11b7 <reverse_host+0x87>
mov    -0x128(%rbp),%edi
call   1030 <gai_strerror@plt>
mov    %rax,%rdi
call   1050 <alloc_string@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<reverse_host>:
sub    $0x118,%rsp
mov    0x2eb2(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
lea    0x10(%rsp),%rdx
mov    $0x100,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1040 <getnameinfo@plt>
test   %eax,%eax
je     1168 <reverse_host+0x38>
mov    %eax,%edi
call   1030 <gai_strerror@plt>
mov    %rax,%rdi
jmp    116d <reverse_host+0x3d>
lea    0x10(%rsp),%rdi
call   1050 <alloc_string@plt>
add    $0x118,%rsp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@NI_NAMEREQD = global i32 0
define i8* @reverse_host(%struct.sockaddr* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca %struct.sockaddr*
%5 = alloca i32
%6 = alloca [256 x i8]
%7 = alloca i32
%8 = alloca i32
store %struct.sockaddr* %0, %struct.sockaddr** %4
store i32 %1, i32* %5
%9 = load i32, i32* @NI_NAMEREQD
store i32 %9, i32* %7
%10 = load %struct.sockaddr*, %struct.sockaddr** %4
%11 = load i32, i32* %5
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%13 = load i32, i32* %7
%14 = call i32 @getnameinfo(%struct.sockaddr* %10, i32 %11, i8* %12, i32 256, i32* null, i32 0, i32 %13)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = icmp ne i32 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %2
%18 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%19 = call i8* @alloc_string(i8* %18)
store i8* %19, i8** %3
br label %24
20: ; preds = %2
%21 = load i32, i32* %8
%22 = call i8* @gai_strerror(i32 %21)
%23 = call i8* @alloc_string(i8* %22)
store i8* %23, i8** %3
br label %24
24: ; preds = %20, %17
%25 = load i8*, i8** %3
ret i8* %25
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@NI_NAMEREQD = global i32 0
define i8* @reverse_host(%struct.sockaddr* %0, i32 %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @NI_NAMEREQD
%6 = call i32 @getnameinfo(%struct.sockaddr* %0, i32 %1, i8* %4, i32 256, i32* null, i32 0, i32 %5)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = call i8* @alloc_string(i8* %4)
br label %13
10: ; preds = %2
%11 = call i8* @gai_strerror(i32 %6)
%12 = call i8* @alloc_string(i8* %11)
br label %13
13: ; preds = %10, %8
%14 = phi i8* [ %12, %10 ], [ %9, %8 ]
ret i8* %14
}