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

/* Variables and functions */
scalar_t__ AF_INET;
scalar_t__ AF_INET6;
int /*<<< orphan*/ htons(int /*<<< orphan*/);
uint16_t saport(const struct sockaddr *sa) {
  return htons(sa->sa_family == AF_INET ? ((struct sockaddr_in *)sa)->sin_port
               : sa->sa_family == AF_INET6
                   ? ((struct sockaddr_in6 *)sa)->sin6_port
                   : 0);
}


x86 assembly (clang, -O0):
<saport>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    0x2ebe(%rip),%rcx
cmp    (%rcx),%rax
jne    1141 <saport+0x31>
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
jmp    1176 <saport+0x66>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    0x2e91(%rip),%rcx
cmp    (%rcx),%rax
jne    1166 <saport+0x56>
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
jmp    1170 <saport+0x60>
xor    %eax,%eax
mov    %eax,-0x10(%rbp)
jmp    1170 <saport+0x60>
mov    -0x10(%rbp),%eax
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <htons@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<saport>:
mov    (%rdi),%rax
mov    0x2ece(%rip),%rcx
mov    0x2ebf(%rip),%rdx
xor    %edi,%edi
cmp    (%rdx),%rax
cmove  %eax,%edi
cmp    (%rcx),%rax
cmove  %eax,%edi
jmp    1030 <htons@plt>

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 @saport(%struct.sockaddr* %0) {
%2 = alloca %struct.sockaddr*
store %struct.sockaddr* %0, %struct.sockaddr** %2
%3 = load %struct.sockaddr*, %struct.sockaddr** %2
%4 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %3, i32 0, i32 0
%5 = load i64, i64* %4
%6 = load i64, i64* @AF_INET
%7 = icmp eq i64 %5, %6
br i1 %7, label %8, label %13
8: ; preds = %1
%9 = load %struct.sockaddr*, %struct.sockaddr** %2
%10 = bitcast %struct.sockaddr* %9 to %struct.sockaddr_in*
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%12 = load i32, i32* %11
br label %27
13: ; preds = %1
%14 = load %struct.sockaddr*, %struct.sockaddr** %2
%15 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %14, i32 0, i32 0
%16 = load i64, i64* %15
%17 = load i64, i64* @AF_INET6
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %24
19: ; preds = %13
%20 = load %struct.sockaddr*, %struct.sockaddr** %2
%21 = bitcast %struct.sockaddr* %20 to %struct.sockaddr_in6*
%22 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %21, i32 0, i32 0
%23 = load i32, i32* %22
br label %25
24: ; preds = %13
br label %25
25: ; preds = %24, %19
%26 = phi i32 [ %23, %19 ], [ 0, %24 ]
br label %27
27: ; preds = %25, %8
%28 = phi i32 [ %12, %8 ], [ %26, %25 ]
%29 = call i32 @htons(i32 %28)
ret i32 %29
}

LLVM IR (-O2):
%struct.sockaddr = type { i64 }
@AF_INET = global i64 0
@AF_INET6 = global i64 0
define i32 @saport(%struct.sockaddr* nocapture %0) {
%2 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 0, i32 0
%3 = load i64, i64* %2
%4 = load i64, i64* @AF_INET
%5 = icmp eq i64 %3, %4
%6 = trunc i64 %3 to i32
%7 = load i64, i64* @AF_INET6
%8 = icmp eq i64 %3, %7
%9 = select i1 %5, i1 true, i1 %8
%10 = select i1 %9, i32 %6, i32 0
%11 = tail call i32 @htons(i32 %10)
ret i32 %11
}