#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 /*<<< orphan*/ smc_out(int, unsigned char);
void write_data(unsigned char page, unsigned char offset, unsigned char count,
                unsigned char shortlong, unsigned char *user_buffer) {
  unsigned int address;
  address = (page << 9) + offset;
  smc_out(2, ((address >> 8) | 0x40) & 0x7f);
  smc_out(3, address);
  while (count-- != 0) {
    smc_out(4, *user_buffer++);
  }
  if (shortlong == 1) {
    count = 0;
    do {
      smc_out(4, *user_buffer);
      user_buffer++;
      count--;
    } while (count != 0);
  }
}


x86 assembly (clang, -O0):
<write_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %cl,%al
mov    %dl,%cl
mov    %sil,%dl
mov    %dil,%sil
mov    %sil,-0x1(%rbp)
mov    %dl,-0x2(%rbp)
mov    %cl,-0x3(%rbp)
mov    %al,-0x4(%rbp)
mov    %r8,-0x10(%rbp)
movzbl -0x1(%rbp),%eax
shl    $0x9,%eax
movzbl -0x2(%rbp),%ecx
add    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
shr    $0x8,%eax
or     $0x40,%eax
and    $0x7f,%eax
mov    $0x2,%edi
movzbl %al,%esi
call   1030 <smc_out@plt>
mov    -0x14(%rbp),%eax
mov    $0x3,%edi
movzbl %al,%esi
call   1030 <smc_out@plt>
mov    -0x3(%rbp),%al
mov    %al,%cl
add    $0xff,%cl
mov    %cl,-0x3(%rbp)
movzbl %al,%eax
cmp    $0x0,%eax
je     11a4 <write_data+0x94>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    $0x4,%edi
movzbl (%rax),%esi
call   1030 <smc_out@plt>
jmp    116c <write_data+0x5c>
movzbl -0x4(%rbp),%eax
cmp    $0x1,%eax
jne    11ec <write_data+0xdc>
movb   $0x0,-0x3(%rbp)
mov    -0x10(%rbp),%rax
mov    $0x4,%edi
movzbl (%rax),%esi
call   1030 <smc_out@plt>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x3(%rbp),%al
add    $0xff,%al
mov    %al,-0x3(%rbp)
movzbl -0x3(%rbp),%eax
cmp    $0x0,%eax
jne    11b5 <write_data+0xa5>
jmp    11ec <write_data+0xdc>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_data>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %r8,%rbx
mov    %ecx,%r14d
mov    %edx,%ebp
mov    %esi,%r15d
add    %dil,%dil
and    $0x3e,%dil
or     $0x40,%dil
movzbl %dil,%esi
mov    $0x2,%edi
call   1030 <smc_out@plt>
mov    $0x3,%edi
mov    %r15d,%esi
call   1030 <smc_out@plt>
test   %ebp,%ebp
je     1167 <write_data+0x57>
nopl   0x0(%rax)
movzbl (%rbx),%esi
add    $0x1,%rbx
mov    $0x4,%edi
call   1030 <smc_out@plt>
add    $0xff,%bpl
jne    1150 <write_data+0x40>
cmp    $0x1,%r14b
jne    1187 <write_data+0x77>
xor    %ebp,%ebp
nop
movzbl (%rbx,%rbp,1),%esi
mov    $0x4,%edi
call   1030 <smc_out@plt>
add    $0x1,%rbp
test   %bpl,%bpl
jne    1170 <write_data+0x60>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @write_data(i8 zeroext %0, i8 zeroext %1, i8 zeroext %2, i8 zeroext %3, i8* %4) {
%6 = alloca i8
%7 = alloca i8
%8 = alloca i8
%9 = alloca i8
%10 = alloca i8*
%11 = alloca i32
store i8 %0, i8* %6
store i8 %1, i8* %7
store i8 %2, i8* %8
store i8 %3, i8* %9
store i8* %4, i8** %10
%12 = load i8, i8* %6
%13 = zext i8 %12 to i32
%14 = shl i32 %13, 9
%15 = load i8, i8* %7
%16 = zext i8 %15 to i32
%17 = add nsw i32 %14, %16
store i32 %17, i32* %11
%18 = load i32, i32* %11
%19 = lshr i32 %18, 8
%20 = or i32 %19, 64
%21 = and i32 %20, 127
%22 = trunc i32 %21 to i8
%23 = call i32 @smc_out(i32 2, i8 zeroext %22)
%24 = load i32, i32* %11
%25 = trunc i32 %24 to i8
%26 = call i32 @smc_out(i32 3, i8 zeroext %25)
br label %27
27: ; preds = %32, %5
%28 = load i8, i8* %8
%29 = add i8 %28, -1
store i8 %29, i8* %8
%30 = zext i8 %28 to i32
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %27
%33 = load i8*, i8** %10
%34 = getelementptr inbounds i8, i8* %33, i32 1
store i8* %34, i8** %10
%35 = load i8, i8* %33
%36 = call i32 @smc_out(i32 4, i8 zeroext %35)
br label %27
37: ; preds = %27
%38 = load i8, i8* %9
%39 = zext i8 %38 to i32
%40 = icmp eq i32 %39, 1
br i1 %40, label %41, label %55
41: ; preds = %37
store i8 0, i8* %8
br label %42
42: ; preds = %50, %41
%43 = load i8*, i8** %10
%44 = load i8, i8* %43
%45 = call i32 @smc_out(i32 4, i8 zeroext %44)
%46 = load i8*, i8** %10
%47 = getelementptr inbounds i8, i8* %46, i32 1
store i8* %47, i8** %10
%48 = load i8, i8* %8
%49 = add i8 %48, -1
store i8 %49, i8* %8
br label %50
50: ; preds = %42
%51 = load i8, i8* %8
%52 = zext i8 %51 to i32
%53 = icmp ne i32 %52, 0
br i1 %53, label %42, label %54
54: ; preds = %50
br label %55
55: ; preds = %54, %37
ret void
}

LLVM IR (-O2):
define void @write_data(i8 zeroext %0, i8 zeroext %1, i8 zeroext %2, i8 zeroext %3, i8* nocapture %4) {
%6 = shl i8 %0, 1
%7 = and i8 %6, 62
%8 = or i8 %7, 64
%9 = tail call i32 @smc_out(i32 2, i8 zeroext %8)
%10 = tail call i32 @smc_out(i32 3, i8 zeroext %1)
%11 = icmp eq i8 %2, 0
br i1 %11, label %20, label %12
12: ; preds = %5, %12
%13 = phi i8 [ %15, %12 ], [ %2, %5 ]
%14 = phi i8* [ %16, %12 ], [ %4, %5 ]
%15 = add i8 %13, -1
%16 = getelementptr inbounds i8, i8* %14, i64 1
%17 = load i8, i8* %14
%18 = tail call i32 @smc_out(i32 4, i8 zeroext %17)
%19 = icmp eq i8 %15, 0
br i1 %19, label %20, label %12
20: ; preds = %12, %5
%21 = phi i8* [ %4, %5 ], [ %16, %12 ]
%22 = icmp eq i8 %3, 1
br i1 %22, label %23, label %31
23: ; preds = %20, %23
%24 = phi i8* [ %28, %23 ], [ %21, %20 ]
%25 = phi i8 [ %29, %23 ], [ 0, %20 ]
%26 = load i8, i8* %24
%27 = tail call i32 @smc_out(i32 4, i8 zeroext %26)
%28 = getelementptr inbounds i8, i8* %24, i64 1
%29 = add i8 %25, -1
%30 = icmp eq i8 %29, 0
br i1 %30, label %31, label %23
31: ; preds = %23, %20
ret void
}