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

/* Variables and functions */
scalar_t__ readWord(int, int, int *);
int writeWord(int, int, int);
int writeBitsW(uint8_t devAddr, uint8_t regAddr, uint8_t bitStart,
               uint8_t length, uint16_t data) {
  uint16_t w;
  if (readWord(devAddr, regAddr, &w) != 0) {
    uint8_t mask = ((1 << length) - 1) << (bitStart - length + 1);
    data <<= (bitStart - length + 1);
    data &= mask;
    w &= ~(mask);
    w |= data;
    return writeWord(devAddr, regAddr, w);
  } else {
    return -1;
  }
}


x86 assembly (clang, -O0):
<writeBitsW>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8d,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
lea    -0x1c(%rbp),%rdx
call   1030 <readWord@plt>
cmp    $0x0,%rax
je     11b1 <writeBitsW+0x91>
mov    -0x14(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
sub    $0x1,%eax
mov    -0x10(%rbp),%ecx
sub    -0x14(%rbp),%ecx
add    $0x1,%ecx
shl    %cl,%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%ecx
sub    -0x14(%rbp),%ecx
add    $0x1,%ecx
mov    -0x18(%rbp),%eax
shl    %cl,%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%eax
and    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%eax
xor    $0xffffffff,%eax
and    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
or     -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x1c(%rbp),%edx
call   1040 <writeWord@plt>
mov    %eax,-0x4(%rbp)
jmp    11b8 <writeBitsW+0x98>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeBitsW>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %r8d,%ebx
mov    %ecx,%r12d
mov    %edx,%ebp
mov    %esi,%r14d
mov    %edi,%r15d
lea    0xc(%rsp),%rdx
call   1030 <readWord@plt>
test   %rax,%rax
je     117f <writeBitsW+0x5f>
mov    $0xffffffff,%eax
mov    %r12d,%ecx
shl    %cl,%eax
not    %eax
sub    %r12d,%ebp
add    $0x1,%bpl
mov    %ebp,%ecx
shl    %cl,%eax
shl    %cl,%ebx
mov    0xc(%rsp),%ecx
xor    %ecx,%ebx
and    %eax,%ebx
xor    %ecx,%ebx
mov    %ebx,0xc(%rsp)
mov    %r15d,%edi
mov    %r14d,%esi
mov    %ebx,%edx
call   1040 <writeWord@plt>
jmp    1184 <writeBitsW+0x64>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @writeBitsW(i32 %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 %0, i32* %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
%14 = load i32, i32* %7
%15 = load i32, i32* %8
%16 = call i64 @readWord(i32 %14, i32 %15, i32* %12)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %47
18: ; preds = %5
%19 = load i32, i32* %10
%20 = shl i32 1, %19
%21 = sub nsw i32 %20, 1
%22 = load i32, i32* %9
%23 = load i32, i32* %10
%24 = sub nsw i32 %22, %23
%25 = add nsw i32 %24, 1
%26 = shl i32 %21, %25
store i32 %26, i32* %13
%27 = load i32, i32* %9
%28 = load i32, i32* %10
%29 = sub nsw i32 %27, %28
%30 = add nsw i32 %29, 1
%31 = load i32, i32* %11
%32 = shl i32 %31, %30
store i32 %32, i32* %11
%33 = load i32, i32* %13
%34 = load i32, i32* %11
%35 = and i32 %34, %33
store i32 %35, i32* %11
%36 = load i32, i32* %13
%37 = xor i32 %36, -1
%38 = load i32, i32* %12
%39 = and i32 %38, %37
store i32 %39, i32* %12
%40 = load i32, i32* %11
%41 = load i32, i32* %12
%42 = or i32 %41, %40
store i32 %42, i32* %12
%43 = load i32, i32* %7
%44 = load i32, i32* %8
%45 = load i32, i32* %12
%46 = call i32 @writeWord(i32 %43, i32 %44, i32 %45)
store i32 %46, i32* %6
br label %48
47: ; preds = %5
store i32 -1, i32* %6
br label %48
48: ; preds = %47, %18
%49 = load i32, i32* %6
ret i32 %49
}

LLVM IR (-O2):
define i32 @writeBitsW(i32 %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = bitcast i32* %6 to i8*
%8 = call i64 @readWord(i32 %0, i32 %1, i32* %6)
%9 = icmp eq i64 %8, 0
br i1 %9, label %23, label %10
10: ; preds = %5
%11 = shl nsw i32 -1, %3
%12 = xor i32 %11, -1
%13 = sub nsw i32 %2, %3
%14 = add nsw i32 %13, 1
%15 = shl i32 %12, %14
%16 = shl i32 %4, %14
%17 = and i32 %16, %15
%18 = xor i32 %15, -1
%19 = load i32, i32* %6
%20 = and i32 %19, %18
%21 = or i32 %20, %17
store i32 %21, i32* %6
%22 = call i32 @writeWord(i32 %0, i32 %1, i32 %21)
br label %23
23: ; preds = %5, %10
%24 = phi i32 [ %22, %10 ], [ -1, %5 ]
ret i32 %24
}