// File: /workspace/github_cpp_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
xorl %edi, %edi
callq 0x2170
leaq 0x28(%rsp), %rdi
movq %rsp, %rbx
movq %rax, %rsi
movq %rbx, %rdx
callq 0x311a
xorps %xmm0, %xmm0
movaps %xmm0, (%rbx)
movq $0x0, 0x10(%rbx)
callq 0x2040
movq %rax, %r15
leaq 0x213f(%rip), %r12      # 0x54e0
movq 0x4c28(%rip), %r14      # 0x7fd0
leaq 0x2169(%rip), %rbp      # 0x5518
movl $0x0, (%r15)
movq 0x28(%rsp), %rdi
movq %r12, %rsi
callq 0x2220
movq %rax, %rbx
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x33ee
movq 0x8(%rsp), %rsi
cmpq 0x10(%rsp), %rsi
je 0x34a6
movq %rbx, (%rsi)
addq $0x8, 0x8(%rsp)
jmp 0x3527
movl $0x19, %edx
movq %r14, %rdi
leaq 0x20e5(%rip), %rsi      # 0x54e2
callq 0x2210
movq 0x28(%rsp), %rsi
movq 0x30(%rsp), %rdx
movq %r14, %rdi
callq 0x2210
movq %rax, %r13
movl $0xf, %edx
movq %rax, %rdi
leaq 0x20d6(%rip), %rsi      # 0x54fc
callq 0x2210
movq 0x8(%rsp), %rsi
subq (%rsp), %rsi
sarq $0x3, %rsi
movq %r13, %rdi
callq 0x2120
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x21d0
movsbl %al, %esi
movq %r13, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2130
movl $0x8, %edx
movq %r14, %rdi
leaq 0x2093(%rip), %rsi      # 0x550c
callq 0x2210
movl (%r15), %edi
callq 0x2090
testq %rax, %rax
je 0x34b5
movq %rax, %r13
movq %rax, %rdi
callq 0x20c0
movq %r14, %rdi
movq %r13, %rsi
movq %rax, %rdx
callq 0x2210
jmp 0x34cd
movq %rsp, %rdi
leaq 0x20(%rsp), %rdx
callq 0x3746
jmp 0x3527
movq (%r14), %rax
movq -0x18(%rax), %rax
leaq (%r14,%rax), %rdi
movl 0x20(%r14,%rax), %esi
orl $0x1, %esi
callq 0x22d0
movl $0x2, %edx
movq %r14, %rdi
leaq 0x2039(%rip), %rsi      # 0x5515
callq 0x2210
movl (%r15), %esi
movq %r14, %rdi
callq 0x2300
movq %rax, %r13
movl $0x1, %edx
movq %rax, %rdi
movq %rbp, %rsi
callq 0x2210
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x21d0
movsbl %al, %esi
movq %r13, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2130
testq %rbx, %rbx
jne 0x33af
movq (%r14), %rax
addq -0x18(%rax), %r14
movq %r14, %rdi
movl $0xa, %esi
callq 0x21d0
movsbl %al, %esi
movq 0x4a82(%rip), %rdi      # 0x7fd0
callq 0x2030
movq %rax, %rdi
callq 0x2130
movq 0x4a6e(%rip), %rdi      # 0x7fd0
leaq 0x1fb1(%rip), %rsi      # 0x551a
movl $0x3a, %edx
callq 0x2210
movb $0x0, 0x20(%rsp)
movq 0x4a61(%rip), %rdi      # 0x7fe0
movq (%rdi), %rax
addq -0x18(%rax), %rdi
xorl %esi, %esi
callq 0x22d0
movq 0x4a4c(%rip), %rdi      # 0x7fe0
movabsq $0x7fffffffffffffff, %rsi # imm = 0x7FFFFFFFFFFFFFFF
movl $0xa, %edx
callq 0x2100
movq 0x4a31(%rip), %rdi      # 0x7fe0
leaq 0x20(%rsp), %rsi
callq 0x2260
movb 0x20(%rsp), %al
andb $-0x21, %al
cmpb $0x4e, %al
jne 0x3676
movq 0x4a02(%rip), %rdi      # 0x7fd0
leaq 0x1f98(%rip), %rsi      # 0x556d
movl $0x9, %edx
callq 0x2210
movq 0x8(%rsp), %rsi
subq (%rsp), %rsi
sarq $0x3, %rsi
movq 0x49dd(%rip), %rdi      # 0x7fd0
callq 0x2120
movq %rax, %rbx
leaq 0x1f75(%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
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x364c
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x2200
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3667
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x2200
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x4953(%rip), %rdi      # 0x7fd0
leaq 0x1ed1(%rip), %rsi      # 0x5555
movl $0x9, %edx
callq 0x2210
movq 0x8(%rsp), %rsi
subq (%rsp), %rsi
sarq $0x3, %rsi
movq 0x492e(%rip), %rdi      # 0x7fd0
callq 0x2120
movq %rax, %rbx
leaq 0x1eae(%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 (%rsp), %rbx
movq 0x8(%rsp), %r14
cmpq %r14, %rbx
je 0x3636
movq (%rbx), %rdi
callq 0x21e0
addq $0x8, %rbx
jmp 0x36ee
jmp 0x3709
jmp 0x3709
movq %rax, %rbx
movq (%rsp), %rdi
testq %rdi, %rdi
je 0x3722
movq 0x10(%rsp), %rsi
subq %rdi, %rsi
callq 0x2200
leaq 0x38(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x373d
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x2200
movq %rbx, %rdi
callq 0x2320
nop