#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 */

/* Variables and functions */
unsigned long truncateInt(unsigned long in, bool sign, char varLength) {
  switch (varLength) {
  case 0:
    in &= 0xFFFFFFFFUL;
    if (sign && in & 0x80000000) {
      in |= 0xFFFFFFFF00000000;
    }
    break;
  case 'h':
    in &= 0xFFFF;
    if (sign && in & 0x8000) {
      in |= 0xFFFFFFFFFFFF0000;
    }
    break;
  }
  return in;
}


x86 assembly (clang, -O0):
<truncateInt>:
push   %rbp
mov    %rsp,%rbp
mov    %dl,%al
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %al,-0xd(%rbp)
movsbl -0xd(%rbp),%eax
mov    %eax,-0x14(%rbp)
test   %eax,%eax
je     1135 <truncateInt+0x35>
jmp    1124 <truncateInt+0x24>
mov    -0x14(%rbp),%eax
sub    $0x68,%eax
je     1180 <truncateInt+0x80>
jmp    11bf <truncateInt+0xbf>
movabs $0xffffffff,%rax
and    -0x8(%rbp),%rax
mov    %rax,-0x8(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     117b <truncateInt+0x7b>
movabs $0x80000000,%rax
and    -0x8(%rbp),%rax
cmp    $0x0,%rax
je     117b <truncateInt+0x7b>
movabs $0xffffffff00000000,%rax
ff ff ff
or     -0x8(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11bf <truncateInt+0xbf>
mov    -0x8(%rbp),%rax
and    $0xffff,%rax
mov    %rax,-0x8(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11ba <truncateInt+0xba>
mov    -0x8(%rbp),%rax
and    $0x8000,%rax
cmp    $0x0,%rax
je     11ba <truncateInt+0xba>
mov    -0x8(%rbp),%rax
or     $0xffffffffffff0000,%rax
mov    %rax,-0x8(%rbp)
jmp    11bf <truncateInt+0xbf>
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<truncateInt>:
mov    %rdi,%rax
cmp    $0x68,%edx
je     111f <truncateInt+0x1f>
test   %edx,%edx
jne    1138 <truncateInt+0x38>
mov    %eax,%ecx
cltq
movabs $0xffffffff00000000,%rdx
ff ff ff
and    %rax,%rdx
jmp    112d <truncateInt+0x2d>
movzwl %ax,%ecx
movswq %cx,%rdx
and    $0xffffffffffff0000,%rdx
xor    %eax,%eax
test   %esi,%esi
cmovne %rdx,%rax
or     %rcx,%rax
ret

LLVM IR (-O0):
define i64 @truncateInt(i64 %0, i32 %1, i8 signext %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8
store i64 %0, i64* %4
store i32 %1, i32* %5
store i8 %2, i8* %6
%7 = load i8, i8* %6
%8 = sext i8 %7 to i32
switch i32 %8, label %35 [
i32 0, label %9
i32 104, label %22
]
9: ; preds = %3
%10 = load i64, i64* %4
%11 = and i64 %10, 4294967295
store i64 %11, i64* %4
%12 = load i32, i32* %5
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %21
14: ; preds = %9
%15 = load i64, i64* %4
%16 = and i64 %15, 2147483648
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %14
%19 = load i64, i64* %4
%20 = or i64 %19, -4294967296
store i64 %20, i64* %4
br label %21
21: ; preds = %18, %14, %9
br label %35
22: ; preds = %3
%23 = load i64, i64* %4
%24 = and i64 %23, 65535
store i64 %24, i64* %4
%25 = load i32, i32* %5
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %34
27: ; preds = %22
%28 = load i64, i64* %4
%29 = and i64 %28, 32768
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = load i64, i64* %4
%33 = or i64 %32, -65536
store i64 %33, i64* %4
br label %34
34: ; preds = %31, %27, %22
br label %35
35: ; preds = %3, %34, %21
%36 = load i64, i64* %4
ret i64 %36
}

LLVM IR (-O2):
define i64 @truncateInt(i64 %0, i32 %1, i8 signext %2) {
%4 = sext i8 %2 to i32
switch i32 %4, label %21 [
i32 0, label %5
i32 104, label %13
]
5: ; preds = %3
%6 = and i64 %0, 4294967295
%7 = icmp eq i32 %1, 0
%8 = and i64 %0, 2147483648
%9 = icmp eq i64 %8, 0
%10 = or i1 %7, %9
%11 = select i1 %10, i64 0, i64 -4294967296
%12 = or i64 %11, %6
br label %21
13: ; preds = %3
%14 = and i64 %0, 65535
%15 = icmp eq i32 %1, 0
%16 = and i64 %0, 32768
%17 = icmp eq i64 %16, 0
%18 = or i1 %15, %17
%19 = select i1 %18, i64 0, i64 -65536
%20 = or i64 %19, %14
br label %21
21: ; preds = %13, %5, %3
%22 = phi i64 [ %0, %3 ], [ %12, %5 ], [ %20, %13 ]
ret i64 %22
}