// File: ninjabear[P]dr-doom/src/file_handles.cpp
// Function: many_open_file_handles()
void many_open_file_handles() {
    std::string temp_file_name = std::tmpnam(nullptr);
    std::vector<std::FILE*> file_handles;

    for (;;) {
        errno = 0; // reset this
        FILE* fp = fopen(temp_file_name.c_str(), "w");
        if (fp==NULL) {
            std::cout << "Failed to open handle to " << temp_file_name << " on iteration #" << file_handles.size() << std::endl; 
            std::cout << "Reason: " << std::strerror(errno) << " (" << errno << ")" << std::endl;
            break;
        }
        file_handles.push_back(fp);
    }

    std::cout << std::endl;
    std::cout << "Would you like to close the open file handles now? [Y/n]: ";

    char cont = 0;
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::cin.get(cont);

    if (cont != 'n' && cont != 'N') {
        std::cout << "Closing #" << file_handles.size() << " file handles" << std::endl;

        for (FILE* fh : file_handles) { 
            fclose(fh);
        }
    } else {
        std::cout << "Leaving #" << file_handles.size() << " file handles dangling on exit" << std::endl;
    }


}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
xorl %eax, %eax
movl %eax, %edi
callq 0x2190
movq %rax, -0x90(%rbp)
leaq -0x21(%rbp), %rdi
movq %rdi, -0x88(%rbp)
callq 0x23a0
movq -0x90(%rbp), %rsi
movq -0x88(%rbp), %rdx
leaq -0x20(%rbp), %rdi
callq 0x3200
jmp 0x3fa4
leaq -0x21(%rbp), %rdi
callq 0x2250
leaq -0x50(%rbp), %rdi
callq 0x4370
callq 0x2030
movl $0x0, (%rax)
leaq -0x20(%rbp), %rdi
callq 0x20d0
movq %rax, %rdi
leaq 0x4510(%rip), %rsi      # 0x84e4
callq 0x2280
movq %rax, -0x98(%rbp)
jmp 0x3fe2
movq -0x98(%rbp), %rax
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x58(%rbp)
jne 0x4167
movq 0x8fd1(%rip), %rdi      # 0xcfd0
leaq 0x44e0(%rip), %rsi      # 0x84e6
callq 0x21f0
movq %rax, -0xa0(%rbp)
jmp 0x4014
movq -0xa0(%rbp), %rdi
leaq -0x20(%rbp), %rsi
callq 0x21c0
movq %rax, -0xa8(%rbp)
jmp 0x402d
movq -0xa8(%rbp), %rdi
leaq 0x44c5(%rip), %rsi      # 0x8500
callq 0x21f0
movq %rax, -0xb0(%rbp)
jmp 0x4049
leaq -0x50(%rbp), %rdi
callq 0x4390
movq -0xb0(%rbp), %rdi
movq %rax, %rsi
callq 0x2050
movq %rax, -0xb8(%rbp)
jmp 0x406a
movq -0xb8(%rbp), %rdi
movq 0x8f38(%rip), %rsi      # 0xcfb0
callq 0x2230
jmp 0x407f
movq 0x8f4a(%rip), %rdi      # 0xcfd0
leaq 0x4483(%rip), %rsi      # 0x8510
callq 0x21f0
movq %rax, -0xc0(%rbp)
jmp 0x409b
callq 0x2030
movl (%rax), %edi
callq 0x20a0
movq -0xc0(%rbp), %rdi
movq %rax, %rsi
callq 0x21f0
movq %rax, -0xc8(%rbp)
jmp 0x40bf
movq -0xc8(%rbp), %rdi
leaq 0x444c(%rip), %rsi      # 0x8519
callq 0x21f0
movq %rax, -0xd0(%rbp)
jmp 0x40db
callq 0x2030
movq -0xd0(%rbp), %rdi
movl (%rax), %esi
callq 0x2370
movq %rax, -0xd8(%rbp)
jmp 0x40f7
movq -0xd8(%rbp), %rdi
leaq 0x4417(%rip), %rsi      # 0x851c
callq 0x21f0
movq %rax, -0xe0(%rbp)
jmp 0x4113
movq -0xe0(%rbp), %rdi
movq 0x8e8f(%rip), %rsi      # 0xcfb0
callq 0x2230
jmp 0x4128
jmp 0x417b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x21(%rbp), %rdi
callq 0x2250
jmp 0x4362
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x50(%rbp), %rdi
callq 0x4510
leaq -0x20(%rbp), %rdi
callq 0x2408
jmp 0x4362
leaq -0x50(%rbp), %rdi
leaq -0x58(%rbp), %rsi
callq 0x43b0
jmp 0x4176
jmp 0x3fb6
movq 0x8e4e(%rip), %rdi      # 0xcfd0
movq 0x8e27(%rip), %rsi      # 0xcfb0
callq 0x2230
jmp 0x4190
movq 0x8e39(%rip), %rdi      # 0xcfd0
leaq 0x4380(%rip), %rsi      # 0x851e
callq 0x21f0
jmp 0x41a5
movb $0x0, -0x59(%rbp)
movq 0x8e30(%rip), %rdi      # 0xcfe0
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
xorl %esi, %esi
callq 0x2340
jmp 0x41c3
callq 0x4420
movq %rax, %rsi
movq 0x8e0e(%rip), %rdi      # 0xcfe0
movl $0xa, %edx
callq 0x2120
jmp 0x41de
movq 0x8dfb(%rip), %rdi      # 0xcfe0
leaq -0x59(%rbp), %rsi
callq 0x22d0
jmp 0x41f0
movsbl -0x59(%rbp), %eax
cmpl $0x6e, %eax
je 0x42d7
movsbl -0x59(%rbp), %eax
cmpl $0x4e, %eax
je 0x42d7
movq 0x8dbf(%rip), %rdi      # 0xcfd0
leaq 0x4341(%rip), %rsi      # 0x8559
callq 0x21f0
movq %rax, -0xe8(%rbp)
jmp 0x4226
leaq -0x50(%rbp), %rdi
callq 0x4390
movq -0xe8(%rbp), %rdi
movq %rax, %rsi
callq 0x2050
movq %rax, -0xf0(%rbp)
jmp 0x4247
movq -0xf0(%rbp), %rdi
leaq 0x430e(%rip), %rsi      # 0x8563
callq 0x21f0
movq %rax, -0xf8(%rbp)
jmp 0x4263
movq -0xf8(%rbp), %rdi
movq 0x8d3f(%rip), %rsi      # 0xcfb0
callq 0x2230
jmp 0x4278
leaq -0x50(%rbp), %rax
movq %rax, -0x68(%rbp)
movq -0x68(%rbp), %rdi
callq 0x4430
movq %rax, -0x70(%rbp)
movq -0x68(%rbp), %rdi
callq 0x4460
movq %rax, -0x78(%rbp)
leaq -0x70(%rbp), %rdi
leaq -0x78(%rbp), %rsi
callq 0x4490
testb $0x1, %al
jne 0x42ad
jmp 0x42d5
leaq -0x70(%rbp), %rdi
callq 0x44d0
movq (%rax), %rax
movq %rax, -0x80(%rbp)
movq -0x80(%rbp), %rdi
callq 0x2200
jmp 0x42c8
jmp 0x42ca
leaq -0x70(%rbp), %rdi
callq 0x44f0
jmp 0x429a
jmp 0x4347
movq 0x8cf2(%rip), %rdi      # 0xcfd0
leaq 0x428c(%rip), %rsi      # 0x8571
callq 0x21f0
movq %rax, -0x100(%rbp)
jmp 0x42f3
leaq -0x50(%rbp), %rdi
callq 0x4390
movq -0x100(%rbp), %rdi
movq %rax, %rsi
callq 0x2050
movq %rax, -0x108(%rbp)
jmp 0x4314
movq -0x108(%rbp), %rdi
leaq 0x4259(%rip), %rsi      # 0x857b
callq 0x21f0
movq %rax, -0x110(%rbp)
jmp 0x4330
movq -0x110(%rbp), %rdi
movq 0x8c72(%rip), %rsi      # 0xcfb0
callq 0x2230
jmp 0x4345
jmp 0x4347
leaq -0x50(%rbp), %rdi
callq 0x4510
leaq -0x20(%rbp), %rdi
callq 0x2408
addq $0x110, %rsp            # imm = 0x110
popq %rbp
retq
movq -0x30(%rbp), %rdi
callq 0x2390
nopl (%rax,%rax)