#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 uint32_t;
typedef int uint16_t;

/* Variables and functions */
int flash_eeprom_read_uint32(int);
int /*<<< orphan*/ flash_eeprom_write_uint32(int, int);
void write_uint16(uint32_t offset, uint16_t value) {
  uint32_t base_offset = offset & ~3;
  uint32_t byte_offset = offset & 3;
  if (byte_offset < 3) {
    uint32_t base_word = flash_eeprom_read_uint32(base_offset);
    base_word &= ~(0xFFFF << (8 * byte_offset));
    base_word |= value << (8 * byte_offset);
    flash_eeprom_write_uint32(base_offset, base_word);
  } else {
    uint32_t base_word = flash_eeprom_read_uint32(base_offset);
    base_word &= ~(0xFF << 24);
    base_word |= (value & 0xFF) << 24;
    flash_eeprom_write_uint32(base_offset, base_word);
    base_word = flash_eeprom_read_uint32(base_offset + 4);
    base_word &= ~0xFF;
    base_word |= (value >> 8) & 0xFF;
    flash_eeprom_write_uint32(base_offset + 4, base_word);
  }
}


x86 assembly (clang, -O0):
<write_uint16>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%eax
and    $0xfffffffc,%eax
mov    %eax,-0xc(%rbp)
mov    -0x4(%rbp),%eax
and    $0x3,%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x3,-0x10(%rbp)
jge    118c <write_uint16+0x6c>
mov    -0xc(%rbp),%edi
call   1030 <flash_eeprom_read_uint32@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%ecx
shl    $0x3,%ecx
mov    $0xffff,%eax
shl    %cl,%eax
xor    $0xffffffff,%eax
and    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%eax
mov    -0x10(%rbp),%ecx
shl    $0x3,%ecx
shl    %cl,%eax
or     -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1040 <flash_eeprom_write_uint32@plt>
jmp    11f6 <write_uint16+0xd6>
mov    -0xc(%rbp),%edi
call   1030 <flash_eeprom_read_uint32@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
and    $0xffffff,%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
and    $0xff,%eax
shl    $0x18,%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1040 <flash_eeprom_write_uint32@plt>
mov    -0xc(%rbp),%edi
add    $0x4,%edi
call   1030 <flash_eeprom_read_uint32@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
and    $0xffffff00,%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
or     -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
add    $0x4,%edi
mov    -0x18(%rbp),%esi
call   1040 <flash_eeprom_write_uint32@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_uint16>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %edi,%ebp
mov    %edi,%ebx
and    $0xfffffffc,%ebx
and    $0x3,%ebp
mov    %ebx,%edi
call   1030 <flash_eeprom_read_uint32@plt>
cmp    $0x3,%ebp
jne    116c <write_uint16+0x4c>
and    $0xffffff,%eax
mov    %r14d,%esi
shl    $0x18,%esi
or     %eax,%esi
mov    %ebx,%edi
call   1040 <flash_eeprom_write_uint32@plt>
add    $0x4,%ebx
mov    %ebx,%edi
call   1030 <flash_eeprom_read_uint32@plt>
and    $0xffffff00,%eax
mov    %r14d,%ecx
movzbl %ch,%esi
or     %eax,%esi
mov    %ebx,%edi
jmp    1187 <write_uint16+0x67>
shl    $0x3,%ebp
mov    $0xffff,%edx
mov    %ebp,%ecx
shl    %cl,%edx
not    %edx
and    %edx,%eax
shl    %cl,%r14d
or     %eax,%r14d
mov    %ebx,%edi
mov    %r14d,%esi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <flash_eeprom_write_uint32@plt>

LLVM IR (-O0):
define void @write_uint16(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%9 = load i32, i32* %3
%10 = and i32 %9, -4
store i32 %10, i32* %5
%11 = load i32, i32* %3
%12 = and i32 %11, 3
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp slt i32 %13, 3
br i1 %14, label %15, label %33
15: ; preds = %2
%16 = load i32, i32* %5
%17 = call i32 @flash_eeprom_read_uint32(i32 %16)
store i32 %17, i32* %7
%18 = load i32, i32* %6
%19 = mul nsw i32 8, %18
%20 = shl i32 65535, %19
%21 = xor i32 %20, -1
%22 = load i32, i32* %7
%23 = and i32 %22, %21
store i32 %23, i32* %7
%24 = load i32, i32* %4
%25 = load i32, i32* %6
%26 = mul nsw i32 8, %25
%27 = shl i32 %24, %26
%28 = load i32, i32* %7
%29 = or i32 %28, %27
store i32 %29, i32* %7
%30 = load i32, i32* %5
%31 = load i32, i32* %7
%32 = call i32 @flash_eeprom_write_uint32(i32 %30, i32 %31)
br label %60
33: ; preds = %2
%34 = load i32, i32* %5
%35 = call i32 @flash_eeprom_read_uint32(i32 %34)
store i32 %35, i32* %8
%36 = load i32, i32* %8
%37 = and i32 %36, 16777215
store i32 %37, i32* %8
%38 = load i32, i32* %4
%39 = and i32 %38, 255
%40 = shl i32 %39, 24
%41 = load i32, i32* %8
%42 = or i32 %41, %40
store i32 %42, i32* %8
%43 = load i32, i32* %5
%44 = load i32, i32* %8
%45 = call i32 @flash_eeprom_write_uint32(i32 %43, i32 %44)
%46 = load i32, i32* %5
%47 = add nsw i32 %46, 4
%48 = call i32 @flash_eeprom_read_uint32(i32 %47)
store i32 %48, i32* %8
%49 = load i32, i32* %8
%50 = and i32 %49, -256
store i32 %50, i32* %8
%51 = load i32, i32* %4
%52 = ashr i32 %51, 8
%53 = and i32 %52, 255
%54 = load i32, i32* %8
%55 = or i32 %54, %53
store i32 %55, i32* %8
%56 = load i32, i32* %5
%57 = add nsw i32 %56, 4
%58 = load i32, i32* %8
%59 = call i32 @flash_eeprom_write_uint32(i32 %57, i32 %58)
br label %60
60: ; preds = %33, %15
ret void
}

LLVM IR (-O2):
define void @write_uint16(i32 %0, i32 %1) {
%3 = and i32 %0, -4
%4 = and i32 %0, 3
%5 = icmp eq i32 %4, 3
%6 = tail call i32 @flash_eeprom_read_uint32(i32 %3)
br i1 %5, label %15, label %7
7: ; preds = %2
%8 = shl nuw nsw i32 %4, 3
%9 = shl nuw i32 65535, %8
%10 = xor i32 %9, -1
%11 = and i32 %6, %10
%12 = shl i32 %1, %8
%13 = or i32 %11, %12
%14 = tail call i32 @flash_eeprom_write_uint32(i32 %3, i32 %13)
br label %27
15: ; preds = %2
%16 = and i32 %6, 16777215
%17 = shl i32 %1, 24
%18 = or i32 %16, %17
%19 = tail call i32 @flash_eeprom_write_uint32(i32 %3, i32 %18)
%20 = add nsw i32 %3, 4
%21 = tail call i32 @flash_eeprom_read_uint32(i32 %20)
%22 = and i32 %21, -256
%23 = lshr i32 %1, 8
%24 = and i32 %23, 255
%25 = or i32 %22, %24
%26 = tail call i32 @flash_eeprom_write_uint32(i32 %20, i32 %25)
br label %27
27: ; preds = %15, %7
ret void
}