#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 unsigned long uint32_t;

/* Variables and functions */
uint32_t NVIC_EncodePriority(uint32_t PriorityGroup, uint32_t PreemptPriority,
                             uint32_t SubPriority) {
  uint32_t PriorityGroupTmp = (PriorityGroup & (uint32_t)0x07UL);
  uint32_t PreemptPriorityBits;
  uint32_t SubPriorityBits;
  PreemptPriorityBits = ((7UL - PriorityGroupTmp) > (uint32_t)(4U))
                            ? (uint32_t)(4U)
                            : (uint32_t)(7UL - PriorityGroupTmp);
  SubPriorityBits = ((PriorityGroupTmp + (uint32_t)(4U)) < (uint32_t)7UL)
                        ? (uint32_t)0UL
                        : (uint32_t)((PriorityGroupTmp - 7UL) + (uint32_t)(4U));
  return (((PreemptPriority & (uint32_t)((1UL << (PreemptPriorityBits)) - 1UL))
           << SubPriorityBits) |
          ((SubPriority & (uint32_t)((1UL << (SubPriorityBits)) - 1UL))));
}


x86 assembly (clang, -O0):
<NVIC_EncodePriority>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
and    $0x7,%rax
mov    %rax,-0x20(%rbp)
mov    $0x7,%eax
sub    -0x20(%rbp),%rax
cmp    $0x4,%rax
jbe    113d <NVIC_EncodePriority+0x3d>
mov    $0x4,%eax
mov    %rax,-0x38(%rbp)
jmp    114a <NVIC_EncodePriority+0x4a>
mov    $0x7,%eax
sub    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
add    $0x4,%rax
cmp    $0x7,%rax
jae    116f <NVIC_EncodePriority+0x6f>
xor    %eax,%eax
mov    %rax,-0x40(%rbp)
jmp    117f <NVIC_EncodePriority+0x7f>
mov    -0x20(%rbp),%rax
sub    $0x7,%rax
add    $0x4,%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
shl    %cl,%rdx
mov    %rdx,%rcx
sub    $0x1,%rcx
and    %rcx,%rax
mov    -0x30(%rbp),%rcx
shl    %cl,%rax
mov    -0x18(%rbp),%rcx
mov    %rcx,-0x48(%rbp)
mov    -0x30(%rbp),%rcx
mov    $0x1,%edx
shl    %cl,%rdx
mov    -0x48(%rbp),%rcx
sub    $0x1,%rdx
and    %rdx,%rcx
or     %rcx,%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<NVIC_EncodePriority>:
and    $0x7,%edi
mov    %rdi,%rax
xor    $0x7,%rax
xor    %r8d,%r8d
sub    $0x3,%rdi
cmovae %rdi,%r8
cmp    $0x4,%rax
mov    $0x4,%ecx
cmovb  %eax,%ecx
mov    $0xffffffff,%eax
shl    %cl,%eax
not    %eax
and    %eax,%esi
mov    %r8d,%ecx
shl    %cl,%rsi
mov    $0xffffffff,%eax
shl    %cl,%eax
not    %eax
and    %edx,%eax
or     %rsi,%rax
ret

LLVM IR (-O0):
define i64 @NVIC_EncodePriority(i64 %0, i64 %1, i64 %2) {
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
store i64 %0, i64* %4
store i64 %1, i64* %5
store i64 %2, i64* %6
%10 = load i64, i64* %4
%11 = and i64 %10, 7
store i64 %11, i64* %7
%12 = load i64, i64* %7
%13 = sub i64 7, %12
%14 = icmp ugt i64 %13, 4
br i1 %14, label %15, label %16
15: ; preds = %3
br label %19
16: ; preds = %3
%17 = load i64, i64* %7
%18 = sub i64 7, %17
br label %19
19: ; preds = %16, %15
%20 = phi i64 [ 4, %15 ], [ %18, %16 ]
store i64 %20, i64* %8
%21 = load i64, i64* %7
%22 = add i64 %21, 4
%23 = icmp ult i64 %22, 7
br i1 %23, label %24, label %25
24: ; preds = %19
br label %29
25: ; preds = %19
%26 = load i64, i64* %7
%27 = sub i64 %26, 7
%28 = add i64 %27, 4
br label %29
29: ; preds = %25, %24
%30 = phi i64 [ 0, %24 ], [ %28, %25 ]
store i64 %30, i64* %9
%31 = load i64, i64* %5
%32 = load i64, i64* %8
%33 = shl i64 1, %32
%34 = sub i64 %33, 1
%35 = and i64 %31, %34
%36 = load i64, i64* %9
%37 = shl i64 %35, %36
%38 = load i64, i64* %6
%39 = load i64, i64* %9
%40 = shl i64 1, %39
%41 = sub i64 %40, 1
%42 = and i64 %38, %41
%43 = or i64 %37, %42
ret i64 %43
}

LLVM IR (-O2):
define i64 @NVIC_EncodePriority(i64 %0, i64 %1, i64 %2) {
%4 = and i64 %0, 7
%5 = xor i64 %4, 7
%6 = icmp ult i64 %5, 4
%7 = select i1 %6, i64 %5, i64 4
%8 = tail call i64 @llvm.usub.sat.i64(i64 %4, i64 3)
%9 = shl nsw i64 -1, %7
%10 = xor i64 %9, -1
%11 = and i64 %10, %1
%12 = shl i64 %11, %8
%13 = shl nsw i64 -1, %8
%14 = xor i64 %13, -1
%15 = and i64 %14, %2
%16 = or i64 %12, %15
ret i64 %16
}