// File: /workspace/github_c_makefile_star2_O2/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 0x3808(%rip), %rsi      # 0x6131
callq 0x20e0
pushq $0x1
popq %rsi
movq %rax, %rdi
callq 0x2210
testq %rax, %rax
je 0x29fb
movq %rax, %r12
movl $0x16362004, %edi       # imm = 0x16362004
callq 0x2130
testq %rax, %rax
je 0x2a33
movq %rax, %r13
movq %r12, %rdi
movq %rax, %rsi
xorl %edx, %edx
callq 0x23d0
testq %rax, %rax
je 0x2a68
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 0x2aa8
movq %rax, %r12
movq %rax, %rdi
movq %r14, %rsi
movq %r13, %rdx
callq 0x21c0
pushq $0x20
popq %rdi
callq 0x22f0
testq %rax, %rax
je 0x2ad4
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)
andl $0x0, 0x18(%r14)
jmp 0x2b0a
callq 0x2190
movq %rax, (%rsp)
leaq 0x3729(%rip), %rsi      # 0x6134
leaq 0x375b(%rip), %rdx      # 0x616d
leaq 0x36bd(%rip), %rcx      # 0x60d6
xorl %r14d, %r14d
xorl %edi, %edi
movl $0x280, %r8d            # imm = 0x280
movq %rbx, %r9
xorl %eax, %eax
callq 0x22e0
jmp 0x2b12
callq 0x2190
movq %rax, (%rsp)
leaq 0x3741(%rip), %rsi      # 0x6184
leaq 0x3723(%rip), %rdx      # 0x616d
leaq 0x3685(%rip), %rcx      # 0x60d6
xorl %r14d, %r14d
xorl %edi, %edi
movl $0x28a, %r8d            # imm = 0x28A
movq %rbx, %r9
xorl %eax, %eax
callq 0x22e0
jmp 0x2aa3
callq 0x2190
movq %rax, (%rsp)
leaq 0x375a(%rip), %rsi      # 0x61d2
leaq 0x36ee(%rip), %rdx      # 0x616d
leaq 0x3650(%rip), %rcx      # 0x60d6
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 0x2b0d
leaq 0x376b(%rip), %rsi      # 0x621a
leaq 0x36b7(%rip), %rdx      # 0x616d
leaq 0x3619(%rip), %rcx      # 0x60d6
xorl %r14d, %r14d
xorl %edi, %edi
movl $0x2a7, %r8d            # imm = 0x2A7
movq %rbx, %r9
xorl %eax, %eax
callq 0x22e0
jmp 0x2b0a
leaq 0x35d5(%rip), %rsi      # 0x60b0
leaq 0x368b(%rip), %rdx      # 0x616d
leaq 0x35ed(%rip), %rcx      # 0x60d6
leaq 0x361d(%rip), %r9       # 0x610d
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