// File: /workspace/github_cpp_makefile_star2_O3/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 %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x48, %rsp
xorl %edi, %edi
callq 0x2170
leaq 0x28(%rsp), %r14
leaq 0x10(%rsp), %rbx
movq %r14, %rdi
movq %rax, %rsi
movq %rbx, %rdx
callq 0x311a
xorps %xmm0, %xmm0
movaps %xmm0, (%rbx)
movq $0x0, 0x10(%rbx)
callq 0x2040
movq %rax, %rbx
movl $0x0, (%rax)
movq (%r14), %rdi
leaq 0x2140(%rip), %rsi      # 0x54e0
callq 0x2220
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x33ff
leaq 0x10(%rsp), %r14
leaq 0x8(%rsp), %r15
leaq 0x2120(%rip), %r12      # 0x54e0
movq 0x18(%rsp), %rsi
cmpq 0x20(%rsp), %rsi
je 0x33d7
movq %rax, (%rsi)
addq $0x8, 0x18(%rsp)
jmp 0x33e2
movq %r14, %rdi
movq %r15, %rdx
callq 0x3766
movl $0x0, (%rbx)
movq 0x28(%rsp), %rdi
movq %r12, %rsi
callq 0x2220
movq %rax, 0x8(%rsp)
testq %rax, %rax
jne 0x33c0
movq 0x4bca(%rip), %rdi      # 0x7fd0
leaq 0x20d5(%rip), %rsi      # 0x54e2
movl $0x19, %edx
callq 0x2210
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
movq 0x4ba8(%rip), %rdi      # 0x7fd0
callq 0x2210
movq %rax, %r14
leaq 0x20c5(%rip), %rsi      # 0x54fc
movl $0xf, %edx
movq %rax, %rdi
callq 0x2210
movq 0x18(%rsp), %rsi
subq 0x10(%rsp), %rsi
sarq $0x3, %rsi
movq %r14, %rdi
callq 0x2120
movq %rax, %r14
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21d0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2130
movq 0x4b45(%rip), %r14      # 0x7fd0
leaq 0x207a(%rip), %rsi      # 0x550c
movl $0x8, %edx
movq %r14, %rdi
callq 0x2210
movl (%rbx), %edi
callq 0x2090
testq %rax, %rax
je 0x34ca
movq %rax, %r15
movq %rax, %rdi
callq 0x20c0
movq 0x4b13(%rip), %rdi      # 0x7fd0
movq %r15, %rsi
movq %rax, %rdx
callq 0x2210
jmp 0x34e2
movq (%r14), %rax
movq -0x18(%rax), %rax
leaq (%r14,%rax), %rdi
movl 0x20(%r14,%rax), %esi
orl $0x1, %esi
callq 0x22d0
movq 0x4ae7(%rip), %rdi      # 0x7fd0
leaq 0x2025(%rip), %rsi      # 0x5515
movl $0x2, %edx
callq 0x2210
movl (%rbx), %esi
movq 0x4acd(%rip), %rbx      # 0x7fd0
movq %rbx, %rdi
callq 0x2300
movq %rax, %r14
leaq 0x2003(%rip), %rsi      # 0x5518
movl $0x1, %edx
movq %rax, %rdi
callq 0x2210
movq (%r14), %rax
movq -0x18(%rax), %rdi
addq %r14, %rdi
movl $0xa, %esi
callq 0x21d0
movsbl %al, %esi
movq %r14, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2130
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x21d0
movsbl %al, %esi
movq 0x4a69(%rip), %rdi      # 0x7fd0
callq 0x2030
movq %rax, %rdi
callq 0x2130
movq 0x4a55(%rip), %rdi      # 0x7fd0
leaq 0x1f98(%rip), %rsi      # 0x551a
movl $0x3a, %edx
callq 0x2210
movb $0x0, 0x8(%rsp)
movq 0x4a48(%rip), %rdi      # 0x7fe0
movq (%rdi), %rax
addq -0x18(%rax), %rdi
xorl %esi, %esi
callq 0x22d0
movq 0x4a33(%rip), %rdi      # 0x7fe0
movabsq $0x7fffffffffffffff, %rsi # imm = 0x7FFFFFFFFFFFFFFF
movl $0xa, %edx
callq 0x2100
movq 0x4a18(%rip), %rdi      # 0x7fe0
leaq 0x8(%rsp), %rsi
callq 0x2260
movb 0x8(%rsp), %al
andb $-0x21, %al
cmpb $0x4e, %al
jne 0x3651
movq 0x49ed(%rip), %rdi      # 0x7fd0
leaq 0x1f83(%rip), %rsi      # 0x556d
movl $0x9, %edx
callq 0x2210
movq 0x18(%rsp), %rsi
subq 0x10(%rsp), %rsi
sarq $0x3, %rsi
movq 0x49c7(%rip), %rdi      # 0x7fd0
callq 0x2120
movq %rax, %rbx
leaq 0x1f5f(%rip), %rsi      # 0x5577
movl $0x1e, %edx
movq %rax, %rdi
callq 0x2210
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21d0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2130
jmp 0x36e1
movq 0x4978(%rip), %rdi      # 0x7fd0
leaq 0x1ef6(%rip), %rsi      # 0x5555
movl $0x9, %edx
callq 0x2210
movq 0x18(%rsp), %rsi
subq 0x10(%rsp), %rsi
sarq $0x3, %rsi
movq 0x4952(%rip), %rdi      # 0x7fd0
callq 0x2120
movq %rax, %rbx
leaq 0x1ed2(%rip), %rsi      # 0x555f
movl $0xd, %edx
movq %rax, %rdi
callq 0x2210
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x21d0
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2130
movq 0x10(%rsp), %rbx
movq 0x18(%rsp), %r14
cmpq %r14, %rbx
je 0x36e6
movq (%rbx), %rdi
callq 0x21e0
addq $0x8, %rbx
cmpq %r14, %rbx
jne 0x36d0
movq 0x10(%rsp), %rbx
testq %rbx, %rbx
je 0x36fb
movq 0x20(%rsp), %rsi
subq %rbx, %rsi
movq %rbx, %rdi
callq 0x2200
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3716
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x2200
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
jmp 0x3728
jmp 0x3728
jmp 0x3728
movq %rax, %rbx
movq 0x10(%rsp), %rdi
testq %rdi, %rdi
je 0x3742
movq 0x20(%rsp), %rsi
subq %rdi, %rsi
callq 0x2200
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x375d
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x2200
movq %rbx, %rdi
callq 0x2320
nop