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

/* Variables and functions */
#define AF_INET 129
#define AF_INET6 128
int /*<<< orphan*/ ntohs(int /*<<< orphan*/);
in_port_t get_port(struct sockaddr *sa) {
  switch (sa->sa_family) {
  case AF_INET:
    return ntohs(((struct sockaddr_in *)sa)->sin_port);
  case AF_INET6:
    return ntohs(((struct sockaddr_in6 *)sa)->sin6_port);
  default:
    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),%eax
mov    %eax,-0x14(%rbp)
sub    $0x80,%eax
je     115b <get_port+0x4b>
jmp    1135 <get_port+0x25>
mov    -0x14(%rbp),%eax
sub    $0x81,%eax
jne    116e <get_port+0x5e>
jmp    1148 <get_port+0x38>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1030 <ntohs@plt>
mov    %eax,-0x4(%rbp)
jmp    1175 <get_port+0x65>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1030 <ntohs@plt>
mov    %eax,-0x4(%rbp)
jmp    1175 <get_port+0x65>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_port>:
mov    (%rdi),%edi
cmp    $0x81,%edi
je     1127 <get_port+0x17>
cmp    $0x80,%edi
jne    112c <get_port+0x1c>
mov    $0x80,%edi
jmp    1030 <ntohs@plt>
xor    %eax,%eax
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr_in6 = type { i32 }
define i32 @get_port(%struct.sockaddr* %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr*
store %struct.sockaddr* %0, %struct.sockaddr** %3
%4 = load %struct.sockaddr*, %struct.sockaddr** %3
%5 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %4, i32 0, i32 0
%6 = load i32, i32* %5
switch i32 %6, label %19 [
i32 129, label %7
i32 128, label %13
]
7: ; preds = %1
%8 = load %struct.sockaddr*, %struct.sockaddr** %3
%9 = bitcast %struct.sockaddr* %8 to %struct.sockaddr_in*
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%11 = load i32, i32* %10
%12 = call i32 @ntohs(i32 %11)
store i32 %12, i32* %2
br label %20
13: ; preds = %1
%14 = load %struct.sockaddr*, %struct.sockaddr** %3
%15 = bitcast %struct.sockaddr* %14 to %struct.sockaddr_in6*
%16 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %15, i32 0, i32 0
%17 = load i32, i32* %16
%18 = call i32 @ntohs(i32 %17)
store i32 %18, i32* %2
br label %20
19: ; preds = %1
store i32 0, i32* %2
br label %20
20: ; preds = %19, %13, %7
%21 = load i32, i32* %2
ret i32 %21
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
define i32 @get_port(%struct.sockaddr* nocapture %0) {
%2 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 0, i32 0
%3 = load i32, i32* %2
switch i32 %3, label %8 [
i32 129, label %5
i32 128, label %4
]
4: ; preds = %1
br label %5
5: ; preds = %1, %4
%6 = phi i32 [ 128, %4 ], [ %3, %1 ]
%7 = tail call i32 @ntohs(i32 %6)
br label %8
8: ; preds = %5, %1
%9 = phi i32 [ 0, %1 ], [ %7, %5 ]
ret i32 %9
}