#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;
int /*<<< orphan*/ ntohs(int /*<<< orphan*/);
uint16_t nt_util_sockaddr_port(struct sockaddr const *sa) {
  if (sa->sa_family == AF_INET) {
    struct sockaddr_in const *sin;
    sin = (struct sockaddr_in const *)sa;
    return ntohs(sin->sin_port);
  } else {
    struct sockaddr_in6 const *sin;
    sin = (struct sockaddr_in6 const *)sa;
    return ntohs(sin->sin6_port);
  }
}


x86 assembly (clang, -O0):
<nt_util_sockaddr_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2ebe(%rip),%rcx
cmp    (%rcx),%rax
jne    114e <nt_util_sockaddr_port+0x3e>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1030 <ntohs@plt>
mov    %eax,-0x4(%rbp)
jmp    1164 <nt_util_sockaddr_port+0x54>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1030 <ntohs@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nt_util_sockaddr_port>:
mov    (%rdi),%edi
jmp    1030 <ntohs@plt>

LLVM IR (-O0):
%struct.sockaddr = type { i64 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
@AF_INET = global i64 0
define i32 @nt_util_sockaddr_port(%struct.sockaddr* %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr*
%4 = alloca %struct.sockaddr_in*
%5 = alloca %struct.sockaddr_in6*
store %struct.sockaddr* %0, %struct.sockaddr** %3
%6 = load %struct.sockaddr*, %struct.sockaddr** %3
%7 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %6, i32 0, i32 0
%8 = load i64, i64* %7
%9 = load i64, i64* @AF_INET
%10 = icmp eq i64 %8, %9
br i1 %10, label %11, label %18
11: ; preds = %1
%12 = load %struct.sockaddr*, %struct.sockaddr** %3
%13 = bitcast %struct.sockaddr* %12 to %struct.sockaddr_in*
store %struct.sockaddr_in* %13, %struct.sockaddr_in** %4
%14 = load %struct.sockaddr_in*, %struct.sockaddr_in** %4
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i32 0, i32 0
%16 = load i32, i32* %15
%17 = call i32 @ntohs(i32 %16)
store i32 %17, i32* %2
br label %25
18: ; preds = %1
%19 = load %struct.sockaddr*, %struct.sockaddr** %3
%20 = bitcast %struct.sockaddr* %19 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %20, %struct.sockaddr_in6** %5
%21 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %5
%22 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %21, i32 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @ntohs(i32 %23)
store i32 %24, i32* %2
br label %25
25: ; preds = %18, %11
%26 = load i32, i32* %2
ret i32 %26
}

LLVM IR (-O2):
%struct.sockaddr = type { i64 }
@AF_INET = global i64 0
define i32 @nt_util_sockaddr_port(%struct.sockaddr* nocapture %0) {
%2 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 0, i32 0
%3 = load i64, i64* %2
%4 = trunc i64 %3 to i32
%5 = tail call i32 @ntohs(i32 %4)
ret i32 %5
}