#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 xaddr {
  int dummy;
};
struct sockaddr_storage {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ ss;
typedef int socklen_t;

/* Variables and functions */
int /*<<< orphan*/ NI_NUMERICHOST;
int addr_xaddr_to_sa(struct xaddr const *, struct sockaddr *, int *,
                     int /*<<< orphan*/);
int getnameinfo(struct sockaddr *, int, char *, size_t, int /*<<< orphan*/ *,
                int /*<<< orphan*/, int /*<<< orphan*/);
int addr_ntop(const struct xaddr *n, char *p, size_t len) {
  struct sockaddr_storage ss;
  socklen_t slen = sizeof(ss);
  if (addr_xaddr_to_sa(n, ((struct sockaddr *)(&ss)), &slen, 0) == -1)
    return (-1);
  if (n == NULL || p == NULL || len == 0)
    return (-1);
  if (getnameinfo(((struct sockaddr *)(&ss)), slen, p, len, NULL, 0,
                  NI_NUMERICHOST) == -1)
    return (-1);
  return (0);
}


x86 assembly (clang, -O0):
<addr_ntop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x4,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
xor    %ecx,%ecx
call   1040 <addr_xaddr_to_sa@plt>
cmp    $0xffffffff,%eax
jne    1163 <addr_ntop+0x43>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d2 <addr_ntop+0xb2>
cmpq   $0x0,-0x10(%rbp)
je     1184 <addr_ntop+0x64>
cmpq   $0x0,-0x18(%rbp)
je     1184 <addr_ntop+0x64>
cmpq   $0x0,-0x20(%rbp)
jne    1190 <addr_ntop+0x70>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d2 <addr_ntop+0xb2>
lea    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
xor    %r8d,%r8d
xor    %r9d,%r9d
mov    %eax,(%rsp)
call   1030 <getnameinfo@plt>
cmp    $0xffffffff,%eax
jne    11cb <addr_ntop+0xab>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d2 <addr_ntop+0xb2>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<addr_ntop>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
movl   $0x4,0x14(%rsp)
lea    0x18(%rsp),%rsi
lea    0x14(%rsp),%rdx
xor    %ecx,%ecx
call   1040 <addr_xaddr_to_sa@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    $0xffffffff,%ecx
je     1198 <addr_ntop+0x78>
test   %rbx,%rbx
je     1198 <addr_ntop+0x78>
test   %r15,%r15
je     1198 <addr_ntop+0x78>
test   %r14,%r14
je     1198 <addr_ntop+0x78>
mov    0x14(%rsp),%esi
mov    0x2e6f(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
lea    0x18(%rsp),%rdi
mov    %r15,%rdx
mov    %r14,%rcx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1030 <getnameinfo@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
sete   %al
neg    %eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.xaddr = type { i32 }
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@NI_NUMERICHOST = global i32 0
define i32 @addr_ntop(%struct.xaddr* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca %struct.xaddr*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca %struct.sockaddr_storage
%9 = alloca i32
store %struct.xaddr* %0, %struct.xaddr** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 4, i32* %9
%10 = load %struct.xaddr*, %struct.xaddr** %5
%11 = bitcast %struct.sockaddr_storage* %8 to %struct.sockaddr*
%12 = call i32 @addr_xaddr_to_sa(%struct.xaddr* %10, %struct.sockaddr* %11, i32* %9, i32 0)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 -1, i32* %4
br label %35
15: ; preds = %3
%16 = load %struct.xaddr*, %struct.xaddr** %5
%17 = icmp eq %struct.xaddr* %16, null
br i1 %17, label %24, label %18
18: ; preds = %15
%19 = load i8*, i8** %6
%20 = icmp eq i8* %19, null
br i1 %20, label %24, label %21
21: ; preds = %18
%22 = load i64, i64* %7
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %21, %18, %15
store i32 -1, i32* %4
br label %35
25: ; preds = %21
%26 = bitcast %struct.sockaddr_storage* %8 to %struct.sockaddr*
%27 = load i32, i32* %9
%28 = load i8*, i8** %6
%29 = load i64, i64* %7
%30 = load i32, i32* @NI_NUMERICHOST
%31 = call i32 @getnameinfo(%struct.sockaddr* %26, i32 %27, i8* %28, i64 %29, i32* null, i32 0, i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %34
33: ; preds = %25
store i32 -1, i32* %4
br label %35
34: ; preds = %25
store i32 0, i32* %4
br label %35
35: ; preds = %34, %33, %24, %14
%36 = load i32, i32* %4
ret i32 %36
}

LLVM IR (-O2):
%struct.xaddr = type { i32 }
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@NI_NUMERICHOST = global i32 0
define i32 @addr_ntop(%struct.xaddr* %0, i8* %1, i64 %2) {
%4 = alloca %struct.sockaddr_storage
%5 = alloca i32
%6 = bitcast %struct.sockaddr_storage* %4 to i8*
%7 = bitcast i32* %5 to i8*
store i32 4, i32* %5
%8 = bitcast %struct.sockaddr_storage* %4 to %struct.sockaddr*
%9 = call i32 @addr_xaddr_to_sa(%struct.xaddr* %0, %struct.sockaddr* %8, i32* %5, i32 0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %23, label %11
11: ; preds = %3
%12 = icmp eq %struct.xaddr* %0, null
%13 = icmp eq i8* %1, null
%14 = or i1 %12, %13
%15 = icmp eq i64 %2, 0
%16 = or i1 %14, %15
br i1 %16, label %23, label %17
17: ; preds = %11
%18 = load i32, i32* %5
%19 = load i32, i32* @NI_NUMERICHOST
%20 = call i32 @getnameinfo(%struct.sockaddr* %8, i32 %18, i8* %1, i64 %2, i32* null, i32 0, i32 %19)
%21 = icmp eq i32 %20, -1
%22 = sext i1 %21 to i32
br label %23
23: ; preds = %17, %11, %3
%24 = phi i32 [ -1, %3 ], [ -1, %11 ], [ %22, %17 ]
ret i32 %24
}