#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 sin6_port;
};
struct sockaddr_in {
  int sin_port;
};
struct sockaddr {
  scalar_t__ sa_family;
};

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
int /*<<< orphan*/ assert(int);
int chitcp_addr_port_cmp(struct sockaddr *addr1, struct sockaddr *addr2) {
  assert(addr1->sa_family == AF_INET || addr1->sa_family == AF_INET6);
  assert(addr2->sa_family == AF_INET || addr2->sa_family == AF_INET6);
  if (addr1->sa_family != addr2->sa_family) {
    return addr1->sa_family - addr2->sa_family;
  } else if (addr1->sa_family == AF_INET) {
    struct sockaddr_in *sin1 = (struct sockaddr_in *)addr1;
    struct sockaddr_in *sin2 = (struct sockaddr_in *)addr2;
    return sin1->sin_port - sin2->sin_port;
  } else if (addr1->sa_family == AF_INET6) {
    struct sockaddr_in6 *sin1 = (struct sockaddr_in6 *)addr1;
    struct sockaddr_in6 *sin2 = (struct sockaddr_in6 *)addr2;
    return sin1->sin6_port - sin2->sin6_port;
  }
  return -1;
}


x86 assembly (clang, -O0):
<chitcp_addr_port_cmp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
mov    $0x1,%al
mov    0x2eb8(%rip),%rdx
cmp    (%rdx),%rcx
mov    %al,-0x39(%rbp)
je     1153 <chitcp_addr_port_cmp+0x43>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2e96(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x39(%rbp)
mov    -0x39(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rcx
mov    $0x1,%al
mov    0x2e78(%rip),%rdx
cmp    (%rdx),%rcx
mov    %al,-0x3a(%rbp)
je     1193 <chitcp_addr_port_cmp+0x83>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    0x2e56(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x3a(%rbp)
mov    -0x3a(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x18(%rbp),%rcx
cmp    (%rcx),%rax
je     11ca <chitcp_addr_port_cmp+0xba>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    -0x18(%rbp),%rcx
sub    (%rcx),%rax
mov    %eax,-0x4(%rbp)
jmp    1251 <chitcp_addr_port_cmp+0x141>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2e10(%rip),%rcx
cmp    (%rcx),%rax
jne    1205 <chitcp_addr_port_cmp+0xf5>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
mov    -0x28(%rbp),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1251 <chitcp_addr_port_cmp+0x141>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2dcd(%rip),%rcx
cmp    (%rcx),%rax
jne    1240 <chitcp_addr_port_cmp+0x130>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%eax
mov    -0x38(%rbp),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1251 <chitcp_addr_port_cmp+0x141>
jmp    1245 <chitcp_addr_port_cmp+0x135>
jmp    124a <chitcp_addr_port_cmp+0x13a>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chitcp_addr_port_cmp>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    (%rdi),%rax
mov    0x2ec3(%rip),%r12
cmp    (%r12),%rax
sete   %cl
mov    0x2ead(%rip),%r15
cmp    (%r15),%rax
mov    %rsi,%rbx
sete   %al
or     %cl,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    (%rbx),%rax
cmp    (%r12),%rax
sete   %cl
cmp    (%r15),%rax
sete   %al
or     %cl,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    (%r14),%rax
mov    (%rbx),%rcx
cmp    %rcx,%rax
jne    1171 <chitcp_addr_port_cmp+0x61>
cmp    (%r12),%rax
jne    1175 <chitcp_addr_port_cmp+0x65>
sub    %ecx,%eax
jmp    1184 <chitcp_addr_port_cmp+0x74>
mov    %eax,%edx
sub    %ecx,%edx
cmp    (%r15),%rax
mov    $0xffffffff,%eax
cmove  %edx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.sockaddr = type { i64 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
@AF_INET = global i64 0
@AF_INET6 = global i64 0
define i32 @chitcp_addr_port_cmp(%struct.sockaddr* %0, %struct.sockaddr* %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr*
%5 = alloca %struct.sockaddr*
%6 = alloca %struct.sockaddr_in*
%7 = alloca %struct.sockaddr_in*
%8 = alloca %struct.sockaddr_in6*
%9 = alloca %struct.sockaddr_in6*
store %struct.sockaddr* %0, %struct.sockaddr** %4
store %struct.sockaddr* %1, %struct.sockaddr** %5
%10 = load %struct.sockaddr*, %struct.sockaddr** %4
%11 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %10, i32 0, i32 0
%12 = load i64, i64* %11
%13 = load i64, i64* @AF_INET
%14 = icmp eq i64 %12, %13
br i1 %14, label %21, label %15
15: ; preds = %2
%16 = load %struct.sockaddr*, %struct.sockaddr** %4
%17 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %16, i32 0, i32 0
%18 = load i64, i64* %17
%19 = load i64, i64* @AF_INET6
%20 = icmp eq i64 %18, %19
br label %21
21: ; preds = %15, %2
%22 = phi i1 [ true, %2 ], [ %20, %15 ]
%23 = zext i1 %22 to i32
%24 = call i32 @assert(i32 %23)
%25 = load %struct.sockaddr*, %struct.sockaddr** %5
%26 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %25, i32 0, i32 0
%27 = load i64, i64* %26
%28 = load i64, i64* @AF_INET
%29 = icmp eq i64 %27, %28
br i1 %29, label %36, label %30
30: ; preds = %21
%31 = load %struct.sockaddr*, %struct.sockaddr** %5
%32 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %31, i32 0, i32 0
%33 = load i64, i64* %32
%34 = load i64, i64* @AF_INET6
%35 = icmp eq i64 %33, %34
br label %36
36: ; preds = %30, %21
%37 = phi i1 [ true, %21 ], [ %35, %30 ]
%38 = zext i1 %37 to i32
%39 = call i32 @assert(i32 %38)
%40 = load %struct.sockaddr*, %struct.sockaddr** %4
%41 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %40, i32 0, i32 0
%42 = load i64, i64* %41
%43 = load %struct.sockaddr*, %struct.sockaddr** %5
%44 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %43, i32 0, i32 0
%45 = load i64, i64* %44
%46 = icmp ne i64 %42, %45
br i1 %46, label %47, label %56
47: ; preds = %36
%48 = load %struct.sockaddr*, %struct.sockaddr** %4
%49 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %48, i32 0, i32 0
%50 = load i64, i64* %49
%51 = load %struct.sockaddr*, %struct.sockaddr** %5
%52 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %51, i32 0, i32 0
%53 = load i64, i64* %52
%54 = sub nsw i64 %50, %53
%55 = trunc i64 %54 to i32
store i32 %55, i32* %3
br label %95
56: ; preds = %36
%57 = load %struct.sockaddr*, %struct.sockaddr** %4
%58 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %57, i32 0, i32 0
%59 = load i64, i64* %58
%60 = load i64, i64* @AF_INET
%61 = icmp eq i64 %59, %60
br i1 %61, label %62, label %74
62: ; preds = %56
%63 = load %struct.sockaddr*, %struct.sockaddr** %4
%64 = bitcast %struct.sockaddr* %63 to %struct.sockaddr_in*
store %struct.sockaddr_in* %64, %struct.sockaddr_in** %6
%65 = load %struct.sockaddr*, %struct.sockaddr** %5
%66 = bitcast %struct.sockaddr* %65 to %struct.sockaddr_in*
store %struct.sockaddr_in* %66, %struct.sockaddr_in** %7
%67 = load %struct.sockaddr_in*, %struct.sockaddr_in** %6
%68 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %67, i32 0, i32 0
%69 = load i32, i32* %68
%70 = load %struct.sockaddr_in*, %struct.sockaddr_in** %7
%71 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %70, i32 0, i32 0
%72 = load i32, i32* %71
%73 = sub nsw i32 %69, %72
store i32 %73, i32* %3
br label %95
74: ; preds = %56
%75 = load %struct.sockaddr*, %struct.sockaddr** %4
%76 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %75, i32 0, i32 0
%77 = load i64, i64* %76
%78 = load i64, i64* @AF_INET6
%79 = icmp eq i64 %77, %78
br i1 %79, label %80, label %92
80: ; preds = %74
%81 = load %struct.sockaddr*, %struct.sockaddr** %4
%82 = bitcast %struct.sockaddr* %81 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %82, %struct.sockaddr_in6** %8
%83 = load %struct.sockaddr*, %struct.sockaddr** %5
%84 = bitcast %struct.sockaddr* %83 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %84, %struct.sockaddr_in6** %9
%85 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %8
%86 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %85, i32 0, i32 0
%87 = load i32, i32* %86
%88 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %9
%89 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %88, i32 0, i32 0
%90 = load i32, i32* %89
%91 = sub nsw i32 %87, %90
store i32 %91, i32* %3
br label %95
92: ; preds = %74
br label %93
93: ; preds = %92
br label %94
94: ; preds = %93
store i32 -1, i32* %3
br label %95
95: ; preds = %94, %80, %62, %47
%96 = load i32, i32* %3
ret i32 %96
}

LLVM IR (-O2):
%struct.sockaddr = type { i64 }
@AF_INET = global i64 0
@AF_INET6 = global i64 0
define i32 @chitcp_addr_port_cmp(%struct.sockaddr* nocapture %0, %struct.sockaddr* nocapture %1) {
%3 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 0, i32 0
%4 = load i64, i64* %3
%5 = load i64, i64* @AF_INET
%6 = icmp eq i64 %4, %5
%7 = load i64, i64* @AF_INET6
%8 = icmp eq i64 %4, %7
%9 = select i1 %6, i1 true, i1 %8
%10 = zext i1 %9 to i32
%11 = tail call i32 @assert(i32 %10)
%12 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %1, i64 0, i32 0
%13 = load i64, i64* %12
%14 = load i64, i64* @AF_INET
%15 = icmp eq i64 %13, %14
%16 = load i64, i64* @AF_INET6
%17 = icmp eq i64 %13, %16
%18 = select i1 %15, i1 true, i1 %17
%19 = zext i1 %18 to i32
%20 = tail call i32 @assert(i32 %19)
%21 = load i64, i64* %3
%22 = load i64, i64* %12
%23 = icmp eq i64 %21, %22
%24 = trunc i64 %21 to i32
%25 = trunc i64 %22 to i32
br i1 %23, label %29, label %26
26: ; preds = %2
%27 = sub nsw i64 %21, %22
%28 = trunc i64 %27 to i32
br label %39
29: ; preds = %2
%30 = load i64, i64* @AF_INET
%31 = icmp eq i64 %21, %30
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = sub nsw i32 %24, %25
br label %39
34: ; preds = %29
%35 = load i64, i64* @AF_INET6
%36 = icmp eq i64 %21, %35
%37 = sub nsw i32 %24, %25
%38 = select i1 %36, i32 %37, i32 -1
br label %39
39: ; preds = %34, %32, %26
%40 = phi i32 [ %28, %26 ], [ %33, %32 ], [ %38, %34 ]
ret i32 %40
}