// File: /workspace/github_cpp_makefile_star2_O2/spjmurray[P]nagios-plugin-check-scsi-smart/check_scsi_smart.cc
// Function: check_smart_attributes(int, std::map<unsigned char, unsigned long, std::less<unsigned char>, std::allocator<std::pair<unsigned char const, unsigned long>>>&, std::map<unsigned char, unsigned long, std::less<unsigned char>, std::allocator<std::pair<unsigned char const, unsigned long>>>&, int&, int&, int&, int&, int&, std::ostream&)
void check_smart_attributes(int fd, SmartThresholdMap& critical_thresholds, SmartThresholdMap& warning_thresholds,
                            int& code, int& prdfail, int& advisory, int& crit, int& warn, ostream& perfdata) {

  // Load the SMART data and thresholds pages
  smart_data sd;
  ata_smart_read_data(fd, reinterpret_cast<unsigned char*>(&sd));

  smart_thresholds st;
  ata_smart_read_thresholds(fd, reinterpret_cast<unsigned char*>(&st));

  // Perform actual SMART threshold checks
  for(int i=0; i<SMART_ATTRIBUTE_NUM; i++) {

    SmartAttribute attribute(sd.attributes[i]);
    SmartThreshold threshold(st.thresholds[i]);

    if(!attribute.idValid())
      continue;

    // Check the validity of the attribute value and whether the threshold has been exceeded
    if(attribute.valueValid() && (attribute <= threshold)) {

      // Predicted failure is within 24 hours, otherwise the device lifespan has been exceeded
      if(attribute.getPreFail())
        prdfail++;
      else
        advisory++;

    }

    // Check against custom raw thresholds
    uint64_t crit_threshold = critical_thresholds[attribute.getID()];
    uint64_t warn_threshold = warning_thresholds[attribute.getID()];

    if(crit_threshold && (attribute.getRaw() >= crit_threshold)) {
      crit++;
    } else if(warn_threshold && (attribute.getRaw() >= warn_threshold)) {
      warn++;
    }

    // Accumulate the performance data
    perfdata << " " << attribute << ";";
    if(warn_threshold)
      perfdata << warn_threshold;
    perfdata << ";";
    if(crit_threshold)
      perfdata << crit_threshold;
    perfdata << ";;";

  }

  // Determine the state to report
  if(advisory || warn)
    code = max(code, NAGIOS_WARNING);

  if(prdfail || crit)
    code = max(code, NAGIOS_CRITICAL);

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x448, %rsp            # imm = 0x448
movq %r9, 0x28(%rsp)
movq %r8, 0x30(%rsp)
movq %rcx, 0x20(%rsp)
movq %rdx, 0x40(%rsp)
movq %rsi, 0x38(%rsp)
movl %edi, %ebx
movq 0x490(%rsp), %rbp
leaq 0x248(%rsp), %rsi
callq 0x269d
leaq 0x48(%rsp), %rsi
movl %ebx, %edi
callq 0x26e6
pushq $0x2
popq %r12
leaq 0x10(%rsp), %rbx
leaq 0xf(%rsp), %r14
cmpq $0x16a, %r12            # imm = 0x16A
je 0x2921
leaq (%rsp,%r12), %rsi
addq $0x248, %rsi            # imm = 0x248
movq %rbx, %rdi
callq 0x37b0
leaq (%rsp,%r12), %rsi
addq $0x48, %rsi
movq %r14, %rdi
callq 0x38a6
cmpb $0x0, 0x10(%rsp)
je 0x2918
movb 0x13(%rsp), %al
decb %al
cmpb $-0x4, %al
ja 0x2853
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3826
testb %al, %al
je 0x2853
cmpb $0x0, 0x11(%rsp)
movq 0x28(%rsp), %rax
cmovneq 0x30(%rsp), %rax
incl (%rax)
movb 0x10(%rsp), %al
movb %al, 0xe(%rsp)
movq 0x38(%rsp), %rdi
leaq 0xe(%rsp), %r15
movq %r15, %rsi
callq 0x3136
movq (%rax), %r13
movb 0x10(%rsp), %al
movb %al, 0xe(%rsp)
movq 0x40(%rsp), %rdi
movq %r15, %rsi
callq 0x3136
movq (%rax), %r15
movq 0x18(%rsp), %rax
leaq -0x1(%r13), %rdx
movq 0x480(%rsp), %rcx
cmpq %rax, %rdx
jb 0x28af
leaq -0x1(%r15), %rdx
movq 0x488(%rsp), %rcx
cmpq %rax, %rdx
jae 0x28b1
incl (%rcx)
movq %rbp, %rdi
leaq 0x1993(%rip), %rsi      # 0x424e
callq 0x2180
movq %rax, %rdi
movq %rbx, %rsi
callq 0x382f
movq %rax, %rdi
leaq 0x192e(%rip), %rsi      # 0x4203
callq 0x2180
testq %r15, %r15
je 0x28ea
movq %rbp, %rdi
movq %r15, %rsi
callq 0x20e0
movq %rbp, %rdi
leaq 0x190f(%rip), %rsi      # 0x4203
callq 0x2180
testq %r13, %r13
je 0x2909
movq %rbp, %rdi
movq %r13, %rsi
callq 0x20e0
movq %rbp, %rdi
leaq 0x18ef(%rip), %rsi      # 0x4202
callq 0x2180
addq $0xc, %r12
jmp 0x27ed
movq 0x488(%rsp), %rax
movl (%rax), %eax
movq 0x28(%rsp), %rcx
orl (%rcx), %eax
je 0x2946
movq 0x20(%rsp), %rdx
movl (%rdx), %eax
cmpl $0x2, %eax
pushq $0x1
popq %rcx
cmovgel %eax, %ecx
movl %ecx, (%rdx)
movq 0x480(%rsp), %rax
movl (%rax), %eax
movq 0x30(%rsp), %rcx
orl (%rcx), %eax
je 0x296b
movq 0x20(%rsp), %rdx
movl (%rdx), %eax
cmpl $0x3, %eax
pushq $0x2
popq %rcx
cmovgel %eax, %ecx
movl %ecx, (%rdx)
addq $0x448, %rsp            # imm = 0x448
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq