// File: /workspace/github_c_makefile_star2_O1/Nealsbo[P]C-Software-Renderer/src/objloader.c
// Function: Model_CreateBaseTriangle
obj_model_t *Model_CreateBaseTriangle( 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   = 3;                                             //Allocation prepared data for model, box in that case
    model->trisCount   = 1;

    float v_arr[3][3]  = {{-1.0, -1.0, 0.0},
                          { 0.0,  1.0, 0.0},
                          { 1.0, -1.0, 0.0}};

    float vn_arr[3][3] = {{ 0.0, 0.0, 1.0},
                          { 0.0, 0.0, 1.0},
                          { 0.0, 0.0, 1.0}};

    float vt_arr[3][2] = {{ 0.0,  0.0},
                          { 0.5,  1.0},
                          { 1.0,  0.0}};

    int   f_arr[1][9]  = {{ 1, 1, 1, 2, 2, 2, 3, 3, 3}};

    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(0, 245, 116, 255);
    model->diffmap     = NULL;
    model->normalmap   = NULL;

    model->vertices    = malloc(sizeof(vec3) * 3);
    if(model->vertices == NULL){
        printf("Error: Malloc vertices error'%s'\n", model->name);
        Model_Free(model);
        return NULL;
    }
    for(i = 0; i < 3; i++){
        model->vertices[i] = vec3_create(v_arr[i][0], v_arr[i][1], v_arr[i][2]);
    }

    model->normals     = (vec3 *)malloc(sizeof(vec3) * 3);
    for(i = 0; i < 3; i++){
        model->normals[i] = vec3_create(vn_arr[i][0], vn_arr[i][1], vn_arr[i][2]);
    }

    model->textcoords  = (vec2*)malloc(sizeof(vec2) * 3);
    for(i = 0; i < 3; i++){
        model->textcoords[i] = vec2_create(vt_arr[i][0], vt_arr[i][1]);
    }

    model->faces  = (obj_face_t *)malloc(sizeof(obj_face_t));
    model->faces[0]  = Face_Create(3, vec3i_arrayToVec3ip(9, f_arr[0] ));

    model->diffmap = (texture_t *)malloc(sizeof(texture_t));
    model->diffmap->bitmap = Bitmap_LoadPPM6("./assets/texture.ppm");

    return model;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x88, %rsp
movq %rdi, %r14
movl $0x170, %edi            # imm = 0x170
callq 0x11a0
testq %rax, %rax
je 0x4d54
movq %rax, %rbx
movl $0x100, %edx            # imm = 0x100
movq %rax, %rdi
movq %r14, %rsi
callq 0x1100
movl $0x3, %eax
movl %eax, 0x120(%rbx)
movl $0x1, 0x12c(%rbx)
xorps %xmm0, %xmm0
movaps %xmm0, (%rsp)
movaps %xmm0, 0x10(%rsp)
movl $0x0, 0x20(%rsp)
movabsq $-0x407fffff40800000, %rcx # imm = 0xBF800000BF800000
movq %rcx, (%rsp)
movl $0x3f800000, %ecx       # imm = 0x3F800000
movl %ecx, 0x10(%rsp)
movabsq $-0x407fffffc0800000, %rdx # imm = 0xBF8000003F800000
movq %rdx, 0x18(%rsp)
movaps %xmm0, 0x30(%rsp)
movaps %xmm0, 0x40(%rsp)
movl %ecx, 0x38(%rsp)
movl %ecx, 0x44(%rsp)
movl %ecx, 0x50(%rsp)
movl %eax, 0x80(%rsp)
movaps 0x3a1c(%rip), %xmm0     # 0x85a0
movaps %xmm0, 0x70(%rsp)
movaps 0x3a00(%rip), %xmm0     # 0x8590
movaps %xmm0, 0x60(%rsp)
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
xorps %xmm2, %xmm2
callq 0x2a5a
movlps %xmm0, 0x130(%rbx)
movss %xmm1, 0x138(%rbx)
xorps %xmm0, %xmm0
xorps %xmm1, %xmm1
xorps %xmm2, %xmm2
callq 0x2a5a
movlps %xmm0, 0x13c(%rbx)
movss %xmm1, 0x144(%rbx)
movss 0x343d(%rip), %xmm0     # 0x8014
movaps %xmm0, %xmm1
movaps %xmm0, %xmm2
callq 0x2a5a
movlps %xmm0, 0x148(%rbx)
movss %xmm1, 0x150(%rbx)
xorl %edi, %edi
movl $0xf5, %esi
movl $0x74, %edx
movl $0xff, %ecx
callq 0x1e50
movl %eax, 0x154(%rbx)
movl $0x24, %edi
callq 0x11a0
xorps %xmm0, %xmm0
movups %xmm0, 0x158(%rbx)
movq %rax, 0x100(%rbx)
testq %rax, %rax
je 0x4d69
movl $0x8, %r14d
movq 0x100(%rbx), %r15
movss -0x8(%rsp,%r14), %xmm0
movss -0x4(%rsp,%r14), %xmm1
movss (%rsp,%r14), %xmm2
callq 0x2a5a
movlps %xmm0, -0x8(%r15,%r14)
movss %xmm1, (%r15,%r14)
addq $0xc, %r14
cmpq $0x2c, %r14
jne 0x4c37
movl $0x24, %edi
callq 0x11a0
movq %rax, 0x110(%rbx)
movl $0x8, %r14d
movq 0x110(%rbx), %r15
movss 0x28(%rsp,%r14), %xmm0
movss 0x2c(%rsp,%r14), %xmm1
movss 0x30(%rsp,%r14), %xmm2
callq 0x2a5a
movlps %xmm0, -0x8(%r15,%r14)
movss %xmm1, (%r15,%r14)
addq $0xc, %r14
cmpq $0x2c, %r14
jne 0x4c84
movl $0x18, %edi
callq 0x11a0
movq %rax, 0x118(%rbx)
xorl %r14d, %r14d
leaq 0x389a(%rip), %r15      # 0x8570
movq 0x118(%rbx), %r12
movss (%r15,%r14,8), %xmm0
movss 0x4(%r15,%r14,8), %xmm1
callq 0x27fd
movlps %xmm0, (%r12,%r14,8)
incq %r14
cmpq $0x3, %r14
jne 0x4cd6
movl $0x10, %edi
callq 0x11a0
movq %rax, %r14
movq %rax, 0x108(%rbx)
leaq 0x60(%rsp), %rsi
movl $0x9, %edi
callq 0x2939
movl $0x3, (%r14)
movq %rax, 0x8(%r14)
movl $0x8, %edi
callq 0x11a0
movq %rax, 0x158(%rbx)
leaq 0x3d67(%rip), %rdi      # 0x8aaa
callq 0x223a
movq 0x158(%rbx), %rcx
movq %rax, (%rcx)
jmp 0x4d84
leaq 0x3d05(%rip), %rdi      # 0x8a60
xorl %ebx, %ebx
movq %r14, %rsi
xorl %eax, %eax
callq 0x1040
jmp 0x4d84
leaq 0x3d18(%rip), %rdi      # 0x8a88
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1040
movq %rbx, %rdi
callq 0x4d96
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq