#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 {
  int sa_family;
};

/* Variables and functions */
int /*<<< orphan*/ INET6_ADDRSTRLEN;
#define PF_INET 129
#define PF_INET6 128
int /*<<< orphan*/ inet_ntop(int, int /*<<< orphan*/ *, char *,
                             int /*<<< orphan*/);
char *af_sa_to_str(const struct sockaddr *sa, char dst[INET6_ADDRSTRLEN]) {
  dst[0] = 0;
  switch (sa->sa_family) {
  case PF_INET:
    inet_ntop(sa->sa_family, &((struct sockaddr_in *)(sa))->sin_addr, dst,
              INET6_ADDRSTRLEN);
    break;
  case PF_INET6:
    inet_ntop(sa->sa_family, &((struct sockaddr_in6 *)(sa))->sin6_addr, dst,
              INET6_ADDRSTRLEN);
    break;
  default:
    return (NULL);
  }
  return (dst);
}


x86 assembly (clang, -O0):
<af_sa_to_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
sub    $0x80,%eax
je     1174 <af_sa_to_str+0x64>
jmp    1140 <af_sa_to_str+0x30>
mov    -0x1c(%rbp),%eax
sub    $0x81,%eax
jne    1195 <af_sa_to_str+0x85>
jmp    1153 <af_sa_to_str+0x43>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    0x2e80(%rip),%rax
mov    (%rax),%ecx
call   1030 <inet_ntop@plt>
jmp    11a2 <af_sa_to_str+0x92>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    0x2e5f(%rip),%rax
mov    (%rax),%ecx
call   1030 <inet_ntop@plt>
jmp    11a2 <af_sa_to_str+0x92>
movq   $0x0,-0x8(%rbp)
jmp    11aa <af_sa_to_str+0x9a>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<af_sa_to_str>:
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%rsi
movb   $0x0,(%rbx)
mov    (%rdi),%edi
cmp    $0x81,%edi
je     1131 <af_sa_to_str+0x21>
cmp    $0x80,%edi
jne    1144 <af_sa_to_str+0x34>
mov    $0x80,%edi
mov    0x2eb0(%rip),%rax
mov    (%rax),%ecx
mov    %rbx,%rdx
call   1030 <inet_ntop@plt>
jmp    1146 <af_sa_to_str+0x36>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
@INET6_ADDRSTRLEN = global i32 0
define i8* @af_sa_to_str(%struct.sockaddr* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca %struct.sockaddr*
%5 = alloca i8*
store %struct.sockaddr* %0, %struct.sockaddr** %4
store i8* %1, i8** %5
%6 = load i32, i32* @INET6_ADDRSTRLEN
%7 = zext i32 %6 to i64
%8 = load i8*, i8** %5
%9 = getelementptr inbounds i8, i8* %8, i64 0
store i8 0, i8* %9
%10 = load %struct.sockaddr*, %struct.sockaddr** %4
%11 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %10, i32 0, i32 0
%12 = load i32, i32* %11
switch i32 %12, label %33 [
i32 129, label %13
i32 128, label %23
]
13: ; preds = %2
%14 = load %struct.sockaddr*, %struct.sockaddr** %4
%15 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %14, i32 0, i32 0
%16 = load i32, i32* %15
%17 = load %struct.sockaddr*, %struct.sockaddr** %4
%18 = bitcast %struct.sockaddr* %17 to %struct.sockaddr_in*
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %18, i32 0, i32 0
%20 = load i8*, i8** %5
%21 = load i32, i32* @INET6_ADDRSTRLEN
%22 = call i32 @inet_ntop(i32 %16, i32* %19, i8* %20, i32 %21)
br label %34
23: ; preds = %2
%24 = load %struct.sockaddr*, %struct.sockaddr** %4
%25 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %24, i32 0, i32 0
%26 = load i32, i32* %25
%27 = load %struct.sockaddr*, %struct.sockaddr** %4
%28 = bitcast %struct.sockaddr* %27 to %struct.sockaddr_in6*
%29 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %28, i32 0, i32 0
%30 = load i8*, i8** %5
%31 = load i32, i32* @INET6_ADDRSTRLEN
%32 = call i32 @inet_ntop(i32 %26, i32* %29, i8* %30, i32 %31)
br label %34
33: ; preds = %2
store i8* null, i8** %3
br label %36
34: ; preds = %23, %13
%35 = load i8*, i8** %5
store i8* %35, i8** %3
br label %36
36: ; preds = %34, %33
%37 = load i8*, i8** %3
ret i8* %37
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@INET6_ADDRSTRLEN = global i32 0
define i8* @af_sa_to_str(%struct.sockaddr* %0, i8* %1) {
store i8 0, i8* %1
%3 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 0, i32 0
%4 = load i32, i32* %3
switch i32 %4, label %10 [
i32 129, label %6
i32 128, label %5
]
5: ; preds = %2
br label %6
6: ; preds = %2, %5
%7 = phi i32 [ 128, %5 ], [ %4, %2 ]
%8 = load i32, i32* @INET6_ADDRSTRLEN
%9 = tail call i32 @inet_ntop(i32 %7, i32* %3, i8* %1, i32 %8)
br label %10
10: ; preds = %6, %2
%11 = phi i8* [ null, %2 ], [ %1, %6 ]
ret i8* %11
}