// File: /workspace/github_cpp_makefile_star2_O1/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 0x2250
cmpq $0x0, (%r12)
je 0x10fc2
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 0x2270
movq %rax, 0x38(%rsp)
movq %r13, 0x40(%rsp)
imulq %r13, %r12
shlq $0x2, %r12
leaq 0x70(%rsp), %rdx
movl $0x0, (%rdx)
leaq 0x50(%rsp), %rdi
leaq 0xf(%rsp), %rcx
movq %r12, %rsi
callq 0x11320
leal -0x1(%r15), %ecx
cmpl %ebx, %ecx
jae 0x10f05
movl %r15d, %esi
movq 0x50(%rsp), %r12
movl %r15d, %eax
imull %ebp, %eax
shll $0x2, %eax
movl %eax, 0x30(%rsp)
leal (,%r15,4), %r13d
xorl %r8d, %r8d
movl $0x3, 0x8(%rsp)
movl %r15d, %eax
movl %ebx, 0x34(%rsp)
movl %r8d, 0x10(%rsp)
movq %rax, 0x48(%rsp)
cmpl %ebp, %ecx
jae 0x10ede
movl 0x10(%rsp), %eax
imulq 0x40(%rsp), %rax
movq %rax, 0x20(%rsp)
xorl %r8d, %r8d
movl 0x8(%rsp), %eax
movl %eax, %r11d
leal -0x3(%r11), %eax
movzbl (%r14,%rax), %eax
leal -0x2(%r11), %edx
movzbl (%r14,%rdx), %edx
leal -0x1(%r11), %r10d
movzbl (%r14,%r10), %r10d
movq %r15, %rdi
movl %r11d, %r15d
movzbl (%r14,%r15), %r15d
movq %rcx, %r9
movq %rsi, %rcx
movl %ebp, %esi
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, %r10d
xorl %ebx, %r15d
imull %ebp, %r15d
movl %esi, %ebp
movq %rcx, %rsi
movq %r9, %rcx
addl %r15d, %eax
addl %r15d, %edx
addl %r15d, %r10d
imulq $0x1010102, %rax, %rax  # imm = 0x1010102
shrq $0x20, %rax
imulq $0x1010102, %rdx, %r15  # imm = 0x1010102
shrq $0x20, %r15
imulq $0x1010102, %r10, %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 %rsi
movl %r15d, (%r12,%rax,4)
movq %rdi, %r15
addl %r15d, %r8d
leal (%r9,%r8), %eax
addl %r13d, %r11d
cmpl %ebp, %eax
jb 0x10e24
movq 0x48(%rsp), %r8
leal (%r8,%r15), %eax
leal (%r8,%r15), %edx
decl %edx
movl 0x8(%rsp), %edi
addl 0x30(%rsp), %edi
movl %edi, 0x8(%rsp)
movl 0x34(%rsp), %ebx
cmpl %ebx, %edx
jb 0x10df9
movq 0x50(%rsp), %rdx
movq 0x38(%rsp), %rbx
movq %rbx, %rdi
xorl %esi, %esi
movl 0x2c(%rsp), %ecx
callq 0x2110
movq 0x18(%rsp), %rdi
callq 0x2150
movq 0x18(%rsp), %rdi
movq %rbx, %rsi
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x21b0
movq 0x18(%rsp), %rdi
callq 0x2060
xorl %r14d, %r14d
leaq 0x70(%rsp), %rbx
movl $0x1, %ebp
testl %r14d, %r14d
jne 0x10fa0
xorl %r15d, %r15d
movl %r15d, %r14d
movq %rbx, %rdi
callq 0x20d0
testl %eax, %eax
je 0x10f94
movl $0x2, %r15d
cmpl $0x100, 0x70(%rsp)      # imm = 0x100
je 0x10f57
xorl %edi, %edi
callq 0x2290
cmpb $0x0, 0x1b(%rax)
jne 0x10f57
cmpl $0x300, 0x70(%rsp)      # imm = 0x300
cmovel %ebp, %r14d
movl %r14d, %r15d
jmp 0x10f57
movl $0x5, %edi
callq 0x2100
jmp 0x10f4f
callq 0x2050
xorl %ebp, %ebp
cmpl $0x2, %r14d
sete %bl
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x10fbd
callq 0x2160
movb %bl, %bpl
jmp 0x11006
movq 0x900f(%rip), %rbx      # 0x19fd8
leaq 0x4c2c(%rip), %rsi      # 0x15bfc
movl $0x14, %edx
movq %rbx, %rdi
callq 0x21c0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x20f0
xorl %ebp, %ebp
movl %ebp, %eax
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x11020
jmp 0x11020
jmp 0x11020
movq %rax, %rbx
movq 0x50(%rsp), %rdi
testq %rdi, %rdi
je 0x11032
callq 0x2160
movq %rbx, %rdi
callq 0x2260