#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*/ ntohs(int /*<<< orphan*/);
uint16_t get_port(const struct sockaddr *ip) {
  if (ip->sa_family == AF_INET) {
    struct sockaddr_in *v4 = (struct sockaddr_in *)ip;
    return ntohs(v4->sin_port);
  }
  if (ip->sa_family == AF_INET6) {
    struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)ip;
    return ntohs(v6->sin6_port);
  }
  return 0;
}


x86 assembly (clang, -O0):
<get_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 <get_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    1187 <get_port+0x77>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    0x2e84(%rip),%rcx
cmp    (%rcx),%rax
jne    1180 <get_port+0x70>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%edi
call   1030 <ntohs@plt>
mov    %eax,-0x4(%rbp)
jmp    1187 <get_port+0x77>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_port>:
mov    (%rdi),%rdi
mov    0x2ece(%rip),%rax
cmp    (%rax),%rdi
je     112e <get_port+0x1e>
mov    0x2eba(%rip),%rax
cmp    (%rax),%rdi
je     112e <get_port+0x1e>
xor    %eax,%eax
ret
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
@AF_INET6 = global i64 0
define i32 @get_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 %32
18: ; preds = %1
%19 = load %struct.sockaddr*, %struct.sockaddr** %3
%20 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %19, i32 0, i32 0
%21 = load i64, i64* %20
%22 = load i64, i64* @AF_INET6
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %31
24: ; preds = %18
%25 = load %struct.sockaddr*, %struct.sockaddr** %3
%26 = bitcast %struct.sockaddr* %25 to %struct.sockaddr_in6*
store %struct.sockaddr_in6* %26, %struct.sockaddr_in6** %5
%27 = load %struct.sockaddr_in6*, %struct.sockaddr_in6** %5
%28 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %27, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 @ntohs(i32 %29)
store i32 %30, i32* %2
br label %32
31: ; preds = %18
store i32 0, i32* %2
br label %32
32: ; preds = %31, %24, %11
%33 = load i32, i32* %2
ret i32 %33
}

LLVM IR (-O2):
%struct.sockaddr = type { i64 }
@AF_INET = global i64 0
@AF_INET6 = global i64 0
define i32 @get_port(%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 = load i64, i64* @AF_INET6
%7 = icmp eq i64 %3, %6
%8 = select i1 %5, i1 true, i1 %7
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = trunc i64 %3 to i32
%11 = tail call i32 @ntohs(i32 %10)
br label %12
12: ; preds = %1, %9
%13 = phi i32 [ %11, %9 ], [ 0, %1 ]
ret i32 %13
}