#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 dummy;
};
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int sa_family;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ socklen_t;

/* Variables and functions */
#define AF_INET 129
#define AF_INET6 128
int /*<<< orphan*/ EAFNOSUPPORT;
int /*<<< orphan*/ errno;
int sancus_socket_ntop_ipv4(char *, size_t, struct sockaddr_in *);
int sancus_socket_ntop_ipv6(char *, size_t, struct sockaddr_in6 *);
ssize_t sancus_socket_ntop(char *str, size_t size, const struct sockaddr *sa,
                           socklen_t UNUSED_sa_len __attribute__((unused))) {
  switch (sa->sa_family) {
  case AF_INET:
    return sancus_socket_ntop_ipv4(str, size, (struct sockaddr_in *)sa);
  case AF_INET6:
    return sancus_socket_ntop_ipv6(str, size, (struct sockaddr_in6 *)sa);
  default:
    errno = EAFNOSUPPORT;
    return -1;
  }
}


x86 assembly (clang, -O0):
<sancus_socket_ntop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
sub    $0x80,%eax
je     117c <sancus_socket_ntop+0x5c>
jmp    1150 <sancus_socket_ntop+0x30>
mov    -0x28(%rbp),%eax
sub    $0x81,%eax
jne    1195 <sancus_socket_ntop+0x75>
jmp    1163 <sancus_socket_ntop+0x43>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <sancus_socket_ntop_ipv4@plt>
mov    %eax,-0x4(%rbp)
jmp    11ae <sancus_socket_ntop+0x8e>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <sancus_socket_ntop_ipv6@plt>
mov    %eax,-0x4(%rbp)
jmp    11ae <sancus_socket_ntop+0x8e>
mov    0x2e3c(%rip),%rax
mov    (%rax),%ecx
mov    0x2e3b(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sancus_socket_ntop>:
mov    (%rdx),%eax
cmp    $0x80,%eax
je     1135 <sancus_socket_ntop+0x15>
cmp    $0x81,%eax
jne    113a <sancus_socket_ntop+0x1a>
jmp    1030 <sancus_socket_ntop_ipv4@plt>
jmp    1040 <sancus_socket_ntop_ipv6@plt>
mov    0x2e97(%rip),%rax
mov    (%rax),%eax
mov    0x2e96(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
@EAFNOSUPPORT = global i32 0
@errno = global i32 0
define i32 @sancus_socket_ntop(i8* %0, i64 %1, %struct.sockaddr* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca %struct.sockaddr*
%9 = alloca i32
store i8* %0, i8** %6
store i64 %1, i64* %7
store %struct.sockaddr* %2, %struct.sockaddr** %8
store i32 %3, i32* %9
%10 = load %struct.sockaddr*, %struct.sockaddr** %8
%11 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %10, i32 0, i32 0
%12 = load i32, i32* %11
switch i32 %12, label %25 [
i32 129, label %13
i32 128, label %19
]
13: ; preds = %4
%14 = load i8*, i8** %6
%15 = load i64, i64* %7
%16 = load %struct.sockaddr*, %struct.sockaddr** %8
%17 = bitcast %struct.sockaddr* %16 to %struct.sockaddr_in*
%18 = call i32 @sancus_socket_ntop_ipv4(i8* %14, i64 %15, %struct.sockaddr_in* %17)
store i32 %18, i32* %5
br label %27
19: ; preds = %4
%20 = load i8*, i8** %6
%21 = load i64, i64* %7
%22 = load %struct.sockaddr*, %struct.sockaddr** %8
%23 = bitcast %struct.sockaddr* %22 to %struct.sockaddr_in6*
%24 = call i32 @sancus_socket_ntop_ipv6(i8* %20, i64 %21, %struct.sockaddr_in6* %23)
store i32 %24, i32* %5
br label %27
25: ; preds = %4
%26 = load i32, i32* @EAFNOSUPPORT
store i32 %26, i32* @errno
store i32 -1, i32* %5
br label %27
27: ; preds = %25, %19, %13
%28 = load i32, i32* %5
ret i32 %28
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
@EAFNOSUPPORT = global i32 0
@errno = global i32 0
define i32 @sancus_socket_ntop(i8* %0, i64 %1, %struct.sockaddr* %2, i32 %3) {
%5 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %2, i64 0, i32 0
%6 = load i32, i32* %5
switch i32 %6, label %13 [
i32 129, label %7
i32 128, label %10
]
7: ; preds = %4
%8 = bitcast %struct.sockaddr* %2 to %struct.sockaddr_in*
%9 = tail call i32 @sancus_socket_ntop_ipv4(i8* %0, i64 %1, %struct.sockaddr_in* %8)
br label %15
10: ; preds = %4
%11 = bitcast %struct.sockaddr* %2 to %struct.sockaddr_in6*
%12 = tail call i32 @sancus_socket_ntop_ipv6(i8* %0, i64 %1, %struct.sockaddr_in6* %11)
br label %15
13: ; preds = %4
%14 = load i32, i32* @EAFNOSUPPORT
store i32 %14, i32* @errno
br label %15
15: ; preds = %13, %10, %7
%16 = phi i32 [ -1, %13 ], [ %12, %10 ], [ %9, %7 ]
ret i32 %16
}