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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
int32_t insertBits(int32_t original, int32_t bitsToInsert, int fromIndex,
                   int toIndex) {
  if (fromIndex - toIndex <= 0) {
    fprintf(stderr, "toIndex needs to be less than fromIndex\n");
    exit(EXIT_FAILURE);
  }
  int32_t mask = 0;
  mask = (1 << fromIndex) - 1;
  mask = ~(mask << toIndex);
  bitsToInsert = bitsToInsert << toIndex;
  original = original & mask;
  return original | bitsToInsert;
}


x86 assembly (clang, -O0):
<insertBits>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
mov    -0xc(%rbp),%eax
sub    -0x10(%rbp),%eax
cmp    $0x0,%eax
jg     1166 <insertBits+0x46>
mov    0x2e96(%rip),%rax
mov    (%rax),%edi
lea    0xead(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
call   1040 <exit@plt>
movl   $0x0,-0x14(%rbp)
mov    -0xc(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
sub    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x10(%rbp),%ecx
shl    %cl,%eax
xor    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%eax
mov    -0x10(%rbp),%ecx
shl    %cl,%eax
mov    %eax,-0x8(%rbp)
mov    -0x4(%rbp),%eax
and    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
or     -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<insertBits>:
cmp    %ecx,%edx
jle    1140 <insertBits+0x20>
mov    %ecx,%r8d
mov    $0xffffffff,%eax
mov    %edx,%ecx
shl    %cl,%eax
not    %eax
mov    %r8d,%ecx
shl    %cl,%eax
shl    %cl,%esi
not    %eax
and    %edi,%eax
or     %esi,%eax
ret
push   %rax
mov    0x2e98(%rip),%rax
mov    (%rax),%edi
lea    0xeaf(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2e7b(%rip),%rax
mov    (%rax),%edi
call   1040 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [41 x i8] c"toIndex needs to be less than fromIndex\0A\00"
@EXIT_FAILURE = global i32 0
define i32 @insertBits(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%10 = load i32, i32* %7
%11 = load i32, i32* %8
%12 = sub nsw i32 %10, %11
%13 = icmp sle i32 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %4
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %4
store i32 0, i32* %9
%20 = load i32, i32* %7
%21 = shl i32 1, %20
%22 = sub nsw i32 %21, 1
store i32 %22, i32* %9
%23 = load i32, i32* %9
%24 = load i32, i32* %8
%25 = shl i32 %23, %24
%26 = xor i32 %25, -1
store i32 %26, i32* %9
%27 = load i32, i32* %6
%28 = load i32, i32* %8
%29 = shl i32 %27, %28
store i32 %29, i32* %6
%30 = load i32, i32* %5
%31 = load i32, i32* %9
%32 = and i32 %30, %31
store i32 %32, i32* %5
%33 = load i32, i32* %5
%34 = load i32, i32* %6
%35 = or i32 %33, %34
ret i32 %35
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [41 x i8] c"toIndex needs to be less than fromIndex\0A\00"
@EXIT_FAILURE = global i32 0
define i32 @insertBits(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = icmp sgt i32 %2, %3
br i1 %5, label %11, label %6
6: ; preds = %4
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = tail call i32 @exit(i32 %9)
unreachable
11: ; preds = %4
%12 = shl nsw i32 -1, %2
%13 = xor i32 %12, -1
%14 = shl i32 %13, %3
%15 = xor i32 %14, -1
%16 = shl i32 %1, %3
%17 = and i32 %15, %0
%18 = or i32 %17, %16
ret i32 %18
}