// File: trapexit[P]bbf/src/bbf_write_uncorrectable.cpp
// Function: bbf::write_uncorrectable(Options const&)
AppError
  write_uncorrectable(const Options &opts_)
  {
    int rv;
    BlkDev blkdev;
    std::string captcha;
    std::string input_file;
    std::vector<uint64_t> badblocks;

    input_file = opts_.input_file;

    rv = blkdev.open_rdwr(opts_.device,!opts_.force);
    if(rv < 0)
      return AppError::opening_device(-rv,opts_.device);

    captcha = captcha::calculate(blkdev);
    if(opts_.captcha != captcha)
      return AppError::captcha(opts_.captcha,captcha);

    if(input_file.empty())
      input_file = BadBlockFile::filepath(blkdev);

    rv = BadBlockFile::read(input_file,badblocks);
    if(rv < 0)
      return AppError::reading_badblocks_file(-rv,input_file);

    switch(opts_.instruction)
      {
      case Options::WRITE_PSEUDO_UNCORRECTABLE_WL:
        l::write_pseudo_uncorrectable_loop(blkdev,true,badblocks);
        break;
      case Options::WRITE_PSEUDO_UNCORRECTABLE_WOL:
        l::write_pseudo_uncorrectable_loop(blkdev,false,badblocks);
        break;
      case Options::WRITE_FLAGGED_UNCORRECTABLE_WL:
        l::write_flagged_uncorrectable_loop(blkdev,true,badblocks);
        break;
      case Options::WRITE_FLAGGED_UNCORRECTABLE_WOL:
        l::write_flagged_uncorrectable_loop(blkdev,false,badblocks);
        break;
      }

    rv = blkdev.close();
    if(rv < 0)
      return AppError::closing_device(-rv,opts_.device);

    return AppError::success();
  }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x190, %rsp            # imm = 0x190
movq %rdi, -0x170(%rbp)
movq %rdi, %rax
movq %rax, -0x168(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0xb0(%rbp), %rdi
callq 0x14b10
leaq -0xd0(%rbp), %rdi
callq 0x46e0
leaq -0xf0(%rbp), %rdi
movq %rdi, -0x160(%rbp)
callq 0x46e0
leaq -0x108(%rbp), %rdi
callq 0x7250
movq -0x160(%rbp), %rdi
movq -0x10(%rbp), %rsi
addq $0x78, %rsi
callq 0x4060
jmp 0x14671
movq -0x10(%rbp), %rax
movq %rax, %rsi
addq $0x8, %rsi
movb 0xb8(%rax), %al
notb %al
movzbl %al, %edx
andl $0x1, %edx
leaq -0xb0(%rbp), %rdi
callq 0x14d40
movl %eax, -0x174(%rbp)
jmp 0x1469e
movl -0x174(%rbp), %eax
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jge 0x1471e
movq -0x170(%rbp), %rdi
movl -0x14(%rbp), %esi
negl %esi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
callq 0x17610
jmp 0x146c8
movl $0x1, -0x118(%rbp)
jmp 0x14935
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x110(%rbp)
movl %eax, -0x114(%rbp)
leaq -0x108(%rbp), %rdi
callq 0x7360
leaq -0xf0(%rbp), %rdi
callq 0x4300
leaq -0xd0(%rbp), %rdi
callq 0x4300
leaq -0xb0(%rbp), %rdi
callq 0x14b40
jmp 0x14975
leaq -0x138(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x16d10
jmp 0x14733
leaq -0xd0(%rbp), %rdi
leaq -0x138(%rbp), %rsi
callq 0x4640
leaq -0x138(%rbp), %rdi
callq 0x4300
movq -0x10(%rbp), %rdi
addq $0x98, %rdi
leaq -0xd0(%rbp), %rsi
callq 0x7270
testb $0x1, %al
jne 0x1476f
jmp 0x1479e
movq -0x170(%rbp), %rdi
movq -0x10(%rbp), %rsi
addq $0x98, %rsi
leaq -0xd0(%rbp), %rdx
callq 0x17910
jmp 0x1478f
movl $0x1, -0x118(%rbp)
jmp 0x14935
leaq -0xf0(%rbp), %rdi
callq 0x4840
testb $0x1, %al
jne 0x147b0
jmp 0x147e4
leaq -0x158(%rbp), %rdi
leaq -0xb0(%rbp), %rsi
callq 0x4ed0
jmp 0x147c5
leaq -0xf0(%rbp), %rdi
leaq -0x158(%rbp), %rsi
callq 0x4640
leaq -0x158(%rbp), %rdi
callq 0x4300
leaq -0xf0(%rbp), %rdi
leaq -0x108(%rbp), %rsi
callq 0x5020
movl %eax, -0x178(%rbp)
jmp 0x147ff
movl -0x178(%rbp), %eax
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jge 0x14837
movq -0x170(%rbp), %rdi
movl -0x14(%rbp), %esi
negl %esi
leaq -0xf0(%rbp), %rdx
callq 0x17790
jmp 0x14828
movl $0x1, -0x118(%rbp)
jmp 0x14935
movq -0x10(%rbp), %rax
movl (%rax), %eax
addl $-0xd, %eax
movl %eax, %ecx
movq %rcx, -0x180(%rbp)
subl $0x3, %eax
ja 0x148d3
movq -0x180(%rbp), %rax
leaq 0xf0c4(%rip), %rcx      # 0x23924
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
leaq -0xb0(%rbp), %rdi
movl $0x1, %esi
leaq -0x108(%rbp), %rdx
callq 0x14990
jmp 0x14883
jmp 0x148d3
leaq -0xb0(%rbp), %rdi
xorl %esi, %esi
leaq -0x108(%rbp), %rdx
callq 0x14990
jmp 0x1489c
jmp 0x148d3
leaq -0xb0(%rbp), %rdi
movl $0x1, %esi
leaq -0x108(%rbp), %rdx
callq 0x14a20
jmp 0x148b8
jmp 0x148d3
leaq -0xb0(%rbp), %rdi
xorl %esi, %esi
leaq -0x108(%rbp), %rdx
callq 0x14a20
jmp 0x148d1
jmp 0x148d3
leaq -0xb0(%rbp), %rdi
callq 0x14e00
movl %eax, -0x184(%rbp)
jmp 0x148e7
movl -0x184(%rbp), %eax
movl %eax, -0x14(%rbp)
cmpl $0x0, -0x14(%rbp)
jge 0x1491d
movq -0x170(%rbp), %rdi
movl -0x14(%rbp), %esi
negl %esi
movq -0x10(%rbp), %rdx
addq $0x8, %rdx
callq 0x17690
jmp 0x14911
movl $0x1, -0x118(%rbp)
jmp 0x14935
movq -0x170(%rbp), %rdi
callq 0x17420
jmp 0x1492b
movl $0x1, -0x118(%rbp)
leaq -0x108(%rbp), %rdi
callq 0x7360
leaq -0xf0(%rbp), %rdi
callq 0x4300
leaq -0xd0(%rbp), %rdi
callq 0x4300
leaq -0xb0(%rbp), %rdi
callq 0x14b40
movq -0x168(%rbp), %rax
addq $0x190, %rsp            # imm = 0x190
popq %rbp
retq
movq -0x110(%rbp), %rdi
callq 0x4890
nopw %cs:(%rax,%rax)