// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x80, %rsp
movl %edx, %r14d
movzwl 0x2(%rdi), %edx
leal -0xf01a(%rdx), %ecx
cmpl $0x10, %ecx
ja 0x2707
movq %rsi, %rax
movq %rdi, %rbx
movzwl (%rdi), %r15d
shrl $0x4, %r15d
leaq 0xaf0(%rip), %rsi       # 0x3060
movslq (%rsi,%rcx,4), %rcx
addq %rsi, %rcx
jmpq *%rcx
leaq 0xb42(%rip), %rsi       # 0x30c2
leaq -0xa0(%rbp), %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1060
movzwl %r15w, %eax
xorl %ecx, %ecx
cmpl $0x6e3, %eax            # imm = 0x6E3
sete %cl
shll $0x4, %ecx
addq $0x19, %rcx
cmpl $0x46b, %eax            # imm = 0x46B
movl $0x29, %r12d
cmovneq %rcx, %r12
jmp 0x26b7
leaq 0xaf9(%rip), %rsi       # 0x30ba
leaq -0xa0(%rbp), %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1060
movzwl %r15w, %eax
xorl %r12d, %r12d
cmpl $0x543, %eax            # imm = 0x543
jmp 0x2679
leaq 0xac9(%rip), %rsi       # 0x30b3
leaq -0xa0(%rbp), %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1060
movzwl %r15w, %eax
xorl %r12d, %r12d
cmpl $0x217, %eax            # imm = 0x217
jmp 0x2679
leaq 0xac8(%rip), %rsi       # 0x30d8
leaq -0xa0(%rbp), %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1060
movzwl %r15w, %eax
xorl %r12d, %r12d
cmpl $0x6e5, %eax            # imm = 0x6E5
jmp 0x26ab
leaq 0xa9b(%rip), %rsi       # 0x30d1
leaq -0xa0(%rbp), %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1060
movzwl %r15w, %eax
xorl %r12d, %r12d
cmpl $0x7a9, %eax            # imm = 0x7A9
jmp 0x26ab
leaq 0xa50(%rip), %rsi       # 0x30ac
leaq -0xa0(%rbp), %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1060
movzwl %r15w, %eax
xorl %r12d, %r12d
cmpl $0x3d5, %eax            # imm = 0x3D5
sete %r12b
shll $0x4, %r12d
addq $0x3a, %r12
jmp 0x26b7
leaq 0xa3c(%rip), %rsi       # 0x30ca
leaq -0xa0(%rbp), %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1060
movzwl %r15w, %eax
xorl %r12d, %r12d
cmpl $0x6e1, %eax            # imm = 0x6E1
sete %r12b
shll $0x4, %r12d
addq $0x19, %r12
leaq 0x983(%rip), %rsi       # 0x3041
leaq -0xa0(%rbp), %rdi
callq 0x1130
movq %rax, %r15
testl %r14d, %r14d
je 0x26f0
addq %r12, %rbx
movl %r14d, %r14d
xorl %r12d, %r12d
movzbl (%rbx,%r12), %edi
movq %r15, %rsi
callq 0x1150
incq %r12
cmpq %r12, %r14
jne 0x26db
movq %r15, %rdi
addq $0x80, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x10d0
movq 0x28d2(%rip), %rax      # 0x4fe0
movq (%rax), %rdi
leaq 0x9c8(%rip), %rsi       # 0x30e0
xorl %eax, %eax
addq $0x80, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x1170
nop