// File: yueww[P]TSR/processFlows.c
// Function: outputFlows(void*)
void *outputFlows(void *ptr){
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
    /*
    FILE *fp=fopen("./data","wb");
    if(fp==NULL){
        printf("fail to create file\n");
        pthread_exit(0);
    }
    */
    /*
    int fd;
    fd=open("./data",O_RDWR | O_CREAT | O_APPEND,S_IRUSR | S_IWUSR | S_IROTH | S_IWOTH | S_IRGRP | S_IWGRP);
    if(fd==-1){
        printf("fail to create file\n");
        pthread_exit(0);
    }
    
    
    int writeNum=0;
    while(1){
        pthread_mutex_lock(&mutex);
        while(n<FLOWSPERPROCESS) pthread_cond_wait(&cond_c,&mutex);
        if(out==BLOCKSIZE) out=0;
        writeNum=write(fd,flowBlock+out,FLOWSPERPROCESS*FLOWSIZE);
        if(writeNum==-1){
            printf("write error!\n");
            exit(0);
        }
        out+=FLOWSPERPROCESS;
        n-=FLOWSPERPROCESS;
        if(n<BLOCKSIZE) pthread_cond_signal(&cond_p);
        pthread_mutex_unlock(&mutex);
    }
    */
    //printf("file descriptor is %d\n",fd);
    //int writeNum;
    //pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS,NULL);
    
    while(1){
        pthread_mutex_lock(&mutex);
        while(n<FLOWSPERPROCESS) pthread_cond_wait(&cond_c,&mutex);
        if(out==BLOCKSIZE) out=0;
        memcpy(flows,flowBlock+out,FLOWSPERPROCESS*FLOWSIZE);
        out+=FLOWSPERPROCESS;
        n-=FLOWSPERPROCESS;
        resort();
        byte exportFlag=index();
        if(exportFlag){
            pid_t pid;
            if((pid=fork())<0){
                printf("fail to create export process,program will exit!\n");
                exit(0);
            }else if(pid==0){
                *(int *)shmptr=1;
                int *exportRes=exportDataAndIndex();
                FILE *fp=fopen("./exportRecord.log","a+");
                int writeRes[6];
                writeRes[0]=fprintf(fp,"export %d flows.\n",exportRes[0]/48);
                writeRes[1]=fprintf(fp,"data take up %d bytes\n",exportRes[0]);
                writeRes[2]=fprintf(fp,"srcIp take up %d bytes\n",exportRes[1]);
                writeRes[3]=fprintf(fp,"dstIp take up %d bytes\n",exportRes[2]);
                writeRes[4]=fprintf(fp,"srcPort take up %d bytes\n",exportRes[3]);
                writeRes[5]=fprintf(fp,"dstPort take up %d bytes\n",exportRes[4]);
                if(writeRes[0]<0 || writeRes[1]<0 || writeRes[2]<0 || writeRes[3]<0 ||writeRes[4]<0 ||writeRes[5]<0){
                    printf("error in write log!\n");
                } 
                free(exportRes);
                fclose(fp);
                *(int *)shmptr=0;
                exit(0);
            }else{
                memcpy(flowWaitExport,flowWaitExport+outBase,(inBase-outBase)*FLOWSIZE);
                int i;
                for(i=0;i<outBase;i++){
                    srcIpIndexNodeBuf[i].next=NULL;
                    dstIpIndexNodeBuf[i].next=NULL;
                    srcPortIndexNodeBuf[i].next=NULL;
                    dstPortIndexNodeBuf[i].next=NULL;
                }
                memset(srcIpInnerNodeBuf,0,sizeof(innerNode)*currUsedSrcIpInnerNodes);
                memset(dstIpInnerNodeBuf,0,sizeof(innerNode)*currUsedDstIpInnerNodes);
                memset(srcIpLeafNodeBuf,0,sizeof(leafNode)*currUsedSrcIpLeafNodes);
                memset(dstIpLeafNodeBuf,0,sizeof(leafNode)*currUsedDstIpLeafNodes);
                memset(&srcPortIndexTable,0,sizeof(portIndexTable));
                memset(&dstPortIndexTable,0,sizeof(portIndexTable));
                srcIpRootNode=&srcIpInnerNodeBuf[0];
                dstIpRootNode=&dstIpInnerNodeBuf[0];
                inBase-=outBase;
                outBase=0;
                currUsedSrcIpInnerNodes=1;
                currUsedDstIpInnerNodes=1;
                currUsedSrcIpLeafNodes=0;
                currUsedDstIpLeafNodes=0;
                currUsedSrcIpIndexNodes=0;
                currUsedDstIpIndexNodes=0;
                currUsedSrcPortIndexNodes=0;
                currUsedDstPortIndexNodes=0;
            }
        }
        
        /*
        if((writeNum=write(fd,flows,FLOWSIZE*FLOWSPERPROCESS))!=FLOWSIZE*FLOWSPERPROCESS){
            printf("only write %d bytes,write error!\n",writeNum);
            pthread_mutex_unlock(&mutex);
            pthread_exit(0);
        }
        printf("write 40 flows to file\n");
        */
    
        if(n<BLOCKSIZE) pthread_cond_signal(&cond_p);
        pthread_mutex_unlock(&mutex);
    }
    
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x8(%rbp)
movl $0x1, %edi
xorl %eax, %eax
movl %eax, %esi
callq 0x20d0
leaq 0x6a8f(%rip), %rdi      # 0xf2e0
callq 0x2440
leaq 0x6a77(%rip), %rax      # 0xf2d4
cmpl $0xfa0, (%rax)          # imm = 0xFA0
jge 0x887a
leaq 0x6ab0d7cc(%rip), %rdi  # 0x6ab16038
leaq 0x6a6d(%rip), %rsi      # 0xf2e0
callq 0x2480
jmp 0x8856
cmpl $0x61a80, 0x6ab0d7ac(%rip) # imm = 0x61A80
jne 0x8890
movl $0x0, 0x6ab0d7a0(%rip)  # 0x6ab16030
movl 0x6ab0d79a(%rip), %eax  # 0x6ab16030
leaq 0x16ae3(%rip), %rsi     # 0x1f380
imulq $0x30, %rax, %rax
addq %rax, %rsi
leaq 0x6ab0d7c5(%rip), %rdi  # 0x6ab16070
movl $0x2ee00, %edx          # imm = 0x2EE00
callq 0x2260
movl 0x6ab0d775(%rip), %eax  # 0x6ab16030
addl $0xfa0, %eax            # imm = 0xFA0
movl %eax, 0x6ab0d76a(%rip)  # 0x6ab16030
leaq 0x6a07(%rip), %rax      # 0xf2d4
movl (%rax), %ecx
subl $0xfa0, %ecx            # imm = 0xFA0
leaq 0x69f8(%rip), %rax      # 0xf2d4
movl %ecx, (%rax)
callq 0x50e0
callq 0x5670
movb %al, -0x9(%rbp)
cmpb $0x0, -0x9(%rbp)
je 0x8c71
callq 0x20b0
movl %eax, -0x10(%rbp)
cmpl $0x0, %eax
jge 0x8917
leaq 0x1b04(%rip), %rdi      # 0xa40d
movb $0x0, %al
callq 0x2040
xorl %edi, %edi
callq 0x2390
cmpl $0x0, -0x10(%rbp)
jne 0x8a5c
leaq 0x6ab3cc60(%rip), %rax  # 0x6ab45588
movq (%rax), %rax
movl $0x1, (%rax)
callq 0x68c0
movq %rax, -0x18(%rbp)
leaq 0x1afe(%rip), %rdi      # 0xa43f
leaq 0x1b0a(%rip), %rsi      # 0xa452
callq 0x2360
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rax
movl (%rax), %eax
movl $0x30, %ecx
cltd
idivl %ecx
movl %eax, %edx
leaq 0x1ae9(%rip), %rsi      # 0xa455
movb $0x0, %al
callq 0x2420
movl %eax, -0x40(%rbp)
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rax
movl (%rax), %edx
leaq 0x1ae0(%rip), %rsi      # 0xa467
movb $0x0, %al
callq 0x2420
movl %eax, -0x3c(%rbp)
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x4(%rax), %edx
leaq 0x1adb(%rip), %rsi      # 0xa47e
movb $0x0, %al
callq 0x2420
movl %eax, -0x38(%rbp)
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x8(%rax), %edx
leaq 0x1ad7(%rip), %rsi      # 0xa496
movb $0x0, %al
callq 0x2420
movl %eax, -0x34(%rbp)
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0xc(%rax), %edx
leaq 0x1ad3(%rip), %rsi      # 0xa4ae
movb $0x0, %al
callq 0x2420
movl %eax, -0x30(%rbp)
movq -0x20(%rbp), %rdi
movq -0x18(%rbp), %rax
movl 0x10(%rax), %edx
leaq 0x1ad1(%rip), %rsi      # 0xa4c8
movb $0x0, %al
callq 0x2420
movl %eax, -0x2c(%rbp)
cmpl $0x0, -0x40(%rbp)
jl 0x8a25
cmpl $0x0, -0x3c(%rbp)
jl 0x8a25
cmpl $0x0, -0x38(%rbp)
jl 0x8a25
cmpl $0x0, -0x34(%rbp)
jl 0x8a25
cmpl $0x0, -0x30(%rbp)
jl 0x8a25
cmpl $0x0, -0x2c(%rbp)
jge 0x8a33
leaq 0x1ab6(%rip), %rdi      # 0xa4e2
movb $0x0, %al
callq 0x2040
movq -0x18(%rbp), %rdi
callq 0x2370
movq -0x20(%rbp), %rdi
callq 0x22f0
leaq 0x6ab3cb3c(%rip), %rax  # 0x6ab45588
movq (%rax), %rax
movl $0x0, (%rax)
xorl %edi, %edi
callq 0x2390
leaq 0x653d3a91(%rip), %rax  # 0x653dc4f4
movslq (%rax), %rax
leaq 0xed36273(%rip), %rsi   # 0xed3ece0
imulq $0x30, %rax, %rax
addq %rax, %rsi
leaq 0x653d3a75(%rip), %rax  # 0x653dc4f0
movl (%rax), %eax
leaq 0x653d3a70(%rip), %rcx  # 0x653dc4f4
subl (%rcx), %eax
imull $0x30, %eax, %eax
movslq %eax, %rdx
leaq 0xed3624d(%rip), %rdi   # 0xed3ece0
callq 0x2260
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
leaq 0x653d3a4b(%rip), %rcx  # 0x653dc4f4
cmpl (%rcx), %eax
jge 0x8b23
movslq -0x44(%rbp), %rcx
leaq 0x2b6f9a28(%rip), %rax  # 0x2b7024e0
shlq $0x4, %rcx
addq %rcx, %rax
movq $0x0, 0x8(%rax)
movslq -0x44(%rbp), %rcx
leaq 0x34f9020e(%rip), %rax  # 0x34f98ce0
shlq $0x4, %rcx
addq %rcx, %rax
movq $0x0, 0x8(%rax)
movslq -0x44(%rbp), %rcx
leaq 0x3e8269f4(%rip), %rax  # 0x3e82f4e0
shlq $0x4, %rcx
addq %rcx, %rax
movq $0x0, 0x8(%rax)
movslq -0x44(%rbp), %rcx
leaq 0x480bd1da(%rip), %rax  # 0x480c5ce0
shlq $0x4, %rcx
addq %rcx, %rax
movq $0x0, 0x8(%rax)
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x8a9f
leaq 0x653d39de(%rip), %rax  # 0x653dc508
movslq (%rax), %rdx
shlq $0xb, %rdx
leaq 0x519539a8(%rip), %rdi  # 0x5195c4e0
xorl %esi, %esi
callq 0x2170
leaq 0x653d39ca(%rip), %rax  # 0x653dc510
movslq (%rax), %rdx
shlq $0xb, %rdx
leaq 0x5406398c(%rip), %rdi  # 0x5406c4e0
xorl %esi, %esi
callq 0x2170
leaq 0x653d39aa(%rip), %rax  # 0x653dc50c
movslq (%rax), %rdx
shlq $0xc, %rdx
leaq 0x56773970(%rip), %rdi  # 0x5677c4e0
xorl %esi, %esi
callq 0x2170
leaq 0x653d3996(%rip), %rax  # 0x653dc514
movslq (%rax), %rdx
shlq $0xc, %rdx
leaq 0x5dca3954(%rip), %rdi  # 0x5dcac4e0
xorl %esi, %esi
callq 0x2170
leaq 0x651d3956(%rip), %rdi  # 0x651dc4f0
xorl %esi, %esi
movl $0x100000, %edx         # imm = 0x100000
callq 0x2170
leaq 0x652d3943(%rip), %rdi  # 0x652dc4f0
xorl %esi, %esi
movl $0x100000, %edx         # imm = 0x100000
callq 0x2170
leaq 0x651d3920(%rip), %rax  # 0x651dc4e0
leaq 0x51953919(%rip), %rcx  # 0x5195c4e0
movq %rcx, (%rax)
leaq 0x651d3917(%rip), %rax  # 0x651dc4e8
leaq 0x54063908(%rip), %rcx  # 0x5406c4e0
movq %rcx, (%rax)
leaq 0x653d3912(%rip), %rax  # 0x653dc4f4
movl (%rax), %eax
leaq 0x653d3905(%rip), %rcx  # 0x653dc4f0
movl (%rcx), %ecx
subl %eax, %ecx
leaq 0x653d38fa(%rip), %rax  # 0x653dc4f0
movl %ecx, (%rax)
leaq 0x653d38f5(%rip), %rax  # 0x653dc4f4
movl $0x0, (%rax)
leaq 0x653d38fc(%rip), %rax  # 0x653dc508
movl $0x1, (%rax)
leaq 0x653d38f7(%rip), %rax  # 0x653dc510
movl $0x1, (%rax)
leaq 0x653d38e6(%rip), %rax  # 0x653dc50c
movl $0x0, (%rax)
leaq 0x653d38e1(%rip), %rax  # 0x653dc514
movl $0x0, (%rax)
leaq 0x653d38b8(%rip), %rax  # 0x653dc4f8
movl $0x0, (%rax)
leaq 0x653d38af(%rip), %rax  # 0x653dc4fc
movl $0x0, (%rax)
leaq 0x653d38a6(%rip), %rax  # 0x653dc500
movl $0x0, (%rax)
leaq 0x653d389d(%rip), %rax  # 0x653dc504
movl $0x0, (%rax)
jmp 0x8c6f
jmp 0x8c71
leaq 0x665c(%rip), %rax      # 0xf2d4
cmpl $0x61a80, (%rax)        # imm = 0x61A80
jge 0x8c8c
leaq 0x6681(%rip), %rdi      # 0xf308
callq 0x2030
leaq 0x664d(%rip), %rdi      # 0xf2e0
callq 0x21f0
jmp 0x884a
nopl (%rax)