#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 */
typedef int /*<<< orphan*/ uint8_t;
struct sockaddr_storage {
  scalar_t__ ss_family;
};
struct sockaddr_in6 {
  int /*<<< orphan*/ sin6_port;
  int /*<<< orphan*/ sin6_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
};
struct in_addr {
  int dummy;
};
struct in6_addr {
  int dummy;
};
typedef int /*<<< orphan*/ remote_data_str;
typedef int /*<<< orphan*/ local_data_str;

/* Variables and functions */
scalar_t__ AF_INET6;
int /*<<< orphan*/ aeron_format_to_hex(char *, int, int /*<<< orphan*/ *,
                                       size_t);
unsigned short ntohs(int /*<<< orphan*/);
int snprintf(char *, size_t, char *, char *, unsigned short, char *,
             unsigned short);
int aeron_uri_udp_canonicalize(char *canonical_form, size_t length,
                               struct sockaddr_storage *local_data,
                               struct sockaddr_storage *remote_data) {
  char local_data_str[((2 * (sizeof(struct sockaddr_in6))) + 1)];
  char remote_data_str[((2 * (sizeof(struct sockaddr_in6))) + 1)];
  uint8_t *local_data_addr, *remote_data_addr;
  unsigned short local_data_port = 0, remote_data_port = 0;
  size_t local_data_length = sizeof(struct in_addr),
         remote_data_length = sizeof(struct in_addr);
  if (AF_INET6 == local_data->ss_family) {
    struct sockaddr_in6 *addr = (struct sockaddr_in6 *)local_data;
    local_data_addr = (uint8_t *)&addr->sin6_addr;
    local_data_port = ntohs(addr->sin6_port);
    local_data_length = sizeof(struct in6_addr);
  } else {
    struct sockaddr_in *addr = (struct sockaddr_in *)local_data;
    local_data_addr = (uint8_t *)&addr->sin_addr;
    local_data_port = ntohs(addr->sin_port);
  }
  if (AF_INET6 == remote_data->ss_family) {
    struct sockaddr_in6 *addr = (struct sockaddr_in6 *)remote_data;
    remote_data_addr = (uint8_t *)&addr->sin6_addr;
    remote_data_port = ntohs(addr->sin6_port);
    remote_data_length = sizeof(struct in6_addr);
  } else {
    struct sockaddr_in *addr = (struct sockaddr_in *)remote_data;
    remote_data_addr = (uint8_t *)&addr->sin_addr;
    remote_data_port = ntohs(addr->sin_port);
  }
  aeron_format_to_hex(local_data_str, sizeof(local_data_str), local_data_addr,
                      local_data_length);
  aeron_format_to_hex(remote_data_str, sizeof(remote_data_str),
                      remote_data_addr, remote_data_length);
  return snprintf(canonical_form, length, "UDP-%s-%d-%s-%d", local_data_str,
                  local_data_port, remote_data_str, remote_data_port);
}


x86 assembly (clang, -O0):
<aeron_uri_udp_canonicalize>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movw   $0x0,-0x72(%rbp)
movw   $0x0,-0x74(%rbp)
movq   $0x4,-0x80(%rbp)
movq   $0x4,-0x88(%rbp)
mov    0x2e77(%rip),%rax
mov    (%rax),%rax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%rax
jne    11ba <aeron_uri_udp_canonicalize+0x8a>
mov    -0x18(%rbp),%rax
mov    %rax,-0x90(%rbp)
mov    -0x90(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x68(%rbp)
mov    -0x90(%rbp),%rax
mov    (%rax),%edi
call   1040 <ntohs@plt>
mov    %ax,-0x72(%rbp)
movq   $0x4,-0x80(%rbp)
jmp    11e6 <aeron_uri_udp_canonicalize+0xb6>
mov    -0x18(%rbp),%rax
mov    %rax,-0x98(%rbp)
mov    -0x98(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x68(%rbp)
mov    -0x98(%rbp),%rax
mov    (%rax),%edi
call   1040 <ntohs@plt>
mov    %ax,-0x72(%rbp)
mov    0x2dfb(%rip),%rax
mov    (%rax),%rax
mov    -0x20(%rbp),%rcx
cmp    (%rcx),%rax
jne    1239 <aeron_uri_udp_canonicalize+0x109>
mov    -0x20(%rbp),%rax
mov    %rax,-0xa0(%rbp)
mov    -0xa0(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x70(%rbp)
mov    -0xa0(%rbp),%rax
mov    (%rax),%edi
call   1040 <ntohs@plt>
mov    %ax,-0x74(%rbp)
movq   $0x4,-0x88(%rbp)
jmp    1265 <aeron_uri_udp_canonicalize+0x135>
mov    -0x20(%rbp),%rax
mov    %rax,-0xa8(%rbp)
mov    -0xa8(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x70(%rbp)
mov    -0xa8(%rbp),%rax
mov    (%rax),%edi
call   1040 <ntohs@plt>
mov    %ax,-0x74(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x68(%rbp),%rdx
mov    -0x80(%rbp),%rcx
mov    $0x11,%esi
call   1050 <aeron_format_to_hex@plt>
lea    -0x60(%rbp),%rdi
mov    -0x70(%rbp),%rdx
mov    -0x88(%rbp),%rcx
mov    $0x11,%esi
call   1050 <aeron_format_to_hex@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x40(%rbp),%rcx
mov    -0x72(%rbp),%ax
lea    -0x60(%rbp),%r9
lea    0xd51(%rip),%rdx
movzwl %ax,%r8d
movzwl -0x74(%rbp),%eax
mov    %eax,(%rsp)
call   1030 <snprintf@plt>
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<aeron_uri_udp_canonicalize>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %rcx,%rbx
mov    %rdx,%rbp
mov    %rsi,%r14
mov    %rdi,%r15
mov    (%rdx),%edi
add    $0x4,%rbp
call   1040 <ntohs@plt>
mov    %eax,%r12d
mov    (%rbx),%edi
add    $0x4,%rbx
call   1040 <ntohs@plt>
mov    %eax,%r13d
lea    0x10(%rsp),%rdi
mov    $0x4,%ecx
mov    $0x11,%esi
mov    %rbp,%rdx
call   1050 <aeron_format_to_hex@plt>
lea    0x30(%rsp),%rbp
mov    $0x4,%ecx
mov    %rbp,%rdi
mov    $0x11,%esi
mov    %rbx,%rdx
call   1050 <aeron_format_to_hex@plt>
movzwl %r13w,%eax
mov    %eax,(%rsp)
movzwl %r12w,%r8d
lea    0xe57(%rip),%rdx
mov    %r15,%rdi
mov    %r14,%rsi
lea    0x10(%rsp),%rcx
mov    %rbp,%r9
call   1030 <snprintf@plt>
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_storage = type { i64 }
%struct.sockaddr_in6 = type { i32, i32 }
%struct.sockaddr_in = type { i32, i32 }
@AF_INET6 = global i64 0
@.str = [16 x i8] c"UDP-%s-%d-%s-%d\00"
define i32 @aeron_uri_udp_canonicalize(i8* %0, i64 %1, %struct.sockaddr_storage* %2, %struct.sockaddr_storage* %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca %struct.sockaddr_storage*
%8 = alloca %struct.sockaddr_storage*
%9 = alloca [17 x i8]
%10 = alloca [17 x i8]
%11 = alloca i32*
%12 = alloca i32*
%13 = alloca i16
%14 = alloca i16
%15 = alloca i64
%16 = alloca i64
%17 = alloca %struct.sockaddr_in6*
%18 = alloca %struct.sockaddr_in*
%19 = alloca %struct.sockaddr_in6*
%20 = alloca %struct.sockaddr_in*
store i8* %0, i8** %5
store i64 %1, i64* %6
store %struct.sockaddr_storage* %2, %struct.sockaddr_storage** %7
store %struct.sockaddr_storage* %3, %struct.sockaddr_storage** %8
store i16 0, i16* %13
store i16 0, i16* %14
store i64 4, i64* %15
store i64 4, i64* %16
%21 = load i64, i64* @AF_INET6
%22 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %7
%23 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %22, i32 0, i32 0
%24 = load i64, i64* %23
%25 = icmp eq i64 %21, %24
br i1 %25, label %26, label %35
26: ; preds = %4
%27 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %7
%28 = bitcast %struct.sockaddr_storage* %27 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %28, %struct.sockaddr_in6** %17
%29 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %17
%30 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %29, i32 0, i32 1
store i32* %30, i32** %11
%31 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %17
%32 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %31, i32 0, i32 0
%33 = load i32, i32* %32
%34 = call zeroext i16 @ntohs(i32 %33)
store i16 %34, i16* %13
store i64 4, i64* %15
br label %44
35: ; preds = %4
%36 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %7
%37 = bitcast %struct.sockaddr_storage* %36 to %struct.sockaddr_in*
store %struct.sockaddr_in* %37, %struct.sockaddr_in** %18
%38 = load %struct.sockaddr_in*, %struct.sockaddr_in** %18
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %38, i32 0, i32 1
store i32* %39, i32** %11
%40 = load %struct.sockaddr_in*, %struct.sockaddr_in** %18
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call zeroext i16 @ntohs(i32 %42)
store i16 %43, i16* %13
br label %44
44: ; preds = %35, %26
%45 = load i64, i64* @AF_INET6
%46 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %8
%47 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %46, i32 0, i32 0
%48 = load i64, i64* %47
%49 = icmp eq i64 %45, %48
br i1 %49, label %50, label %59
50: ; preds = %44
%51 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %8
%52 = bitcast %struct.sockaddr_storage* %51 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %52, %struct.sockaddr_in6** %19
%53 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %19
%54 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %53, i32 0, i32 1
store i32* %54, i32** %12
%55 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %19
%56 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %55, i32 0, i32 0
%57 = load i32, i32* %56
%58 = call zeroext i16 @ntohs(i32 %57)
store i16 %58, i16* %14
store i64 4, i64* %16
br label %68
59: ; preds = %44
%60 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %8
%61 = bitcast %struct.sockaddr_storage* %60 to %struct.sockaddr_in*
store %struct.sockaddr_in* %61, %struct.sockaddr_in** %20
%62 = load %struct.sockaddr_in*, %struct.sockaddr_in** %20
%63 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %62, i32 0, i32 1
store i32* %63, i32** %12
%64 = load %struct.sockaddr_in*, %struct.sockaddr_in** %20
%65 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %64, i32 0, i32 0
%66 = load i32, i32* %65
%67 = call zeroext i16 @ntohs(i32 %66)
store i16 %67, i16* %14
br label %68
68: ; preds = %59, %50
%69 = getelementptr inbounds [17 x i8], [17 x i8]* %9, i64 0, i64 0
%70 = load i32*, i32** %11
%71 = load i64, i64* %15
%72 = call i32 @aeron_format_to_hex(i8* %69, i32 17, i32* %70, i64 %71)
%73 = getelementptr inbounds [17 x i8], [17 x i8]* %10, i64 0, i64 0
%74 = load i32*, i32** %12
%75 = load i64, i64* %16
%76 = call i32 @aeron_format_to_hex(i8* %73, i32 17, i32* %74, i64 %75)
%77 = load i8*, i8** %5
%78 = load i64, i64* %6
%79 = getelementptr inbounds [17 x i8], [17 x i8]* %9, i64 0, i64 0
%80 = load i16, i16* %13
%81 = getelementptr inbounds [17 x i8], [17 x i8]* %10, i64 0, i64 0
%82 = load i16, i16* %14
%83 = call i32 @snprintf(i8* %77, i64 %78, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %79, i16 zeroext %80, i8* %81, i16 zeroext %82)
ret i32 %83
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i64 }
%struct.sockaddr_in6 = type { i32, i32 }
%struct.sockaddr_in = type { i32, i32 }
@AF_INET6 = global i64 0
@.str = [16 x i8] c"UDP-%s-%d-%s-%d\00"
define i32 @aeron_uri_udp_canonicalize(i8* %0, i64 %1, %struct.sockaddr_storage* %2, %struct.sockaddr_storage* %3) {
%5 = alloca [17 x i8]
%6 = alloca [17 x i8]
%7 = getelementptr inbounds [17 x i8], [17 x i8]* %5, i64 0, i64 0
%8 = getelementptr inbounds [17 x i8], [17 x i8]* %6, i64 0, i64 0
%9 = load i64, i64* @AF_INET6
%10 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %2, i64 0, i32 0
%11 = load i64, i64* %10
%12 = icmp eq i64 %9, %11
%13 = trunc i64 %11 to i32
%14 = bitcast %struct.sockaddr_storage* %2 to %struct.sockaddr_in6*
%15 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %14, i64 0, i32 1
%16 = bitcast %struct.sockaddr_storage* %2 to %struct.sockaddr_in*
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %16, i64 0, i32 1
%18 = select i1 %12, i32* %15, i32* %17
%19 = tail call zeroext i16 @ntohs(i32 %13)
%20 = load i64, i64* @AF_INET6
%21 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %3, i64 0, i32 0
%22 = load i64, i64* %21
%23 = icmp eq i64 %20, %22
%24 = trunc i64 %22 to i32
%25 = bitcast %struct.sockaddr_storage* %3 to %struct.sockaddr_in6*
%26 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %25, i64 0, i32 1
%27 = bitcast %struct.sockaddr_storage* %3 to %struct.sockaddr_in*
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %27, i64 0, i32 1
%29 = select i1 %23, i32* %26, i32* %28
%30 = tail call zeroext i16 @ntohs(i32 %24)
%31 = call i32 @aeron_format_to_hex(i8* %7, i32 17, i32* %18, i64 4)
%32 = call i32 @aeron_format_to_hex(i8* %8, i32 17, i32* %29, i64 4)
%33 = call i32 @snprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %7, i16 zeroext %19, i8* %8, i16 zeroext %30)
ret i32 %33
}