// File: /workspace/github_cpp_makefile_star2_O1/spjmurray[P]nagios-plugin-check-scsi-smart/check_scsi_smart.cc
// Function: main
int main(int argc, char** argv) {

  const char* device = 0;
  const char* warning = "";
  const char* critical = "";

  static struct option long_options[] = {
    { "help",     no_argument,       0, 'h' },
    { "version",  no_argument,       0, 'V' },
    { "device",   required_argument, 0, 'd' },
    { "warning",  required_argument, 0, 'w' },
    { "critical", required_argument, 0, 'c' },
    { 0,          0,                 0, 0   }
  };

  int c;
  while((c = getopt_long(argc, argv, "hVd:w:c:", long_options, 0)) != -1) {
    switch(c) {
      case 'h':
        help();
        exit(0);
      case 'V':
        version();
        exit(0);
      case 'd':
        device = optarg;
        break;
      case 'w':
        warning = optarg;
        break;
      case 'c':
        critical = optarg;
        break;
      default:
        usage();
        exit(1);
        break;
    }
  }

  // Check for required arguments
  if(!device) {
    help();
    exit(NAGIOS_UNKNOWN);
  }

  // Parse optional arguments
  SmartThresholdMap warning_thresholds;
  if(!parse_thresholds(warning_thresholds, warning)) {
    help();
    exit(NAGIOS_UNKNOWN);
  }

  SmartThresholdMap critical_thresholds;
  if(!parse_thresholds(critical_thresholds, critical)) {
    help();
    exit(NAGIOS_UNKNOWN);
  }

  // Check the device is compatible with the check
  int fd = open(device, O_RDWR);
  if(fd == -1) {
    cerr << "UNKNOWN: unable to open device " << device << endl;
    exit(NAGIOS_UNKNOWN);
  }

  int sg_version;
  if((ioctl(fd, SG_GET_VERSION_NUM, &sg_version) == -1) || sg_version < 30000) {
    cerr << "UNKNOWN: " << device << " is either not an sg device, or the driver is old" << endl;
    exit(NAGIOS_UNKNOWN);
  }

  // Check the device can use SMART and that it is enabled
  uint16_t identify[SECTOR_SIZE / 2];
  if(!ata_identify(fd, reinterpret_cast<unsigned char*>(identify))) {
    cout << "OK: ATA command set unsupported" << endl;
    exit(NAGIOS_OK);
  }

  if(~StorageEndian::swap(identify[82]) & 0x01) {
    cout << "OK: SMART feature set unsupported" << endl;
    exit(NAGIOS_OK);
  }

  if(~StorageEndian::swap(identify[85]) & 0x01) {
    cout << "UNKNOWN: SMART feature set disabled" << endl;
    exit(NAGIOS_UNKNOWN);
  }

  int code = NAGIOS_OK;
  int prdfail = 0;
  int advisory = 0;
  int crit = 0;
  int warn = 0;
  int logs = 0;
  stringstream perfdata;

  // Perform the checks
  check_smart_attributes(fd, critical_thresholds, warning_thresholds, code, prdfail, advisory, crit, warn, perfdata);
  check_smart_log(fd, code, logs);

  // Print out the results and performance data
  const char* status[] = { "OK", "WARNING", "CRITICAL" };
  cout << status[code]
       << ": prdfail " << prdfail
       << ", advisory " << advisory
       << ", critical " << crit
       << ", warning " << warn
       << ", logs " << logs
       << " |" << perfdata.str()
       << endl;

  close(fd);

  return code;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x488, %rsp            # imm = 0x488
movq %rsi, %r12
movl %edi, %ebp
leaq 0x11fc(%rip), %rax      # 0x624f
xorl %r14d, %r14d
leaq 0x11c0(%rip), %r13      # 0x621d
leaq 0x411c(%rip), %rbx      # 0x9180
movq 0x3f45(%rip), %r15      # 0x8fb0
movq %rax, 0x28(%rsp)
movq %rax, 0x20(%rsp)
movl %ebp, %edi
movq %r12, %rsi
movq %r13, %rdx
movq %rbx, %rcx
xorl %r8d, %r8d
callq 0x4260
cmpl $0x64, %eax
jne 0x5092
movq (%r15), %r14
jmp 0x5075
cmpl $0x62, %eax
jle 0x50b4
cmpl $0x63, %eax
je 0x50af
cmpl $0x77, %eax
jne 0x5487
movq (%r15), %rax
movq %rax, 0x28(%rsp)
jmp 0x5075
movq (%r15), %rax
jmp 0x5070
cmpl $-0x1, %eax
jne 0x5493
testq %r14, %r14
je 0x54b3
leaq 0xc8(%rsp), %rax
movl $0x0, (%rax)
xorl %ecx, %ecx
movq %rcx, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movq %rcx, 0x20(%rax)
leaq 0x70(%rsp), %rdi
leaq 0x280(%rsp), %rdx
movq 0x28(%rsp), %rsi
callq 0x57b4
leaq 0xc0(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x4d33
movl %eax, %ebx
leaq 0x80(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5132
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x41a0
testb %bl, %bl
je 0x54bd
leaq 0x98(%rsp), %rax
movl $0x0, (%rax)
xorl %ecx, %ecx
movq %rcx, 0x8(%rax)
movq %rax, 0x10(%rax)
movq %rax, 0x18(%rax)
movq %rcx, 0x20(%rax)
leaq 0x50(%rsp), %rdi
leaq 0x280(%rsp), %rdx
movq 0x20(%rsp), %rsi
callq 0x57b4
leaq 0x90(%rsp), %rdi
leaq 0x50(%rsp), %rsi
callq 0x4d33
movl %eax, %ebx
leaq 0x60(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x51a0
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x41a0
testb %bl, %bl
je 0x54c7
movq %r14, %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x4120
movl %eax, %ebx
cmpl $-0x1, %eax
je 0x54d1
leaq 0x1c(%rsp), %rdx
movl $0x2282, %esi           # imm = 0x2282
movl %ebx, %edi
xorl %eax, %eax
callq 0x4200
cmpl $-0x1, %eax
je 0x5514
cmpl $0x752f, 0x1c(%rsp)     # imm = 0x752F
jle 0x5514
xorps %xmm0, %xmm0
leaq 0xf0(%rsp), %rsi
movaps %xmm0, (%rsi)
movb $-0x7b, (%rsi)
movzwl 0x1(%rsi), %eax
andl $0xffffe0e1, %eax       # imm = 0xFFFFE0E1
orl $0xe08, %eax            # imm = 0xE08
movw %ax, 0x1(%rsi)
movb $0x1, 0x6(%rsi)
movb $-0x14, 0xe(%rsi)
leaq 0x280(%rsp), %rcx
movl %ebx, %edi
movl $0x10, %edx
movl $0x200, %r8d            # imm = 0x200
callq 0x48aa
testb %al, %al
je 0x556c
testb $0x1, 0x324(%rsp)
je 0x5595
testb $0x1, 0x32a(%rsp)
je 0x55be
xorl %eax, %eax
movl %eax, 0x4(%rsp)
movl %eax, 0x18(%rsp)
movl %eax, 0x14(%rsp)
movl %eax, 0x10(%rsp)
movl %eax, 0xc(%rsp)
movl %eax, 0x8(%rsp)
leaq 0xf0(%rsp), %rdi
callq 0x4100
leaq 0x100(%rsp), %rax
subq $0x8, %rsp
leaq 0x14(%rsp), %r10
leaq 0x18(%rsp), %r11
leaq 0x98(%rsp), %rsi
leaq 0xc8(%rsp), %rdx
leaq 0xc(%rsp), %rcx
leaq 0x20(%rsp), %r8
leaq 0x1c(%rsp), %r9
movl %ebx, %edi
pushq %rax
pushq %r10
pushq %r11
callq 0x4a83
addq $0x20, %rsp
leaq 0x4(%rsp), %rsi
leaq 0x8(%rsp), %rdx
movl %ebx, %edi
callq 0x4c82
movslq 0x4(%rsp), %r14
leaq 0x3292(%rip), %rax      # 0x8570
movq (%rax,%r14,8), %r15
movq %r15, %rdi
callq 0x40a0
movq 0x3cd7(%rip), %rdi      # 0x8fc8
movq %r15, %rsi
movq %rax, %rdx
callq 0x41c0
movq 0x3cc5(%rip), %rdi      # 0x8fc8
leaq 0xff2(%rip), %rsi       # 0x62fc
movl $0xa, %edx
callq 0x41c0
movl 0x18(%rsp), %esi
movq 0x3ca9(%rip), %rdi      # 0x8fc8
callq 0x4280
movq %rax, %r15
leaq 0xfd9(%rip), %rsi       # 0x6307
movl $0xb, %edx
movq %rax, %rdi
callq 0x41c0
movl 0x14(%rsp), %esi
movq %r15, %rdi
callq 0x4280
movq %rax, %r15
leaq 0xfc2(%rip), %rsi       # 0x6313
movl $0xb, %edx
movq %rax, %rdi
callq 0x41c0
movl 0x10(%rsp), %esi
movq %r15, %rdi
callq 0x4280
movq %rax, %r15
leaq 0xfab(%rip), %rsi       # 0x631f
movl $0xa, %edx
movq %rax, %rdi
callq 0x41c0
movl 0xc(%rsp), %esi
movq %r15, %rdi
callq 0x4280
movq %rax, %r15
leaq 0xf93(%rip), %rsi       # 0x632a
movl $0x7, %edx
movq %rax, %rdi
callq 0x41c0
movl 0x8(%rsp), %esi
movq %r15, %rdi
callq 0x4280
movq %rax, %r15
leaq 0xf78(%rip), %rsi       # 0x6332
movl $0x2, %edx
movq %rax, %rdi
callq 0x41c0
leaq 0x108(%rsp), %rsi
leaq 0x30(%rsp), %rdi
callq 0x4240
movq 0x30(%rsp), %rsi
movq 0x38(%rsp), %rdx
movq %r15, %rdi
callq 0x41c0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x4170
movsbl %al, %esi
movq %r15, %rdi
callq 0x4040
movq %rax, %rdi
callq 0x40e0
leaq 0x40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5430
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x41a0
movl %ebx, %edi
callq 0x42c0
movq 0x3b62(%rip), %rsi      # 0x8fa0
leaq 0xf0(%rsp), %rdi
callq 0x4130
leaq 0x170(%rsp), %rdi
callq 0x4070
leaq 0x90(%rsp), %rdi
callq 0x57fc
leaq 0xc0(%rsp), %rdi
callq 0x57fc
movl %r14d, %eax
addq $0x488, %rsp            # imm = 0x488
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
cmpl $0x68, %eax
jne 0x54a4
callq 0x452e
jmp 0x549d
cmpl $0x56, %eax
jne 0x54a4
callq 0x442c
xorl %edi, %edi
callq 0x41d0
callq 0x4498
movl $0x1, %edi
callq 0x41d0
callq 0x452e
jmp 0x55e2
callq 0x452e
jmp 0x55e2
callq 0x452e
jmp 0x55e2
movq 0x3b10(%rip), %rdi      # 0x8fe8
leaq 0xd47(%rip), %rsi       # 0x6226
movl $0x1f, %edx
callq 0x41c0
movq %r14, %rdi
callq 0x40a0
movq 0x3af0(%rip), %rdi      # 0x8fe8
movq %r14, %rsi
movq %rax, %rdx
callq 0x41c0
movq 0x3ade(%rip), %rdi      # 0x8fe8
callq 0x40b0
jmp 0x55e2
movq 0x3acd(%rip), %rdi      # 0x8fe8
leaq 0xd24(%rip), %rsi       # 0x6246
movl $0x9, %edx
callq 0x41c0
movq %r14, %rdi
callq 0x40a0
movq 0x3aad(%rip), %rdi      # 0x8fe8
movq %r14, %rsi
movq %rax, %rdx
callq 0x41c0
movq 0x3a9b(%rip), %rdi      # 0x8fe8
leaq 0xcfc(%rip), %rsi       # 0x6250
movl $0x31, %edx
callq 0x41c0
movq 0x3a83(%rip), %rdi      # 0x8fe8
callq 0x40b0
jmp 0x55e2
movq 0x3a55(%rip), %rdi      # 0x8fc8
leaq 0xd08(%rip), %rsi       # 0x6282
movl $0x1f, %edx
callq 0x41c0
movq 0x3a3d(%rip), %rdi      # 0x8fc8
callq 0x40b0
jmp 0x549d
movq 0x3a2c(%rip), %rdi      # 0x8fc8
leaq 0xcff(%rip), %rsi       # 0x62a2
movl $0x21, %edx
callq 0x41c0
movq 0x3a14(%rip), %rdi      # 0x8fc8
callq 0x40b0
jmp 0x549d
movq 0x3a03(%rip), %rdi      # 0x8fc8
leaq 0xcf8(%rip), %rsi       # 0x62c4
movl $0x23, %edx
callq 0x41c0
movq 0x39eb(%rip), %rdi      # 0x8fc8
callq 0x40b0
movl $0x3, %edi
callq 0x41d0
jmp 0x564b
jmp 0x5642
jmp 0x564b
jmp 0x5670
jmp 0x564b
movq %rax, %rbx
leaq 0x60(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5694
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x41a0
jmp 0x5694
jmp 0x564b
movq %rax, %rbx
leaq 0x80(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x56a1
movq 0x80(%rsp), %rsi
incq %rsi
callq 0x41a0
jmp 0x56a1
movq %rax, %rbx
jmp 0x56a1
jmp 0x564b
jmp 0x5670
movq %rax, %rbx
jmp 0x5694
movq %rax, %rbx
leaq 0x40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x5673
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x41a0
jmp 0x5673
movq %rax, %rbx
movq 0x3926(%rip), %rsi      # 0x8fa0
leaq 0xf0(%rsp), %rdi
callq 0x4130
leaq 0x170(%rsp), %rdi
callq 0x4070
leaq 0x90(%rsp), %rdi
callq 0x57fc
leaq 0xc0(%rsp), %rdi
callq 0x57fc
movq %rbx, %rdi
callq 0x4290