// File: /workspace/github_cpp_makefile_star2_O1/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 0x27ff
movq %rsi, %rax
movq %rdi, %rbx
movzwl (%rdi), %r15d
shrl $0x4, %r15d
leaq 0x19f8(%rip), %rsi      # 0x4060
movslq (%rsi,%rcx,4), %rcx
addq %rsi, %rcx
jmpq *%rcx
leaq 0x1a4a(%rip), %rsi      # 0x40c2
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 0x27af
leaq 0x1a01(%rip), %rsi      # 0x40ba
leaq -0xa0(%rbp), %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1060
movzwl %r15w, %eax
xorl %r12d, %r12d
cmpl $0x543, %eax            # imm = 0x543
jmp 0x2771
leaq 0x19d1(%rip), %rsi      # 0x40b3
leaq -0xa0(%rbp), %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1060
movzwl %r15w, %eax
xorl %r12d, %r12d
cmpl $0x217, %eax            # imm = 0x217
jmp 0x2771
leaq 0x19d0(%rip), %rsi      # 0x40d8
leaq -0xa0(%rbp), %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1060
movzwl %r15w, %eax
xorl %r12d, %r12d
cmpl $0x6e5, %eax            # imm = 0x6E5
jmp 0x27a3
leaq 0x19a3(%rip), %rsi      # 0x40d1
leaq -0xa0(%rbp), %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x1060
movzwl %r15w, %eax
xorl %r12d, %r12d
cmpl $0x7a9, %eax            # imm = 0x7A9
jmp 0x27a3
leaq 0x1958(%rip), %rsi      # 0x40ac
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 0x27af
leaq 0x1944(%rip), %rsi      # 0x40ca
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 0x188b(%rip), %rsi      # 0x4041
leaq -0xa0(%rbp), %rdi
callq 0x1130
movq %rax, %r15
testl %r14d, %r14d
je 0x27e8
addq %r12, %rbx
movl %r14d, %r14d
xorl %r12d, %r12d
movzbl (%rbx,%r12), %edi
movq %r15, %rsi
callq 0x1150
incq %r12
cmpq %r12, %r14
jne 0x27d3
movq %r15, %rdi
addq $0x80, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x10d0
movq 0x37da(%rip), %rax      # 0x5fe0
movq (%rax), %rdi
leaq 0x18d0(%rip), %rsi      # 0x40e0
xorl %eax, %eax
addq $0x80, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x1170
nop