// File: /workspace/github_cpp_makefile_star2_O2/Lut99[P]RayTracer/src/lib/RenderWorld.cpp
// Function: RayTracer::RenderWorld::from_json(nlohmann::basic_json<std::map, std::vector, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, bool, long, unsigned long, double, std::allocator, nlohmann::adl_serializer>)
RenderWorld* RenderWorld::from_json(nlohmann::json json_obj) {
    // Check if the object has an object type
    if (!json_obj.is_object()) {
        throw InvalidObjectFormat("RenderWorld", json::object().type_name(), json_obj.type_name());
    }

    // Parse all the items in this array
    RenderWorld* world = new RenderWorld();

    // Parse the objects field if it exists
    if (!json_obj["objects"].is_null()) {
        // Check if the fields are arrays
        if (!json_obj["objects"].is_array()) {
            delete world;
            throw InvalidFieldFormat("RenderWorld", "objects", json::array().type_name(), json_obj["objects"].type_name());
        }

        // Parse the objects
        for (std::size_t i = 0; i < json_obj["objects"].size(); i++) {
            world->add_object(RenderObject::from_json(json_obj["objects"][i]));
        }
    }
    
    // Parse the init function field
    if (!json_obj["init"].is_null()) {
        // Check if it is a string
        if (!json_obj["init"].is_string()) {
            delete world;
            throw InvalidFieldFormat("RenderWorld", "init", "string", json_obj["init"].type_name());
        }

        cout << "Test 1" << endl;

        // Check if there is a file ending in .so with that name
        std::string path = json_obj["init"].get<std::string>();
        if (FILE* file = fopen(path.c_str(), "r")) {
            fclose(file);
        } else {
            // Doesn't exist
            delete world;
            throw FileIOException("RenderWorld", path, errno);
        }

        cout << "Test 2" << endl;

        // There is, so try to load it using the dl library
        void* handle = dlopen(path.c_str(), RTLD_NOW);
        if (handle == NULL) {
            throw SOLoadException("RenderWorld", path, dlerror());
        }
        void (*init)(const RenderWorld& world) = (void (*)(const RenderWorld& world)) dlsym(handle, "init");
        if (init == NULL) {
            dlclose(handle);
            throw SOFuncLoadException("RenderWorld", path, "init", dlerror());
        }

        cout << "Test 3" << endl;

        // Run the function on the given world
        try {
            (*init)(*world);
        } catch (std::exception& e) {
            dlclose(handle);
            throw SORunException("RenderWorld", path, "init", string(e.what()));
        }

        cout << "Test 4" << endl;

        // Close the library
        //dlclose(handle);

        cout << "Test 5" << endl;
    }

    // Optimize the inner tree
    world->optimize();

    return world;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x338, %rsp            # imm = 0x338
movq %rdi, %rbx
cmpb $0x1, (%rdi)
jne 0x84ac4
pushq $0x10
popq %rdi
callq 0x52470
movq %rax, %r14
movq %rax, %rdi
callq 0x84390
leaq 0x7599(%rip), %rsi      # 0x8be76
movq %rbx, %rdi
callq 0x56e34
cmpb $0x0, (%rax)
je 0x8496e
leaq 0x7581(%rip), %rsi      # 0x8be76
movq %rbx, %rdi
callq 0x56e34
cmpb $0x2, (%rax)
jne 0x84b74
xorl %r15d, %r15d
leaq 0x7566(%rip), %r12      # 0x8be76
leaq 0x28(%rsp), %r13
movq %rbx, %rdi
movq %r12, %rsi
callq 0x56e34
movq %rax, %rdi
callq 0x6ec2e
cmpq %rax, %r15
jae 0x8496e
movq %rbx, %rdi
movq %r12, %rsi
callq 0x56e34
movq %rax, %rdi
movq %r15, %rsi
callq 0x7eefe
movq %r13, %rdi
movq %rax, %rsi
callq 0x56f56
movq %r13, %rdi
callq 0x801a8
movq %r14, %rdi
movq %rax, %rsi
callq 0x84470
movq %r13, %rdi
callq 0x56fca
incq %r15
jmp 0x84915
leaq 0xe113(%rip), %rsi      # 0x92a88
movq %rbx, %rdi
callq 0x56e34
cmpb $0x0, (%rax)
je 0x84aa7
leaq 0xe0fb(%rip), %rsi      # 0x92a88
movq %rbx, %rdi
callq 0x56e34
cmpb $0x3, (%rax)
jne 0x84c5d
movq 0x2d5e3(%rip), %rdi     # 0xb1f88
leaq 0xe0e1(%rip), %rsi      # 0x92a8d
callq 0x52450
movq %rax, %rdi
callq 0x52210
leaq 0xe0c8(%rip), %rsi      # 0x92a88
movq %rbx, %rdi
callq 0x56e34
leaq 0x8(%rsp), %rbx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x85238
movq (%rbx), %rdi
leaq 0x8b03(%rip), %rsi      # 0x8d4e5
callq 0x52520
testq %rax, %rax
je 0x84d32
movq %rax, %rdi
callq 0x52460
movq 0x2d589(%rip), %rdi     # 0xb1f88
leaq 0xe08e(%rip), %rsi      # 0x92a94
callq 0x52450
movq %rax, %rdi
callq 0x52210
movq 0x8(%rsp), %rdi
pushq $0x2
popq %rsi
callq 0x525f0
testq %rax, %rax
je 0x84db0
movq %rax, %r15
leaq 0xe055(%rip), %rsi      # 0x92a88
movq %rax, %rdi
callq 0x52430
testq %rax, %rax
je 0x84e42
movq %rax, %rbx
movq 0x2d53a(%rip), %rdi     # 0xb1f88
leaq 0xe046(%rip), %rsi      # 0x92a9b
callq 0x52450
movq %rax, %rdi
callq 0x52210
movq %r14, %rdi
callq *%rbx
movq 0x2d51a(%rip), %rdi     # 0xb1f88
leaq 0xe02d(%rip), %rsi      # 0x92aa2
callq 0x52450
movq %rax, %rdi
callq 0x52210
movq 0x2d4ff(%rip), %rdi     # 0xb1f88
leaq 0xe019(%rip), %rsi      # 0x92aa9
callq 0x52450
movq %rax, %rdi
callq 0x52210
leaq 0x8(%rsp), %rdi
callq 0x52888
movq %r14, %rdi
callq 0x84774
movq %r14, %rax
addq $0x338, %rsp            # imm = 0x338
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x88, %edi
callq 0x52220
movq %rax, %r14
leaq 0x6e44(%rip), %rsi      # 0x8b91c
leaq 0x318(%rsp), %rdi
leaq 0x4(%rsp), %rdx
callq 0x56548
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x8067c
leaq 0x8(%rsp), %rdi
callq 0x6f406
leaq 0x2f8(%rsp), %rdi
leaq 0x5(%rsp), %rdx
movq %rax, %rsi
callq 0x56548
movq %rbx, %rdi
callq 0x6f406
leaq 0x2d8(%rsp), %rdi
leaq 0x6(%rsp), %rdx
movq %rax, %rsi
callq 0x56548
movb $0x1, %bpl
leaq 0x318(%rsp), %rsi
leaq 0x2f8(%rsp), %rdx
leaq 0x2d8(%rsp), %rcx
movq %r14, %rdi
callq 0x7f052
xorl %ebp, %ebp
leaq 0x2c958(%rip), %rsi     # 0xb14b8
leaq -0x59c1(%rip), %rdx     # 0x7f1a6
movq %r14, %rdi
callq 0x52760
jmp 0x8519a
movq (%r14), %rax
movq %r14, %rdi
callq *0x8(%rax)
movl $0xa8, %edi
callq 0x52220
movq %rax, %r14
leaq 0x6d8b(%rip), %rsi      # 0x8b91c
leaq 0x2b8(%rsp), %rdi
leaq 0x4(%rsp), %rdx
callq 0x56548
leaq 0x72cc(%rip), %rsi      # 0x8be76
leaq 0x298(%rsp), %rdi
leaq 0x5(%rsp), %rdx
callq 0x56548
leaq 0x8(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x7eed0
leaq 0x8(%rsp), %rdi
callq 0x6f406
leaq 0x278(%rsp), %rdi
leaq 0x6(%rsp), %rdx
movq %rax, %rsi
callq 0x56548
leaq 0x7286(%rip), %rsi      # 0x8be76
movq %rbx, %rdi
callq 0x56e34
movq %rax, %rdi
callq 0x6f406
leaq 0x258(%rsp), %rdi
leaq 0x7(%rsp), %rdx
movq %rax, %rsi
callq 0x56548
movb $0x1, %bpl
leaq 0x2b8(%rsp), %rsi
leaq 0x298(%rsp), %rdx
leaq 0x278(%rsp), %rcx
leaq 0x258(%rsp), %r8
movq %r14, %rdi
callq 0x8079c
xorl %ebp, %ebp
leaq 0x2c9d7(%rip), %rsi     # 0xb1620
leaq -0x42c2(%rip), %rdx     # 0x8098e
movq %r14, %rdi
callq 0x52760
jmp 0x8519a
movq (%r14), %rax
movq %r14, %rdi
callq *0x8(%rax)
movl $0xa8, %edi
callq 0x52220
movq %rax, %r14
leaq 0x6ca2(%rip), %rsi      # 0x8b91c
leaq 0x238(%rsp), %rdi
leaq 0x8(%rsp), %rdx
callq 0x56548
leaq 0xddf5(%rip), %rsi      # 0x92a88
leaq 0x218(%rsp), %rdi
leaq 0x4(%rsp), %rdx
callq 0x56548
leaq 0x96b2(%rip), %rsi      # 0x8e35e
leaq 0x1f8(%rsp), %rdi
leaq 0x5(%rsp), %rdx
callq 0x56548
leaq 0xddc3(%rip), %rsi      # 0x92a88
movq %rbx, %rdi
callq 0x56e34
movq %rax, %rdi
callq 0x6f406
leaq 0x1d8(%rsp), %rdi
leaq 0x6(%rsp), %rdx
movq %rax, %rsi
callq 0x56548
movb $0x1, %bpl
leaq 0x238(%rsp), %rsi
leaq 0x218(%rsp), %rdx
leaq 0x1f8(%rsp), %rcx
leaq 0x1d8(%rsp), %r8
movq %r14, %rdi
callq 0x8079c
xorl %ebp, %ebp
leaq 0x2c902(%rip), %rsi     # 0xb1620
leaq -0x4397(%rip), %rdx     # 0x8098e
movq %r14, %rdi
callq 0x52760
jmp 0x8519a
movq (%r14), %rax
movq %r14, %rdi
callq *0x8(%rax)
pushq $0x70
popq %rdi
callq 0x52220
movq %rax, %r14
leaq 0x6bcf(%rip), %rsi      # 0x8b91c
leaq 0x1b8(%rsp), %rdi
leaq 0x4(%rsp), %rdx
callq 0x56548
leaq 0x198(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x525e0
callq 0x52060
movl (%rax), %ecx
movb $0x1, %bpl
leaq 0x1b8(%rsp), %rsi
leaq 0x198(%rsp), %rdx
movq %r14, %rdi
callq 0x85278
xorl %ebp, %ebp
leaq 0x2cb44(%rip), %rsi     # 0xb18e0
leaq 0x5fd(%rip), %rdx       # 0x853a0
movq %r14, %rdi
callq 0x52760
jmp 0x8519a
movl $0x88, %edi
callq 0x52220
movq %rax, %r14
leaq 0x6b58(%rip), %rsi      # 0x8b91c
leaq 0x178(%rsp), %rdi
leaq 0x4(%rsp), %rdx
callq 0x56548
leaq 0x158(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x525e0
callq 0x52440
leaq 0x138(%rsp), %rdi
leaq 0x5(%rsp), %rdx
movq %rax, %rsi
callq 0x56548
movb $0x1, %bpl
leaq 0x178(%rsp), %rsi
leaq 0x158(%rsp), %rdx
leaq 0x138(%rsp), %rcx
movq %r14, %rdi
callq 0x853c0
xorl %ebp, %ebp
leaq 0x2caca(%rip), %rsi     # 0xb18f8
leaq 0x6c1(%rip), %rdx       # 0x854f6
movq %r14, %rdi
callq 0x52760
jmp 0x8519a
movq %r15, %rdi
callq 0x523b0
movl $0xc8, %edi
callq 0x52220
movq %rax, %r14
leaq 0x6abe(%rip), %rsi      # 0x8b91c
leaq 0x118(%rsp), %rdi
leaq 0x4(%rsp), %rdx
callq 0x56548
leaq 0xf8(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x525e0
leaq 0xdbff(%rip), %rsi      # 0x92a88
leaq 0xd8(%rsp), %rdi
leaq 0x5(%rsp), %rdx
callq 0x56548
callq 0x52440
leaq 0xb8(%rsp), %rdi
leaq 0x6(%rsp), %rdx
movq %rax, %rsi
callq 0x56548
movb $0x1, %bpl
leaq 0x118(%rsp), %rsi
leaq 0xf8(%rsp), %rdx
leaq 0xd8(%rsp), %rcx
leaq 0xb8(%rsp), %r8
movq %r14, %rdi
callq 0x85516
xorl %ebp, %ebp
leaq 0x2ca27(%rip), %rsi     # 0xb1910
leaq 0x86e(%rip), %rdx       # 0x8575e
movq %r14, %rdi
callq 0x52760
jmp 0x8519a
movq %rax, %rbx
leaq 0x258(%rsp), %rdi
callq 0x52888
jmp 0x84f2b
jmp 0x84f25
movq %rax, %rbx
leaq 0x1d8(%rsp), %rdi
callq 0x52888
jmp 0x84f67
jmp 0x84f61
movq %rax, %rbx
movb $0x1, %bpl
leaq 0x278(%rsp), %rdi
callq 0x52888
jmp 0x84f7c
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0x52888
jmp 0x84f52
movq %rax, %rbx
movb $0x1, %bpl
leaq 0xd8(%rsp), %rdi
callq 0x52888
jmp 0x84fb5
movq %rax, %rbx
movb $0x1, %bpl
leaq 0x1f8(%rsp), %rdi
callq 0x52888
jmp 0x84ff1
movq %rax, %rbx
movb $0x1, %bpl
leaq 0x8(%rsp), %rdi
callq 0x56fca
jmp 0x85006
movq %rax, %rbx
leaq 0x2d8(%rsp), %rdi
callq 0x52888
jmp 0x84fa0
movq %rax, %rbx
movb $0x1, %bpl
leaq 0x2f8(%rsp), %rdi
callq 0x52888
jmp 0x8501b
movq %rax, %rbx
movb $0x1, %bpl
leaq 0xf8(%rsp), %rdi
callq 0x52888
jmp 0x8502d
movq %rax, %rbx
leaq 0x138(%rsp), %rdi
callq 0x52888
jmp 0x84fdc
movq %rax, %rbx
movb $0x1, %bpl
leaq 0x158(%rsp), %rdi
callq 0x52888
jmp 0x8503d
movq %rax, %rbx
movb $0x1, %bpl
leaq 0x218(%rsp), %rdi
callq 0x52888
jmp 0x8507c
movq %rax, %rbx
movb $0x1, %bpl
leaq 0x298(%rsp), %rdi
callq 0x52888
jmp 0x8508c
movq %rax, %rbx
movb $0x1, %bpl
leaq 0x8(%rsp), %rdi
callq 0x56fca
jmp 0x8509c
movq %rax, %rbx
movb $0x1, %bpl
leaq 0x118(%rsp), %rdi
jmp 0x85067
movq %rax, %rbx
movb $0x1, %bpl
leaq 0x178(%rsp), %rdi
jmp 0x85067
movq %rax, %rbx
leaq 0x198(%rsp), %rdi
callq 0x52888
jmp 0x8505f
movq %rax, %rbx
movb $0x1, %bpl
leaq 0x1b8(%rsp), %rdi
callq 0x52888
testb %bpl, %bpl
jne 0x850ba
jmp 0x85216
movq %rax, %rbx
movb $0x1, %bpl
leaq 0x238(%rsp), %rdi
jmp 0x850a4
movq %rax, %rbx
movb $0x1, %bpl
leaq 0x2b8(%rsp), %rdi
jmp 0x850a4
movq %rax, %rbx
movb $0x1, %bpl
leaq 0x318(%rsp), %rdi
callq 0x52888
testb %bpl, %bpl
jne 0x850ce
jmp 0x8522f
jmp 0x850b7
jmp 0x850b7
movq %rax, %rbx
movq %r14, %rdi
callq 0x52350
jmp 0x85216
jmp 0x850cb
jmp 0x850cb
movq %rax, %rbx
movq %r14, %rdi
callq 0x52350
jmp 0x8522f
movq %rax, %rbx
cmpl $0x1, %edx
jne 0x85216
movq %rbx, %rdi
callq 0x521b0
movq %rax, %rbx
movq %r15, %rdi
callq 0x523b0
movl $0xa8, %edi
callq 0x52220
movq %rax, %r14
leaq 0x680e(%rip), %rsi      # 0x8b91c
leaq 0x98(%rsp), %rdi
leaq 0x4(%rsp), %rdx
callq 0x56548
leaq 0x78(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x525e0
leaq 0xd952(%rip), %rsi      # 0x92a88
leaq 0x58(%rsp), %rdi
leaq 0x5(%rsp), %rdx
callq 0x56548
movq (%rbx), %rax
movq %rbx, %rdi
callq *0x10(%rax)
leaq 0x38(%rsp), %rdi
leaq 0x6(%rsp), %rdx
movq %rax, %rsi
callq 0x56548
movb $0x1, %bpl
leaq 0x98(%rsp), %rsi
leaq 0x78(%rsp), %rdx
leaq 0x58(%rsp), %rcx
leaq 0x38(%rsp), %r8
movq %r14, %rdi
callq 0x8578e
xorl %ebp, %ebp
leaq 0x2c79d(%rip), %rsi     # 0xb1928
leaq 0x7d2(%rip), %rdx       # 0x85964
movq %r14, %rdi
callq 0x52760
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x52888
jmp 0x851af
movq %rax, %rbx
movb $0x1, %bpl
leaq 0x58(%rsp), %rdi
callq 0x52888
jmp 0x851c1
movq %rax, %rbx
movb $0x1, %bpl
leaq 0x78(%rsp), %rdi
callq 0x52888
jmp 0x851d3
movq %rax, %rbx
movb $0x1, %bpl
leaq 0x98(%rsp), %rdi
callq 0x52888
testb %bpl, %bpl
jne 0x851ea
jmp 0x851f2
movq %rax, %rbx
movq %r14, %rdi
callq 0x52350
callq 0x52720
jmp 0x85216
movq %rax, %rdi
callq 0x56ca4
movq %rax, %rbx
pushq $0x10
popq %rsi
movq %r14, %rdi
callq 0x52480
jmp 0x8522f
jmp 0x85213
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x52888
jmp 0x8522f
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x56fca
movq %rbx, %rdi
callq 0x527a0
nop