#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_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  scalar_t__ sa_family;
};

/* Variables and functions */
scalar_t__ AF_INET;
int /*<<< orphan*/ inet_ntop(scalar_t__, struct sockaddr *, char *, size_t);
int get_addr_str(char *buf, size_t len, struct sockaddr *sa) {
  struct sockaddr *addr;
  if (sa->sa_family == AF_INET) {
    addr = (struct sockaddr *)&(((struct sockaddr_in *)sa)->sin_addr);
  } else {
    addr = (struct sockaddr *)&(((struct sockaddr_in6 *)sa)->sin6_addr);
  }
  inet_ntop(sa->sa_family, addr, buf, len);
  return 0;
}


x86 assembly (clang, -O0):
<get_addr_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    0x2eb6(%rip),%rcx
cmp    (%rcx),%rax
jne    1148 <get_addr_str+0x38>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    1150 <get_addr_str+0x40>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
call   1030 <inet_ntop@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_addr_str>:
push   %rax
mov    %rsi,%rcx
mov    %rdi,%rax
mov    (%rdx),%rdi
mov    %rdx,%rsi
mov    %rax,%rdx
call   1030 <inet_ntop@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
%struct.sockaddr = type { i64 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
@AF_INET = global i64 0
define i32 @get_addr_str(i8* %0, i64 %1, %struct.sockaddr* %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca %struct.sockaddr*
%7 = alloca %struct.sockaddr*
store i8* %0, i8** %4
store i64 %1, i64* %5
store %struct.sockaddr* %2, %struct.sockaddr** %6
%8 = load %struct.sockaddr*, %struct.sockaddr** %6
%9 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %8, i32 0, i32 0
%10 = load i64, i64* %9
%11 = load i64, i64* @AF_INET
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %18
13: ; preds = %3
%14 = load %struct.sockaddr*, %struct.sockaddr** %6
%15 = bitcast %struct.sockaddr* %14 to %struct.sockaddr_in*
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %15, i32 0, i32 0
%17 = bitcast i32* %16 to %struct.sockaddr*
store %struct.sockaddr* %17, %struct.sockaddr** %7
br label %23
18: ; preds = %3
%19 = load %struct.sockaddr*, %struct.sockaddr** %6
%20 = bitcast %struct.sockaddr* %19 to %struct.sockaddr_in6*
%21 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %20, i32 0, i32 0
%22 = bitcast i32* %21 to %struct.sockaddr*
store %struct.sockaddr* %22, %struct.sockaddr** %7
br label %23
23: ; preds = %18, %13
%24 = load %struct.sockaddr*, %struct.sockaddr** %6
%25 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %24, i32 0, i32 0
%26 = load i64, i64* %25
%27 = load %struct.sockaddr*, %struct.sockaddr** %7
%28 = load i8*, i8** %4
%29 = load i64, i64* %5
%30 = call i32 @inet_ntop(i64 %26, %struct.sockaddr* %27, i8* %28, i64 %29)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr = type { i64 }
@AF_INET = global i64 0
define i32 @get_addr_str(i8* %0, i64 %1, %struct.sockaddr* %2) {
%4 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %2, i64 0, i32 0
%5 = load i64, i64* %4
%6 = tail call i32 @inet_ntop(i64 %5, %struct.sockaddr* %2, i8* %0, i64 %1)
ret i32 0
}