#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 */
unsigned char inb(int);
int /*<<< orphan*/ outb(int, int);
int /*<<< orphan*/ outw(int, unsigned short);
int write_sector(unsigned char *sector, unsigned char drive, unsigned int lba) {
  unsigned short *s = (unsigned short *)sector;
  outb(0x1F6, 0xE0 | (drive << 4) | ((lba >> 24) & 0x0F));
  outb(0x1F2, 1);
  outb(0x1F3, lba & 0xFF);
  outb(0x1F4, (lba >> 8) & 0xFF);
  outb(0x1F5, (lba >> 16) & 0xFF);
  outb(0x1F7, 0x30);
  unsigned char status;
  while ((status = inb(0x1F7))) {
    if (!(status & 0x80) && (status & 0x08))
      break;
  }
  int i;
  for (i = 0; i < 256; i++) {
    outw(0x1F0, s[i]);
  }
  outb(0x1F7, 0xE7);
  return 0;
}


x86 assembly (clang, -O0):
<write_sector>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
movzbl -0x9(%rbp),%esi
shl    $0x4,%esi
or     $0xe0,%esi
mov    -0x10(%rbp),%eax
shr    $0x18,%eax
and    $0xf,%eax
or     %eax,%esi
mov    $0x1f6,%edi
call   1030 <outb@plt>
mov    $0x1f2,%edi
mov    $0x1,%esi
call   1030 <outb@plt>
mov    -0x10(%rbp),%esi
and    $0xff,%esi
mov    $0x1f3,%edi
call   1030 <outb@plt>
mov    -0x10(%rbp),%esi
shr    $0x8,%esi
and    $0xff,%esi
mov    $0x1f4,%edi
call   1030 <outb@plt>
mov    -0x10(%rbp),%esi
shr    $0x10,%esi
and    $0xff,%esi
mov    $0x1f5,%edi
call   1030 <outb@plt>
mov    $0x1f7,%edi
mov    $0x30,%esi
call   1030 <outb@plt>
mov    $0x1f7,%edi
call   1040 <inb@plt>
mov    %al,-0x19(%rbp)
cmp    $0x0,%al
je     120d <write_sector+0xdd>
movzbl -0x19(%rbp),%eax
and    $0x80,%eax
cmp    $0x0,%eax
jne    1208 <write_sector+0xd8>
movzbl -0x19(%rbp),%eax
and    $0x8,%eax
cmp    $0x0,%eax
je     1208 <write_sector+0xd8>
jmp    120d <write_sector+0xdd>
jmp    11cc <write_sector+0x9c>
movl   $0x0,-0x20(%rbp)
cmpl   $0x100,-0x20(%rbp)
jge    1245 <write_sector+0x115>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    $0x1f0,%edi
movzwl (%rax,%rcx,2),%esi
call   1050 <outw@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1214 <write_sector+0xe4>
mov    $0x1f7,%edi
mov    $0xe7,%esi
call   1030 <outb@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_sector>:
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rdi,%r14
shl    $0x4,%esi
mov    %edx,%eax
shr    $0x18,%eax
and    $0xf,%eax
or     %eax,%esi
or     $0xe0,%esi
mov    $0x1f6,%edi
call   1030 <outb@plt>
mov    $0x1f2,%edi
mov    $0x1,%esi
call   1030 <outb@plt>
movzbl %bl,%esi
mov    $0x1f3,%edi
call   1030 <outb@plt>
movzbl %bh,%esi
mov    $0x1f4,%edi
call   1030 <outb@plt>
shr    $0x10,%ebx
movzbl %bl,%esi
mov    $0x1f5,%edi
call   1030 <outb@plt>
mov    $0x1f7,%edi
mov    $0x30,%esi
call   1030 <outb@plt>
xchg   %ax,%ax
mov    $0x1f7,%edi
call   1040 <inb@plt>
test   %al,%al
je     11b4 <write_sector+0x84>
and    $0x88,%al
cmp    $0x8,%al
jne    11a0 <write_sector+0x70>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
movzwl (%r14,%rbx,2),%esi
mov    $0x1f0,%edi
call   1050 <outw@plt>
add    $0x1,%rbx
cmp    $0x100,%rbx
jne    11c0 <write_sector+0x90>
mov    $0x1f7,%edi
mov    $0xe7,%esi
call   1030 <outb@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @write_sector(i8* %0, i8 zeroext %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8
%6 = alloca i32
%7 = alloca i16*
%8 = alloca i8
%9 = alloca i32
store i8* %0, i8** %4
store i8 %1, i8* %5
store i32 %2, i32* %6
%10 = load i8*, i8** %4
%11 = bitcast i8* %10 to i16*
store i16* %11, i16** %7
%12 = load i8, i8* %5
%13 = zext i8 %12 to i32
%14 = shl i32 %13, 4
%15 = or i32 224, %14
%16 = load i32, i32* %6
%17 = lshr i32 %16, 24
%18 = and i32 %17, 15
%19 = or i32 %15, %18
%20 = call i32 @outb(i32 502, i32 %19)
%21 = call i32 @outb(i32 498, i32 1)
%22 = load i32, i32* %6
%23 = and i32 %22, 255
%24 = call i32 @outb(i32 499, i32 %23)
%25 = load i32, i32* %6
%26 = lshr i32 %25, 8
%27 = and i32 %26, 255
%28 = call i32 @outb(i32 500, i32 %27)
%29 = load i32, i32* %6
%30 = lshr i32 %29, 16
%31 = and i32 %30, 255
%32 = call i32 @outb(i32 501, i32 %31)
%33 = call i32 @outb(i32 503, i32 48)
br label %34
34: ; preds = %48, %3
%35 = call zeroext i8 @inb(i32 503)
store i8 %35, i8* %8
%36 = icmp ne i8 %35, 0
br i1 %36, label %37, label %49
37: ; preds = %34
%38 = load i8, i8* %8
%39 = zext i8 %38 to i32
%40 = and i32 %39, 128
%41 = icmp ne i32 %40, 0
br i1 %41, label %48, label %42
42: ; preds = %37
%43 = load i8, i8* %8
%44 = zext i8 %43 to i32
%45 = and i32 %44, 8
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %48
47: ; preds = %42
br label %49
48: ; preds = %42, %37
br label %34
49: ; preds = %47, %34
store i32 0, i32* %9
br label %50
50: ; preds = %60, %49
%51 = load i32, i32* %9
%52 = icmp slt i32 %51, 256
br i1 %52, label %53, label %63
53: ; preds = %50
%54 = load i16*, i16** %7
%55 = load i32, i32* %9
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i16, i16* %54, i64 %56
%58 = load i16, i16* %57
%59 = call i32 @outw(i32 496, i16 zeroext %58)
br label %60
60: ; preds = %53
%61 = load i32, i32* %9
%62 = add nsw i32 %61, 1
store i32 %62, i32* %9
br label %50
63: ; preds = %50
%64 = call i32 @outb(i32 503, i32 231)
ret i32 0
}

LLVM IR (-O2):
define i32 @write_sector(i8* nocapture %0, i8 zeroext %1, i32 %2) {
%4 = zext i8 %1 to i32
%5 = shl nuw nsw i32 %4, 4
%6 = lshr i32 %2, 24
%7 = and i32 %6, 15
%8 = or i32 %7, %5
%9 = or i32 %8, 224
%10 = tail call i32 @outb(i32 502, i32 %9)
%11 = tail call i32 @outb(i32 498, i32 1)
%12 = and i32 %2, 255
%13 = tail call i32 @outb(i32 499, i32 %12)
%14 = lshr i32 %2, 8
%15 = and i32 %14, 255
%16 = tail call i32 @outb(i32 500, i32 %15)
%17 = lshr i32 %2, 16
%18 = and i32 %17, 255
%19 = tail call i32 @outb(i32 501, i32 %18)
%20 = tail call i32 @outb(i32 503, i32 48)
br label %21
21: ; preds = %21, %3
%22 = tail call zeroext i8 @inb(i32 503)
%23 = icmp eq i8 %22, 0
%24 = and i8 %22, -120
%25 = icmp eq i8 %24, 8
%26 = or i1 %23, %25
br i1 %26, label %27, label %21
27: ; preds = %21
%28 = bitcast i8* %0 to i16*
br label %29
29: ; preds = %27, %29
%30 = phi i64 [ 0, %27 ], [ %34, %29 ]
%31 = getelementptr inbounds i16, i16* %28, i64 %30
%32 = load i16, i16* %31
%33 = tail call i32 @outw(i32 496, i16 zeroext %32)
%34 = add nuw nsw i64 %30, 1
%35 = icmp eq i64 %34, 256
br i1 %35, label %36, label %29
36: ; preds = %29
%37 = tail call i32 @outb(i32 503, i32 231)
ret i32 0
}