#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 */
int mir3da_register_read(void *, short, unsigned char *);
int mir3da_register_write(void *, short, unsigned char);
int mir3da_register_mask_write(void *handle, short addr, unsigned char mask,
                               unsigned char data) {
  int res = 0;
  unsigned char tmp_data;
  res = mir3da_register_read(handle, addr, &tmp_data);
  if (res)
    return res;
  tmp_data &= ~mask;
  tmp_data |= data & mask;
  res = mir3da_register_write(handle, addr, tmp_data);
  return res;
}


x86 assembly (clang, -O0):
<mir3da_register_mask_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %cl,%al
mov    %dl,%cl
mov    %si,%dx
mov    %rdi,-0x10(%rbp)
mov    %dx,-0x12(%rbp)
mov    %cl,-0x13(%rbp)
mov    %al,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x19(%rbp),%rdx
movswl -0x12(%rbp),%esi
call   1030 <mir3da_register_read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     116d <mir3da_register_mask_write+0x4d>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ad <mir3da_register_mask_write+0x8d>
movzbl -0x13(%rbp),%ecx
xor    $0xffffffff,%ecx
movzbl -0x19(%rbp),%eax
and    %ecx,%eax
mov    %al,-0x19(%rbp)
movzbl -0x14(%rbp),%ecx
movzbl -0x13(%rbp),%eax
and    %eax,%ecx
movzbl -0x19(%rbp),%eax
or     %ecx,%eax
mov    %al,-0x19(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x12(%rbp),%ax
movswl %ax,%esi
movzbl -0x19(%rbp),%edx
call   1040 <mir3da_register_write@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mir3da_register_mask_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%ebx
mov    %edx,%r15d
mov    %esi,%ebp
mov    %rdi,%r14
lea    0x7(%rsp),%rdx
call   1030 <mir3da_register_read@plt>
test   %eax,%eax
jne    115c <mir3da_register_mask_write+0x3c>
mov    0x7(%rsp),%al
xor    %al,%bl
and    %r15b,%bl
xor    %al,%bl
mov    %bl,0x7(%rsp)
movzbl %bl,%edx
movswl %bp,%esi
mov    %r14,%rdi
call   1040 <mir3da_register_write@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @mir3da_register_mask_write(i8* %0, i16 signext %1, i8 zeroext %2, i8 zeroext %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i16
%8 = alloca i8
%9 = alloca i8
%10 = alloca i32
%11 = alloca i8
store i8* %0, i8** %6
store i16 %1, i16* %7
store i8 %2, i8* %8
store i8 %3, i8* %9
store i32 0, i32* %10
%12 = load i8*, i8** %6
%13 = load i16, i16* %7
%14 = call i32 @mir3da_register_read(i8* %12, i16 signext %13, i8* %11)
store i32 %14, i32* %10
%15 = load i32, i32* %10
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %4
%18 = load i32, i32* %10
store i32 %18, i32* %5
br label %41
19: ; preds = %4
%20 = load i8, i8* %8
%21 = zext i8 %20 to i32
%22 = xor i32 %21, -1
%23 = load i8, i8* %11
%24 = zext i8 %23 to i32
%25 = and i32 %24, %22
%26 = trunc i32 %25 to i8
store i8 %26, i8* %11
%27 = load i8, i8* %9
%28 = zext i8 %27 to i32
%29 = load i8, i8* %8
%30 = zext i8 %29 to i32
%31 = and i32 %28, %30
%32 = load i8, i8* %11
%33 = zext i8 %32 to i32
%34 = or i32 %33, %31
%35 = trunc i32 %34 to i8
store i8 %35, i8* %11
%36 = load i8*, i8** %6
%37 = load i16, i16* %7
%38 = load i8, i8* %11
%39 = call i32 @mir3da_register_write(i8* %36, i16 signext %37, i8 zeroext %38)
store i32 %39, i32* %10
%40 = load i32, i32* %10
store i32 %40, i32* %5
br label %41
41: ; preds = %19, %17
%42 = load i32, i32* %5
ret i32 %42
}

LLVM IR (-O2):
define i32 @mir3da_register_mask_write(i8* %0, i16 signext %1, i8 zeroext %2, i8 zeroext %3) {
%5 = alloca i8
%6 = call i32 @mir3da_register_read(i8* %0, i16 signext %1, i8* %5)
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %15
8: ; preds = %4
%9 = xor i8 %2, -1
%10 = load i8, i8* %5
%11 = and i8 %10, %9
%12 = and i8 %3, %2
%13 = or i8 %11, %12
store i8 %13, i8* %5
%14 = call i32 @mir3da_register_write(i8* %0, i16 signext %1, i8 zeroext %13)
br label %15
15: ; preds = %4, %8
%16 = phi i32 [ %14, %8 ], [ %6, %4 ]
ret i32 %16
}