// File: /workspace/github_cpp_makefile_star2_O2/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 0x2673
movq %rsi, %rax
movq %rdi, %rbx
movzwl (%rdi), %r15d
shrl $0x4, %r15d
leaq 0xb60(%rip), %rsi       # 0x3060
movslq (%rsi,%rcx,4), %rcx
addq %rsi, %rcx
jmpq *%rcx
leaq 0xbb2(%rip), %rsi       # 0x30c2
leaq -0xa0(%rbp), %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1060
xorl %ecx, %ecx
cmpw $0x6e3, %r15w           # imm = 0x6E3
sete %cl
shll $0x4, %ecx
addq $0x19, %rcx
cmpw $0x46b, %r15w           # imm = 0x46B
pushq $0x29
popq %rax
cmovneq %rcx, %rax
jmp 0x2626
leaq 0xb6e(%rip), %rsi       # 0x30ba
leaq -0xa0(%rbp), %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1060
xorl %eax, %eax
cmpw $0x543, %r15w           # imm = 0x543
jmp 0x25f0
leaq 0xb42(%rip), %rsi       # 0x30b3
leaq -0xa0(%rbp), %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1060
xorl %eax, %eax
cmpw $0x217, %r15w           # imm = 0x217
jmp 0x25f0
leaq 0xb45(%rip), %rsi       # 0x30d8
leaq -0xa0(%rbp), %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1060
xorl %eax, %eax
cmpw $0x6e5, %r15w           # imm = 0x6E5
jmp 0x261c
leaq 0xb1c(%rip), %rsi       # 0x30d1
leaq -0xa0(%rbp), %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1060
xorl %eax, %eax
cmpw $0x7a9, %r15w           # imm = 0x7A9
jmp 0x261c
leaq 0xad5(%rip), %rsi       # 0x30ac
leaq -0xa0(%rbp), %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1060
xorl %eax, %eax
cmpw $0x3d5, %r15w           # imm = 0x3D5
sete %al
shll $0x4, %eax
addq $0x3a, %rax
jmp 0x2626
leaq 0xac7(%rip), %rsi       # 0x30ca
leaq -0xa0(%rbp), %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1060
xorl %eax, %eax
cmpw $0x6e1, %r15w           # imm = 0x6E1
sete %al
shll $0x4, %eax
addq $0x19, %rax
addq %rax, %rbx
leaq 0xa11(%rip), %rsi       # 0x3041
leaq -0xa0(%rbp), %rdi
callq 0x1150
movq %rax, %r15
movl %r14d, %r14d
xorl %r12d, %r12d
cmpq %r12, %r14
je 0x265c
movzbl (%rbx,%r12), %edi
movq %r15, %rsi
callq 0x1170
incq %r12
jmp 0x2645
movq %r15, %rdi
addq $0x80, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x10f0
movq 0x2966(%rip), %rax      # 0x4fe0
movq (%rax), %rdi
leaq 0xa5c(%rip), %rsi       # 0x30e0
xorl %eax, %eax
addq $0x80, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x1190
nop