// File: /workspace/github_cpp_makefile_star2_O1/olegk0[P]readAVI/player.cpp
// Function: UnpackPNG(unsigned char*, unsigned int, unsigned int, unsigned char*)
int UnpackPNG(unsigned char *src_buf, unsigned src_length, unsigned bpp, unsigned char *dst_buf)
{
    int ret = 0;
    png_infop info_ptr = NULL, end_info = NULL;
    png_structp png_ptr = NULL;
    png_byte header[8];
    png_byte *image_data = NULL;
    png_bytep *row_pointers = NULL;

    // read the header
    memcpy(header, src_buf, 8);

    if (png_sig_cmp(header, 0, 8)) {
	fprintf(stderr, "error: is not a PNG.\n");
	return -1;
    }

    png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    if (!png_ptr) {
	fprintf(stderr, "error: png_create_read_struct returned 0.\n");
	return -1;
    }

    try {
	// create png info struct
	info_ptr = png_create_info_struct(png_ptr);
	if (!info_ptr) {
	    fprintf(stderr, "error: png_create_info_struct returned 0.\n");
	    throw 1;
	}

	// create png info struct
	end_info = png_create_info_struct(png_ptr);
	if (!end_info) {
	    fprintf(stderr, "error: png_create_info_struct returned 0.\n");
	    throw 1;
	}

	// the code in this if statement gets called if libpng encounters an error
	if (setjmp(png_jmpbuf(png_ptr))) {
	    fprintf(stderr, "error from libpng\n");
	    throw 1;
	}

	// init png reading
	//png_init_io(png_ptr, fp);
	read_buf_str_t read_buf_str;
	read_buf_str.buf = src_buf;
	read_buf_str.len = src_length;
	read_buf_str.pos = 8;

	png_set_read_fn(png_ptr, &read_buf_str, ReadBuf);

	// let libpng know you already read the first 8 bytes
	png_set_sig_bytes(png_ptr, 8);

	// read all the info up to the image data
	png_read_info(png_ptr, info_ptr);

	// variables to pass to get info
	int bit_depth, color_type;
	png_uint_32 temp_width, temp_height;

	// get info about png
	png_get_IHDR(png_ptr, info_ptr, &temp_width, &temp_height, &bit_depth, &color_type, NULL, NULL, NULL);

	// Update the png info struct.
	png_read_update_info(png_ptr, info_ptr);

	// Row size in bytes.
	int rowbytes = png_get_rowbytes(png_ptr, info_ptr);

	// glTexImage2d requires rows to be 4-byte aligned
	rowbytes += 3 - ((rowbytes - 1) % 4);

	// Allocate the image_data as a big block, to be given to opengl
	image_data = (png_byte *) malloc(rowbytes * temp_height * sizeof(png_byte) + 15);
	if (image_data == NULL) {
	    fprintf(stderr, "error: could not allocate memory for PNG image data\n");
	    throw 1;
	}

	// row_pointers is for pointing to image_data for reading the png with libpng
	row_pointers = (png_bytep *) malloc(temp_height * sizeof(png_bytep));
	if (row_pointers == NULL) {
	    fprintf(stderr, "error: could not allocate memory for PNG row pointers\n");
	    throw 1;
	}

	// set the individual row_pointers to point at the correct offsets of image_data
	unsigned i;
	for (i = 0; i < temp_height; i++) {
//	    row_pointers[temp_height - 1 - i] = image_data + i * rowbytes;
	    row_pointers[i] = image_data + i * rowbytes;
	}

	// read the png into image_data through row_pointers
	png_read_image(png_ptr, row_pointers);

	memcpy(dst_buf, image_data, temp_width * temp_height * bpp / 8);

	// clean up
	png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
	free(image_data);
	free(row_pointers);

    } catch (...) {
	if (png_ptr)
	    png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
	if (image_data)
	    free(image_data);
	if (row_pointers)
	    free(row_pointers);
	ret = -1;
    }

    return ret;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x68, %rsp
movq %rcx, %rbx
movl %edx, %ebp
movl %esi, %r14d
movq %rdi, %r15
xorl %eax, %eax
movl %eax, %esi
movq %rsi, 0x20(%rsp)
movq %rsi, 0x30(%rsp)
movq %rsi, 0x18(%rsp)
movq (%rdi), %rax
leaq 0x50(%rsp), %rdi
movq %rax, (%rdi)
movl $0x8, %edx
callq 0x2180
testl %eax, %eax
jne 0x2866
leaq 0x2990(%rip), %rdi      # 0x501a
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rsi
movq %rcx, %rdx
callq 0x2080
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x2870
movq %rax, %rdi
callq 0x23f0
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x287a
movq 0x18(%rsp), %rdi
callq 0x23f0
movq %rax, 0x30(%rsp)
testq %rax, %rax
je 0x2881
movq 0x18(%rsp), %rdi
movq 0x58d7(%rip), %rsi      # 0x7fb8
movl $0xc8, %edx
callq 0x2220
movq %rax, %rdi
callq 0x2360
testl %eax, %eax
jne 0x2888
leaq 0x58(%rsp), %rsi
movq %r15, (%rsi)
movl %r14d, 0x8(%rsi)
movl $0x8, 0xc(%rsi)
movq 0x18(%rsp), %rdi
leaq -0x10e(%rip), %rdx      # 0x260c
callq 0x2130
movq 0x18(%rsp), %rdi
movl $0x8, %esi
callq 0x20c0
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x21a0
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq %rsp, %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
movq $0x0, 0x10(%rax)
leaq 0x3c(%rsp), %rdx
leaq 0x2c(%rsp), %rcx
leaq 0x4c(%rsp), %r8
leaq 0x48(%rsp), %r9
callq 0x2350
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x2050
movq %rbx, 0x40(%rsp)
movq 0x18(%rsp), %rdi
xorl %eax, %eax
movl %eax, %r14d
movq 0x20(%rsp), %rsi
callq 0x2090
movl %eax, %ecx
decl %eax
addl $0x2, %ecx
testl %eax, %eax
cmovnsl %eax, %ecx
movl %ecx, %r13d
andl $-0x4, %r13d
addl $0x4, %r13d
movl 0x2c(%rsp), %eax
movl %eax, %ebx
movl %ebx, %eax
imull %r13d, %eax
movl %eax, %edi
addq $0xf, %rdi
callq 0x22e0
testq %rax, %rax
je 0x28b0
movq %rax, %r12
movq %rax, %r14
leaq (,%rbx,8), %rdi
callq 0x22e0
testq %rax, %rax
je 0x28f8
movq %rax, %r15
testq %rbx, %rbx
je 0x2812
xorl %eax, %eax
movl %eax, %ecx
movl %eax, %edx
addq %r12, %rdx
movq %rdx, (%r15,%rcx,8)
movq %rcx, %rdx
incq %rdx
addl %r13d, %eax
movq %rdx, %rcx
cmpq %rdx, %rbx
jne 0x27f8
movq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0x20f0
imull 0x3c(%rsp), %ebp
imull 0x2c(%rsp), %ebp
shrl $0x3, %ebp
movl %ebp, %edx
movq 0x40(%rsp), %rdi
movq %r14, %rsi
callq 0x21c0
leaq 0x18(%rsp), %rdi
leaq 0x20(%rsp), %rsi
leaq 0x30(%rsp), %rdx
callq 0x2160
movq %r14, %rdi
callq 0x2280
movq %r15, %rdi
callq 0x2280
xorl %eax, %eax
jmp 0x29b3
callq 0x2430
jmp 0x29ae
callq 0x24b8
jmp 0x29ae
callq 0x2496
jmp 0x288d
callq 0x2474
jmp 0x288d
callq 0x2452
movl $0x4, %edi
callq 0x2120
movl $0x1, (%rax)
movq 0x5704(%rip), %rsi      # 0x7fa8
xorl %ecx, %ecx
movl %ecx, %edx
movq %rax, %rdi
callq 0x23d0
movq 0x5719(%rip), %rax      # 0x7fd0
movq (%rax), %rcx
leaq 0x27c9(%rip), %rdi      # 0x508a
movl $0x34, %esi
movl $0x1, %edx
callq 0x2410
movl $0x4, %edi
callq 0x2120
movl $0x1, (%rax)
xorl %ecx, %ecx
movl %ecx, %edx
movq 0x56bd(%rip), %rsi      # 0x7fa8
movq %rdx, %r14
movq %rax, %rdi
callq 0x23d0
jmp 0x293b
movq 0x56d1(%rip), %rax      # 0x7fd0
movq (%rax), %rcx
leaq 0x27b6(%rip), %rdi      # 0x50bf
movl $0x36, %esi
movl $0x1, %edx
callq 0x2410
movl $0x4, %edi
callq 0x2120
movl $0x1, (%rax)
movq 0x5679(%rip), %rsi      # 0x7fa8
xorl %ecx, %ecx
movl %ecx, %edx
movq %rax, %rdi
callq 0x23d0
movq %r15, %rbx
jmp 0x295b
xorl %ecx, %ecx
movl %ecx, %r14d
movq %r14, %rbx
jmp 0x295b
movq %rax, %rdx
xorl %eax, %eax
movl %eax, %r14d
movq %r14, %rbx
jmp 0x295e
xorl %ecx, %ecx
movl %ecx, %ebx
movq %rax, %rdx
movq %rdx, %rdi
jmp 0x296e
xorl %ecx, %ecx
movl %ecx, %r14d
movq %r14, %rbx
movq %rax, %rdi
callq 0x2110
cmpq $0x0, 0x18(%rsp)
je 0x298f
leaq 0x18(%rsp), %rdi
leaq 0x20(%rsp), %rsi
leaq 0x30(%rsp), %rdx
callq 0x2160
testq %r14, %r14
je 0x299c
movq %r14, %rdi
callq 0x2280
testq %rbx, %rbx
je 0x29a9
movq %rbx, %rdi
callq 0x2280
callq 0x23b0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
callq 0x23b0
movq %rbx, %rdi
callq 0x2400
movq %rax, %rdi
callq 0x2beb