// File: /workspace/github_c_makefile_star2_O0/Nealsbo[P]C-Software-Renderer/src/objloader.c
// Function: Model_CreateBaseBox
obj_model_t *Model_CreateBaseBox( const char *model_name){
    int i;
    obj_model_t *model = (obj_model_t *)malloc(sizeof(obj_model_t));
    if(model == NULL){
        printf("Error: Cannot create model object '%s'\n", model_name);
        return NULL;
    }

    memcpy(model->name, model_name, MAX_NAME_LENGTH);                   //Set object name by argument

    model->vertCount   = 8;                                             //Allocation prepared data for model, box in that case
    model->trisCount   = 12;

    float    v_arr[8][3]  = {{-1.0, -1.0,  1.0},                        //Vertices data for box model
                             { 1.0, -1.0,  1.0},
                             {-1.0,  1.0,  1.0},
                             { 1.0,  1.0,  1.0},
                             {-1.0,  1.0, -1.0},
                             { 1.0,  1.0, -1.0},
                             {-1.0, -1.0, -1.0},
                             { 1.0, -1.0, -1.0}};

    float   vn_arr[24][3] = {{ 0.0,  0.0,  1.0},
                             { 0.0,  0.0,  1.0},
                             { 0.0,  0.0,  1.0},
                             { 0.0,  0.0,  1.0},
                             { 0.0,  1.0,  0.0},
                             { 0.0,  1.0,  0.0},
                             { 0.0,  1.0,  0.0},
                             { 0.0,  1.0,  0.0},
                             { 0.0,  0.0, -1.0},
                             { 0.0,  0.0, -1.0},
                             { 0.0,  0.0, -1.0},
                             { 0.0,  0.0, -1.0},
                             { 0.0, -1.0,  0.0},
                             { 0.0, -1.0,  0.0},
                             { 0.0, -1.0,  0.0},
                             { 0.0, -1.0,  0.0},
                             { 1.0,  0.0,  0.0},
                             { 1.0,  0.0,  0.0},
                             { 1.0,  0.0,  0.0},
                             { 1.0,  0.0,  0.0},
                             {-1.0,  0.0,  0.0},
                             {-1.0,  0.0,  0.0},
                             {-1.0,  0.0,  0.0},
                             {-1.0,  0.0,  0.0}};

    float   vt_arr[24][2] = {{ 0.0, 0.0},
                             { 1.0, 0.0},
                             { 1.0, 1.0},
                             { 0.0, 1.0},
                             {-0.0, 0.0},
                             { 1.0, 0.0},
                             { 1.0, 1.0},
                             {-0.0, 1.0},
                             {-0.0, 0.0},
                             { 1.0, 0.0},
                             { 1.0, 1.0},
                             {-0.0, 1.0},
                             { 0.0, 0.0},
                             { 1.0, 0.0},
                             { 1.0, 1.0},
                             { 0.0, 1.0},
                             { 0.0, 0.0},
                             { 1.0, 0.0},
                             { 1.0, 1.0},
                             { 0.0, 1.0},
                             { 0.0, 0.0},
                             { 1.0, 0.0},
                             { 1.0, 1.0},
                             { 0.0, 1.0}};

    int      f_arr[12][9] = {{1,  1,  1,  2,  2,  2,  3,  4,  3},       //Face index data for box model
                             {3,  4,  3,  2,  2,  2,  4,  3,  4},
                             {3,  5,  5,  4,  6,  6,  5,  8,  7},
                             {5,  8,  7,  4,  6,  6,  6,  7,  8},
                             {5,  9,  9,  6, 10, 10,  7, 12, 11},
                             {7, 12, 11,  6, 10, 10,  8, 11, 12},
                             {7, 13, 13,  8, 14, 14,  1, 16, 15},
                             {1, 16, 15,  8, 14, 14,  2, 15, 16},
                             {2, 17, 17,  8, 18, 18,  4, 20, 19},
                             {4, 20, 19,  8, 18, 18,  6, 19, 20},
                             {7, 21, 21,  1, 22, 22,  5, 24, 23},
                             {5, 24, 23,  1, 22, 22,  3, 23, 24}};

    model->position    = vec3_create(0.0, 0.0, 0.0);
    model->rotation    = vec3_create(0.0, 0.0, 0.0);
    model->scale       = vec3_create(1.0, 1.0, 1.0);
    model->baseColor   = Color_Init(255, 255, 255, 255);
    model->diffmap     = NULL;
    model->normalmap   = NULL;

    model->vertices    = (vec3 *)malloc(sizeof(vec3) * 8);
    for(i = 0; i < 8; i++){
        model->vertices[i] = (vec3){v_arr[i][0], v_arr[i][1], v_arr[i][2]};
    }

    model->normals     = (vec3 *)malloc(sizeof(vec3) * 24);
    for(i = 0; i < 24; i++){
        model->normals[i] = vec3_create(vn_arr[i][0], vn_arr[i][1], vn_arr[i][2]);
    }

    model->textcoords  = (vec2 *)malloc(sizeof(vec2) * 24);
    for(i = 0; i < 24; i++){
        model->textcoords[i] = vec2_create(vt_arr[i][0], vt_arr[i][1]);
    }

    model->faces       = (obj_face_t *)malloc(sizeof(obj_face_t) * 12);
    for(i = 0; i < 12; i++){
        model->faces[i]  = Face_Create(3, vec3i_arrayToVec3ip(9, f_arr[i]  ));
    }

    model->diffmap = (texture_t *)malloc(sizeof(texture_t));
    model->diffmap->bitmap = Bitmap_LoadPPM6("./assets/texture.ppm");
    
    model->normalmap = (texture_t *)malloc(sizeof(texture_t));
    model->normalmap->bitmap = Bitmap_LoadPPM6("./assets/texture_n.ppm");

    return model;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x500, %rsp            # imm = 0x500
movq %rdi, -0x10(%rbp)
movl $0x170, %edi            # imm = 0x170
callq 0x11b0
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x9513
movq -0x10(%rbp), %rsi
leaq 0x54a1(%rip), %rdi      # 0xe9a0
movb $0x0, %al
callq 0x1040
movq $0x0, -0x8(%rbp)
jmp 0x9a6a
movq -0x20(%rbp), %rdi
movq -0x10(%rbp), %rsi
movl $0x100, %edx            # imm = 0x100
callq 0x1110
movq -0x20(%rbp), %rax
movl $0x8, 0x120(%rax)
movq -0x20(%rbp), %rax
movl $0xc, 0x12c(%rax)
movaps 0x5088(%rip), %xmm0     # 0xe5d0
movaps %xmm0, -0x30(%rbp)
movaps 0x506d(%rip), %xmm0     # 0xe5c0
movaps %xmm0, -0x40(%rbp)
movaps 0x5052(%rip), %xmm0     # 0xe5b0
movaps %xmm0, -0x50(%rbp)
movaps 0x5037(%rip), %xmm0     # 0xe5a0
movaps %xmm0, -0x60(%rbp)
movaps 0x501c(%rip), %xmm0     # 0xe590
movaps %xmm0, -0x70(%rbp)
movaps 0x5001(%rip), %xmm0     # 0xe580
movaps %xmm0, -0x80(%rbp)
leaq 0x5056(%rip), %rsi      # 0xe5e0
leaq -0x1a0(%rbp), %rdi
movl $0x120, %edx            # imm = 0x120
callq 0x1110
leaq 0x515e(%rip), %rsi      # 0xe700
leaq -0x260(%rbp), %rdi
movl $0xc0, %edx
callq 0x1110
leaq 0x5206(%rip), %rsi      # 0xe7c0
leaq -0x410(%rbp), %rdi
movl $0x1b0, %edx            # imm = 0x1B0
callq 0x1110
movq -0x20(%rbp), %rax
movq %rax, -0x4e0(%rbp)
xorps %xmm2, %xmm2
movss %xmm2, -0x4d4(%rbp)
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0x4270
movq -0x4e0(%rbp), %rax
movss -0x4d4(%rbp), %xmm2
movss %xmm1, -0x428(%rbp)
movlpd %xmm0, -0x430(%rbp)
movl -0x428(%rbp), %ecx
movl %ecx, -0x418(%rbp)
movq -0x430(%rbp), %rcx
movq %rcx, -0x420(%rbp)
movl -0x418(%rbp), %ecx
movl %ecx, 0x138(%rax)
movq -0x420(%rbp), %rcx
movq %rcx, 0x130(%rax)
movq -0x20(%rbp), %rax
movq %rax, -0x4d0(%rbp)
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0x4270
movq -0x4d0(%rbp), %rax
movss %xmm1, -0x448(%rbp)
movlpd %xmm0, -0x450(%rbp)
movl -0x448(%rbp), %ecx
movl %ecx, -0x438(%rbp)
movq -0x450(%rbp), %rcx
movq %rcx, -0x440(%rbp)
movl -0x438(%rbp), %ecx
movl %ecx, 0x144(%rax)
movq -0x440(%rbp), %rcx
movq %rcx, 0x13c(%rax)
movq -0x20(%rbp), %rax
movq %rax, -0x4c8(%rbp)
movss 0x4951(%rip), %xmm2     # 0xe004
movaps %xmm2, %xmm0
movaps %xmm2, %xmm1
callq 0x4270
movq -0x4c8(%rbp), %rax
movss %xmm1, -0x468(%rbp)
movlpd %xmm0, -0x470(%rbp)
movq -0x470(%rbp), %rcx
movq %rcx, -0x45c(%rbp)
movl -0x468(%rbp), %ecx
movl %ecx, -0x454(%rbp)
movq -0x45c(%rbp), %rcx
movq %rcx, 0x148(%rax)
movl -0x454(%rbp), %ecx
movl %ecx, 0x150(%rax)
movq -0x20(%rbp), %rax
movq %rax, -0x4c0(%rbp)
movl $0xff, %ecx
movl %ecx, %edi
movl %ecx, %esi
movl %ecx, %edx
callq 0x2c20
movl %eax, %ecx
movq -0x4c0(%rbp), %rax
movl %ecx, -0x474(%rbp)
movl -0x474(%rbp), %ecx
movl %ecx, 0x154(%rax)
movq -0x20(%rbp), %rax
movq $0x0, 0x158(%rax)
movq -0x20(%rbp), %rax
movq $0x0, 0x160(%rax)
movl $0x60, %edi
callq 0x11b0
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x100(%rax)
movl $0x0, -0x14(%rbp)
cmpl $0x8, -0x14(%rbp)
jge 0x9810
movq -0x20(%rbp), %rax
movq 0x100(%rax), %rax
movslq -0x14(%rbp), %rcx
imulq $0xc, %rcx, %rcx
addq %rcx, %rax
movslq -0x14(%rbp), %rdx
leaq -0x80(%rbp), %rcx
imulq $0xc, %rdx, %rdx
addq %rdx, %rcx
movss (%rcx), %xmm0
movss %xmm0, -0x480(%rbp)
movslq -0x14(%rbp), %rdx
leaq -0x80(%rbp), %rcx
imulq $0xc, %rdx, %rdx
addq %rdx, %rcx
movss 0x4(%rcx), %xmm0
movss %xmm0, -0x47c(%rbp)
movslq -0x14(%rbp), %rdx
leaq -0x80(%rbp), %rcx
imulq $0xc, %rdx, %rdx
addq %rdx, %rcx
movss 0x8(%rcx), %xmm0
movss %xmm0, -0x478(%rbp)
movq -0x480(%rbp), %rcx
movq %rcx, (%rax)
movl -0x478(%rbp), %ecx
movl %ecx, 0x8(%rax)
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x977c
movl $0x120, %edi            # imm = 0x120
callq 0x11b0
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x110(%rax)
movl $0x0, -0x14(%rbp)
cmpl $0x18, -0x14(%rbp)
jge 0x98cc
movq -0x20(%rbp), %rax
movq 0x110(%rax), %rcx
movslq -0x14(%rbp), %rax
shlq $0x2, %rax
leaq (%rax,%rax,2), %rax
addq %rax, %rcx
movq %rcx, -0x4e8(%rbp)
movss -0x1a0(%rbp,%rax), %xmm0
movss -0x19c(%rbp,%rax), %xmm1
movss -0x198(%rbp,%rax), %xmm2
callq 0x4270
movq -0x4e8(%rbp), %rax
movss %xmm1, -0x498(%rbp)
movlpd %xmm0, -0x4a0(%rbp)
movq -0x4a0(%rbp), %rcx
movq %rcx, -0x48c(%rbp)
movl -0x498(%rbp), %ecx
movl %ecx, -0x484(%rbp)
movq -0x48c(%rbp), %rcx
movq %rcx, (%rax)
movl -0x484(%rbp), %ecx
movl %ecx, 0x8(%rax)
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x982f
movl $0xc0, %edi
callq 0x11b0
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x118(%rax)
movl $0x0, -0x14(%rbp)
cmpl $0x18, -0x14(%rbp)
jge 0x9954
movq -0x20(%rbp), %rax
movq 0x118(%rax), %rax
movq %rax, -0x4f0(%rbp)
movslq -0x14(%rbp), %rax
movq %rax, %rcx
movq %rcx, -0x4f8(%rbp)
movss -0x260(%rbp,%rax,8), %xmm0
movss -0x25c(%rbp,%rax,8), %xmm1
callq 0x3b70
movq -0x4f8(%rbp), %rcx
movq -0x4f0(%rbp), %rax
movlpd %xmm0, -0x4a8(%rbp)
movq -0x4a8(%rbp), %rdx
movq %rdx, (%rax,%rcx,8)
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x98eb
movl $0xc0, %edi
callq 0x11b0
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x108(%rax)
movl $0x0, -0x14(%rbp)
cmpl $0xc, -0x14(%rbp)
jge 0x99f8
movq -0x20(%rbp), %rax
movq 0x108(%rax), %rax
movslq -0x14(%rbp), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movq %rax, -0x500(%rbp)
movslq -0x14(%rbp), %rax
leaq -0x410(%rbp), %rsi
imulq $0x24, %rax, %rax
addq %rax, %rsi
movl $0x9, %edi
callq 0x3f80
movq %rax, %rsi
movl $0x3, %edi
callq 0x83c0
movl %eax, %ecx
movq -0x500(%rbp), %rax
movl %ecx, -0x4b8(%rbp)
movq %rdx, -0x4b0(%rbp)
movq -0x4b8(%rbp), %rcx
movq %rcx, (%rax)
movq -0x4b0(%rbp), %rcx
movq %rcx, 0x8(%rax)
movl -0x14(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x14(%rbp)
jmp 0x9973
movl $0x8, %edi
callq 0x11b0
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x158(%rax)
leaq 0x4fd3(%rip), %rdi      # 0xe9ea
callq 0x31f0
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq 0x158(%rax), %rax
movq %rcx, (%rax)
movl $0x8, %edi
callq 0x11b0
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq %rcx, 0x160(%rax)
leaq 0x4fb3(%rip), %rdi      # 0xe9ff
callq 0x31f0
movq %rax, %rcx
movq -0x20(%rbp), %rax
movq 0x160(%rax), %rax
movq %rcx, (%rax)
movq -0x20(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x500, %rsp            # imm = 0x500
popq %rbp
retq
nopw (%rax,%rax)