// File: /workspace/github_cpp_makefile_star2_O3/lvandeve[P]lodepng/examples/example_sdl.cpp
// Function: show(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, unsigned char const*, unsigned int, unsigned int)
int show(const std::string& caption, const unsigned char* rgba, unsigned w, unsigned h) {
  //avoid too large window size by downscaling large image
  unsigned jump = 1;
  if(w / 1024 >= jump) jump = w / 1024 + 1;
  if(h / 1024 >= jump) jump = h / 1024 + 1;

  size_t screenw = w / jump;
  size_t screenh = h / jump;
  size_t pitch = screenw * sizeof(Uint32);
  //init SDL
  SDL_Window* sdl_window;
  SDL_Renderer* sdl_renderer;
  SDL_CreateWindowAndRenderer(screenw, screenh, SDL_WINDOW_OPENGL, &sdl_window, &sdl_renderer);
  SDL_SetWindowTitle(sdl_window, caption.c_str());
  if(!sdl_window) {
    std::cout << "error, no SDL screen" << std::endl;
    return 0;
  }
  SDL_Texture* sdl_texture = SDL_CreateTexture(sdl_renderer, SDL_PIXELFORMAT_ARGB8888,
                                               SDL_TEXTUREACCESS_STREAMING, screenw, screenh);
  std::vector<Uint32> sdl_pixels(screenw * screenh * sizeof(Uint32));

  //plot the pixels of the PNG file
  for(unsigned y = 0; y + jump - 1 < h; y += jump)
  for(unsigned x = 0; x + jump - 1 < w; x += jump) {
    //get RGBA components
    Uint32 r = rgba[4 * y * w + 4 * x + 0]; //red
    Uint32 g = rgba[4 * y * w + 4 * x + 1]; //green
    Uint32 b = rgba[4 * y * w + 4 * x + 2]; //blue
    Uint32 a = rgba[4 * y * w + 4 * x + 3]; //alpha

    //make translucency visible by placing checkerboard pattern behind image
    int checkerColor = 191 + 64 * (((x / 16) % 2) == ((y / 16) % 2));
    r = (a * r + (255 - a) * checkerColor) / 255;
    g = (a * g + (255 - a) * checkerColor) / 255;
    b = (a * b + (255 - a) * checkerColor) / 255;

    //give the color value to the pixel of the screenbuffer
    sdl_pixels[(y * screenw + x) / jump] = 65536 * r + 256 * g + b;
  }

  // render the pixels to the screen
  SDL_UpdateTexture(sdl_texture, NULL, sdl_pixels.data(), pitch);
  SDL_RenderClear(sdl_renderer);
  SDL_RenderCopy(sdl_renderer, sdl_texture, NULL, NULL);
  SDL_RenderPresent(sdl_renderer);

  //pause until you press escape and meanwhile redraw screen
  SDL_Event event;
  int done = 0;
  while(done == 0) {
    while(SDL_PollEvent(&event)) {
      if(event.type == SDL_QUIT) done = 2;
      else if(SDL_GetKeyboardState(NULL)[SDLK_ESCAPE]) done = 2;
      else if(event.type == SDL_KEYDOWN) done = 1; //press any other key for next image
    }
    SDL_Delay(5); //pause 5 ms so it consumes less processing power
  }

  SDL_Quit();
  return done == 2 ? 1 : 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movl %ecx, %ebx
movl %edx, %ebp
movq %rsi, %r14
movl %edx, %eax
shrl $0xa, %eax
incl %eax
cmpl $0x400, %edx            # imm = 0x400
movl $0x1, %ecx
cmovael %eax, %ecx
movq %rdi, 0x10(%rsp)
movl %ebx, %eax
shrl $0xa, %eax
leal 0x1(%rax), %r15d
cmpl %ecx, %eax
cmovbl %ecx, %r15d
movl %edx, %eax
xorl %edx, %edx
divl %r15d
movl %eax, %r13d
movl %ebx, %eax
xorl %edx, %edx
divl %r15d
leaq 0x68(%rsp), %r12
leaq 0x18(%rsp), %r8
movl %r13d, %edi
movl %eax, 0x20(%rsp)
movl %eax, %esi
movl $0x2, %edx
movq %r12, %rcx
callq 0x2070
movq (%r12), %rdi
movq 0x10(%rsp), %rax
movq (%rax), %rsi
callq 0x2260
cmpq $0x0, (%r12)
je 0x10c3b
movl %r13d, %eax
shll $0x2, %eax
movl %eax, 0x2c(%rsp)
movl 0x20(%rsp), %r8d
movl %r8d, %r12d
movq 0x18(%rsp), %rdi
movl $0x16362004, %esi       # imm = 0x16362004
movl $0x1, %edx
movl %r13d, %ecx
callq 0x2280
movq %rax, 0x38(%rsp)
movq %r13, 0x40(%rsp)
imulq %r13, %r12
shlq $0x2, %r12
leaq 0x70(%rsp), %rdx
movl $0x0, (%rdx)
leaq 0x50(%rsp), %r13
leaq 0xf(%rsp), %rcx
movq %r13, %rdi
movq %r12, %rsi
callq 0x10f8e
leal -0x1(%r15), %esi
movq (%r13), %rcx
cmpl %ebx, %esi
jae 0x10b80
movl %r15d, %r12d
movl %r15d, %eax
imull %ebp, %eax
shll $0x2, %eax
movl %eax, 0x30(%rsp)
leal (,%r15,4), %r13d
xorl %edi, %edi
movl $0x3, 0x8(%rsp)
movl %r15d, %eax
movl %ebx, 0x34(%rsp)
movl %edi, 0x10(%rsp)
movq %rax, 0x48(%rsp)
cmpl %ebp, %esi
jae 0x10b56
movl 0x10(%rsp), %eax
imulq 0x40(%rsp), %rax
movq %rax, 0x20(%rsp)
xorl %r8d, %r8d
movl 0x8(%rsp), %eax
movl %eax, %r10d
leal -0x3(%r10), %eax
movzbl (%r14,%rax), %eax
leal -0x2(%r10), %edx
movzbl (%r14,%rdx), %edx
leal -0x1(%r10), %r11d
movzbl (%r14,%r11), %r11d
movq %r15, %rdi
movl %r10d, %r15d
movzbl (%r14,%r15), %r15d
movq %rsi, %r9
movq %rcx, %rsi
movl %ebp, %ecx
movl %r8d, %ebp
xorl 0x10(%rsp), %ebp
shll $0x2, %ebp
andl $0x40, %ebp
movl $0xff, %ebx
xorl %ebx, %ebp
imull %r15d, %eax
imull %r15d, %edx
imull %r15d, %r11d
xorl %ebx, %r15d
imull %ebp, %r15d
movl %ecx, %ebp
movq %rsi, %rcx
movq %r9, %rsi
addl %r15d, %eax
addl %r15d, %edx
addl %r15d, %r11d
imulq $0x1010102, %rax, %rax  # imm = 0x1010102
shrq $0x20, %rax
imulq $0x1010102, %rdx, %r15  # imm = 0x1010102
shrq $0x20, %r15
imulq $0x1010102, %r11, %rdx  # imm = 0x1010102
shrq $0x20, %rdx
shll $0x10, %eax
shll $0x8, %r15d
addl %edx, %r15d
addl %eax, %r15d
movl %r8d, %eax
addq 0x20(%rsp), %rax
xorl %edx, %edx
divq %r12
movl %r15d, (%rcx,%rax,4)
movq %rdi, %r15
addl %r15d, %r8d
leal (%r9,%r8), %eax
addl %r13d, %r10d
cmpl %ebp, %eax
jb 0x10a9c
movq 0x48(%rsp), %rdi
leal (%rdi,%r15), %eax
leal (%rdi,%r15), %edx
decl %edx
movl 0x8(%rsp), %r8d
addl 0x30(%rsp), %r8d
movl %r8d, 0x8(%rsp)
movl 0x34(%rsp), %ebx
cmpl %ebx, %edx
jb 0x10a72
movq 0x38(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
movq %rcx, %rdx
movl 0x2c(%rsp), %ecx
callq 0x2120
movq 0x18(%rsp), %rdi
callq 0x2160
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x21c0
movq 0x18(%rsp), %rdi
callq 0x2060
xorl %r14d, %r14d
leaq 0x70(%rsp), %rbx
movl $0x1, %ebp
testl %r14d, %r14d
jne 0x10c19
xorl %r15d, %r15d
movl %r15d, %r14d
movq %rbx, %rdi
callq 0x20d0
testl %eax, %eax
je 0x10c0d
movl $0x2, %r15d
cmpl $0x100, 0x70(%rsp)      # imm = 0x100
je 0x10bd0
xorl %edi, %edi
callq 0x22a0
cmpb $0x0, 0x1b(%rax)
jne 0x10bd0
cmpl $0x300, 0x70(%rsp)      # imm = 0x300
cmovel %ebp, %r14d
movl %r14d, %r15d
jmp 0x10bd0
movl $0x5, %edi
callq 0x2110
jmp 0x10bc8
callq 0x2050
xorl %ebp, %ebp
cmpl $0x2, %r14d
sete %bl
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x10c36
callq 0x2170
movb %bl, %bpl
jmp 0x10c7f
movq 0x9396(%rip), %rbx      # 0x19fd8
leaq 0x50d7(%rip), %rsi      # 0x15d20
movl $0x14, %edx
movq %rbx, %rdi
callq 0x21d0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2180
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2100
xorl %ebp, %ebp
movl %ebp, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x10c99
jmp 0x10c99
jmp 0x10c99
movq %rax, %rbx
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x10cab
callq 0x2170
movq %rbx, %rdi
callq 0x2270