#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*/ log_print(char *, int);
in_port_t sockaddr_port(struct sockaddr *sa) {
  switch (sa->sa_family) {
  case AF_INET6:
    return ((struct sockaddr_in6 *)sa)->sin6_port;
  case AF_INET:
    return ((struct sockaddr_in *)sa)->sin_port;
  default:
    log_print("sockaddr_port: unsupported protocol family %d", sa->sa_family);
    return 0;
  }
}


x86 assembly (clang, -O0):
<sockaddr_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     1148 <sockaddr_port+0x38>
jmp    1135 <sockaddr_port+0x25>
mov    -0x14(%rbp),%eax
sub    $0x81,%eax
je     1156 <sockaddr_port+0x46>
jmp    1164 <sockaddr_port+0x54>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    117d <sockaddr_port+0x6d>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    117d <sockaddr_port+0x6d>
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
lea    0xe8f(%rip),%rdi
call   1030 <log_print@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sockaddr_port>:
push   %rax
mov    (%rdi),%eax
mov    %eax,%ecx
and    $0xfffffffe,%ecx
cmp    $0x80,%ecx
je     1130 <sockaddr_port+0x20>
lea    0xed9(%rip),%rdi
mov    %eax,%esi
call   1030 <log_print@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
%struct.sockaddr_in6 = type { i32 }
%struct.sockaddr_in = type { i32 }
@.str = [46 x i8] c"sockaddr_port: unsupported protocol family %d\00"
define i32 @sockaddr_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 %17 [
i32 128, label %7
i32 129, label %12
]
7: ; preds = %1
%8 = load %struct.sockaddr*, %struct.sockaddr** %3
%9 = bitcast %struct.sockaddr* %8 to %struct.sockaddr_in6*
%10 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %9, i32 0, i32 0
%11 = load i32, i32* %10
store i32 %11, i32* %2
br label %22
12: ; preds = %1
%13 = load %struct.sockaddr*, %struct.sockaddr** %3
%14 = bitcast %struct.sockaddr* %13 to %struct.sockaddr_in*
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i32 0, i32 0
%16 = load i32, i32* %15
store i32 %16, i32* %2
br label %22
17: ; preds = %1
%18 = load %struct.sockaddr*, %struct.sockaddr** %3
%19 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %18, i32 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 @log_print(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i32 %20)
store i32 0, i32* %2
br label %22
22: ; preds = %17, %12, %7
%23 = load i32, i32* %2
ret i32 %23
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@.str = [46 x i8] c"sockaddr_port: unsupported protocol family %d\00"
define i32 @sockaddr_port(%struct.sockaddr* nocapture %0) {
%2 = getelementptr inbounds %struct.sockaddr, %struct.sockaddr* %0, i64 0, i32 0
%3 = load i32, i32* %2
%4 = and i32 %3, -2
%5 = icmp eq i32 %4, 128
br i1 %5, label %8, label %6
6: ; preds = %1
%7 = tail call i32 @log_print(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i32 %3)
br label %8
8: ; preds = %1, %6
%9 = phi i32 [ 0, %6 ], [ %3, %1 ]
ret i32 %9
}