#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*/ OSD_Puts(char *);
int cmd_write(int, unsigned long);
int sd_write_sector(unsigned long lba, unsigned char *buf) {
  int i, t, timeout;
  *(volatile unsigned int *)(0xFFFFFFD0 + 0x04) = (0xff);
  {
    while ((*(volatile unsigned int *)(0xFFFFFFD0 + 0x0) & (1 << 15)))
      ;
    *(volatile unsigned int *)(0xFFFFFFD0 + 0x0) = (1 | (1 << 8));
  };
  *(volatile unsigned int *)(0xFFFFFFD0 + 0x04) = (0xff);
  t = cmd_write(0xff0058, lba);
  if (t != 0) {
    OSD_Puts("Write failed\n");
    return (1);
  }
  *(volatile unsigned int *)(0xFFFFFFD0 + 0x04) = (0xFF);
  *(volatile unsigned int *)(0xFFFFFFD0 + 0x04) = (0xFE);
  for (i = 0; i < 128; i++) {
    int t = *(int *)buf;
    *(volatile unsigned int *)(0xFFFFFFD0 + 0x04) = ((t >> 24) & 255);
    *(volatile unsigned int *)(0xFFFFFFD0 + 0x04) = ((t >> 16) & 255);
    *(volatile unsigned int *)(0xFFFFFFD0 + 0x04) = ((t >> 8) & 255);
    *(volatile unsigned int *)(0xFFFFFFD0 + 0x04) = (t & 255);
    buf += 4;
  }
  *(volatile unsigned int *)(0xFFFFFFD0 + 0x04) = (0xFF);
  *(volatile unsigned int *)(0xFFFFFFD0 + 0x04) = (0xFF);
  *(volatile unsigned int *)(0xFFFFFFD0 + 0x04) = (0xFF);
  timeout = 100000;
  do {
    *(volatile unsigned int *)(0xFFFFFFD0 + 0x04) = (0xFF);
    i = (*(volatile unsigned int *)(0xFFFFFFD0 + 0x04) & 255);
  } while ((i == 0) && --timeout);
  *(volatile unsigned int *)(0xFFFFFFD0 + 0x04) = (0xff);
  {
    while ((*(volatile unsigned int *)(0xFFFFFFD0 + 0x0) & (1 << 15)))
      ;
    *(volatile unsigned int *)(0xFFFFFFD0 + 0x0) = (0);
  };
  return (0);
}


x86 assembly (clang, -O0):
<sd_write_sector>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    $0xffffffd4,%eax
movl   $0xff,(%rax)
mov    $0xffffffd0,%eax
mov    (%rax),%eax
and    $0x8000,%eax
cmp    $0x0,%eax
je     1155 <sd_write_sector+0x35>
jmp    113b <sd_write_sector+0x1b>
mov    $0xffffffd0,%eax
movl   $0x101,(%rax)
mov    $0xffffffd4,%eax
movl   $0xff,(%rax)
mov    -0x10(%rbp),%rsi
mov    $0xff0058,%edi
call   1040 <cmd_write@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     119e <sd_write_sector+0x7e>
lea    0xe73(%rip),%rdi
call   1030 <OSD_Puts@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d9 <sd_write_sector+0x1b9>
mov    $0xffffffd4,%eax
movl   $0xff,(%rax)
mov    $0xffffffd4,%eax
movl   $0xfe,(%rax)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x80,-0x1c(%rbp)
jge    1234 <sd_write_sector+0x114>
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%ecx
sar    $0x18,%ecx
and    $0xff,%ecx
mov    $0xffffffd4,%eax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%ecx
sar    $0x10,%ecx
and    $0xff,%ecx
mov    $0xffffffd4,%eax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%ecx
sar    $0x8,%ecx
and    $0xff,%ecx
mov    $0xffffffd4,%eax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%ecx
and    $0xff,%ecx
mov    $0xffffffd4,%eax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11bb <sd_write_sector+0x9b>
mov    $0xffffffd4,%eax
movl   $0xff,(%rax)
mov    $0xffffffd4,%eax
movl   $0xff,(%rax)
mov    $0xffffffd4,%eax
movl   $0xff,(%rax)
movl   $0x186a0,-0x24(%rbp)
mov    $0xffffffd4,%eax
movl   $0xff,(%rax)
mov    $0xffffffd4,%eax
mov    (%rax),%eax
and    $0xff,%eax
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x1c(%rbp)
mov    %al,-0x29(%rbp)
jne    1297 <sd_write_sector+0x177>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
setne  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    125c <sd_write_sector+0x13c>
mov    $0xffffffd4,%eax
movl   $0xff,(%rax)
mov    $0xffffffd0,%eax
mov    (%rax),%eax
and    $0x8000,%eax
cmp    $0x0,%eax
je     12c7 <sd_write_sector+0x1a7>
jmp    12ad <sd_write_sector+0x18d>
mov    $0xffffffd0,%eax
movl   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sd_write_sector>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rsi
mov    $0xffffffd4,%ebx
movl   $0xff,(%rbx)
cs nopw 0x0(%rax,%rax,1)
nop
testl  $0x8000,-0x4(%rbx)
jne    1140 <sd_write_sector+0x20>
movl   $0x101,-0x4(%rbx)
movl   $0xff,(%rbx)
mov    $0xff0058,%edi
call   1040 <cmd_write@plt>
test   %eax,%eax
je     117a <sd_write_sector+0x5a>
lea    0xe95(%rip),%rdi
call   1030 <OSD_Puts@plt>
mov    $0x1,%eax
jmp    1222 <sd_write_sector+0x102>
movl   $0xff,(%rbx)
movl   $0xfe,(%rbx)
xor    %eax,%eax
nopl   0x0(%rax,%rax,1)
mov    (%r14,%rax,4),%ecx
mov    %ecx,%edx
movzbl %ch,%esi
movzbl %cl,%edi
shr    $0x18,%ecx
mov    %ecx,(%rbx)
shr    $0x10,%edx
movzbl %dl,%ecx
mov    %ecx,(%rbx)
mov    %esi,(%rbx)
mov    %edi,(%rbx)
mov    0x4(%r14,%rax,4),%ecx
mov    %ecx,%edx
movzbl %ch,%esi
movzbl %cl,%edi
shr    $0x18,%ecx
mov    %ecx,(%rbx)
shr    $0x10,%edx
movzbl %dl,%ecx
mov    %ecx,(%rbx)
mov    %esi,(%rbx)
mov    %edi,(%rbx)
add    $0x2,%rax
cmp    $0x80,%eax
jne    1190 <sd_write_sector+0x70>
movl   $0xff,(%rbx)
movl   $0xff,(%rbx)
movl   $0xff,(%rbx)
mov    $0xfffe7961,%eax
nopl   (%rax)
movl   $0xff,(%rbx)
mov    (%rbx),%ecx
test   %cl,%cl
jne    1205 <sd_write_sector+0xe5>
lea    0x1(%rax),%ecx
test   %eax,%eax
mov    %ecx,%eax
jne    11f0 <sd_write_sector+0xd0>
movl   $0xff,(%rbx)
nopl   0x0(%rax,%rax,1)
testl  $0x8000,-0x4(%rbx)
jne    1210 <sd_write_sector+0xf0>
movl   $0x0,-0x4(%rbx)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [14 x i8] c"Write failed\0A\00"
define i32 @sd_write_sector(i64 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i64 %0, i64* %4
store i8* %1, i8** %5
store volatile i32 255, i32* inttoptr (i64 4294967252 to i32*)
br label %10
10: ; preds = %14, %2
%11 = load volatile i32, i32* inttoptr (i64 4294967248 to i32*)
%12 = and i32 %11, 32768
%13 = icmp ne i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %10
br label %10
15: ; preds = %10
store volatile i32 257, i32* inttoptr (i64 4294967248 to i32*)
store volatile i32 255, i32* inttoptr (i64 4294967252 to i32*)
%16 = load i64, i64* %4
%17 = call i32 @cmd_write(i32 16711768, i64 %16)
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %15
%21 = call i32 @OSD_Puts(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %66
22: ; preds = %15
store volatile i32 255, i32* inttoptr (i64 4294967252 to i32*)
store volatile i32 254, i32* inttoptr (i64 4294967252 to i32*)
store i32 0, i32* %6
br label %23
23: ; preds = %43, %22
%24 = load i32, i32* %6
%25 = icmp slt i32 %24, 128
br i1 %25, label %26, label %46
26: ; preds = %23
%27 = load i8*, i8** %5
%28 = bitcast i8* %27 to i32*
%29 = load i32, i32* %28
store i32 %29, i32* %9
%30 = load i32, i32* %9
%31 = ashr i32 %30, 24
%32 = and i32 %31, 255
store volatile i32 %32, i32* inttoptr (i64 4294967252 to i32*)
%33 = load i32, i32* %9
%34 = ashr i32 %33, 16
%35 = and i32 %34, 255
store volatile i32 %35, i32* inttoptr (i64 4294967252 to i32*)
%36 = load i32, i32* %9
%37 = ashr i32 %36, 8
%38 = and i32 %37, 255
store volatile i32 %38, i32* inttoptr (i64 4294967252 to i32*)
%39 = load i32, i32* %9
%40 = and i32 %39, 255
store volatile i32 %40, i32* inttoptr (i64 4294967252 to i32*)
%41 = load i8*, i8** %5
%42 = getelementptr inbounds i8, i8* %41, i64 4
store i8* %42, i8** %5
br label %43
43: ; preds = %26
%44 = load i32, i32* %6
%45 = add nsw i32 %44, 1
store i32 %45, i32* %6
br label %23
46: ; preds = %23
store volatile i32 255, i32* inttoptr (i64 4294967252 to i32*)
store volatile i32 255, i32* inttoptr (i64 4294967252 to i32*)
store volatile i32 255, i32* inttoptr (i64 4294967252 to i32*)
store i32 100000, i32* %8
br label %47
47: ; preds = %57, %46
store volatile i32 255, i32* inttoptr (i64 4294967252 to i32*)
%48 = load volatile i32, i32* inttoptr (i64 4294967252 to i32*)
%49 = and i32 %48, 255
store i32 %49, i32* %6
br label %50
50: ; preds = %47
%51 = load i32, i32* %6
%52 = icmp eq i32 %51, 0
br i1 %52, label %53, label %57
53: ; preds = %50
%54 = load i32, i32* %8
%55 = add nsw i32 %54, -1
store i32 %55, i32* %8
%56 = icmp ne i32 %55, 0
br label %57
57: ; preds = %53, %50
%58 = phi i1 [ false, %50 ], [ %56, %53 ]
br i1 %58, label %47, label %59
59: ; preds = %57
store volatile i32 255, i32* inttoptr (i64 4294967252 to i32*)
br label %60
60: ; preds = %64, %59
%61 = load volatile i32, i32* inttoptr (i64 4294967248 to i32*)
%62 = and i32 %61, 32768
%63 = icmp ne i32 %62, 0
br i1 %63, label %64, label %65
64: ; preds = %60
br label %60
65: ; preds = %60
store volatile i32 0, i32* inttoptr (i64 4294967248 to i32*)
store i32 0, i32* %3
br label %66
66: ; preds = %65, %20
%67 = load i32, i32* %3
ret i32 %67
}

LLVM IR (-O2):
@.str = [14 x i8] c"Write failed\0A\00"
define i32 @sd_write_sector(i64 %0, i8* nocapture %1) {
store volatile i32 255, i32* inttoptr (i64 4294967252 to i32*)
br label %3
3: ; preds = %3, %2
%4 = load volatile i32, i32* inttoptr (i64 4294967248 to i32*)
%5 = and i32 %4, 32768
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %3
7: ; preds = %3
store volatile i32 257, i32* inttoptr (i64 4294967248 to i32*)
store volatile i32 255, i32* inttoptr (i64 4294967252 to i32*)
%8 = tail call i32 @cmd_write(i32 16711768, i64 %0)
%9 = icmp eq i32 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %7
%11 = tail call i32 @OSD_Puts(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %51
12: ; preds = %7
store volatile i32 255, i32* inttoptr (i64 4294967252 to i32*)
store volatile i32 254, i32* inttoptr (i64 4294967252 to i32*)
br label %13
13: ; preds = %13, %12
%14 = phi i8* [ %1, %12 ], [ %33, %13 ]
%15 = phi i32 [ 0, %12 ], [ %34, %13 ]
%16 = bitcast i8* %14 to i32*
%17 = load i32, i32* %16
%18 = lshr i32 %17, 24
store volatile i32 %18, i32* inttoptr (i64 4294967252 to i32*)
%19 = lshr i32 %17, 16
%20 = and i32 %19, 255
store volatile i32 %20, i32* inttoptr (i64 4294967252 to i32*)
%21 = lshr i32 %17, 8
%22 = and i32 %21, 255
store volatile i32 %22, i32* inttoptr (i64 4294967252 to i32*)
%23 = and i32 %17, 255
store volatile i32 %23, i32* inttoptr (i64 4294967252 to i32*)
%24 = getelementptr inbounds i8, i8* %14, i64 4
%25 = bitcast i8* %24 to i32*
%26 = load i32, i32* %25
%27 = lshr i32 %26, 24
store volatile i32 %27, i32* inttoptr (i64 4294967252 to i32*)
%28 = lshr i32 %26, 16
%29 = and i32 %28, 255
store volatile i32 %29, i32* inttoptr (i64 4294967252 to i32*)
%30 = lshr i32 %26, 8
%31 = and i32 %30, 255
store volatile i32 %31, i32* inttoptr (i64 4294967252 to i32*)
%32 = and i32 %26, 255
store volatile i32 %32, i32* inttoptr (i64 4294967252 to i32*)
%33 = getelementptr inbounds i8, i8* %14, i64 8
%34 = add nuw nsw i32 %15, 2
%35 = icmp eq i32 %34, 128
br i1 %35, label %36, label %13
36: ; preds = %13
store volatile i32 255, i32* inttoptr (i64 4294967252 to i32*)
store volatile i32 255, i32* inttoptr (i64 4294967252 to i32*)
store volatile i32 255, i32* inttoptr (i64 4294967252 to i32*)
br label %37
37: ; preds = %37, %36
%38 = phi i32 [ 100000, %36 ], [ %42, %37 ]
store volatile i32 255, i32* inttoptr (i64 4294967252 to i32*)
%39 = load volatile i32, i32* inttoptr (i64 4294967252 to i32*)
%40 = and i32 %39, 255
%41 = icmp ne i32 %40, 0
%42 = add nsw i32 %38, -1
%43 = icmp eq i32 %42, 0
%44 = select i1 %41, i1 true, i1 %43
br i1 %44, label %45, label %37
45: ; preds = %37
store volatile i32 255, i32* inttoptr (i64 4294967252 to i32*)
br label %46
46: ; preds = %46, %45
%47 = load volatile i32, i32* inttoptr (i64 4294967248 to i32*)
%48 = and i32 %47, 32768
%49 = icmp eq i32 %48, 0
br i1 %49, label %50, label %46
50: ; preds = %46
store volatile i32 0, i32* inttoptr (i64 4294967248 to i32*)
br label %51
51: ; preds = %50, %10
%52 = phi i32 [ 1, %10 ], [ 0, %50 ]
ret i32 %52
}