// File: /workspace/github_c_makefile_star2_O1/adelciotto[P]scg/examples/../scg.h
// Function: scg_image_new_from_bmp
scg_image_t *scg_image_new_from_bmp(const char *filepath) {
    SDL_Surface *surface = SDL_LoadBMP(filepath);
    if (surface == NULL) {
        scg_log_errorf("Failed to load image file at %s. %s", filepath,
                       SDL_GetError());

        return NULL;
    }

    // Convert the surface to RGBA32 pixel format.
    SDL_PixelFormat *pixel_format = SDL_AllocFormat(SCG__IMAGE_PIXEL_FORMAT);
    if (pixel_format == NULL) {
        scg_log_errorf(
            "Failed to allocate pixel format for image file at %s. %s",
            filepath, SDL_GetError());

        SDL_FreeSurface(surface);
        return NULL;
    }
    SDL_Surface *converted_surface =
        SDL_ConvertSurface(surface, pixel_format, 0);
    if (converted_surface == NULL) {
        scg_log_errorf("Failed to convert surface for image file at %s. %s",
                       filepath, SDL_GetError());

        SDL_FreeFormat(pixel_format);
        SDL_FreeSurface(surface);
        return NULL;
    }

    // We no longer need the original surface.
    SDL_FreeFormat(pixel_format);
    SDL_FreeSurface(surface);

    int surface_w = converted_surface->w;
    int surface_h = converted_surface->h;
    int surface_pitch = converted_surface->pitch;
    uint32_t *surface_pixels = (uint32_t *)converted_surface->pixels;

    // Allocate new pixel buffer and copy over the converted surface pixel data.
    uint32_t *pixels = malloc(surface_pitch * surface_h);
    if (pixels == NULL) {
        scg_log_errorf("Failed to allocate memory for image file at %s",
                       filepath);

        SDL_FreeSurface(converted_surface);
        return NULL;
    }
    memcpy(pixels, surface_pixels, surface_pitch * surface_h);

    scg_image_t *image = malloc(sizeof(*image));
    if (image == NULL) {
        scg_log_error("Failed to allocate memory for image");

        free(pixels);
        SDL_FreeSurface(converted_surface);
        return NULL;
    }

    // Assign all the image properties.
    image->width = surface_w;
    image->height = surface_h;
    image->pitch = surface_pitch;
    image->pixels = pixels;
    image->blend_mode = SCG_BLEND_MODE_NONE;

    // We no longer need the converted surface.
    SDL_FreeSurface(converted_surface);

    return image;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rdi, %rbx
leaq 0x37e4(%rip), %rsi      # 0x6121
callq 0x20e0
movq %rax, %rdi
movl $0x1, %esi
callq 0x2210
testq %rax, %rax
je 0x2a16
movq %rax, %r12
movl $0x16362004, %edi       # imm = 0x16362004
callq 0x2130
testq %rax, %rax
je 0x2a4e
movq %rax, %r13
movq %r12, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x23d0
testq %rax, %rax
je 0x2a83
movq %rax, %r15
movq %r13, %rdi
callq 0x2220
movq %r12, %rdi
callq 0x2230
movl 0x10(%r15), %eax
movl %eax, 0x14(%rsp)
movl 0x14(%r15), %ecx
movl 0x18(%r15), %ebp
movq 0x20(%r15), %r14
movl %ebp, %eax
movl %ecx, 0x10(%rsp)
imull %ecx, %eax
movslq %eax, %r13
movq %r13, %rdi
callq 0x22f0
testq %rax, %rax
je 0x2ac3
movq %rax, %r12
movq %rax, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x21c0
movl $0x20, %edi
callq 0x22f0
testq %rax, %rax
je 0x2aef
movq %rax, %r14
movl 0x14(%rsp), %eax
movl %eax, (%r14)
movl 0x10(%rsp), %eax
movl %eax, 0x4(%r14)
movl %ebp, 0x8(%r14)
movq %r12, 0x10(%r14)
movl $0x0, 0x18(%r14)
jmp 0x2b25
callq 0x2190
movq %rax, (%rsp)
leaq 0x36fe(%rip), %rsi      # 0x6124
leaq 0x3730(%rip), %rdx      # 0x615d
leaq 0x3692(%rip), %rcx      # 0x60c6
xorl %r14d, %r14d
xorl %edi, %edi
movl $0x280, %r8d            # imm = 0x280
movq %rbx, %r9
xorl %eax, %eax
callq 0x22e0
jmp 0x2b2d
callq 0x2190
movq %rax, (%rsp)
leaq 0x3716(%rip), %rsi      # 0x6174
leaq 0x36f8(%rip), %rdx      # 0x615d
leaq 0x365a(%rip), %rcx      # 0x60c6
xorl %r14d, %r14d
xorl %edi, %edi
movl $0x28a, %r8d            # imm = 0x28A
movq %rbx, %r9
xorl %eax, %eax
callq 0x22e0
jmp 0x2abe
callq 0x2190
movq %rax, (%rsp)
leaq 0x372f(%rip), %rsi      # 0x61c2
leaq 0x36c3(%rip), %rdx      # 0x615d
leaq 0x3625(%rip), %rcx      # 0x60c6
xorl %r14d, %r14d
xorl %edi, %edi
movl $0x293, %r8d            # imm = 0x293
movq %rbx, %r9
xorl %eax, %eax
callq 0x22e0
movq %r13, %rdi
callq 0x2220
movq %r12, %rdi
jmp 0x2b28
leaq 0x3740(%rip), %rsi      # 0x620a
leaq 0x368c(%rip), %rdx      # 0x615d
leaq 0x35ee(%rip), %rcx      # 0x60c6
xorl %r14d, %r14d
xorl %edi, %edi
movl $0x2a7, %r8d            # imm = 0x2A7
movq %rbx, %r9
xorl %eax, %eax
callq 0x22e0
jmp 0x2b25
leaq 0x35aa(%rip), %rsi      # 0x60a0
leaq 0x3660(%rip), %rdx      # 0x615d
leaq 0x35c2(%rip), %rcx      # 0x60c6
leaq 0x35f2(%rip), %r9       # 0x60fd
xorl %r14d, %r14d
xorl %edi, %edi
movl $0x2b0, %r8d            # imm = 0x2B0
xorl %eax, %eax
callq 0x22e0
movq %r12, %rdi
callq 0x2290
movq %r15, %rdi
callq 0x2230
movq %r14, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq