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

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int bitpack_fitss(unsigned int, unsigned int);
uint64_t bitpack_sets(uint64_t vec, unsigned lsb, unsigned width, int64_t val) {
  assert(lsb < 64);
  assert(width <= 64);
  assert(lsb + width <= 64);
  assert(bitpack_fitss(val, width));
  uint64_t mask = ~0ull >> (64 - width) << lsb;
  return (vec & ~mask) | (mask & (val << lsb));
}


x86 assembly (clang, -O0):
<bitpack_sets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
cmpl   $0x40,-0x8(%rbp)
setb   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
cmpl   $0x40,-0xc(%rbp)
setbe  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x8(%rbp),%eax
add    -0xc(%rbp),%eax
cmp    $0x40,%eax
setbe  %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x10(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1040 <bitpack_fitss@plt>
mov    %eax,%edi
call   1030 <assert@plt>
mov    $0x40,%eax
sub    -0xc(%rbp),%eax
mov    %eax,%eax
mov    %eax,%ecx
mov    $0xffffffffffffffff,%rax
shr    %cl,%rax
mov    -0x8(%rbp),%ecx
shl    %cl,%rax
mov    %eax,-0x14(%rbp)
mov    -0x4(%rbp),%eax
mov    -0x14(%rbp),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
mov    -0x14(%rbp),%ecx
mov    %ecx,-0x18(%rbp)
mov    -0x10(%rbp),%edx
mov    -0x8(%rbp),%ecx
shl    %cl,%edx
mov    -0x18(%rbp),%ecx
and    %edx,%ecx
or     %ecx,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bitpack_sets>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%ebx
mov    %edx,%ebp
mov    %esi,%r15d
mov    %edi,%r14d
xor    %edi,%edi
cmp    $0x40,%esi
setb   %dil
call   1030 <assert@plt>
xor    %edi,%edi
cmp    $0x41,%ebp
setb   %dil
call   1030 <assert@plt>
lea    (%r15,%rbp,1),%eax
xor    %edi,%edi
cmp    $0x41,%eax
setb   %dil
call   1030 <assert@plt>
mov    %ebx,%edi
mov    %ebp,%esi
call   1040 <bitpack_fitss@plt>
mov    %eax,%edi
call   1030 <assert@plt>
neg    %bpl
mov    $0xffffffffffffffff,%rax
mov    %ebp,%ecx
shr    %cl,%rax
mov    %r15d,%ecx
shl    %cl,%rax
shl    %cl,%ebx
xor    %r14d,%ebx
and    %eax,%ebx
xor    %r14d,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @bitpack_sets(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* %6
%11 = icmp ult i32 %10, 64
%12 = zext i1 %11 to i32
%13 = call i32 @assert(i32 %12)
%14 = load i32, i32* %7
%15 = icmp ule i32 %14, 64
%16 = zext i1 %15 to i32
%17 = call i32 @assert(i32 %16)
%18 = load i32, i32* %6
%19 = load i32, i32* %7
%20 = add i32 %18, %19
%21 = icmp ule i32 %20, 64
%22 = zext i1 %21 to i32
%23 = call i32 @assert(i32 %22)
%24 = load i32, i32* %8
%25 = load i32, i32* %7
%26 = call i32 @bitpack_fitss(i32 %24, i32 %25)
%27 = call i32 @assert(i32 %26)
%28 = load i32, i32* %7
%29 = sub i32 64, %28
%30 = zext i32 %29 to i64
%31 = lshr i64 -1, %30
%32 = load i32, i32* %6
%33 = zext i32 %32 to i64
%34 = shl i64 %31, %33
%35 = trunc i64 %34 to i32
store i32 %35, i32* %9
%36 = load i32, i32* %5
%37 = load i32, i32* %9
%38 = xor i32 %37, -1
%39 = and i32 %36, %38
%40 = load i32, i32* %9
%41 = load i32, i32* %8
%42 = load i32, i32* %6
%43 = shl i32 %41, %42
%44 = and i32 %40, %43
%45 = or i32 %39, %44
ret i32 %45
}

LLVM IR (-O2):
define i32 @bitpack_sets(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = icmp ult i32 %1, 64
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
%8 = icmp ult i32 %2, 65
%9 = zext i1 %8 to i32
%10 = tail call i32 @assert(i32 %9)
%11 = add i32 %2, %1
%12 = icmp ult i32 %11, 65
%13 = zext i1 %12 to i32
%14 = tail call i32 @assert(i32 %13)
%15 = tail call i32 @bitpack_fitss(i32 %3, i32 %2)
%16 = tail call i32 @assert(i32 %15)
%17 = sub i32 64, %2
%18 = zext i32 %17 to i64
%19 = lshr i64 -1, %18
%20 = zext i32 %1 to i64
%21 = shl i64 %19, %20
%22 = trunc i64 %21 to i32
%23 = xor i32 %22, -1
%24 = and i32 %23, %0
%25 = shl i32 %3, %1
%26 = and i32 %25, %22
%27 = or i32 %24, %26
ret i32 %27
}