// File: /workspace/github_cpp_makefile_star2_O2/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, %r8d
movl %edx, %r13d
movq %rsi, %r12
movl %edx, %eax
shrl $0xa, %eax
incl %eax
cmpl $0x400, %edx            # imm = 0x400
pushq $0x1
popq %rcx
cmovael %eax, %ecx
movq %rdi, 0x20(%rsp)
movl %r8d, %eax
shrl $0xa, %eax
leal 0x1(%rax), %ebx
cmpl %ecx, %eax
cmovbl %ecx, %ebx
xorl %r15d, %r15d
movl %edx, %eax
xorl %edx, %edx
divl %ebx
movl %eax, %r14d
movl %r8d, 0x34(%rsp)
movl %r8d, %eax
xorl %edx, %edx
divl %ebx
pushq $0x2
popq %rdx
leaq 0x68(%rsp), %rbp
leaq 0x10(%rsp), %r8
movl %r14d, %edi
movl %eax, 0x18(%rsp)
movl %eax, %esi
movq %rbp, %rcx
callq 0x2070
movq (%rbp), %rdi
movq 0x20(%rsp), %rax
movq (%rax), %rsi
callq 0x2240
cmpq $0x0, (%rbp)
je 0xebc8
movl %r14d, %eax
shll $0x2, %eax
movl %eax, 0x2c(%rsp)
movl 0x18(%rsp), %r8d
movl %r8d, %ebp
movq 0x10(%rsp), %rdi
pushq $0x1
popq %rdx
movl $0x16362004, %esi       # imm = 0x16362004
movl %r14d, %ecx
callq 0x2260
movq %rax, 0x40(%rsp)
imulq %r14, %rbp
shlq $0x2, %rbp
leaq 0x70(%rsp), %rdx
andl $0x0, (%rdx)
leaq 0x50(%rsp), %rdi
leaq 0xf(%rsp), %rcx
movq %rbp, %rsi
callq 0xedf8
movl %ebx, %eax
movq %rax, 0x18(%rsp)
movl %ebx, %eax
imull %r13d, %eax
shll $0x2, %eax
movl %eax, 0x30(%rsp)
leal (,%rbx,4), %eax
movl %eax, 0x3c(%rsp)
leal -0x1(%rbx), %eax
movq %rax, 0x20(%rsp)
pushq $0x3
popq %rax
movl %eax, 0x8(%rsp)
movl $0xff, %ebp
movq %rbx, %r10
movq %r14, 0x48(%rsp)
leal (%r15,%r10), %eax
decl %eax
cmpl 0x34(%rsp), %eax
jae 0xeb13
leal (%r15,%r10), %eax
movl %eax, 0x38(%rsp)
movq %r15, %r11
movl %r15d, %r9d
imulq %r14, %r9
xorl %r14d, %r14d
movq 0x50(%rsp), %rbx
movl 0x8(%rsp), %eax
movl %eax, %r15d
movq 0x20(%rsp), %rax
addl %r14d, %eax
cmpl %r13d, %eax
jae 0xeaf6
leal -0x3(%r15), %eax
movzbl (%r12,%rax), %eax
leal -0x2(%r15), %ecx
movzbl (%r12,%rcx), %esi
leal -0x1(%r15), %ecx
movzbl (%r12,%rcx), %ecx
movl %r15d, %edx
movzbl (%r12,%rdx), %edi
movl %r14d, %edx
xorl %r11d, %edx
shll $0x2, %edx
andl $0x40, %edx
xorl %ebp, %edx
imull %edi, %eax
imull %edi, %esi
imull %edi, %ecx
xorl %ebp, %edi
imull %edx, %edi
addl %edi, %eax
xorl %edx, %edx
divl %ebp
movl %r13d, %r8d
movl %eax, %r13d
addl %edi, %esi
movl %esi, %eax
xorl %edx, %edx
divl %ebp
movl %eax, %esi
addl %edi, %ecx
movl %ecx, %eax
xorl %edx, %edx
divl %ebp
shll $0x8, %esi
addl %eax, %esi
movl %r14d, %eax
shll $0x10, %r13d
addl %r13d, %esi
movl %r8d, %r13d
addq %r9, %rax
xorl %edx, %edx
divq 0x18(%rsp)
movl %esi, (%rbx,%rax,4)
leal (%r14,%r10), %eax
addl 0x3c(%rsp), %r15d
movl %eax, %r14d
jmp 0xea58
movl 0x8(%rsp), %eax
addl 0x30(%rsp), %eax
movl %eax, 0x8(%rsp)
movl 0x38(%rsp), %eax
movl %eax, %r15d
movq 0x48(%rsp), %r14
jmp 0xea27
movq 0x50(%rsp), %rdx
movq 0x40(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
movl 0x2c(%rsp), %ecx
callq 0x2110
movq 0x10(%rsp), %rdi
callq 0x2150
movq 0x10(%rsp), %rdi
movq %rbx, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x21b0
movq 0x10(%rsp), %rdi
callq 0x2050
xorl %r12d, %r12d
leaq 0x70(%rsp), %r14
pushq $0x5
popq %r15
pushq $0x2
popq %rbx
pushq $0x1
popq %rbp
testl %r12d, %r12d
jne 0xebae
xorl %eax, %eax
movl %eax, %r12d
movq %r14, %rdi
callq 0x20e0
testl %eax, %eax
je 0xeba4
cmpl $0x100, 0x70(%rsp)      # imm = 0x100
movl %ebx, %eax
je 0xeb69
xorl %edi, %edi
callq 0x2280
cmpb $0x0, 0x1b(%rax)
movl %ebx, %eax
jne 0xeb69
cmpl $0x300, 0x70(%rsp)      # imm = 0x300
cmovel %ebp, %r12d
movl %r12d, %eax
jmp 0xeb69
movl %r15d, %edi
callq 0x2100
jmp 0xeb62
callq 0x2040
xorl %ebx, %ebx
cmpl $0x2, %r12d
sete %bl
leaq 0x50(%rsp), %rdi
callq 0xee82
jmp 0xebe5
movq 0x9409(%rip), %rdi      # 0x17fd8
leaq 0x4046(%rip), %rsi      # 0x12c1c
callq 0x2170
movq %rax, %rdi
callq 0x20b0
xorl %ebx, %ebx
movl %ebx, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xebff
jmp 0xebff
jmp 0xebff
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0xee82
movq %rbx, %rdi
callq 0x2250
movq %rax, %rdi
callq 0xe54c