// File: trapexit[P]bbf/src/sg.cpp
// Function: sg::buf_to_identity(char const*, sg::identity&)
void
  buf_to_identity(const char    buf[256*2],
                  sg::identity &ident)
  {
    uint16_t tmpbuf[256];
    const uint16_t *buf16 = (const uint16_t*)buf;

    ::memset(&ident,0,sizeof(sg::identity));
    for(unsigned int i = 0; i < 256; i++)
      tmpbuf[i] = ntohs(buf16[i]);

    ident.rpm = buf16[217];
    ident.trim_supported = !!(buf16[169] & 0x0001);
    ident.form_factor    = (buf16[168] & 0x0003);
    ident.smart_supported    = (((buf16[83] & 0xC000) == 0x4000) && (buf16[82] & 0x0001));
    ident.smart_enabled      = (((buf16[87] & 0xC000) == 0x4000) && (buf16[85] & 0x0001));
    ident.security_supported = (((buf16[83] & 0xC000) == 0x4000) && (buf16[82] & 0x0002));
    ident.security_enabled   = (((buf16[87] & 0xC000) == 0x4000) && (buf16[85] & 0x0002));
    ident.security_locked                   = !!(buf16[128] & 0x0004);
    ident.security_frozen                   = !!(buf16[128] & 0x0008);
    ident.security_count_expired            = !!(buf16[128] & 0x0010);
    ident.security_enhanced_erase_supported = !!(buf16[128] & 0x0020);
    ident.write_uncorrectable = (((buf16[83]  & 0xC000) == 0x4000) &&
                                 ((buf16[86]  & 0x8000) == 0x8000) &&
                                 ((buf16[119] & 0xC004) == 0x4004) &&
                                 ((buf16[120] & 0xC004) == 0x4004));
    ident.block_erase        = !!(buf16[59] & 0x8000);
    ident.overwrite          = !!(buf16[59] & 0x4000);
    ident.crypto_scramble    = !!(buf16[59] & 0x2000);
    ident.sanitize           = !!(buf16[59] & 0x1000);
    ident.supports_sata_gen1 = !!(buf16[76] & 0x0002);
    ident.supports_sata_gen2 = !!(buf16[76] & 0x0004);
    ident.supports_sata_gen3 = !!(buf16[76] & 0x0008);

    ident.security_normal_erase_time   = (((buf16[89] & 0x8000) ?
                                           (buf16[89] & 0x7FFF) :
                                           (buf16[89] & 0x00FF)) * 2);
    if((ident.security_normal_erase_time == 0x7FFF) ||
       (ident.security_normal_erase_time == 0x00FF))
      ident.security_normal_erase_time += 90;
    ident.security_enhanced_erase_time = (((buf16[90] & 0x8000) ?
                                           (buf16[90] & 0x7FFF) :
                                           (buf16[90] & 0x00FF)) * 2);
    if((ident.security_enhanced_erase_time == 0x7FFF) ||
       (ident.security_enhanced_erase_time == 0x00FF))
      ident.security_enhanced_erase_time += 90;

    sscanf((char*)&tmpbuf[10],"%20s",ident.serial_number);
    sscanf((char*)&tmpbuf[23],"%8s",ident.firmware_revision);
    sscanf((char*)&tmpbuf[27],"%40c",ident.model_number);
  }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x230, %rsp            # imm = 0x230
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x218(%rbp)
movq -0x10(%rbp), %rdi
xorl %esi, %esi
movl $0x58, %edx
callq 0x42b0
movl $0x0, -0x21c(%rbp)
cmpl $0x100, -0x21c(%rbp)    # imm = 0x100
jae 0x210bc
movq -0x218(%rbp), %rax
movl -0x21c(%rbp), %ecx
movzwl (%rax,%rcx,2), %edi
callq 0x48e0
movw %ax, %cx
movl -0x21c(%rbp), %eax
movw %cx, -0x210(%rbp,%rax,2)
movl -0x21c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x21c(%rbp)
jmp 0x21078
movq -0x218(%rbp), %rax
movzwl 0x1b2(%rax), %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x4(%rax)
movq -0x218(%rbp), %rax
movzwl 0x152(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movb %cl, %dl
movb 0x2(%rax), %cl
andb $0x1, %dl
andb $-0x2, %cl
orb %dl, %cl
movb %cl, 0x2(%rax)
movq -0x218(%rbp), %rax
movzwl 0x150(%rax), %eax
andl $0x3, %eax
movb %al, %cl
movq -0x10(%rbp), %rax
movb %cl, 0x3(%rax)
movq -0x218(%rbp), %rax
movzwl 0xa6(%rax), %ecx
andl $0xc000, %ecx           # imm = 0xC000
xorl %eax, %eax
cmpl $0x4000, %ecx           # imm = 0x4000
movb %al, -0x21d(%rbp)
jne 0x21162
movq -0x218(%rbp), %rax
movzwl 0xa4(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
setne %al
movb %al, -0x21d(%rbp)
movb -0x21d(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movw %cx, %dx
movw (%rax), %cx
andw $0x1, %dx
shlw %dx
andw $-0x3, %cx
orw %dx, %cx
movw %cx, (%rax)
movq -0x218(%rbp), %rax
movzwl 0xae(%rax), %ecx
andl $0xc000, %ecx           # imm = 0xC000
xorl %eax, %eax
cmpl $0x4000, %ecx           # imm = 0x4000
movb %al, -0x21e(%rbp)
jne 0x211cb
movq -0x218(%rbp), %rax
movzwl 0xaa(%rax), %eax
andl $0x1, %eax
cmpl $0x0, %eax
setne %al
movb %al, -0x21e(%rbp)
movb -0x21e(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movw %cx, %dx
movw (%rax), %cx
andw $0x1, %dx
shlw $0x2, %dx
andw $-0x5, %cx
orw %dx, %cx
movw %cx, (%rax)
movq -0x218(%rbp), %rax
movzwl 0xa6(%rax), %ecx
andl $0xc000, %ecx           # imm = 0xC000
xorl %eax, %eax
cmpl $0x4000, %ecx           # imm = 0x4000
movb %al, -0x21f(%rbp)
jne 0x21235
movq -0x218(%rbp), %rax
movzwl 0xa4(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
setne %al
movb %al, -0x21f(%rbp)
movb -0x21f(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movw %cx, %dx
movw (%rax), %cx
andw $0x1, %dx
shlw $0x3, %dx
andw $-0x9, %cx
orw %dx, %cx
movw %cx, (%rax)
movq -0x218(%rbp), %rax
movzwl 0xae(%rax), %ecx
andl $0xc000, %ecx           # imm = 0xC000
xorl %eax, %eax
cmpl $0x4000, %ecx           # imm = 0x4000
movb %al, -0x220(%rbp)
jne 0x2129f
movq -0x218(%rbp), %rax
movzwl 0xaa(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
setne %al
movb %al, -0x220(%rbp)
movb -0x220(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movw %cx, %dx
movw (%rax), %cx
andw $0x1, %dx
shlw $0x4, %dx
andw $-0x11, %cx
orw %dx, %cx
movw %cx, (%rax)
movq -0x218(%rbp), %rax
movzwl 0x100(%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movw %cx, %dx
movw (%rax), %cx
andw $0x1, %dx
shlw $0x5, %dx
andw $-0x21, %cx
orw %dx, %cx
movw %cx, (%rax)
movq -0x218(%rbp), %rax
movzwl 0x100(%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movw %cx, %dx
movw (%rax), %cx
andw $0x1, %dx
shlw $0x6, %dx
andw $-0x41, %cx
orw %dx, %cx
movw %cx, (%rax)
movq -0x218(%rbp), %rax
movzwl 0x100(%rax), %eax
andl $0x10, %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movw %cx, %dx
movw (%rax), %cx
andw $0x1, %dx
shlw $0x7, %dx
andw $0xff7f, %cx            # imm = 0xFF7F
orw %dx, %cx
movw %cx, (%rax)
movq -0x218(%rbp), %rax
movzwl 0x100(%rax), %eax
andl $0x20, %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movw %cx, %dx
movw (%rax), %cx
andw $0x1, %dx
shlw $0x8, %dx
andw $0xfeff, %cx            # imm = 0xFEFF
orw %dx, %cx
movw %cx, (%rax)
movq -0x218(%rbp), %rax
movzwl 0xa6(%rax), %ecx
andl $0xc000, %ecx           # imm = 0xC000
xorl %eax, %eax
cmpl $0x4000, %ecx           # imm = 0x4000
movb %al, -0x221(%rbp)
jne 0x2144f
movq -0x218(%rbp), %rax
movzwl 0xac(%rax), %ecx
andl $0x8000, %ecx           # imm = 0x8000
xorl %eax, %eax
cmpl $0x8000, %ecx           # imm = 0x8000
movb %al, -0x221(%rbp)
jne 0x2144f
movq -0x218(%rbp), %rax
movzwl 0xee(%rax), %ecx
andl $0xc004, %ecx           # imm = 0xC004
xorl %eax, %eax
cmpl $0x4004, %ecx           # imm = 0x4004
movb %al, -0x221(%rbp)
jne 0x2144f
movq -0x218(%rbp), %rax
movzwl 0xf0(%rax), %eax
andl $0xc004, %eax           # imm = 0xC004
cmpl $0x4004, %eax           # imm = 0x4004
sete %al
movb %al, -0x221(%rbp)
movb -0x221(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movw %cx, %dx
movw (%rax), %cx
andw $0x1, %dx
andw $-0x2, %cx
orw %dx, %cx
movw %cx, (%rax)
movq -0x218(%rbp), %rax
movzwl 0x76(%rax), %eax
andl $0x8000, %eax           # imm = 0x8000
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movw %cx, %dx
movw (%rax), %cx
andw $0x1, %dx
shlw $0x9, %dx
andw $0xfdff, %cx            # imm = 0xFDFF
orw %dx, %cx
movw %cx, (%rax)
movq -0x218(%rbp), %rax
movzwl 0x76(%rax), %eax
andl $0x4000, %eax           # imm = 0x4000
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movw %cx, %dx
movw (%rax), %cx
andw $0x1, %dx
shlw $0xa, %dx
andw $0xfbff, %cx            # imm = 0xFBFF
orw %dx, %cx
movw %cx, (%rax)
movq -0x218(%rbp), %rax
movzwl 0x76(%rax), %eax
andl $0x2000, %eax           # imm = 0x2000
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movw %cx, %dx
movw (%rax), %cx
andw $0x1, %dx
shlw $0xb, %dx
andw $0xf7ff, %cx            # imm = 0xF7FF
orw %dx, %cx
movw %cx, (%rax)
movq -0x218(%rbp), %rax
movzwl 0x76(%rax), %eax
andl $0x1000, %eax           # imm = 0x1000
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movw %cx, %dx
movw (%rax), %cx
andw $0x1, %dx
shlw $0xc, %dx
andw $0xefff, %cx            # imm = 0xEFFF
orw %dx, %cx
movw %cx, (%rax)
movq -0x218(%rbp), %rax
movzwl 0x98(%rax), %eax
andl $0x2, %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movw %cx, %dx
movw (%rax), %cx
andw $0x1, %dx
shlw $0xd, %dx
andw $0xdfff, %cx            # imm = 0xDFFF
orw %dx, %cx
movw %cx, (%rax)
movq -0x218(%rbp), %rax
movzwl 0x98(%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movw %cx, %dx
movw (%rax), %cx
andw $0x1, %dx
shlw $0xe, %dx
andw $0xbfff, %cx            # imm = 0xBFFF
orw %dx, %cx
movw %cx, (%rax)
movq -0x218(%rbp), %rax
movzwl 0x98(%rax), %eax
andl $0x8, %eax
cmpl $0x0, %eax
setne %al
xorb $-0x1, %al
xorb $-0x1, %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, %ecx
movq -0x10(%rbp), %rax
movw %cx, %dx
movw (%rax), %cx
andw $0x1, %dx
shlw $0xf, %dx
andw $0x7fff, %cx            # imm = 0x7FFF
orw %dx, %cx
movw %cx, (%rax)
movq -0x218(%rbp), %rax
movzwl 0xb2(%rax), %eax
andl $0x8000, %eax           # imm = 0x8000
cmpl $0x0, %eax
je 0x2165c
movq -0x218(%rbp), %rax
movzwl 0xb2(%rax), %eax
andl $0x7fff, %eax           # imm = 0x7FFF
movl %eax, -0x228(%rbp)
jmp 0x21675
movq -0x218(%rbp), %rax
movzwl 0xb2(%rax), %eax
andl $0xff, %eax
movl %eax, -0x228(%rbp)
movl -0x228(%rbp), %ecx
shll %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0x8(%rax)
movq -0x10(%rbp), %rax
cmpl $0x7fff, 0x8(%rax)      # imm = 0x7FFF
je 0x2169e
movq -0x10(%rbp), %rax
cmpl $0xff, 0x8(%rax)
jne 0x216ab
movq -0x10(%rbp), %rax
movl 0x8(%rax), %ecx
addl $0x5a, %ecx
movl %ecx, 0x8(%rax)
movq -0x218(%rbp), %rax
movzwl 0xb4(%rax), %eax
andl $0x8000, %eax           # imm = 0x8000
cmpl $0x0, %eax
je 0x216de
movq -0x218(%rbp), %rax
movzwl 0xb4(%rax), %eax
andl $0x7fff, %eax           # imm = 0x7FFF
movl %eax, -0x22c(%rbp)
jmp 0x216f7
movq -0x218(%rbp), %rax
movzwl 0xb4(%rax), %eax
andl $0xff, %eax
movl %eax, -0x22c(%rbp)
movl -0x22c(%rbp), %ecx
shll %ecx
movq -0x10(%rbp), %rax
movl %ecx, 0xc(%rax)
movq -0x10(%rbp), %rax
cmpl $0x7fff, 0xc(%rax)      # imm = 0x7FFF
je 0x21720
movq -0x10(%rbp), %rax
cmpl $0xff, 0xc(%rax)
jne 0x2172d
movq -0x10(%rbp), %rax
movl 0xc(%rax), %ecx
addl $0x5a, %ecx
movl %ecx, 0xc(%rax)
leaq -0x210(%rbp), %rdi
addq $0x14, %rdi
movq -0x10(%rbp), %rdx
addq $0x10, %rdx
leaq 0xab04(%rip), %rsi      # 0x2c24b
movb $0x0, %al
callq 0x4520
leaq -0x210(%rbp), %rdi
addq $0x2e, %rdi
movq -0x10(%rbp), %rdx
addq $0x25, %rdx
leaq 0xaae8(%rip), %rsi      # 0x2c250
movb $0x0, %al
callq 0x4520
leaq -0x210(%rbp), %rdi
addq $0x36, %rdi
movq -0x10(%rbp), %rdx
addq $0x2e, %rdx
leaq 0xaacb(%rip), %rsi      # 0x2c254
movb $0x0, %al
callq 0x4520
addq $0x230, %rsp            # imm = 0x230
popq %rbp
retq
nopl (%rax)