// File: /workspace/github_c_makefile_star2_O1/zetwhite[P]SimpleEtherCATMaster/main.cpp
// Function: main
int main(int argc, char* argv[]){
    if(argc != 3){
        showUsage(argv[0]); 
    }

    uint8_t myMac[6]; 
    getMyMac(myMac, argv[1]); 

    uint8_t destMac[6]; 
    transformMac(destMac, argv[2]); 
    for(int i = 0; i <6; i++){
        cout << destMac[i] << " "; 
    }

    EthercatHelper ecathdr(0xd, 0, 1);
    EthernetHelper enethdr(myMac, destMac, 0x88a4); 

    Packet p(1518); 
    p.addFrame(enethdr.toArray(), enethdr.size);  
    p.addFrame(ecathdr.toArray(), ecathdr.size);    



    // ==== create socket and send ! 
    int sock_raw = socket(AF_PACKET, SOCK_RAW, IPPROTO_RAW); 
    if(sock_raw == -1)
        cout <<"error on socket\n"; 
    
    struct ifreq ifreq_i;
    memset(&ifreq_i,0,sizeof(ifreq_i));
    int interface_len =  strlen(argv[1]); 
    printf("%d\n", interface_len); 
    strncpy(ifreq_i.ifr_name,argv[1],interface_len); //giving name of Interface
    
    if((ioctl(sock_raw,SIOCGIFINDEX,&ifreq_i))<0){
        printf("error in index ioctl reading\n");//getting Index Name
        printf("error : %s\n", strerror(errno)); 
        exit(EXIT_FAILURE);
    }

    struct sockaddr_ll sadr_ll;
    sadr_ll.sll_ifindex = ifreq_i.ifr_ifindex; // index of interface
    sadr_ll.sll_halen = ETH_ALEN; // length of destination mac address
    //memcpy(sadr_ll.sll_addr, myMac, 6); 
    int send_len = sendto(sock_raw,p.buff,p.length, 0,(const struct sockaddr*)&sadr_ll,sizeof(struct sockaddr_ll));
    if(send_len<0)
    {
        printf("error in sending....sendlen=%d....errno=%d\n",send_len,errno);
        return -1;
    }


    return 0; 
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rsi, %rbx
cmpl $0x3, %edi
je 0x14ba
movq (%rbx), %rsi
leaq -0x68(%rbp), %r14
leaq -0x90(%rbp), %rdx
movq %r14, %rdi
callq 0x170a
movq %r14, %rdi
callq 0x134c
leaq -0x58(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x14ba
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x1150
movq 0x8(%rbx), %rsi
leaq -0x34(%rbp), %rdi
callq 0x18c0
movq 0x10(%rbx), %rsi
leaq -0x2e(%rbp), %rdi
callq 0x19df
xorl %r13d, %r13d
movq 0x2afa(%rip), %r14      # 0x3fd8
leaq -0x90(%rbp), %r15
leaq 0xb56(%rip), %r12       # 0x2042
movb -0x2e(%rbp,%r13), %al
movb %al, -0x90(%rbp)
movl $0x1, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x1160
movl $0x1, %edx
movq %rax, %rdi
movq %r12, %rsi
callq 0x1160
incq %r13
cmpq $0x6, %r13
jne 0x14ec
leaq -0xa0(%rbp), %r14
movq %r14, %rdi
movl $0xd, %esi
xorl %edx, %edx
movl $0x1, %ecx
callq 0x1854
leaq -0x98(%rbp), %r12
leaq -0x34(%rbp), %rsi
leaq -0x2e(%rbp), %rdx
movq %r12, %rdi
movl $0x88a4, %ecx           # imm = 0x88A4
callq 0x17e4
leaq -0x48(%rbp), %r15
movl $0x5ee, 0x8(%r15)       # imm = 0x5EE
movl $0x5ee, %edi            # imm = 0x5EE
callq 0x1030
movq %rax, (%r15)
movl $0x5ee, %edx            # imm = 0x5EE
movq %rax, %rdi
xorl %esi, %esi
callq 0x10c0
movq %r12, %rdi
callq 0x1836
leaq 0xb62(%rip), %rcx       # 0x20f0
movl (%rcx), %edx
movq %r15, %rdi
movq %rax, %rsi
callq 0x1a9c
movq %r14, %rdi
callq 0x1898
leaq 0xb4a(%rip), %rcx       # 0x20f4
movl (%rcx), %edx
movq %r15, %rdi
movq %rax, %rsi
callq 0x1a9c
movl $0x11, %edi
movl $0x3, %esi
movl $0xff, %edx
callq 0x1080
movl %eax, %r14d
cmpl $-0x1, %eax
jne 0x15eb
movq 0x29fe(%rip), %rdi      # 0x3fd8
leaq 0xa7c(%rip), %rsi       # 0x205d
movl $0x10, %edx
callq 0x1160
xorps %xmm0, %xmm0
leaq -0x90(%rbp), %r12
movaps %xmm0, 0x10(%r12)
movaps %xmm0, (%r12)
movq $0x0, 0x20(%r12)
movq 0x8(%rbx), %rdi
callq 0x10a0
movq %rax, %r13
leaq 0xa86(%rip), %rdi       # 0x20a2
xorl %r15d, %r15d
movl %r13d, %esi
xorl %eax, %eax
callq 0x1060
movq 0x8(%rbx), %rsi
movslq %r13d, %rdx
movq %r12, %rdi
callq 0x10b0
movl $0x8933, %esi           # imm = 0x8933
movl %r14d, %edi
movq %r12, %rdx
xorl %eax, %eax
callq 0x1180
testl %eax, %eax
js 0x16b2
movl -0x80(%rbp), %eax
leaq -0xb4(%rbp), %r8
movl %eax, 0x4(%r8)
movb $0x6, 0xb(%r8)
movq -0x48(%rbp), %rsi
movslq -0x3c(%rbp), %rdx
movl %r14d, %edi
xorl %ecx, %ecx
movl $0x14, %r9d
callq 0x1070
movq %rax, %rbx
testl %ebx, %ebx
jns 0x169d
callq 0x1050
movl (%rax), %edx
leaq 0x9ec(%rip), %rdi       # 0x207a
movl %ebx, %esi
xorl %eax, %eax
callq 0x1060
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
movl %r15d, %eax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa17(%rip), %rdi       # 0x20d0
callq 0x11a0
callq 0x1050
movl (%rax), %edi
callq 0x1090
leaq 0x99d(%rip), %rdi       # 0x206e
movq %rax, %rsi
xorl %eax, %eax
callq 0x1060
movl $0x1, %edi
callq 0x1170
movq %rax, %rbx
leaq -0x58(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1701
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x1150
movq %rbx, %rdi
callq 0x11c0
nop