// File: /workspace/github_cpp_makefile_star2_O3/trapexit[P]bbf/src/bbf_filethrash.cpp
// Function: bbf::filethrash(Options const&)
AppError
  filethrash(const Options &opts_)
  {
    int rv;
    int cores;
    FileThrashState state;
    struct statfs vfs;
    std::vector<pthread_t> threads;

    printf("Creating file: %s\n",opts_.device.c_str());
    rv = open(opts_.device.c_str(),O_RDWR|O_CREAT|O_SYNC|O_EXCL,0666);
    if(rv == -1)
      return AppError::opening_file(errno,opts_.device);

    state.fd = rv;

    rv = fstatfs(rv,&vfs);
    if(rv == -1)
      goto error;

    state.size = (vfs.f_bavail * vfs.f_frsize);

    printf("Expanding file to fill drive: %lu bytes\n",state.size);
    rv = fallocate(state.fd,FALLOC_FL_ZERO_RANGE,0,state.size);
    if(rv == -1)
      goto error;

    cores = l::nprocs();
    printf("Spawning thrashing threads: %d (one per core)\n",cores);
    for(int i = 0; i < cores; i++)
      l::spawn_thread(l::write_thread,&state,&threads);

    printf("CTRL-C to exit...\n");
    pause();

    l::disable_signals();
    state.is_finished = true;
    for(size_t i = 0; i < threads.size(); i++)
      pthread_join(threads[i],NULL);

    close(state.fd);
    unlink(opts_.device.c_str());

    return AppError::success();

  error:
    close(state.fd);
    unlink(opts_.device.c_str());
    return AppError::opening_file(errno,opts_.device);
  }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1c8, %rsp            # imm = 0x1C8
movq %rsi, %r12
movq %rdi, %r15
movl $0xffffffff, 0x20(%rsp) # imm = 0xFFFFFFFF
xorl %eax, %eax
movq %rax, 0x28(%rsp)
movb $0x0, 0x30(%rsp)
movq %rax, 0x10(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movq 0x8(%rsi), %rsi
leaq 0xf327(%rip), %rdi      # 0x16185
xorl %eax, %eax
callq 0x4070
movq 0x8(%r12), %rdi
movl $0x1010c2, %esi         # imm = 0x1010C2
movl $0x1b6, %edx            # imm = 0x1B6
xorl %eax, %eax
callq 0x4360
movl %eax, %ebx
addq $0x8, %r12
cmpl $-0x1, %eax
je 0x704d
movl %ebx, 0x20(%rsp)
leaq 0x50(%rsp), %rsi
movl %ebx, %edi
callq 0x41b0
cmpl $-0x1, %eax
je 0x703d
movq 0x98(%rsp), %r14
imulq 0x70(%rsp), %r14
movq %r14, 0x28(%rsp)
leaq 0xf2db(%rip), %rdi      # 0x16198
movq %r14, %rsi
xorl %eax, %eax
callq 0x4070
movl %ebx, %edi
movl $0x10, %esi
xorl %edx, %edx
movq %r14, %rcx
callq 0x43b0
cmpl $-0x1, %eax
je 0x703d
movq %r12, 0x38(%rsp)
movq %r15, 0x40(%rsp)
movl $0x54, %edi
callq 0x42e0
movq %rax, %r15
leaq 0xf2c2(%rip), %rdi      # 0x161c1
movl %r15d, %esi
xorl %eax, %eax
callq 0x4070
testl %r15d, %r15d
jle 0x6f9d
leaq 0x148(%rsp), %r12
leaq 0xc8(%rsp), %r13
leaq 0x170(%rip), %rbp       # 0x7099
leaq 0x48(%rsp), %rbx
leaq 0x20(%rsp), %r14
movq %r12, %rdi
callq 0x4180
movl $0x2, %edi
movq %r12, %rsi
movq %r13, %rdx
callq 0x40f0
movq %rbx, %rdi
xorl %esi, %esi
movq %rbp, %rdx
movq %r14, %rcx
callq 0x4710
testl %eax, %eax
jne 0x6f89
movq 0x8(%rsp), %rsi
cmpq 0x10(%rsp), %rsi
je 0x6f7e
movq 0x48(%rsp), %rax
movq %rax, (%rsi)
addq $0x8, %rsi
movq %rsi, 0x8(%rsp)
jmp 0x6f89
movq %rsp, %rdi
movq %rbx, %rdx
callq 0x4f8c
movl $0x2, %edi
movq %r13, %rsi
xorl %edx, %edx
callq 0x40f0
decl %r15d
jne 0x6f33
leaq 0xf263(%rip), %rdi      # 0x16207
callq 0x4630
callq 0x40a0
movq 0x40(%rsp), %r15
movq 0x38(%rsp), %r14
leaq 0xc8(%rsp), %rbx
movq %rbx, %rdi
callq 0x4180
movl $0x2, %edi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x40f0
movb $0x1, 0x30(%rsp)
movq (%rsp), %rax
cmpq %rax, 0x8(%rsp)
je 0x700c
xorl %ebx, %ebx
movq (%rax,%rbx,8), %rdi
xorl %esi, %esi
callq 0x4420
incq %rbx
movq (%rsp), %rax
movq 0x8(%rsp), %rcx
subq %rax, %rcx
sarq $0x3, %rcx
cmpq %rcx, %rbx
jb 0x6fe9
movl 0x20(%rsp), %edi
callq 0x4780
movq (%r14), %rdi
callq 0x4220
movq %r15, %rdi
callq 0xdce4
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x705f
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x44b0
jmp 0x705f
movl %ebx, %edi
callq 0x4780
movq (%r12), %rdi
callq 0x4220
callq 0x4060
movl (%rax), %esi
movq %r15, %rdi
movq %r12, %rdx
callq 0xe230
movq %r15, %rax
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x7078
jmp 0x7078
movq %rax, %rbx
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x7091
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x44b0
movq %rbx, %rdi
callq 0x46f0