// File: 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
movq %rsp, %rbp
subq $0x130, %rsp            # imm = 0x130
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movl %ecx, -0x20(%rbp)
movl $0x1, -0x24(%rbp)
movl -0x1c(%rbp), %eax
shrl $0xa, %eax
cmpl -0x24(%rbp), %eax
jb 0x1ae57
movl -0x1c(%rbp), %eax
shrl $0xa, %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
movl -0x20(%rbp), %eax
shrl $0xa, %eax
cmpl -0x24(%rbp), %eax
jb 0x1ae6e
movl -0x20(%rbp), %eax
shrl $0xa, %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
movl -0x1c(%rbp), %eax
xorl %edx, %edx
divl -0x24(%rbp)
movl %eax, %eax
movq %rax, -0x30(%rbp)
movl -0x20(%rbp), %eax
xorl %edx, %edx
divl -0x24(%rbp)
movl %eax, %eax
movq %rax, -0x38(%rbp)
movq -0x30(%rbp), %rax
shlq $0x2, %rax
movq %rax, -0x40(%rbp)
movq -0x30(%rbp), %rax
movl %eax, %edi
movq -0x38(%rbp), %rax
movl %eax, %esi
movl $0x2, %edx
leaq -0x48(%rbp), %rcx
leaq -0x50(%rbp), %r8
callq 0x2070
movq -0x48(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0x10(%rbp), %rdi
callq 0x20a0
movq -0xe8(%rbp), %rdi
movq %rax, %rsi
callq 0x2280
cmpq $0x0, -0x48(%rbp)
jne 0x1af0c
movq 0xb0f3(%rip), %rdi      # 0x25fd8
leaq 0x5aff(%rip), %rsi      # 0x209eb
callq 0x2160
movq %rax, %rdi
movq 0xb0cd(%rip), %rsi      # 0x25fc8
callq 0x2190
movl $0x0, -0x4(%rbp)
jmp 0x1b326
movq -0x50(%rbp), %rdi
movl -0x30(%rbp), %ecx
movl -0x38(%rbp), %r8d
movl $0x16362004, %esi       # imm = 0x16362004
movl $0x1, %edx
callq 0x22b0
movq %rax, -0x58(%rbp)
movq -0x30(%rbp), %rax
movq -0x38(%rbp), %rcx
imulq %rcx, %rax
shlq $0x2, %rax
movq %rax, -0xf8(%rbp)
movl $0x0, -0x74(%rbp)
leaq -0x75(%rbp), %rdi
movq %rdi, -0xf0(%rbp)
callq 0x1b6f0
movq -0xf8(%rbp), %rsi
movq -0xf0(%rbp), %rcx
leaq -0x70(%rbp), %rdi
leaq -0x74(%rbp), %rdx
callq 0x1b710
jmp 0x1af75
leaq -0x75(%rbp), %rdi
callq 0x1b790
movl $0x0, -0x88(%rbp)
movl -0x88(%rbp), %eax
addl -0x24(%rbp), %eax
subl $0x1, %eax
cmpl -0x20(%rbp), %eax
jae 0x1b1ef
movl $0x0, -0x8c(%rbp)
movl -0x8c(%rbp), %eax
addl -0x24(%rbp), %eax
subl $0x1, %eax
cmpl -0x1c(%rbp), %eax
jae 0x1b1d9
movq -0x18(%rbp), %rax
movl -0x88(%rbp), %ecx
movl -0x1c(%rbp), %edx
imull %edx, %ecx
movl -0x8c(%rbp), %esi
shll $0x2, %esi
movl %ecx, %edx
movl %esi, %ecx
leal (%rcx,%rdx,4), %ecx
movzbl (%rax,%rcx), %eax
movl %eax, -0x90(%rbp)
movq -0x18(%rbp), %rax
movl -0x88(%rbp), %ecx
movl -0x1c(%rbp), %edx
imull %edx, %ecx
movl -0x8c(%rbp), %esi
shll $0x2, %esi
movl %ecx, %edx
movl %esi, %ecx
leal (%rcx,%rdx,4), %ecx
movzbl 0x1(%rax,%rcx), %eax
movl %eax, -0x94(%rbp)
movq -0x18(%rbp), %rax
movl -0x88(%rbp), %ecx
movl -0x1c(%rbp), %edx
imull %edx, %ecx
movl -0x8c(%rbp), %esi
shll $0x2, %esi
movl %ecx, %edx
movl %esi, %ecx
leal (%rcx,%rdx,4), %ecx
movzbl 0x2(%rax,%rcx), %eax
movl %eax, -0x98(%rbp)
movq -0x18(%rbp), %rax
movl -0x88(%rbp), %ecx
movl -0x1c(%rbp), %edx
imull %edx, %ecx
movl -0x8c(%rbp), %esi
shll $0x2, %esi
movl %ecx, %edx
movl %esi, %ecx
leal (%rcx,%rdx,4), %ecx
movzbl 0x3(%rax,%rcx), %eax
movl %eax, -0x9c(%rbp)
movl -0x8c(%rbp), %eax
shrl $0x4, %eax
andl $0x1, %eax
movl -0x88(%rbp), %ecx
btl $0x4, %ecx
sbbl $0x0, %eax
sete %al
movzbl %al, %eax
shll $0x6, %eax
orl $0xbf, %eax
movl %eax, -0xa0(%rbp)
movl -0x9c(%rbp), %esi
movl -0x90(%rbp), %ecx
movl %esi, %eax
imull %ecx, %eax
movl $0xff, %edx
movl %edx, %ecx
subl %esi, %ecx
movl -0xa0(%rbp), %esi
imull %esi, %ecx
addl %ecx, %eax
movl $0x80808081, %ecx       # imm = 0x80808081
imulq %rcx, %rax
shrq $0x27, %rax
movl %eax, -0x90(%rbp)
movl -0x9c(%rbp), %edi
movl -0x94(%rbp), %esi
movl %edi, %eax
imull %esi, %eax
movl %edx, %esi
subl %edi, %esi
movl -0xa0(%rbp), %edi
imull %edi, %esi
addl %esi, %eax
imulq %rcx, %rax
shrq $0x27, %rax
movl %eax, -0x94(%rbp)
movl -0x9c(%rbp), %esi
movl -0x98(%rbp), %edi
movl %esi, %eax
imull %edi, %eax
subl %esi, %edx
movl -0xa0(%rbp), %esi
imull %esi, %edx
addl %edx, %eax
imulq %rcx, %rax
shrq $0x27, %rax
movl %eax, -0x98(%rbp)
movl -0x90(%rbp), %eax
shll $0x10, %eax
movl -0x94(%rbp), %ecx
shll $0x8, %ecx
addl %ecx, %eax
movl -0x98(%rbp), %ecx
addl %ecx, %eax
movl %eax, -0x104(%rbp)
movl -0x88(%rbp), %eax
movq -0x30(%rbp), %rcx
imulq %rcx, %rax
movl -0x8c(%rbp), %ecx
addq %rcx, %rax
movl -0x24(%rbp), %ecx
xorl %edx, %edx
divq %rcx
movq %rax, %rsi
leaq -0x70(%rbp), %rdi
callq 0x1b7b0
movq %rax, -0x100(%rbp)
jmp 0x1b17c
movq -0x100(%rbp), %rax
movl -0x104(%rbp), %ecx
movl %ecx, (%rax)
movl -0x24(%rbp), %eax
addl -0x8c(%rbp), %eax
movl %eax, -0x8c(%rbp)
jmp 0x1afa7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x75(%rbp), %rdi
callq 0x1b790
jmp 0x1b332
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x80(%rbp)
movl %eax, -0x84(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x1b7f0
jmp 0x1b324
jmp 0x1b1db
movl -0x24(%rbp), %eax
addl -0x88(%rbp), %eax
movl %eax, -0x88(%rbp)
jmp 0x1af88
movq -0x58(%rbp), %rax
movq %rax, -0x118(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x1b7d0
movq %rax, -0x110(%rbp)
jmp 0x1b20c
movq -0x110(%rbp), %rdx
movq -0x118(%rbp), %rdi
movl -0x40(%rbp), %ecx
xorl %eax, %eax
movl %eax, %esi
callq 0x2100
jmp 0x1b228
movq -0x50(%rbp), %rdi
callq 0x2140
jmp 0x1b233
movq -0x50(%rbp), %rdi
movq -0x58(%rbp), %rsi
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rdx
callq 0x21c0
jmp 0x1b249
movq -0x50(%rbp), %rdi
callq 0x2050
jmp 0x1b254
movl $0x0, -0xdc(%rbp)
cmpl $0x0, -0xdc(%rbp)
jne 0x1b2fc
jmp 0x1b26d
leaq -0xd8(%rbp), %rdi
callq 0x20d0
movl %eax, -0x11c(%rbp)
jmp 0x1b281
movl -0x11c(%rbp), %eax
cmpl $0x0, %eax
je 0x1b2eb
cmpl $0x100, -0xd8(%rbp)     # imm = 0x100
jne 0x1b2a4
movl $0x2, -0xdc(%rbp)
jmp 0x1b2e9
xorl %eax, %eax
movl %eax, %edi
callq 0x22d0
movq %rax, -0x128(%rbp)
jmp 0x1b2b6
movq -0x128(%rbp), %rax
cmpb $0x0, 0x1b(%rax)
je 0x1b2cf
movl $0x2, -0xdc(%rbp)
jmp 0x1b2e7
cmpl $0x300, -0xd8(%rbp)     # imm = 0x300
jne 0x1b2e5
movl $0x1, -0xdc(%rbp)
jmp 0x1b2e7
jmp 0x1b2e9
jmp 0x1b26d
movl $0x5, %edi
callq 0x20f0
jmp 0x1b2f7
jmp 0x1b25e
callq 0x2040
jmp 0x1b303
movl -0xdc(%rbp), %edx
xorl %eax, %eax
movl $0x1, %ecx
cmpl $0x2, %edx
cmovel %ecx, %eax
movl %eax, -0x4(%rbp)
leaq -0x70(%rbp), %rdi
callq 0x1b7f0
jmp 0x1b326
jmp 0x1b332
movl -0x4(%rbp), %eax
addq $0x130, %rsp            # imm = 0x130
popq %rbp
retq
movq -0x80(%rbp), %rdi
callq 0x2290
movq %rax, %rdi
callq 0x19200
nopw %cs:(%rax,%rax)