// File: tsfn[P]doc2txt/src/doc_image.cpp
// Function: storeBlipBlock(unsigned char const*, char const*, unsigned int)
static bool storeBlipBlock(const uchar *block, const char *img_name, uint size) {
  // uchar recVer = *(uchar *)(block) & 0xF;
  ushort recInstance = *(ushort *)(block) >> 4;
  ushort recType = *(ushort *)(block + 2);
  // ushort recLen = *(uint *)(block + 4);

  char str[128];
  const uchar *start = block;
  if (recType == 0xF01A) { // EMF
    sprintf(str, "%s.emf", img_name);
    start += 8 + 16 + (recInstance == 0x3D5 ? 16 : 0) + 34;
  } else if (recType == 0xF01B) { // WMF
    sprintf(str, "%s.wmf", img_name);
    start += 8 + 16 + (recInstance == 0x217 ? 16 : 0) + 34;
  } else if (recType == 0xF01C) { // PICT
    sprintf(str, "%s.pict", img_name);
    start += 8 + 16 + (recInstance == 0x543 ? 16 : 0) + 34;
  } else if (recType == 0xF01D || recType == 0xF02A) { // JPEG
    sprintf(str, "%s.jpeg", img_name);
    start += (recInstance == 0x46B || recInstance == 0x6E3 ? 41 : 25);
  } else if (recType == 0xF01E) { // PNG
    sprintf(str, "%s.png", img_name);
    start += 8 + 16 + (recInstance == 0x6E1 ? 16 : 0) + 1;
  } else if (recType == 0xF01F) { // DIB
    sprintf(str, "%s.dib", img_name);
    start += 8 + 16 + (recInstance == 0x7A9 ? 16 : 0) + 1;
  } else if (recType == 0xF029) { // TIFF
    sprintf(str, "%s.tiff", img_name);
    start += 8 + 16 + (recInstance == 0x6E5 ? 16 : 0) + 1;
  } else {
    fprintf(stderr, "unknown recType: %u\n", (uint)recType);
    return false;
  }

  FILE *file = fopen(str, "wb");
  for (uint i = 0; i < size; ++i) {
    fputc(start[i], file);
  }
  fclose(file);
  return true;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xc0, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movl %edx, -0x1c(%rbp)
movq -0x10(%rbp), %rax
movzwl (%rax), %eax
sarl $0x4, %eax
movw %ax, -0x1e(%rbp)
movq -0x10(%rbp), %rax
movw 0x2(%rax), %ax
movw %ax, -0x20(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0xa8(%rbp)
movzwl -0x20(%rbp), %eax
cmpl $0xf01a, %eax           # imm = 0xF01A
jne 0x3f12
leaq -0xa0(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq 0x2190(%rip), %rsi      # 0x6068
movb $0x0, %al
callq 0x1060
movzwl -0x1e(%rbp), %edx
xorl %ecx, %ecx
movl $0x10, %eax
cmpl $0x3d5, %edx            # imm = 0x3D5
cmovel %eax, %ecx
addl $0x18, %ecx
addl $0x22, %ecx
movq -0xa8(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0xa8(%rbp)
jmp 0x4171
movzwl -0x20(%rbp), %eax
cmpl $0xf01b, %eax           # imm = 0xF01B
jne 0x3f69
leaq -0xa0(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq 0x2140(%rip), %rsi      # 0x606f
movb $0x0, %al
callq 0x1060
movzwl -0x1e(%rbp), %edx
xorl %ecx, %ecx
movl $0x10, %eax
cmpl $0x217, %edx            # imm = 0x217
cmovel %eax, %ecx
addl $0x18, %ecx
addl $0x22, %ecx
movq -0xa8(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0xa8(%rbp)
jmp 0x416f
movzwl -0x20(%rbp), %eax
cmpl $0xf01c, %eax           # imm = 0xF01C
jne 0x3fc0
leaq -0xa0(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq 0x20f0(%rip), %rsi      # 0x6076
movb $0x0, %al
callq 0x1060
movzwl -0x1e(%rbp), %edx
xorl %ecx, %ecx
movl $0x10, %eax
cmpl $0x543, %edx            # imm = 0x543
cmovel %eax, %ecx
addl $0x18, %ecx
addl $0x22, %ecx
movq -0xa8(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0xa8(%rbp)
jmp 0x416d
movzwl -0x20(%rbp), %eax
cmpl $0xf01d, %eax           # imm = 0xF01D
je 0x3fd6
movzwl -0x20(%rbp), %eax
cmpl $0xf02a, %eax           # imm = 0xF02A
jne 0x4044
leaq -0xa0(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq 0x2096(%rip), %rsi      # 0x607e
movb $0x0, %al
callq 0x1060
movzwl -0x1e(%rbp), %ecx
movb $0x1, %al
cmpl $0x46b, %ecx            # imm = 0x46B
movb %al, -0xb5(%rbp)
je 0x4015
movzwl -0x1e(%rbp), %eax
cmpl $0x6e3, %eax            # imm = 0x6E3
sete %al
movb %al, -0xb5(%rbp)
movb -0xb5(%rbp), %dl
movl $0x19, %ecx
movl $0x29, %eax
testb $0x1, %dl
cmovnel %eax, %ecx
movq -0xa8(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0xa8(%rbp)
jmp 0x416b
movzwl -0x20(%rbp), %eax
cmpl $0xf01e, %eax           # imm = 0xF01E
jne 0x409b
leaq -0xa0(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq 0x2025(%rip), %rsi      # 0x6086
movb $0x0, %al
callq 0x1060
movzwl -0x1e(%rbp), %edx
xorl %ecx, %ecx
movl $0x10, %eax
cmpl $0x6e1, %edx            # imm = 0x6E1
cmovel %eax, %ecx
addl $0x18, %ecx
addl $0x1, %ecx
movq -0xa8(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0xa8(%rbp)
jmp 0x4169
movzwl -0x20(%rbp), %eax
cmpl $0xf01f, %eax           # imm = 0xF01F
jne 0x40ef
leaq -0xa0(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq 0x1fd5(%rip), %rsi      # 0x608d
movb $0x0, %al
callq 0x1060
movzwl -0x1e(%rbp), %edx
xorl %ecx, %ecx
movl $0x10, %eax
cmpl $0x7a9, %edx            # imm = 0x7A9
cmovel %eax, %ecx
addl $0x18, %ecx
addl $0x1, %ecx
movq -0xa8(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0xa8(%rbp)
jmp 0x4167
movzwl -0x20(%rbp), %eax
cmpl $0xf029, %eax           # imm = 0xF029
jne 0x4143
leaq -0xa0(%rbp), %rdi
movq -0x18(%rbp), %rdx
leaq 0x1f88(%rip), %rsi      # 0x6094
movb $0x0, %al
callq 0x1060
movzwl -0x1e(%rbp), %edx
xorl %ecx, %ecx
movl $0x10, %eax
cmpl $0x6e5, %edx            # imm = 0x6E5
cmovel %eax, %ecx
addl $0x18, %ecx
addl $0x1, %ecx
movq -0xa8(%rbp), %rax
movslq %ecx, %rcx
addq %rcx, %rax
movq %rax, -0xa8(%rbp)
jmp 0x4165
movq 0x5e96(%rip), %rax      # 0x9fe0
movq (%rax), %rdi
movzwl -0x20(%rbp), %edx
leaq 0x1f44(%rip), %rsi      # 0x609c
movb $0x0, %al
callq 0x1190
movb $0x0, -0x1(%rbp)
jmp 0x41de
jmp 0x4167
jmp 0x4169
jmp 0x416b
jmp 0x416d
jmp 0x416f
jmp 0x4171
leaq -0xa0(%rbp), %rdi
leaq 0x1ec3(%rip), %rsi      # 0x6042
callq 0x1150
movq %rax, -0xb0(%rbp)
movl $0x0, -0xb4(%rbp)
movl -0xb4(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jae 0x41ce
movq -0xa8(%rbp), %rax
movl -0xb4(%rbp), %ecx
movzbl (%rax,%rcx), %edi
movq -0xb0(%rbp), %rsi
callq 0x1170
movl -0xb4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0xb4(%rbp)
jmp 0x4195
movq -0xb0(%rbp), %rdi
callq 0x10f0
movb $0x1, -0x1(%rbp)
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0xc0, %rsp
popq %rbp
retq
nopl (%rax)