#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 uint8_t;
typedef int uint64_t;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
uint64_t replace_bits(uint64_t msrval, uint64_t data, uint8_t first,
                      uint8_t last) {
  assert(first <= last);
  assert(last < 64);
  const uint64_t mask = (((uint64_t)1 << (last - first + 1)) - 1) << first;
  return (msrval & ~mask) | ((data << first) & mask);
}


x86 assembly (clang, -O0):
<replace_bits>:
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
cmp    -0x10(%rbp),%eax
setle  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
cmpl   $0x40,-0x10(%rbp)
setl   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x10(%rbp),%ecx
sub    -0xc(%rbp),%ecx
add    $0x1,%ecx
mov    $0x1,%eax
shl    %cl,%eax
sub    $0x1,%eax
mov    -0xc(%rbp),%ecx
shl    %cl,%eax
mov    %eax,-0x14(%rbp)
mov    -0x4(%rbp),%eax
mov    -0x14(%rbp),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    -0x8(%rbp),%edx
mov    -0xc(%rbp),%ecx
shl    %cl,%edx
mov    %edx,%ecx
and    -0x14(%rbp),%ecx
or     %ecx,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<replace_bits>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %ecx,%r12d
mov    %edx,%r15d
mov    %esi,%ebx
mov    %edi,%r14d
xor    %edi,%edi
mov    %ecx,%ebp
sub    %edx,%ebp
setge  %dil
call   1030 <assert@plt>
xor    %edi,%edi
cmp    $0x40,%r12d
setl   %dil
call   1030 <assert@plt>
add    $0x1,%bpl
mov    $0xffffffff,%eax
mov    %ebp,%ecx
shl    %cl,%eax
not    %eax
mov    %r15d,%ecx
shl    %cl,%eax
shl    %cl,%ebx
xor    %r14d,%ebx
and    %eax,%ebx
xor    %r14d,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @replace_bits(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 = icmp sle i32 %10, %11
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = load i32, i32* %8
%16 = icmp slt i32 %15, 64
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
%19 = load i32, i32* %8
%20 = load i32, i32* %7
%21 = sub nsw i32 %19, %20
%22 = add nsw i32 %21, 1
%23 = shl i32 1, %22
%24 = sub nsw i32 %23, 1
%25 = load i32, i32* %7
%26 = shl i32 %24, %25
store i32 %26, i32* %9
%27 = load i32, i32* %5
%28 = load i32, i32* %9
%29 = xor i32 %28, -1
%30 = and i32 %27, %29
%31 = load i32, i32* %6
%32 = load i32, i32* %7
%33 = shl i32 %31, %32
%34 = load i32, i32* %9
%35 = and i32 %33, %34
%36 = or i32 %30, %35
ret i32 %36
}

LLVM IR (-O2):
define i32 @replace_bits(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = icmp sge i32 %3, %2
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
%8 = icmp slt i32 %3, 64
%9 = zext i1 %8 to i32
%10 = tail call i32 @assert(i32 %9)
%11 = sub i32 1, %2
%12 = add i32 %11, %3
%13 = shl nsw i32 -1, %12
%14 = xor i32 %13, -1
%15 = shl i32 %14, %2
%16 = xor i32 %15, -1
%17 = and i32 %16, %0
%18 = shl i32 %1, %2
%19 = and i32 %15, %18
%20 = or i32 %17, %19
ret i32 %20
}