// File: /workspace/github_c_makefile_star2_O0/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
subq $0xe0, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x3, -0x8(%rbp)
je 0x25aa
movq -0x10(%rbp), %rax
movq (%rax), %rax
movq %rax, -0xd8(%rbp)
leaq -0x31(%rbp), %rdi
movq %rdi, -0xd0(%rbp)
callq 0x2290
movq -0xd8(%rbp), %rsi
movq -0xd0(%rbp), %rdx
leaq -0x30(%rbp), %rdi
callq 0x27e0
jmp 0x255a
leaq -0x30(%rbp), %rdi
callq 0x2440
jmp 0x2565
leaq -0x30(%rbp), %rdi
callq 0x2100
leaq -0x31(%rbp), %rdi
callq 0x21e0
jmp 0x25aa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
jmp 0x259c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x40(%rbp)
movl %eax, -0x44(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x2100
leaq -0x31(%rbp), %rdi
callq 0x21e0
jmp 0x27d6
leaq -0x4a(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
callq 0x2d60
leaq -0x50(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
callq 0x2f00
movl $0x0, -0x54(%rbp)
cmpl $0x6, -0x54(%rbp)
jge 0x2608
movslq -0x54(%rbp), %rax
movq 0x29f4(%rip), %rdi      # 0x4fd8
movzbl -0x50(%rbp,%rax), %esi
callq 0x2250
movq %rax, %rdi
leaq 0xa4a(%rip), %rsi       # 0x3042
callq 0x21a0
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x54(%rbp)
jmp 0x25d3
leaq -0x60(%rbp), %rdi
movl $0xd, %esi
xorl %edx, %edx
movl $0x1, %ecx
callq 0x2c60
leaq -0x4a(%rbp), %rsi
leaq -0x50(%rbp), %rdx
leaq -0x68(%rbp), %rdi
movl $0x88a4, %ecx           # imm = 0x88A4
callq 0x2b80
leaq -0x78(%rbp), %rdi
movl $0x5ee, %esi            # imm = 0x5EE
callq 0x2890
leaq -0x68(%rbp), %rdi
callq 0x2c00
movq %rax, %rsi
leaq 0xa9c(%rip), %rax       # 0x30f0
movl (%rax), %edx
leaq -0x78(%rbp), %rdi
callq 0x2f80
leaq -0x60(%rbp), %rdi
callq 0x2d10
movq %rax, %rsi
leaq 0xa82(%rip), %rax       # 0x30f4
movl (%rax), %edx
leaq -0x78(%rbp), %rdi
callq 0x2f80
movl $0x11, %edi
movl $0x3, %esi
movl $0xff, %edx
callq 0x2090
movl %eax, -0x7c(%rbp)
cmpl $-0x1, -0x7c(%rbp)
jne 0x26ad
movq 0x2937(%rip), %rdi      # 0x4fd8
leaq 0x9b5(%rip), %rsi       # 0x305d
callq 0x21a0
leaq -0xa8(%rbp), %rdi
xorl %esi, %esi
movl $0x28, %edx
callq 0x20f0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x20d0
movl %eax, -0xac(%rbp)
movl -0xac(%rbp), %esi
leaq 0x9e0(%rip), %rdi       # 0x30c0
movb $0x0, %al
callq 0x2050
leaq -0xa8(%rbp), %rdi
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
movslq -0xac(%rbp), %rdx
callq 0x20e0
movl -0x7c(%rbp), %edi
movl $0x8933, %esi           # imm = 0x8933
leaq -0xa8(%rbp), %rdx
movb $0x0, %al
callq 0x2230
cmpl $0x0, %eax
jge 0x2752
leaq 0x94a(%rip), %rdi       # 0x306e
movb $0x0, %al
callq 0x2050
callq 0x2040
movl (%rax), %edi
callq 0x20b0
movq %rax, %rsi
leaq 0x94b(%rip), %rdi       # 0x308c
movb $0x0, %al
callq 0x2050
movl $0x1, %edi
callq 0x2210
movl -0x98(%rbp), %eax
movl %eax, -0xbc(%rbp)
movb $0x6, -0xb5(%rbp)
movl -0x7c(%rbp), %edi
movq -0x78(%rbp), %rsi
movslq -0x6c(%rbp), %rdx
xorl %ecx, %ecx
leaq -0xc0(%rbp), %r8
movl $0x14, %r9d
callq 0x2070
movl %eax, -0xc4(%rbp)
cmpl $0x0, -0xc4(%rbp)
jge 0x27c3
movl -0xc4(%rbp), %eax
movl %eax, -0xdc(%rbp)
callq 0x2040
movl -0xdc(%rbp), %esi
movl (%rax), %edx
leaq 0x8e5(%rip), %rdi       # 0x3098
movb $0x0, %al
callq 0x2050
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x27ca
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xe0, %rsp
popq %rbp
retq
movq -0x40(%rbp), %rdi
callq 0x2280
nop