#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_in6 {
  int /*<<< orphan*/ sin6_port;
  int /*<<< orphan*/ sin6_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  scalar_t__ sa_family;
};
typedef int /*<<< orphan*/ socklen_t;

/* Variables and functions */
scalar_t__ AF_INET;
int /*<<< orphan*/ inet_ntop(scalar_t__, void *, char *, int /*<<< orphan*/);
int ntohs(int /*<<< orphan*/);
int get_addr_name(struct sockaddr *sa, char *dst, socklen_t dst_size) {
  void *addr;
  int port;
  if (sa->sa_family == AF_INET) {
    struct sockaddr_in *sav4 = (struct sockaddr_in *)sa;
    addr = &(sav4->sin_addr);
    port = ntohs(sav4->sin_port);
  } else {
    struct sockaddr_in6 *sav6 = (struct sockaddr_in6 *)sa;
    addr = &(sav6->sin6_addr);
    port = ntohs(sav6->sin6_port);
  }
  if (!inet_ntop(sa->sa_family, addr, dst, dst_size))
    return -1;
  return port;
}


x86 assembly (clang, -O0):
<get_addr_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2ea7(%rip),%rcx
cmp    (%rcx),%rax
jne    1171 <get_addr_name+0x51>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x28(%rbp)
mov    -0x38(%rbp),%rax
mov    (%rax),%edi
call   1040 <ntohs@plt>
mov    %eax,-0x2c(%rbp)
jmp    1193 <get_addr_name+0x73>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x28(%rbp)
mov    -0x40(%rbp),%rax
mov    (%rax),%edi
call   1040 <ntohs@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
call   1030 <inet_ntop@plt>
cmp    $0x0,%eax
jne    11bf <get_addr_name+0x9f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c5 <get_addr_name+0xa5>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_addr_name>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbp
mov    (%rdi),%edi
lea    0x4(%rbp),%rbx
call   1040 <ntohs@plt>
mov    %eax,%r12d
mov    0x0(%rbp),%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    %r14d,%ecx
call   1030 <inet_ntop@plt>
cmp    $0x1,%eax
sbb    %eax,%eax
or     %r12d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr = type { i64 }
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr_in6 = type { i32, i32 }
@AF_INET = global i64 0
define i32 @get_addr_name(%struct.sockaddr* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.sockaddr*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca %struct.sockaddr_in*
%11 = alloca %struct.sockaddr_in6*
store %struct.sockaddr* %0, %struct.sockaddr** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%12 = load %struct.sockaddr*, %struct.sockaddr** %5
%13 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %12, i32 0, i32 0
%14 = load i64, i64* %13
%15 = load i64, i64* @AF_INET
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %27
17: ; preds = %3
%18 = load %struct.sockaddr*, %struct.sockaddr** %5
%19 = bitcast %struct.sockaddr* %18 to %struct.sockaddr_in*
store %struct.sockaddr_in* %19, %struct.sockaddr_in** %10
%20 = load %struct.sockaddr_in*, %struct.sockaddr_in** %10
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %20, i32 0, i32 1
%22 = bitcast i32* %21 to i8*
store i8* %22, i8** %8
%23 = load %struct.sockaddr_in*, %struct.sockaddr_in** %10
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %23, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @ntohs(i32 %25)
store i32 %26, i32* %9
br label %37
27: ; preds = %3
%28 = load %struct.sockaddr*, %struct.sockaddr** %5
%29 = bitcast %struct.sockaddr* %28 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %29, %struct.sockaddr_in6** %11
%30 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %11
%31 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %30, i32 0, i32 1
%32 = bitcast i32* %31 to i8*
store i8* %32, i8** %8
%33 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %11
%34 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %33, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @ntohs(i32 %35)
store i32 %36, i32* %9
br label %37
37: ; preds = %27, %17
%38 = load %struct.sockaddr*, %struct.sockaddr** %5
%39 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %38, i32 0, i32 0
%40 = load i64, i64* %39
%41 = load i8*, i8** %8
%42 = load i8*, i8** %6
%43 = load i32, i32* %7
%44 = call i32 @inet_ntop(i64 %40, i8* %41, i8* %42, i32 %43)
%45 = icmp ne i32 %44, 0
br i1 %45, label %47, label %46
46: ; preds = %37
store i32 -1, i32* %4
br label %49
47: ; preds = %37
%48 = load i32, i32* %9
store i32 %48, i32* %4
br label %49
49: ; preds = %47, %46
%50 = load i32, i32* %4
ret i32 %50
}

LLVM IR (-O2):
%struct.sockaddr = type { i64 }
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr_in6 = type { i32, i32 }
@AF_INET = global i64 0
define i32 @get_addr_name(%struct.sockaddr* %0, i8* %1, i32 %2) {
%4 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 0, i32 0
%5 = load i64, i64* %4
%6 = load i64, i64* @AF_INET
%7 = icmp eq i64 %5, %6
%8 = trunc i64 %5 to i32
%9 = bitcast %struct.sockaddr* %0 to %struct.sockaddr_in*
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i64 0, i32 1
%11 = bitcast %struct.sockaddr* %0 to %struct.sockaddr_in6*
%12 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %11, i64 0, i32 1
%13 = select i1 %7, i32* %10, i32* %12
%14 = tail call i32 @ntohs(i32 %8)
%15 = bitcast i32* %13 to i8*
%16 = load i64, i64* %4
%17 = tail call i32 @inet_ntop(i64 %16, i8* %15, i8* %1, i32 %2)
%18 = icmp eq i32 %17, 0
%19 = select i1 %18, i32 -1, i32 %14
ret i32 %19
}