// File: /workspace/github_cpp_makefile_star2_O2/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 0x32009(%rip), %rcx     # 0x37db0
cmpq $0x5, %rcx
jle 0x5ea1
movl 0x30589(%rip), %eax     # 0x36340
testl %eax, %eax
js 0x5eac
testl %eax, %eax
je 0x5f15
movq 0x8(%rsp), %rax
movq %rax, 0x31fdd(%rip)     # 0x37db0
leaq 0x31e86(%rip), %rdi     # 0x37c60
pushq $0xc
popq %rsi
callq 0x12422
leaq 0x31e8f(%rip), %rdi     # 0x37c78
pushq $0xf
popq %rsi
callq 0x12422
movl 0x3054d(%rip), %eax     # 0x36344
testl %eax, %eax
jg 0x5f22
movabsq $0x301060000000100, %rax # imm = 0x301060000000100
leaq 0x16(%rsp), %rsi
movq %rax, (%rsi)
movl $0x100f603, 0x8(%rsi)   # imm = 0x100F603
movq 0x31e41(%rip), %rdi     # 0x37c60
movq 0x31e42(%rip), %rdx     # 0x37c68
subq %rdi, %rdx
callq 0x21f0
movl 0x3050c(%rip), %edi     # 0x36340
movq 0x31e25(%rip), %rsi     # 0x37c60
movl 0x31e27(%rip), %edx     # 0x37c68
subl %esi, %edx
callq 0x1d70b
xorl %edi, %edi
callq 0x21b0
movl 0x304eb(%rip), %edi     # 0x36340
movq 0x31e1c(%rip), %rsi     # 0x37c78
movl 0x31e1e(%rip), %edx     # 0x37c80
subl %esi, %edx
callq 0x1d758
cltq
movq %rax, 0x31dce(%rip)     # 0x37c40
testl %eax, %eax
jle 0x5f1c
movq 0x31df7(%rip), %rax     # 0x37c78
cmpb $0x3, 0x7(%rax)
jne 0x5f1c
movzwl 0x9(%rax), %eax
rolw $0x8, %ax
movzwl %ax, %eax
movl %eax, 0x304a8(%rip)     # 0x36344
jmp 0x5f22
movl 0x31f11(%rip), %eax     # 0x37db8
jmp 0x5ff5
movabsq $0x302e302e302e30, %rsi # imm = 0x302E302E302E30
xorl %edx, %edx
cmpq %rsi, 0x30a6d(%rip)     # 0x3692c
setne %sil
cmpq $0xb, %rcx
jb 0x5fef
testl %ebx, %ebx
jle 0x5fef
movb %sil, %dl
testl %edx, %edx
je 0x5fef
movl 0x30b46(%rip), %esi     # 0x36a2c
leaq 0x30a3f(%rip), %rdi     # 0x3692c
callq 0x1d43e
movl %eax, 0x30448(%rip)     # 0x36340
testl %eax, %eax
jns 0x5dbf
movq 0x8(%rsp), %rcx
addq $0x3c, %rcx
movq %rcx, 0x31ea0(%rip)     # 0x37db0
jmp 0x5fef
xorl %eax, %eax
jmp 0x5fef
movl 0x30422(%rip), %eax     # 0x36344
addl 0x31e90(%rip), %ebx     # 0x37db8
xorl %ecx, %ecx
testl %ebx, %ebx
cmovgl %ebx, %ecx
movl %ecx, 0x31e83(%rip)     # 0x37db8
cmpl $0xbb7, %eax            # imm = 0xBB7
jg 0x5f4b
movl $0xbb8, 0x303fe(%rip)   # imm = 0xBB8
movl $0xbb8, %eax            # imm = 0xBB8
cmpl %eax, %ebx
jle 0x5f57
movl %eax, 0x31e63(%rip)     # 0x37db8
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 0x31cdc(%rip), %rdi     # 0x37c60
movq 0x31cdd(%rip), %rdx     # 0x37c68
subq %rdi, %rdx
callq 0x21f0
movl 0x303a7(%rip), %edi     # 0x36340
movq 0x31cc0(%rip), %rsi     # 0x37c60
movl 0x31cc2(%rip), %edx     # 0x37c68
subl %esi, %edx
callq 0x1d70b
cltq
movq %rax, 0x31c8a(%rip)     # 0x37c40
testl %eax, %eax
setle %cl
movl 0x31df7(%rip), %eax     # 0x37db8
testl %eax, %eax
sete %dl
orb %cl, %dl
cmpb $0x1, %dl
jne 0x5ff5
movl 0x3036d(%rip), %edi     # 0x36340
callq 0x1d6f4
movl 0x30362(%rip), %eax     # 0x36340
testl %eax, %eax
js 0x5fef
movl $0xfffffffe, 0x30354(%rip) # imm = 0xFFFFFFFE
pushq $-0x2
popq %rax
movl %eax, 0x31dc3(%rip)     # 0x37db8
addq $0xe8, %rsp
popq %rbx
popq %r14
retq