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

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


x86 assembly (clang, -O0):
<NVIC_EncodePriority>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    -0x4(%rbp),%eax
and    $0x7,%eax
mov    %eax,-0x10(%rbp)
mov    $0x7,%eax
sub    -0x10(%rbp),%eax
cmp    $0x4,%eax
jle    1134 <NVIC_EncodePriority+0x34>
mov    $0x4,%eax
mov    %eax,-0x1c(%rbp)
jmp    113f <NVIC_EncodePriority+0x3f>
mov    $0x7,%eax
sub    -0x10(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%eax
add    $0x4,%eax
cmp    $0x7,%eax
jge    115e <NVIC_EncodePriority+0x5e>
xor    %eax,%eax
mov    %eax,-0x20(%rbp)
jmp    116a <NVIC_EncodePriority+0x6a>
mov    -0x10(%rbp),%eax
sub    $0x7,%eax
add    $0x4,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    -0x14(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
sub    $0x1,%ecx
and    %ecx,%eax
mov    -0x18(%rbp),%ecx
shl    %cl,%eax
mov    -0xc(%rbp),%ecx
mov    %ecx,-0x24(%rbp)
mov    -0x18(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    -0x24(%rbp),%ecx
sub    $0x1,%edx
and    %edx,%ecx
or     %ecx,%eax
pop    %rbp
ret

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

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

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