// File: /workspace/github_cpp_makefile_star2_O2/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
andq $0x0, 0x20(%rsp)
movl %edx, %ebp
andq $0x0, 0x30(%rsp)
movl %esi, %r14d
andq $0x0, 0x18(%rsp)
movq %rdi, %r15
movq (%rdi), %rax
leaq 0x50(%rsp), %rdi
movq %rax, (%rdi)
xorl %eax, %eax
movl %eax, %esi
pushq $0x8
popq %rdx
callq 0x21a0
testl %eax, %eax
jne 0x27dd
leaq 0x2a1f(%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 0x27f2
movq %rax, %rdi
callq 0x2420
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x27c8
movq 0x18(%rsp), %rdi
callq 0x2420
movq %rax, 0x30(%rsp)
testq %rax, %rax
je 0x27c8
movq 0x18(%rsp), %rdi
movq 0x5966(%rip), %rsi      # 0x7fb8
movl $0xc8, %edx
callq 0x2270
movq %rax, %rdi
callq 0x23a0
testl %eax, %eax
jne 0x2813
leaq 0x58(%rsp), %rsi
movq %r15, (%rsi)
movl %r14d, 0x8(%rsi)
movl $0x8, 0xc(%rsi)
movq 0x18(%rsp), %rdi
leaq -0x10f(%rip), %rdx      # 0x257c
callq 0x2150
movq 0x18(%rsp), %rdi
pushq $0x8
popq %rsi
callq 0x20c0
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rsi
callq 0x21c0
movq 0x18(%rsp), %rdi
movq 0x20(%rsp), %rsi
movq %rsp, %rax
xorps %xmm0, %xmm0
movups %xmm0, (%rax)
andq $0x0, 0x10(%rax)
leaq 0x3c(%rsp), %rdx
leaq 0x2c(%rsp), %rcx
leaq 0x4c(%rsp), %r8
leaq 0x48(%rsp), %r9
callq 0x2390
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
pushq $0x4
popq %rsi
cltd
idivl %esi
negl %edx
movl %edx, %eax
leal (%rcx,%rax), %r13d
addl $0x3, %r13d
movl 0x2c(%rsp), %eax
movl %eax, %ebx
movl %ebx, %eax
imull %r13d, %eax
movl %eax, %edi
addq $0xf, %rdi
callq 0x2320
testq %rax, %rax
je 0x2850
movq %rax, %r12
movq %rax, %r14
leaq (,%rbx,8), %rdi
callq 0x2320
testq %rax, %rax
je 0x2892
movq %rax, %r15
xorl %eax, %eax
movl %eax, %ecx
cmpq %rcx, %rbx
je 0x2774
movl %eax, %edx
addq %r12, %rdx
movq %rdx, (%r15,%rcx,8)
incq %rcx
addl %r13d, %eax
jmp 0x275e
movq 0x18(%rsp), %rdi
movq %r15, %rsi
callq 0x2100
imull 0x3c(%rsp), %ebp
imull 0x2c(%rsp), %ebp
shrl $0x3, %ebp
movl %ebp, %edx
movq 0x40(%rsp), %rdi
movq %r14, %rsi
callq 0x21e0
leaq 0x18(%rsp), %rdi
leaq 0x20(%rsp), %rsi
leaq 0x30(%rsp), %rdx
callq 0x2180
movq %r14, %rdi
callq 0x22c0
movq %r15, %rdi
callq 0x22c0
xorl %eax, %eax
jmp 0x293b
movq 0x5801(%rip), %rax      # 0x7fd0
movq (%rax), %rcx
leaq 0x2873(%rip), %rdi      # 0x504c
pushq $0x2a
jmp 0x2826
movq 0x57ec(%rip), %rax      # 0x7fd0
movq (%rax), %rcx
leaq 0x2816(%rip), %rdi      # 0x5004
pushq $0x15
jmp 0x2805
movq 0x57d7(%rip), %rax      # 0x7fd0
movq (%rax), %rcx
leaq 0x281e(%rip), %rdi      # 0x5021
pushq $0x2a
popq %rsi
pushq $0x1
popq %rdx
callq 0x2440
jmp 0x2938
movq 0x57b6(%rip), %rax      # 0x7fd0
movq (%rax), %rcx
leaq 0x2853(%rip), %rdi      # 0x5077
pushq $0x12
popq %rsi
pushq $0x1
popq %rdx
callq 0x2440
pushq $0x4
popq %rdi
callq 0x2140
movl $0x1, (%rax)
movq 0x5764(%rip), %rsi      # 0x7fa8
xorl %ecx, %ecx
movl %ecx, %edx
movq %rax, %rdi
callq 0x2400
movq 0x5779(%rip), %rax      # 0x7fd0
movq (%rax), %rcx
leaq 0x2829(%rip), %rdi      # 0x508a
pushq $0x34
popq %rsi
pushq $0x1
popq %rdx
callq 0x2440
pushq $0x4
popq %rdi
callq 0x2140
movl $0x1, (%rax)
xorl %ecx, %ecx
movl %ecx, %edx
movq 0x5723(%rip), %rsi      # 0x7fa8
movq %rdx, %r14
movq %rax, %rdi
callq 0x2400
jmp 0x28cf
movq 0x5737(%rip), %rax      # 0x7fd0
movq (%rax), %rcx
leaq 0x281c(%rip), %rdi      # 0x50bf
pushq $0x36
popq %rsi
pushq $0x1
popq %rdx
callq 0x2440
pushq $0x4
popq %rdi
callq 0x2140
movl $0x1, (%rax)
movq 0x56e5(%rip), %rsi      # 0x7fa8
xorl %ecx, %ecx
movl %ecx, %edx
movq %rax, %rdi
callq 0x2400
movq %r15, %rbx
jmp 0x28ef
xorl %ecx, %ecx
movl %ecx, %r14d
movq %r14, %rbx
jmp 0x28ef
movq %rax, %rdx
xorl %eax, %eax
movl %eax, %r14d
movq %r14, %rbx
jmp 0x28f2
xorl %ecx, %ecx
movl %ecx, %ebx
movq %rax, %rdx
movq %rdx, %rdi
jmp 0x2902
xorl %ecx, %ecx
movl %ecx, %r14d
movq %r14, %rbx
movq %rax, %rdi
callq 0x2120
cmpq $0x0, 0x18(%rsp)
je 0x2923
leaq 0x18(%rsp), %rdi
leaq 0x20(%rsp), %rsi
leaq 0x30(%rsp), %rdx
callq 0x2180
movq %r14, %rdi
callq 0x22c0
movq %rbx, %rdi
callq 0x22c0
callq 0x23f0
pushq $-0x1
popq %rax
addq $0x68, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
callq 0x23f0
movq %rbx, %rdi
callq 0x2430
movq %rax, %rdi
callq 0x2b70