#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;
};

/* Variables and functions */
#define AF_INET 129
#define AF_INET6 128
int /*<<< orphan*/ assert(struct sockaddr const *);
int ntohs(int /*<<< orphan*/);
int ls_sockaddr_get_port(const struct sockaddr *addr) {
  assert(addr);
  switch (addr->sa_family) {
  case AF_INET:
    return ntohs(((struct sockaddr_in *)addr)->sin_port);
  case AF_INET6:
    return ntohs(((struct sockaddr_in6 *)addr)->sin6_port);
  default:
    return -1;
  }
}


x86 assembly (clang, -O0):
<ls_sockaddr_get_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <assert@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
sub    $0x80,%eax
je     1174 <ls_sockaddr_get_port+0x54>
jmp    114e <ls_sockaddr_get_port+0x2e>
mov    -0x14(%rbp),%eax
sub    $0x81,%eax
jne    1187 <ls_sockaddr_get_port+0x67>
jmp    1161 <ls_sockaddr_get_port+0x41>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <ntohs@plt>
mov    %eax,-0x4(%rbp)
jmp    118e <ls_sockaddr_get_port+0x6e>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
call   1040 <ntohs@plt>
mov    %eax,-0x4(%rbp)
jmp    118e <ls_sockaddr_get_port+0x6e>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ls_sockaddr_get_port>:
push   %rbx
mov    %rdi,%rbx
call   1030 <assert@plt>
mov    (%rbx),%edi
cmp    $0x81,%edi
je     1140 <ls_sockaddr_get_port+0x20>
cmp    $0x80,%edi
jne    1146 <ls_sockaddr_get_port+0x26>
mov    $0x80,%edi
pop    %rbx
jmp    1040 <ntohs@plt>
mov    $0xffffffff,%eax
pop    %rbx
ret

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

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