// File: /workspace/github_cpp_makefile_star2_O3/Eba-M[P]E3DC-Control/RscpExampleMain.cpp
// Function: iModbusTCP_Heizstab(int)
int iModbusTCP_Heizstab(int ireq_power) // angeforderte Leistung
{
// Alle 10 sec wird der neue wert an den heizstab geschickt
    static int isocket = -1;
    static bool brequest = false;
    static time_t tlast = 0;
    static int maxpower = -1;
    time_t now;
    time(&now);
    char server_ip[16];
    Modbus_send Msend;
    static int iPower_Heizstab = 0;
        
    if ((now-tlast)>5)
    {
        if ((isocket < 0)&&(strcmp(e3dc_config.heizstab_ip, "0.0.0.0") != 0)&&ireq_power>0&&(now-tlast>10))
            
        {
            isocket = SocketConnect(e3dc_config.heizstab_ip, e3dc_config.heizstab_port);
            if (isocket < 0) // wenn der socket nicht verfügbar, eine Stunde warten
            tlast = now + 60;
        }
        if (isocket > 0)
        {
            //                brequest = true;
            tlast = now;
            send.resize(12);
            receive.resize(15);
            
            if (maxpower <= 0) // ermitteln maximale Leistung
            {
                Msend.Tid = 1*256;
                Msend.Pid = 0;
                Msend.Mlen = 6*256;
                Msend.Dev = 1;
                Msend.Fcd = 3; // Funktioncode auslesen
//                Msend.Fcd = 6; // Funktioncode
                Msend.Reg =  (1014%256)*256 + (1014/256);  // Adresse Register Leistung heizstab
                Msend.Count = 1*256; // Anzahl Register // 22.6° setzen
                memcpy(&send[0],&Msend,send.size());
                SocketSendData(isocket,&send[0],send.size());
                sleep(0.1);
// Auslesen der maximalen Leistung
            
                iLength = SocketRecvData(isocket,&receive[0],receive.size());
                if (iLength > 0)
                {
                    if (receive[7]==3)
                    {
                        maxpower = receive[9]*256+receive[10];
                    }
                }
            }

            
            iPower_Heizstab = iPower_Heizstab + ireq_power;
            if (iPower_Heizstab < 0) iPower_Heizstab = 0;
            if (maxpower < 3000) maxpower = 3000;
            if (iPower_Heizstab > maxpower) iPower_Heizstab = maxpower;
//            if (iPower_Heizstab > 10000) iPower_Heizstab = ireq_power;
            Msend.Tid = 1*256;
            Msend.Pid = 0;
            Msend.Mlen = 6*256;
            Msend.Dev = 1;
            Msend.Fcd = 6; // Funktioncode
            Msend.Reg =  (1000%256)*256 + (1000/256);  // Adresse Register Leistung heizstab
            //            Msend.Count = 1*256; // Anzahl Register // 22.6° setzen
            Msend.Count = (iPower_Heizstab%256)*256+ (iPower_Heizstab/256); // Leistung setzen
            memcpy(&send[0],&Msend,send.size());
            iLength = (SocketSendData(isocket,&send[0],send.size()));
            if (iLength <=0||(iPower_Heizstab==0))
            {
                SocketClose(isocket);
                if (isocket >= 0) isocket = -2;
                iPower_Heizstab = isocket;
            }
        }
        
        else
            iPower_Heizstab  = isocket;
    }
        return iPower_Heizstab;
}

// Assembly:
pushq %r14
pushq %rbx
subq $0xe8, %rsp
movl %edi, %ebx
leaq 0x8(%rsp), %r14
movq %r14, %rdi
callq 0x2250
movq (%r14), %rcx
subq 0x347e3(%rip), %rcx     # 0x3ad90
cmpq $0x5, %rcx
jle 0x66a8
movl 0x32d63(%rip), %eax     # 0x39320
testl %eax, %eax
js 0x66b3
testl %eax, %eax
je 0x6715
movq 0x8(%rsp), %rax
movq %rax, 0x347b7(%rip)     # 0x3ad90
leaq 0x34660(%rip), %rdi     # 0x3ac40
movl $0xc, %esi
callq 0x151b2
leaq 0x34667(%rip), %rdi     # 0x3ac58
movl $0xf, %esi
callq 0x151b2
movl 0x32d23(%rip), %eax     # 0x39324
testl %eax, %eax
jg 0x6722
movabsq $0x301060000000100, %rax # imm = 0x301060000000100
leaq 0x16(%rsp), %rsi
movq %rax, (%rsi)
movl $0x100f603, 0x8(%rsi)   # imm = 0x100F603
movq 0x34617(%rip), %rdi     # 0x3ac40
movq 0x34618(%rip), %rdx     # 0x3ac48
subq %rdi, %rdx
callq 0x21f0
movl 0x32ce2(%rip), %edi     # 0x39320
movq 0x345fb(%rip), %rsi     # 0x3ac40
movl 0x345fd(%rip), %edx     # 0x3ac48
subl %esi, %edx
callq 0x20921
xorl %edi, %edi
callq 0x21b0
movl 0x32cc1(%rip), %edi     # 0x39320
movq 0x345f2(%rip), %rsi     # 0x3ac58
movl 0x345f4(%rip), %edx     # 0x3ac60
subl %esi, %edx
callq 0x2096d
cltq
movq %rax, 0x345a4(%rip)     # 0x3ac20
testl %eax, %eax
jle 0x671c
movq 0x345cd(%rip), %rax     # 0x3ac58
cmpb $0x3, 0x7(%rax)
jne 0x671c
movzwl 0x9(%rax), %eax
rolw $0x8, %ax
movzwl %ax, %eax
movl %eax, 0x32c7e(%rip)     # 0x39324
jmp 0x6722
movl 0x346ea(%rip), %eax     # 0x3ad98
jmp 0x67f7
movabsq $0x302e302e302e30, %rdx # imm = 0x302E302E302E30
cmpq %rdx, 0x33248(%rip)     # 0x3990c
sete %dl
testl %ebx, %ebx
setle %sil
orb %dl, %sil
cmpq $0xb, %rcx
setb %cl
orb %sil, %cl
jne 0x67f1
movl 0x33326(%rip), %esi     # 0x39a0c
leaq 0x3321f(%rip), %rdi     # 0x3990c
callq 0x20617
movl %eax, 0x32c28(%rip)     # 0x39320
testl %eax, %eax
jns 0x65c5
movq 0x8(%rsp), %rcx
addq $0x3c, %rcx
movq %rcx, 0x34680(%rip)     # 0x3ad90
jmp 0x67f1
xorl %eax, %eax
jmp 0x67f1
movl 0x32c02(%rip), %eax     # 0x39324
addl 0x34670(%rip), %ebx     # 0x3ad98
xorl %ecx, %ecx
testl %ebx, %ebx
cmovgl %ebx, %ecx
movl %ecx, 0x34663(%rip)     # 0x3ad98
cmpl $0xbb7, %eax            # imm = 0xBB7
jg 0x674b
movl $0xbb8, 0x32bde(%rip)   # imm = 0xBB8
movl $0xbb8, %eax            # imm = 0xBB8
cmpl %eax, %ebx
jle 0x6757
movl %eax, 0x34643(%rip)     # 0x3ad98
movl %eax, %ecx
movabsq $0x601060000000100, %rax # imm = 0x601060000000100
leaq 0x16(%rsp), %rsi
movq %rax, (%rsi)
movw $0xe803, 0x8(%rsi)      # imm = 0xE803
movl %ecx, %eax
shll $0x8, %eax
shrl $0x8, %ecx
addl %eax, %ecx
movw %cx, 0xa(%rsi)
movq 0x344bc(%rip), %rdi     # 0x3ac40
movq 0x344bd(%rip), %rdx     # 0x3ac48
subq %rdi, %rdx
callq 0x21f0
movl 0x32b87(%rip), %edi     # 0x39320
movq 0x344a0(%rip), %rsi     # 0x3ac40
movl 0x344a2(%rip), %edx     # 0x3ac48
subl %esi, %edx
callq 0x20921
cltq
movq %rax, 0x3446a(%rip)     # 0x3ac20
testl %eax, %eax
setle %cl
movl 0x345d7(%rip), %eax     # 0x3ad98
testl %eax, %eax
sete %dl
orb %cl, %dl
cmpb $0x1, %dl
jne 0x67f7
movl 0x32b4d(%rip), %edi     # 0x39320
callq 0x2090a
movl 0x32b42(%rip), %eax     # 0x39320
testl %eax, %eax
js 0x67f1
movl $0xfffffffe, 0x32b34(%rip) # imm = 0xFFFFFFFE
movl $0xfffffffe, %eax       # imm = 0xFFFFFFFE
movl %eax, 0x345a1(%rip)     # 0x3ad98
addq $0xe8, %rsp
popq %rbx
popq %r14
retq