#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 int int64_t;
typedef int int32_t;

/* Variables and functions */
int INT32_MAX;
int INT32_MIN;
int32_t sat_shift(int32_t in, unsigned n_bits) {
  if (n_bits > 31)
    return (in & (1 << 31)) ? INT32_MIN : INT32_MAX;
  int64_t in64 = in;
  int64_t val = in64 << n_bits;
  if (val < INT32_MIN)
    return INT32_MIN;
  if (val > INT32_MAX)
    return INT32_MAX;
  return val;
}


x86 assembly (clang, -O0):
<sat_shift>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x1f,-0xc(%rbp)
jbe    114d <sat_shift+0x4d>
mov    -0x8(%rbp),%eax
and    $0x80000000,%eax
cmp    $0x0,%eax
je     1136 <sat_shift+0x36>
mov    0x2eac(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
jmp    1142 <sat_shift+0x42>
mov    0x2ea3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11aa <sat_shift+0xaa>
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    -0xc(%rbp),%ecx
shl    %cl,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2e70(%rip),%rcx
cmp    (%rcx),%eax
jae    1181 <sat_shift+0x81>
mov    0x2e61(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11aa <sat_shift+0xaa>
mov    -0x14(%rbp),%eax
mov    0x2e55(%rip),%rcx
cmp    (%rcx),%eax
jbe    11a4 <sat_shift+0xa4>
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11aa <sat_shift+0xaa>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
pop    %rbp
ret

x86 assembly (clang, -O2):
<sat_shift>:
cmp    $0x20,%esi
jb     1113 <sat_shift+0x13>
test   %edi,%edi
js     1134 <sat_shift+0x34>
mov    0x2ed0(%rip),%rax
mov    (%rax),%eax
ret
mov    %esi,%ecx
shl    %cl,%edi
mov    0x2eba(%rip),%rcx
mov    0x2ebb(%rip),%rax
mov    (%rax),%eax
cmp    %eax,%edi
cmovbe %edi,%eax
mov    (%rcx),%ecx
cmp    %ecx,%edi
cmovb  %ecx,%eax
ret
mov    0x2e9d(%rip),%rax
mov    (%rax),%eax
ret

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

LLVM IR (-O2):
@INT32_MIN = global i32 0
@INT32_MAX = global i32 0
define i32 @sat_shift(i32 %0, i32 %1) {
%3 = icmp ugt i32 %1, 31
br i1 %3, label %4, label %9
4: ; preds = %2
%5 = icmp sgt i32 %0, -1
%6 = load i32, i32* @INT32_MIN
%7 = load i32, i32* @INT32_MAX
%8 = select i1 %5, i32 %7, i32 %6
br label %17
9: ; preds = %2
%10 = shl i32 %0, %1
%11 = load i32, i32* @INT32_MIN
%12 = icmp ult i32 %10, %11
%13 = load i32, i32* @INT32_MAX
%14 = icmp ugt i32 %10, %13
%15 = select i1 %14, i32 %13, i32 %10
%16 = select i1 %12, i32 %11, i32 %15
br label %17
17: ; preds = %9, %4
%18 = phi i32 [ %8, %4 ], [ %16, %9 ]
ret i32 %18
}