// File: /workspace/github_cpp_makefile_star2_O3/hollanderic[P]dp832/main.cpp
// Function: main
int main (int argc, char** argv) {
    int opt;
    int channel=1;
    double voltage=0.0;
    double current=0.0;
    bool state=false;
    bool setvoltage=false;
    bool setcurrent=false;
    bool setstate=false;
    bool extra = isExtraMode(argc, argv);

    const std::string psuDevicePath = getDevicePathArg(argc, argv);
    dp830 psu(psuDevicePath.c_str());

    while ((opt = getopt(argc, argv, kValidArgs.c_str())) != -1) {
        switch (opt) {
            case 'c':
                channel = atoi(optarg);
                if ((channel < 1) || (channel > 3)) {
                   fprintf(stderr, "Channel must be in range 1..3\n");
                   return -1;
                }
                break;
            case 'v':
                voltage = std::stod(optarg);
                psu.SetVoltage(channel,voltage);
                setvoltage=true;
                break;
            case 'i':
                current = std::stod(optarg);
                psu.SetCurrent(channel,current);
                setcurrent=true;
                break;
            case 't':
                current = std::stod(optarg);
                psu.SetOCP(channel,current);
                break;
            case 's':
                state = !(atoi(optarg) == 0);
                if (state)
                    psu.On(channel);
                else
                    psu.Off(channel);
                setstate=true;
                break;
            case 'b':
                psu.Bounce(channel,std::stod(optarg));
                break;
            case 'r':
            case 'm': {
                if (extra) {
                  int reps = 1;
                  int tt = 0;
		  if (opt == 'r') {
                     tt = std::stod(optarg);
                     reps = 1000/tt;
                  }
                  for (int i = 0; i < reps; i++) {
                    auto now = (duration_cast<milliseconds>(system_clock::now().time_since_epoch())).count();
                    printf("t=%lu,c=%d,s=%d,vs=%0.03f,is=%0.03f,ocp=%0.03f,v=%0.03f,i=%0.03f,p=%0.03f,tr=%d\n", 
                      now,
                      channel,
                      psu.GetState(channel),
                      psu.GetVoltageSetPoint(channel),
                      psu.GetCurrentSetPoint(channel),
                      psu.GetOCP(channel),
                      psu.MeasureVoltage(channel),
                      psu.MeasureCurrent(channel),
                      psu.MeasurePower(channel),
                      psu.GetOCPTripped(channel));
                    auto delta = (duration_cast<milliseconds>(system_clock::now().time_since_epoch())).count() - now;
                    if ((tt > delta)) {
                      usleep(1000*(tt-delta));
                    }
                  }
                } else { 
                    fprintf(stderr, "Can't use -m in legacy mode, add -x flag\n");
                }
                break;
            }
            case 'w': // ms delay
                if (extra) {
                    usleep(1000*std::stod(optarg));
                } else { 
                    fprintf(stderr, "Can't use -w in legacy mode, add -x flag\n");
                }
                break;
            default:
                break;
        }
    }

    if (!extra) {
      usleep(1*1e6);

      printf("Voltage = %f\n",psu.MeasureVoltage(channel));
      printf("Current = %f\n",psu.MeasureCurrent(channel));
      printf("Power   = %f\n",psu.MeasurePower(channel));
      printf("Volt sp   = %f\n",psu.GetVoltageSetPoint(channel));
      printf("Curr sp   = %f\n",psu.GetCurrentSetPoint(channel));
    }
    //Test Bounce feature - used to bounce a target
    //usleep(3*1e6);
    //psu.Bounce(1,2.5);

  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2a8, %rsp            # imm = 0x2A8
movq %rsi, 0x48(%rsp)
movl %edi, 0x44(%rsp)
movq 0x320b(%rip), %rdx      # 0x6140
movl 0x44(%rsp), %edi
movq 0x48(%rsp), %rsi
callq 0x2080
shll $0x18, %eax
cmpl $0x78000000, %eax       # imm = 0x78000000
je 0x2f51
testl %eax, %eax
jns 0x2f2e
movl %eax, 0x40(%rsp)
movq 0x307c(%rip), %rax      # 0x5fd8
movl $0x1, (%rax)
leaq 0x78(%rsp), %rbx
movq %rbx, %rdi
movl 0x44(%rsp), %esi
movq 0x48(%rsp), %rdx
callq 0x2e0c
movq (%rbx), %rsi
leaq 0x98(%rsp), %rdi
callq 0x24bc
leaq 0x28(%rsp), %r12
movl $0x1, %ebx
leaq 0x98(%rsp), %r13
movq 0x319f(%rip), %rdx      # 0x6140
movl 0x44(%rsp), %edi
movq 0x48(%rsp), %rsi
callq 0x2080
leal -0x62(%rax), %ecx
cmpl $0x15, %ecx
ja 0x327a
leaq 0x11be(%rip), %rdx      # 0x4180
movslq (%rdx,%rcx,4), %rcx
addq %rdx, %rcx
jmpq *%rcx
cmpl $0x0, 0x40(%rsp)
js 0x34de
cmpl $0x72, %eax
jne 0x326c
movq 0x2fda(%rip), %rax      # 0x5fc0
movq (%rax), %rsi
leaq 0x18(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x371e
movq 0x18(%rsp), %r15
callq 0x2030
movq %rax, %r14
movl (%rax), %ebp
movl $0x0, (%rax)
movq %r15, %rdi
leaq 0x10(%rsp), %rsi
callq 0x2110
cmpq %r15, 0x10(%rsp)
je 0x3618
movl (%r14), %eax
testl %eax, %eax
je 0x3339
cmpl $0x22, %eax
jne 0x333c
jmp 0x3660
movq 0x2f7b(%rip), %rax      # 0x5fc0
movq (%rax), %rsi
leaq 0x18(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x371e
movq 0x18(%rsp), %r15
callq 0x2030
movq %rax, %r14
movl (%rax), %ebp
movl $0x0, (%rax)
movq %r15, %rdi
leaq 0x10(%rsp), %rsi
callq 0x2110
cmpq %r15, 0x10(%rsp)
je 0x360c
movl (%r14), %eax
testl %eax, %eax
je 0x3288
cmpl $0x22, %eax
jne 0x328b
jmp 0x363c
movq 0x2f1c(%rip), %rax      # 0x5fc0
movq (%rax), %rsi
leaq 0x18(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x371e
movq 0x18(%rsp), %r15
callq 0x2030
movq %rax, %r14
movl (%rax), %ebp
movl $0x0, (%rax)
movq %r15, %rdi
leaq 0x10(%rsp), %rsi
callq 0x2110
cmpq %r15, 0x10(%rsp)
je 0x35f4
movl (%r14), %eax
testl %eax, %eax
je 0x32bd
cmpl $0x22, %eax
jne 0x32c0
jmp 0x3648
movq 0x2ebd(%rip), %rax      # 0x5fc0
movq (%rax), %rdi
callq 0x21d0
movl %eax, %ebx
addl $-0x4, %eax
cmpl $-0x4, %eax
ja 0x2f9a
jmp 0x359c
movq 0x2e9b(%rip), %rax      # 0x5fc0
movq (%rax), %rsi
leaq 0x18(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x371e
movq 0x18(%rsp), %r15
callq 0x2030
movq %rax, %r14
movl (%rax), %ebp
movl $0x0, (%rax)
movq %r15, %rdi
leaq 0x10(%rsp), %rsi
callq 0x2110
cmpq %r15, 0x10(%rsp)
je 0x35e8
movl (%r14), %eax
testl %eax, %eax
je 0x32cf
cmpl $0x22, %eax
jne 0x32d2
jmp 0x3630
movq 0x2e3c(%rip), %rax      # 0x5fc0
movq (%rax), %rdi
callq 0x21d0
testl %eax, %eax
je 0x34cf
movq %r13, %rdi
movl %ebx, %esi
callq 0x2650
jmp 0x2f9a
movq 0x2e16(%rip), %rax      # 0x5fc0
movq (%rax), %rsi
leaq 0x18(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x371e
movq 0x18(%rsp), %r15
callq 0x2030
movq %rax, %r14
movl (%rax), %ebp
movl $0x0, (%rax)
movq %r15, %rdi
leaq 0x10(%rsp), %rsi
callq 0x2110
cmpq %r15, 0x10(%rsp)
je 0x3600
movl (%r14), %eax
testl %eax, %eax
je 0x3304
cmpl $0x22, %eax
jne 0x3307
jmp 0x3654
cmpl $0x0, 0x40(%rsp)
js 0x34e8
movq 0x2dac(%rip), %rax      # 0x5fc0
movq (%rax), %rsi
leaq 0x18(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x371e
movq 0x18(%rsp), %r15
callq 0x2030
movq %rax, %r14
movl (%rax), %ebp
movl $0x0, (%rax)
movq %r15, %rdi
leaq 0x10(%rsp), %rsi
callq 0x2110
cmpq %r15, 0x10(%rsp)
je 0x3624
movl (%r14), %eax
testl %eax, %eax
je 0x349a
cmpl $0x22, %eax
jne 0x349d
jmp 0x366c
movl $0x1, %r15d
xorl %r12d, %r12d
jmp 0x337a
cmpl $-0x1, %eax
jne 0x2f9a
jmp 0x34f2
movl %ebp, (%r14)
movq 0x18(%rsp), %rdi
cmpq %r12, %rdi
je 0x32ae
movq 0x28(%rsp), %rsi
incq %rsi
movsd %xmm0, 0x38(%rsp)
callq 0x2160
movsd 0x38(%rsp), %xmm0
movq %r13, %rdi
movl %ebx, %esi
callq 0x281e
jmp 0x2f9a
movl %ebp, (%r14)
movq %r13, %rdi
movl %ebx, %esi
callq 0x2cba
jmp 0x34af
movl %ebp, (%r14)
movq 0x18(%rsp), %rdi
cmpq %r12, %rdi
je 0x32f5
movq 0x28(%rsp), %rsi
incq %rsi
movsd %xmm0, 0x38(%rsp)
callq 0x2160
movsd 0x38(%rsp), %xmm0
movq %r13, %rdi
movl %ebx, %esi
callq 0x27de
jmp 0x2f9a
movl %ebp, (%r14)
movq 0x18(%rsp), %rdi
cmpq %r12, %rdi
je 0x332a
movq 0x28(%rsp), %rsi
incq %rsi
movsd %xmm0, 0x38(%rsp)
callq 0x2160
movsd 0x38(%rsp), %xmm0
movq %r13, %rdi
movl %ebx, %esi
callq 0x285e
jmp 0x2f9a
movl %ebp, (%r14)
movq 0x18(%rsp), %rdi
cmpq %r12, %rdi
je 0x335f
movq 0x28(%rsp), %rsi
incq %rsi
movsd %xmm0, 0x38(%rsp)
callq 0x2160
movsd 0x38(%rsp), %xmm0
cvttsd2si %xmm0, %ecx
movl $0x3e8, %eax            # imm = 0x3E8
xorl %edx, %edx
idivl %ecx
testl %eax, %eax
jle 0x2f9a
movl %eax, %r15d
movslq %ecx, %r12
callq 0x2050
movabsq $0x431bde82d7b634db, %rcx # imm = 0x431BDE82D7B634DB
imulq %rcx
movq %rdx, %r14
movq %r13, %rdi
movl %ebx, %esi
callq 0x25f6
movl %eax, %ebp
movq %r13, %rdi
movl %ebx, %esi
callq 0x2b5a
movsd %xmm0, 0x38(%rsp)
movq %r13, %rdi
movl %ebx, %esi
callq 0x2b66
movsd %xmm0, 0x70(%rsp)
movq %r13, %rdi
movl %ebx, %esi
callq 0x28cc
movsd %xmm0, 0x68(%rsp)
movq %r13, %rdi
movl %ebx, %esi
callq 0x2c96
movsd %xmm0, 0x60(%rsp)
movq %r13, %rdi
movl %ebx, %esi
callq 0x2ca2
movsd %xmm0, 0x58(%rsp)
movq %r13, %rdi
movl %ebx, %esi
callq 0x2cae
movsd %xmm0, 0x50(%rsp)
movq %r13, %rdi
movl %ebx, %esi
callq 0x29e2
movq %r14, %rcx
shrq $0x3f, %rcx
sarq $0x12, %r14
addq %rcx, %r14
leaq 0xdff(%rip), %rdi       # 0x4219
movq %r14, %rsi
movl %ebx, %edx
movl %ebp, %ecx
movsd 0x38(%rsp), %xmm0
movsd 0x70(%rsp), %xmm1
movsd 0x68(%rsp), %xmm2
movsd 0x60(%rsp), %xmm3
movsd 0x58(%rsp), %xmm4
movsd 0x50(%rsp), %xmm5
movl %eax, %r8d
movb $0x6, %al
callq 0x2040
callq 0x2050
movabsq $0x431bde82d7b634db, %rcx # imm = 0x431BDE82D7B634DB
imulq %rcx
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x12, %rdx
addq %rax, %rdx
subq %r14, %rdx
cmpq %rdx, %r12
jle 0x3487
movl %r12d, %eax
subl %edx, %eax
imull $0x3e8, %eax, %edi      # imm = 0x3E8
callq 0x21a0
decl %r15d
jne 0x337a
leaq 0x28(%rsp), %r12
jmp 0x2f9a
movl %ebp, (%r14)
mulsd 0xcd3(%rip), %xmm0      # 0x4178
cvttsd2si %xmm0, %rdi
callq 0x21a0
movq 0x18(%rsp), %rdi
cmpq %r12, %rdi
je 0x2f9a
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x2160
jmp 0x2f9a
movq %r13, %rdi
movl %ebx, %esi
callq 0x26dc
jmp 0x2f9a
callq 0x2262
jmp 0x2f9a
callq 0x2240
jmp 0x2f9a
xorl %ebp, %ebp
cmpl $0x0, 0x40(%rsp)
jns 0x35a6
movl $0xf4240, %edi          # imm = 0xF4240
callq 0x21a0
leaq 0x98(%rsp), %rdi
movl %ebx, %esi
callq 0x2c96
leaq 0xd9f(%rip), %rdi       # 0x42be
movb $0x1, %al
callq 0x2040
leaq 0x98(%rsp), %rdi
movl %ebx, %esi
callq 0x2ca2
leaq 0xd90(%rip), %rdi       # 0x42cc
movb $0x1, %al
callq 0x2040
leaq 0x98(%rsp), %rdi
movl %ebx, %esi
callq 0x2cae
leaq 0xd81(%rip), %rdi       # 0x42da
movb $0x1, %al
callq 0x2040
leaq 0x98(%rsp), %rdi
movl %ebx, %esi
callq 0x2b5a
leaq 0xd72(%rip), %rdi       # 0x42e8
movb $0x1, %al
callq 0x2040
leaq 0x98(%rsp), %rdi
movl %ebx, %esi
callq 0x2b66
leaq 0xd65(%rip), %rdi       # 0x42f8
movb $0x1, %al
callq 0x2040
jmp 0x35a6
callq 0x2284
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
leaq 0x98(%rsp), %rdi
callq 0x24f6
leaq 0x88(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x35d4
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x2160
movl %ebp, %eax
addq $0x2a8, %rsp            # imm = 0x2A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xb59(%rip), %rdi       # 0x4148
callq 0x20d0
leaq 0xb4d(%rip), %rdi       # 0x4148
callq 0x20d0
leaq 0xb41(%rip), %rdi       # 0x4148
callq 0x20d0
leaq 0xb35(%rip), %rdi       # 0x4148
callq 0x20d0
leaq 0xb29(%rip), %rdi       # 0x4148
callq 0x20d0
leaq 0xb1d(%rip), %rdi       # 0x4148
callq 0x20d0
leaq 0xb11(%rip), %rdi       # 0x4148
callq 0x21b0
leaq 0xb05(%rip), %rdi       # 0x4148
callq 0x21b0
leaq 0xaf9(%rip), %rdi       # 0x4148
callq 0x21b0
leaq 0xaed(%rip), %rdi       # 0x4148
callq 0x21b0
leaq 0xae1(%rip), %rdi       # 0x4148
callq 0x21b0
leaq 0xad5(%rip), %rdi       # 0x4148
callq 0x21b0
movq %rax, %rbx
jmp 0x36f5
jmp 0x36e5
jmp 0x3689
jmp 0x36e5
jmp 0x36e5
jmp 0x36e5
jmp 0x36e5
movq %rax, %rbx
jmp 0x36ab
jmp 0x36e5
jmp 0x36e5
jmp 0x369a
jmp 0x36b7
jmp 0x36b7
jmp 0x36b7
movq %rax, %rbx
cmpl $0x0, (%r14)
jne 0x36a6
movl %ebp, (%r14)
leaq 0x28(%rsp), %r12
movq 0x18(%rsp), %rdi
cmpq %r12, %rdi
jne 0x36d2
jmp 0x36e8
movq %rax, %rbx
cmpl $0x0, (%r14)
jne 0x36c3
movl %ebp, (%r14)
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x36e8
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x2160
jmp 0x36e8
jmp 0x36e5
jmp 0x36e5
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
callq 0x24f6
leaq 0x88(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x3716
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x2160
movq %rbx, %rdi
callq 0x21f0