// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x70, %rsp
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x44ea(%rip), %rsi      # 0x7111
callq 0x20e0
movq %rax, %rdi
movl $0x1, %esi
callq 0x2210
movq %rax, -0x18(%rbp)
cmpq $0x0, -0x18(%rbp)
jne 0x2c8a
movq -0x10(%rbp), %rax
movq %rax, -0x58(%rbp)
callq 0x2190
movq -0x58(%rbp), %r9
xorl %edi, %edi
leaq 0x44b6(%rip), %rsi      # 0x7114
leaq 0x44e8(%rip), %rdx      # 0x714d
leaq 0x444a(%rip), %rcx      # 0x70b6
movl $0x280, %r8d            # imm = 0x280
movq %rax, (%rsp)
movb $0x0, %al
callq 0x22e0
movq $0x0, -0x8(%rbp)
jmp 0x2eac
movl $0x16362004, %edi       # imm = 0x16362004
callq 0x2130
movq %rax, -0x20(%rbp)
cmpq $0x0, -0x20(%rbp)
jne 0x2cee
movq -0x10(%rbp), %rax
movq %rax, -0x60(%rbp)
callq 0x2190
movq -0x60(%rbp), %r9
xorl %edi, %edi
leaq 0x44ab(%rip), %rsi      # 0x7164
leaq 0x448d(%rip), %rdx      # 0x714d
leaq 0x43ef(%rip), %rcx      # 0x70b6
movl $0x28a, %r8d            # imm = 0x28A
movq %rax, (%rsp)
movb $0x0, %al
callq 0x22e0
movq -0x18(%rbp), %rdi
callq 0x2230
movq $0x0, -0x8(%rbp)
jmp 0x2eac
movq -0x18(%rbp), %rdi
movq -0x20(%rbp), %rsi
xorl %edx, %edx
callq 0x23e0
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x2d60
movq -0x10(%rbp), %rax
movq %rax, -0x68(%rbp)
callq 0x2190
movq -0x68(%rbp), %r9
xorl %edi, %edi
leaq 0x4490(%rip), %rsi      # 0x71b2
leaq 0x4424(%rip), %rdx      # 0x714d
leaq 0x4386(%rip), %rcx      # 0x70b6
movl $0x293, %r8d            # imm = 0x293
movq %rax, (%rsp)
movb $0x0, %al
callq 0x22e0
movq -0x20(%rbp), %rdi
callq 0x2220
movq -0x18(%rbp), %rdi
callq 0x2230
movq $0x0, -0x8(%rbp)
jmp 0x2eac
movq -0x20(%rbp), %rdi
callq 0x2220
movq -0x18(%rbp), %rdi
callq 0x2230
movq -0x28(%rbp), %rax
movl 0x10(%rax), %eax
movl %eax, -0x2c(%rbp)
movq -0x28(%rbp), %rax
movl 0x14(%rax), %eax
movl %eax, -0x30(%rbp)
movq -0x28(%rbp), %rax
movl 0x18(%rax), %eax
movl %eax, -0x34(%rbp)
movq -0x28(%rbp), %rax
movq 0x20(%rax), %rax
movq %rax, -0x40(%rbp)
movl -0x34(%rbp), %eax
imull -0x30(%rbp), %eax
movslq %eax, %rdi
callq 0x22f0
movq %rax, -0x48(%rbp)
cmpq $0x0, -0x48(%rbp)
jne 0x2df4
movq -0x10(%rbp), %r9
xorl %edi, %edi
leaq 0x4437(%rip), %rsi      # 0x71fa
leaq 0x4383(%rip), %rdx      # 0x714d
leaq 0x42e5(%rip), %rcx      # 0x70b6
movl $0x2a7, %r8d            # imm = 0x2A7
movb $0x0, %al
callq 0x22e0
movq -0x28(%rbp), %rdi
callq 0x2230
movq $0x0, -0x8(%rbp)
jmp 0x2eac
movq -0x48(%rbp), %rdi
movq -0x40(%rbp), %rsi
movl -0x34(%rbp), %eax
imull -0x30(%rbp), %eax
movslq %eax, %rdx
callq 0x21c0
movl $0x20, %edi
callq 0x22f0
movq %rax, -0x50(%rbp)
cmpq $0x0, -0x50(%rbp)
jne 0x2e67
xorl %edi, %edi
leaq 0x4267(%rip), %rsi      # 0x7090
leaq 0x431d(%rip), %rdx      # 0x714d
leaq 0x427f(%rip), %rcx      # 0x70b6
movl $0x2b0, %r8d            # imm = 0x2B0
leaq 0x42a9(%rip), %r9       # 0x70ed
movb $0x0, %al
callq 0x22e0
movq -0x48(%rbp), %rdi
callq 0x2290
movq -0x28(%rbp), %rdi
callq 0x2230
movq $0x0, -0x8(%rbp)
jmp 0x2eac
movl -0x2c(%rbp), %ecx
movq -0x50(%rbp), %rax
movl %ecx, (%rax)
movl -0x30(%rbp), %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0x4(%rax)
movl -0x34(%rbp), %ecx
movq -0x50(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x48(%rbp), %rcx
movq -0x50(%rbp), %rax
movq %rcx, 0x10(%rax)
movq -0x50(%rbp), %rax
movl $0x0, 0x18(%rax)
movq -0x28(%rbp), %rdi
callq 0x2230
movq -0x50(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x70, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)