// File: /workspace/github_cpp_makefile_star2_O3/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 0x35504(%rip), %rdi     # 0x3ac40
movl $0xc, %esi
callq 0x151b2
leaq 0x3550b(%rip), %rdi     # 0x3ac58
movl $0x7d, %esi
callq 0x151b2
movzbl 0x33ba6(%rip), %eax     # 0x39304
leal 0x1(%rax), %ecx
shll $0x8, %eax
addl %ebp, %eax
leaq 0x6(%rsp), %rsi
movw %ax, (%rsi)
movb %cl, 0x33b90(%rip)      # 0x39304
movl $0x6000000, 0x2(%rsi)   # imm = 0x6000000
movw $0x601, 0x6(%rsi)       # imm = 0x601
movl %ebp, %eax
shll $0x8, %eax
movw %ax, 0x8(%rsi)
movl %ebx, %eax
shll $0x8, %eax
leal 0xff(%rbx), %ecx
testl %ebx, %ebx
cmovnsl %ebx, %ecx
shrl $0x8, %ecx
addl %eax, %ecx
movw %cx, 0xa(%rsi)
movq 0x35496(%rip), %rdi     # 0x3ac40
movq 0x35497(%rip), %rdx     # 0x3ac48
subq %rdi, %rdx
callq 0x21f0
cmpb $0x1, 0x34665(%rip)     # 0x39e25
jne 0x57d6
movl 0x35580(%rip), %esi     # 0x3ad48
leaq 0x2b31e(%rip), %rdi     # 0x30aed
xorl %eax, %eax
callq 0x2040
leaq 0x2b316(%rip), %rdi     # 0x30af3
movl %ebp, %esi
movl %ebx, %edx
xorl %eax, %eax
callq 0x2040
cmpb $0x0, 0x3555d(%rip)     # 0x3ad4c
je 0x57fd
leaq 0x2b30d(%rip), %rbx     # 0x30b05
jmp 0x58eb
movl 0x35545(%rip), %edi     # 0x3ad48
testl %edi, %edi
jg 0x586d
leaq 0x35472(%rip), %rdi     # 0x3ac80
leaq 0x3410b(%rip), %rsi     # 0x39920
xorl %eax, %eax
callq 0x2050
cmpb $0x1, 0x34602(%rip)     # 0x39e25
jne 0x5833
leaq 0x2b2e3(%rip), %rdi     # 0x30b0f
xorl %eax, %eax
callq 0x2040
leaq 0x35446(%rip), %rdi     # 0x3ac80
movl $0x1f6, %esi            # imm = 0x1F6
callq 0x20617
movl %eax, %edi
movl %eax, 0x354fc(%rip)     # 0x3ad48
cmpb $0x1, 0x345d2(%rip)     # 0x39e25
jne 0x5869
leaq 0x2b2b7(%rip), %rdi     # 0x30b13
xorl %eax, %eax
callq 0x2040
movl 0x354df(%rip), %edi     # 0x3ad48
testl %edi, %edi
jle 0x58e4
movq 0x353cc(%rip), %rsi     # 0x3ac40
movl 0x353ce(%rip), %edx     # 0x3ac48
subl %esi, %edx
callq 0x20921
cltq
movq %rax, 0x35396(%rip)     # 0x3ac20
cmpb $0x1, 0x34594(%rip)     # 0x39e25
jne 0x58a1
leaq 0x2b27d(%rip), %rdi     # 0x30b17
xorl %eax, %eax
callq 0x2040
movl 0x354a1(%rip), %edi     # 0x3ad48
movq 0x353aa(%rip), %rsi     # 0x3ac58
movl 0x353ac(%rip), %edx     # 0x3ac60
subl %esi, %edx
callq 0x2096d
cltq
movq %rax, 0x3535c(%rip)     # 0x3ac20
leaq 0x2b254(%rip), %rbx     # 0x30b1f
cmpb $0x1, 0x34553(%rip)     # 0x39e25
jne 0x58eb
leaq 0x2b240(%rip), %rdi     # 0x30b1b
xorl %eax, %eax
callq 0x2040
jmp 0x58eb
leaq 0x2b234(%rip), %rbx     # 0x30b1f
movq 0x3532e(%rip), %rsi     # 0x3ac20
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2040
movq 0x3531d(%rip), %rax     # 0x3ac20
addq $0xd8, %rsp
popq %rbx
popq %rbp
retq