// File: 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:
subq $0x118, %rsp            # imm = 0x118
movl %edi, 0x10c(%rsp)
movl %esi, 0x108(%rsp)
movl %edx, 0x104(%rsp)
leaq 0x6a19d(%rip), %rdi     # 0x6fc80
movl $0xc, %esi
callq 0x1cb60
leaq 0x6a1a4(%rip), %rdi     # 0x6fc98
movl $0x7d, %esi
callq 0x1cb60
movzbl 0x6881f(%rip), %eax     # 0x6e324
shll $0x8, %eax
addl 0x10c(%rsp), %eax
movw %ax, 0x32(%rsp)
movb 0x6880a(%rip), %al      # 0x6e324
addb $0x1, %al
movb %al, 0x68802(%rip)      # 0x6e324
movw $0x0, 0x34(%rsp)
movw $0x600, 0x36(%rsp)      # imm = 0x600
movb $0x1, 0x38(%rsp)
movb $0x6, 0x39(%rsp)
movl 0x10c(%rsp), %eax
shll $0x8, %eax
movw %ax, 0x3a(%rsp)
movl 0x108(%rsp), %eax
movl $0x100, %ecx            # imm = 0x100
cltd
idivl %ecx
shll $0x8, %edx
movw %dx, %ax
movw %ax, 0x3c(%rsp)
movzwl 0x3c(%rsp), %eax
movl %eax, 0x24(%rsp)
movl 0x108(%rsp), %eax
movl $0x100, %ecx            # imm = 0x100
cltd
idivl %ecx
movl %eax, %ecx
movl 0x24(%rsp), %eax
addl %ecx, %eax
movw %ax, 0x3c(%rsp)
leaq 0x6a0f1(%rip), %rdi     # 0x6fc80
xorl %eax, %eax
movl %eax, %esi
callq 0x1cc00
movq %rax, 0x28(%rsp)
leaq 0x6a0dc(%rip), %rdi     # 0x6fc80
callq 0x1cc20
movq 0x28(%rsp), %rdi
movq %rax, %rdx
leaq 0x32(%rsp), %rsi
callq 0x2220
testb $0x1, 0x6929f(%rip)     # 0x6ee61
je 0x5bd8
movl 0x6a1be(%rip), %esi     # 0x6fd88
leaq 0x5acb9(%rip), %rdi     # 0x6088a
movb $0x0, %al
callq 0x2040
movl 0x10c(%rsp), %esi
movl 0x108(%rsp), %edx
leaq 0x5aca3(%rip), %rdi     # 0x60890
movb $0x0, %al
callq 0x2040
testb $0x1, 0x6a191(%rip)     # 0x6fd8c
je 0x5c26
movq 0x6a05c(%rip), %rsi     # 0x6fc60
leaq 0x5ac97(%rip), %rdi     # 0x608a2
movb $0x0, %al
callq 0x2040
movq 0x6a047(%rip), %rax     # 0x6fc60
movq %rax, 0x110(%rsp)
jmp 0x5d78
cmpl $0x0, 0x6a15b(%rip)     # 0x6fd88
jg 0x5c8f
leaq 0x6a08a(%rip), %rdi     # 0x6fcc0
leaq 0x68cf7(%rip), %rsi     # 0x6e934
addq $0x28, %rsi
movb $0x0, %al
callq 0x2060
testb $0x1, 0x69212(%rip)     # 0x6ee61
je 0x5c5f
leaq 0x5ac54(%rip), %rdi     # 0x608ac
movb $0x0, %al
callq 0x2040
leaq 0x6a05a(%rip), %rdi     # 0x6fcc0
movl $0x1f6, %esi            # imm = 0x1F6
callq 0x360d0
movl %eax, 0x6a112(%rip)     # 0x6fd88
testb $0x1, 0x691e4(%rip)     # 0x6ee61
je 0x5c8d
leaq 0x5ac2a(%rip), %rdi     # 0x608b0
movb $0x0, %al
callq 0x2040
jmp 0x5c8f
cmpl $0x0, 0x6a0f2(%rip)     # 0x6fd88
jle 0x5d54
movl 0x6a0e6(%rip), %eax     # 0x6fd88
movl %eax, 0x14(%rsp)
leaq 0x69fd3(%rip), %rdi     # 0x6fc80
xorl %eax, %eax
movl %eax, %esi
callq 0x1cc00
movq %rax, 0x18(%rsp)
leaq 0x69fbe(%rip), %rdi     # 0x6fc80
callq 0x1cc20
movl 0x14(%rsp), %edi
movq 0x18(%rsp), %rsi
movl %eax, %edx
callq 0x364e0
cltq
movq %rax, 0x69f80(%rip)     # 0x6fc60
testb $0x1, 0x6917a(%rip)     # 0x6ee61
je 0x5cf7
leaq 0x5abc4(%rip), %rdi     # 0x608b4
movb $0x0, %al
callq 0x2040
movl 0x6a08b(%rip), %eax     # 0x6fd88
movl %eax, 0x4(%rsp)
leaq 0x69f90(%rip), %rdi     # 0x6fc98
xorl %eax, %eax
movl %eax, %esi
callq 0x1cc00
movq %rax, 0x8(%rsp)
leaq 0x69f7b(%rip), %rdi     # 0x6fc98
callq 0x1cc20
movl 0x4(%rsp), %edi
movq 0x8(%rsp), %rsi
movl %eax, %edx
callq 0x36590
cltq
movq %rax, 0x69f25(%rip)     # 0x6fc60
testb $0x1, 0x6911f(%rip)     # 0x6ee61
je 0x5d52
leaq 0x5ab6d(%rip), %rdi     # 0x608b8
movb $0x0, %al
callq 0x2040
jmp 0x5d54
movq 0x69f05(%rip), %rsi     # 0x6fc60
leaq 0x5ab5a(%rip), %rdi     # 0x608bc
movb $0x0, %al
callq 0x2040
movq 0x69ef0(%rip), %rax     # 0x6fc60
movq %rax, 0x110(%rsp)
movq 0x110(%rsp), %rax
addq $0x118, %rsp            # imm = 0x118
retq
nopl (%rax,%rax)