// File: /workspace/github_cpp_makefile_star2_O1/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 0x354e9(%rip), %rdi     # 0x3ac40
movl $0xc, %esi
callq 0x152b0
leaq 0x354f0(%rip), %rdi     # 0x3ac58
movl $0x7d, %esi
callq 0x152b0
movzbl 0x33b8b(%rip), %eax     # 0x39304
leal 0x1(%rax), %ecx
shll $0x8, %eax
addl %ebp, %eax
leaq 0x6(%rsp), %rsi
movw %ax, (%rsi)
movb %cl, 0x33b75(%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 0x3547b(%rip), %rdi     # 0x3ac40
movq 0x3547c(%rip), %rdx     # 0x3ac48
subq %rdi, %rdx
callq 0x21f0
cmpb $0x1, 0x3464a(%rip)     # 0x39e25
jne 0x57f1
movl 0x35565(%rip), %esi     # 0x3ad48
leaq 0x2b2ff(%rip), %rdi     # 0x30ae9
xorl %eax, %eax
callq 0x2040
leaq 0x2b2f7(%rip), %rdi     # 0x30aef
movl %ebp, %esi
movl %ebx, %edx
xorl %eax, %eax
callq 0x2040
cmpb $0x0, 0x35542(%rip)     # 0x3ad4c
je 0x5818
leaq 0x2b2ee(%rip), %rbx     # 0x30b01
jmp 0x58fa
cmpl $0x0, 0x35529(%rip)     # 0x3ad48
jg 0x587b
leaq 0x35458(%rip), %rdi     # 0x3ac80
leaq 0x340f1(%rip), %rsi     # 0x39920
xorl %eax, %eax
callq 0x2050
cmpb $0x1, 0x345e8(%rip)     # 0x39e25
jne 0x584d
leaq 0x2b2c5(%rip), %rdi     # 0x30b0b
xorl %eax, %eax
callq 0x2040
leaq 0x3542c(%rip), %rdi     # 0x3ac80
movl $0x1f6, %esi            # imm = 0x1F6
callq 0x20786
movl %eax, 0x354e4(%rip)     # 0x3ad48
cmpb $0x1, 0x345ba(%rip)     # 0x39e25
jne 0x587b
leaq 0x2b29b(%rip), %rdi     # 0x30b0f
xorl %eax, %eax
callq 0x2040
movl 0x354c7(%rip), %edi     # 0x3ad48
leaq 0x2b293(%rip), %rbx     # 0x30b1b
testl %edi, %edi
jle 0x58fa
movq 0x353ad(%rip), %rsi     # 0x3ac40
movl 0x353af(%rip), %edx     # 0x3ac48
subl %esi, %edx
callq 0x20a84
cltq
movq %rax, 0x35377(%rip)     # 0x3ac20
cmpb $0x1, 0x34575(%rip)     # 0x39e25
jne 0x58c0
leaq 0x2b25a(%rip), %rdi     # 0x30b13
xorl %eax, %eax
callq 0x2040
movl 0x35482(%rip), %edi     # 0x3ad48
movq 0x3538b(%rip), %rsi     # 0x3ac58
movl 0x3538d(%rip), %edx     # 0x3ac60
subl %esi, %edx
callq 0x20adc
cltq
movq %rax, 0x3533d(%rip)     # 0x3ac20
cmpb $0x1, 0x3453b(%rip)     # 0x39e25
jne 0x58fa
leaq 0x2b224(%rip), %rdi     # 0x30b17
xorl %eax, %eax
callq 0x2040
movq 0x3531f(%rip), %rsi     # 0x3ac20
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2040
movq 0x3530e(%rip), %rax     # 0x3ac20
addq $0xd8, %rsp
popq %rbx
popq %rbp
retq