#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 unsigned long uint8_t;
struct sockaddr_storage {
  scalar_t__ ss_family;
};
struct sockaddr_in6 {
  int sin6_port;
  int /*<<< orphan*/ sin6_addr;
};
struct sockaddr_in {
  int sin_port;
  int /*<<< orphan*/ sin_addr;
};

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
int /*<<< orphan*/ abort();
int /*<<< orphan*/ log_msg(char *);
int hash_address(void *param) {
  struct sockaddr_storage *address = param;
  unsigned long hash = 5381;
  uint8_t *addr_ptr;
  uint8_t *addr_end;
  if (address->ss_family == AF_INET) {
    struct sockaddr_in *addr4 = param;
    addr_ptr = (uint8_t *)&addr4->sin_addr;
    addr_end = addr_ptr + sizeof(addr4->sin_addr);
    hash = ((hash << 5) + hash) + ((addr4->sin_port & 0xFF00) >> 8);
    hash = ((hash << 5) + hash) + (addr4->sin_port & 0x00FF);
  } else if (address->ss_family == AF_INET6) {
    struct sockaddr_in6 *addr6 = param;
    addr_ptr = (uint8_t *)&addr6->sin6_addr;
    addr_end = addr_ptr + sizeof(addr6->sin6_addr);
    hash = ((hash << 5) + hash) + ((addr6->sin6_port & 0xFF00) >> 8);
    hash = ((hash << 5) + hash) + (addr6->sin6_port & 0x00FF);
  } else {
    log_msg("Unsupported address for hashing.\n");
    abort();
  }
  while (addr_ptr < addr_end) {
    hash = ((hash << 5) + hash) + *addr_ptr;
    addr_ptr++;
  }
  return (int)hash;
}


x86 assembly (clang, -O0):
<hash_address>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
movq   $0x1505,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2e9e(%rip),%rcx
cmp    (%rcx),%rax
jne    11bf <hash_address+0x9f>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0x20,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
shl    $0x5,%rax
add    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    (%rcx),%ecx
and    $0xff00,%ecx
sar    $0x8,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
shl    $0x5,%rax
add    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    (%rcx),%ecx
and    $0xff,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    125a <hash_address+0x13a>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2e13(%rip),%rcx
cmp    (%rcx),%rax
jne    1242 <hash_address+0x122>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
add    $0x20,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
shl    $0x5,%rax
add    -0x18(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    (%rcx),%ecx
and    $0xff00,%ecx
sar    $0x8,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
shl    $0x5,%rax
add    -0x18(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    (%rcx),%ecx
and    $0xff,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    1255 <hash_address+0x135>
lea    0xdb7(%rip),%rdi
call   1040 <log_msg@plt>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    125a <hash_address+0x13a>
jmp    125f <hash_address+0x13f>
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    1295 <hash_address+0x175>
mov    -0x18(%rbp),%rax
shl    $0x5,%rax
add    -0x18(%rbp),%rax
mov    -0x20(%rbp),%rcx
add    (%rcx),%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x20(%rbp)
jmp    125f <hash_address+0x13f>
mov    -0x18(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hash_address>:
push   %rax
mov    (%rdi),%rax
mov    0x2ebd(%rip),%rcx
cmp    (%rcx),%rax
je     113c <hash_address+0x1c>
mov    0x2ea9(%rip),%rcx
cmp    (%rcx),%rax
jne    117c <hash_address+0x5c>
movzbl %ah,%ecx
mov    %ecx,%edx
shl    $0x5,%edx
add    %ecx,%edx
movzbl %al,%eax
add    %edx,%eax
mov    %eax,%ecx
shl    $0x5,%ecx
add    %eax,%ecx
add    0x4(%rdi),%ecx
mov    %ecx,%eax
shl    $0x5,%eax
add    %ecx,%eax
add    0xc(%rdi),%eax
mov    %eax,%ecx
shl    $0x5,%ecx
add    %eax,%ecx
add    0x14(%rdi),%ecx
mov    %ecx,%eax
shl    $0x5,%eax
add    %ecx,%eax
mov    0x1c(%rdi),%ecx
add    %ecx,%eax
add    $0x7df04c5,%eax
pop    %rcx
ret
lea    0xe7d(%rip),%rdi
call   1040 <log_msg@plt>
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
%struct.sockaddr_storage = type { i64 }
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr_in6 = type { i32, i32 }
@AF_INET = global i64 0
@AF_INET6 = global i64 0
@.str = [34 x i8] c"Unsupported address for hashing.\0A\00"
define i32 @hash_address(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.sockaddr_storage*
%4 = alloca i64
%5 = alloca i64*
%6 = alloca i64*
%7 = alloca %struct.sockaddr_in*
%8 = alloca %struct.sockaddr_in6*
store i8* %0, i8** %2
%9 = load i8*, i8** %2
%10 = bitcast i8* %9 to %struct.sockaddr_storage*
store %struct.sockaddr_storage* %10, %struct.sockaddr_storage** %3
store i64 5381, i64* %4
%11 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %3
%12 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %11, i32 0, i32 0
%13 = load i64, i64* %12
%14 = load i64, i64* @AF_INET
%15 = icmp eq i64 %13, %14
br i1 %15, label %16, label %45
16: ; preds = %1
%17 = load i8*, i8** %2
%18 = bitcast i8* %17 to %struct.sockaddr_in*
store %struct.sockaddr_in* %18, %struct.sockaddr_in** %7
%19 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %19, i32 0, i32 1
%21 = bitcast i32* %20 to i64*
store i64* %21, i64** %5
%22 = load i64*, i64** %5
%23 = getelementptr inbounds i64, i64* %22, i64 4
store i64* %23, i64** %6
%24 = load i64, i64* %4
%25 = shl i64 %24, 5
%26 = load i64, i64* %4
%27 = add i64 %25, %26
%28 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %28, i32 0, i32 0
%30 = load i32, i32* %29
%31 = and i32 %30, 65280
%32 = ashr i32 %31, 8
%33 = sext i32 %32 to i64
%34 = add i64 %27, %33
store i64 %34, i64* %4
%35 = load i64, i64* %4
%36 = shl i64 %35, 5
%37 = load i64, i64* %4
%38 = add i64 %36, %37
%39 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %39, i32 0, i32 0
%41 = load i32, i32* %40
%42 = and i32 %41, 255
%43 = sext i32 %42 to i64
%44 = add i64 %38, %43
store i64 %44, i64* %4
br label %84
45: ; preds = %1
%46 = load %struct.sockaddr_storage*, %struct.sockaddr_storage** %3
%47 = getelementptr inbounds %struct.sockaddr_storage, %struct.sockaddr_storage* %46, i32 0, i32 0
%48 = load i64, i64* %47
%49 = load i64, i64* @AF_INET6
%50 = icmp eq i64 %48, %49
br i1 %50, label %51, label %80
51: ; preds = %45
%52 = load i8*, i8** %2
%53 = bitcast i8* %52 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %53, %struct.sockaddr_in6** %8
%54 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %8
%55 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %54, i32 0, i32 1
%56 = bitcast i32* %55 to i64*
store i64* %56, i64** %5
%57 = load i64*, i64** %5
%58 = getelementptr inbounds i64, i64* %57, i64 4
store i64* %58, i64** %6
%59 = load i64, i64* %4
%60 = shl i64 %59, 5
%61 = load i64, i64* %4
%62 = add i64 %60, %61
%63 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %8
%64 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %63, i32 0, i32 0
%65 = load i32, i32* %64
%66 = and i32 %65, 65280
%67 = ashr i32 %66, 8
%68 = sext i32 %67 to i64
%69 = add i64 %62, %68
store i64 %69, i64* %4
%70 = load i64, i64* %4
%71 = shl i64 %70, 5
%72 = load i64, i64* %4
%73 = add i64 %71, %72
%74 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %8
%75 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %74, i32 0, i32 0
%76 = load i32, i32* %75
%77 = and i32 %76, 255
%78 = sext i32 %77 to i64
%79 = add i64 %73, %78
store i64 %79, i64* %4
br label %83
80: ; preds = %45
%81 = call i32 @log_msg(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%82 = call i32 (...) @abort()
unreachable
83: ; preds = %51
br label %84
84: ; preds = %83, %16
br label %85
85: ; preds = %89, %84
%86 = load i64*, i64** %5
%87 = load i64*, i64** %6
%88 = icmp ult i64* %86, %87
br i1 %88, label %89, label %99
89: ; preds = %85
%90 = load i64, i64* %4
%91 = shl i64 %90, 5
%92 = load i64, i64* %4
%93 = add i64 %91, %92
%94 = load i64*, i64** %5
%95 = load i64, i64* %94
%96 = add i64 %93, %95
store i64 %96, i64* %4
%97 = load i64*, i64** %5
%98 = getelementptr inbounds i64, i64* %97, i32 1
store i64* %98, i64** %5
br label %85
99: ; preds = %85
%100 = load i64, i64* %4
%101 = trunc i64 %100 to i32
ret i32 %101
}

LLVM IR (-O2):
@AF_INET = global i64 0
@AF_INET6 = global i64 0
@.str = [34 x i8] c"Unsupported address for hashing.\0A\00"
define i32 @hash_address(i8* %0) {
%2 = bitcast i8* %0 to i64*
%3 = load i64, i64* %2
%4 = load i64, i64* @AF_INET
%5 = icmp eq i64 %3, %4
%6 = load i64, i64* @AF_INET6
%7 = icmp eq i64 %3, %6
%8 = select i1 %5, i1 true, i1 %7
br i1 %8, label %12, label %9
9: ; preds = %1
%10 = tail call i32 @log_msg(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 (...) @abort()
unreachable
12: ; preds = %1
%13 = lshr i64 %3, 8
%14 = and i64 %13, 255
%15 = mul nuw nsw i64 %14, 33
%16 = and i64 %3, 255
%17 = add nuw nsw i64 %16, 5859909
%18 = add nuw nsw i64 %17, %15
%19 = getelementptr inbounds i8, i8* %0, i64 12
%20 = bitcast i8* %19 to i64*
%21 = getelementptr inbounds i8, i8* %0, i64 4
%22 = bitcast i8* %21 to i64*
%23 = load i64, i64* %22
%24 = mul nuw nsw i64 %18, 33
%25 = add i64 %23, %24
%26 = mul i64 %25, 33
%27 = load i64, i64* %20
%28 = add i64 %27, %26
%29 = getelementptr inbounds i8, i8* %0, i64 20
%30 = bitcast i8* %29 to i64*
%31 = mul i64 %28, 33
%32 = load i64, i64* %30
%33 = add i64 %32, %31
%34 = getelementptr inbounds i8, i8* %0, i64 28
%35 = bitcast i8* %34 to i64*
%36 = mul i64 %33, 33
%37 = load i64, i64* %35
%38 = add i64 %37, %36
%39 = trunc i64 %38 to i32
ret i32 %39
}