// File: /workspace/github_cpp_makefile_star2_O2/Eba-M[P]E3DC-Control/RscpExampleMain.cpp
// Function: iModbusTCP_Set(int, int, int)
long iModbusTCP_Set(int reg,int val,int tac)
{
    send.resize(12);
    receive.resize(125);

    Modbus_send Msend;
    Msend.Tid = (tn*256+reg);
    tn++;
    Msend.Pid = 0;
    Msend.Mlen = 6*256;
    Msend.Dev = 1;  // Devadr. 1 oder 255
    Msend.Fcd = 6; // Funktioncode
    //            Msend.Fcd = 6; // Funktioncode
    Msend.Reg = reg*256;  // Adresse Register // Aussentemperatur
    Msend.Count = (val%256)*256; // Inhalt    int size = send.size();
    Msend.Count = Msend.Count + val/256; // Inhalt    int size = send.size();
    memcpy(&send[0],&Msend,send.size());
    if (e3dc_config.debug)
        printf("BSE%i",isocket);
    printf("Modbus Set %i %i ",reg, val);

/*
    if (isocket > 0)
        iLength = SocketRecvData(isocket,&receive[0],receive.size());
    if (iLength < 0)
    {
        SocketClose(isocket);
        isocket = -1;
    }
*/
    if (brequest)
    {
        printf(" len %li ",iLength);
        return iLength;
    }
    if (isocket <= 0)
        {
            sprintf(server_ip,e3dc_config.heizung_ip);
            if (e3dc_config.debug)
                printf("BSC");

            isocket = SocketConnect(server_ip, 502);
            if (e3dc_config.debug)
                printf("ASC");

        }

    if (isocket > 0)
    {
        iLength = SocketSendData(isocket,&send[0],send.size());
        if (e3dc_config.debug)
            printf("BRC");
        
        iLength = SocketRecvData(isocket,&receive[0],receive.size());
        if (e3dc_config.debug)
            printf("ARC");
    }
    printf(" len %i ",iLength);
    return iLength;
}

// Assembly:
pushq %rbp
pushq %rbx
subq $0xd8, %rsp
movl %esi, %ebx
movl %edi, %ebp
leaq 0x32cba(%rip), %rdi     # 0x37c60
pushq $0xc
popq %rsi
callq 0x12422
leaq 0x32cc3(%rip), %rdi     # 0x37c78
pushq $0x7d
popq %rsi
callq 0x12422
movzbl 0x31360(%rip), %eax     # 0x36324
leal 0x1(%rax), %ecx
shll $0x8, %eax
addl %ebp, %eax
leaq 0x6(%rsp), %rsi
movw %ax, (%rsi)
movb %cl, 0x3134a(%rip)      # 0x36324
movl $0x6000000, 0x2(%rsi)   # imm = 0x6000000
movw $0x601, 0x6(%rsi)       # imm = 0x601
movl %ebp, %eax
shll $0x8, %eax
movw %ax, 0x8(%rsi)
movl %ebx, %ecx
shll $0x8, %ecx
movl $0x100, %edi            # imm = 0x100
movl %ebx, %eax
cltd
idivl %edi
addl %ecx, %eax
movw %ax, 0xa(%rsi)
movq 0x32c54(%rip), %rdi     # 0x37c60
movq 0x32c55(%rip), %rdx     # 0x37c68
subq %rdi, %rdx
callq 0x21f0
cmpb $0x1, 0x31e23(%rip)     # 0x36e45
jne 0x5038
movl 0x32d3e(%rip), %esi     # 0x37d68
leaq 0x269ec(%rip), %rdi     # 0x2ba1d
xorl %eax, %eax
callq 0x2040
leaq 0x269e4(%rip), %rdi     # 0x2ba23
movl %ebp, %esi
movl %ebx, %edx
xorl %eax, %eax
callq 0x2040
cmpb $0x0, 0x32d1b(%rip)     # 0x37d6c
je 0x505f
leaq 0x269db(%rip), %rbx     # 0x2ba35
jmp 0x514d
movl 0x32d03(%rip), %edi     # 0x37d68
testl %edi, %edi
jg 0x50cf
leaq 0x32c30(%rip), %rdi     # 0x37ca0
leaq 0x318c9(%rip), %rsi     # 0x36940
xorl %eax, %eax
callq 0x2050
cmpb $0x1, 0x31dc0(%rip)     # 0x36e45
jne 0x5095
leaq 0x269b1(%rip), %rdi     # 0x2ba3f
xorl %eax, %eax
callq 0x2040
leaq 0x32c04(%rip), %rdi     # 0x37ca0
movl $0x1f6, %esi            # imm = 0x1F6
callq 0x1d43e
movl %eax, %edi
movl %eax, 0x32cba(%rip)     # 0x37d68
cmpb $0x1, 0x31d90(%rip)     # 0x36e45
jne 0x50cb
leaq 0x26985(%rip), %rdi     # 0x2ba43
xorl %eax, %eax
callq 0x2040
movl 0x32c9d(%rip), %edi     # 0x37d68
testl %edi, %edi
jle 0x5146
movq 0x32b8a(%rip), %rsi     # 0x37c60
movl 0x32b8c(%rip), %edx     # 0x37c68
subl %esi, %edx
callq 0x1d70b
cltq
movq %rax, 0x32b54(%rip)     # 0x37c40
cmpb $0x1, 0x31d52(%rip)     # 0x36e45
jne 0x5103
leaq 0x2694b(%rip), %rdi     # 0x2ba47
xorl %eax, %eax
callq 0x2040
movl 0x32c5f(%rip), %edi     # 0x37d68
movq 0x32b68(%rip), %rsi     # 0x37c78
movl 0x32b6a(%rip), %edx     # 0x37c80
subl %esi, %edx
callq 0x1d758
cltq
movq %rax, 0x32b1a(%rip)     # 0x37c40
leaq 0x26922(%rip), %rbx     # 0x2ba4f
cmpb $0x1, 0x31d11(%rip)     # 0x36e45
jne 0x514d
leaq 0x2690e(%rip), %rdi     # 0x2ba4b
xorl %eax, %eax
callq 0x2040
jmp 0x514d
leaq 0x26902(%rip), %rbx     # 0x2ba4f
movq 0x32aec(%rip), %rsi     # 0x37c40
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2040
movq 0x32adb(%rip), %rax     # 0x37c40
addq $0xd8, %rsp
popq %rbx
popq %rbp
retq