// File: /workspace/github_cpp_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
pushq $0x1
popq %rdi
xorl %esi, %esi
callq 0x20c0
leaq 0x41f2(%rip), %r14      # 0x92b0
leaq 0x41df(%rip), %r13      # 0x92a4
leaq 0x6ab0af3c(%rip), %r15  # 0x6ab10008
movq %r14, %rdi
callq 0x23f0
movl (%r13), %ebx
cmpl $0xf9f, %ebx            # imm = 0xF9F
jg 0x50ed
movq %r15, %rdi
movq %r14, %rsi
callq 0x2430
jmp 0x50d4
movl 0x6ab0af0c(%rip), %r12d # 0x6ab10000
cmpl $0x61a80, %r12d         # imm = 0x61A80
movl $0x0, %eax
cmovel %eax, %r12d
imulq $0x30, %r12, %rsi
addq 0x3ec9(%rip), %rsi      # 0x8fd8
movl $0x2ee00, %edx          # imm = 0x2EE00
leaq 0x6ab0af25(%rip), %rdi  # 0x6ab10040
callq 0x2250
addl $0xfa0, %r12d           # imm = 0xFA0
movl %r12d, 0x6ab0aed2(%rip) # 0x6ab10000
addl $0xfffff060, %ebx       # imm = 0xFFFFF060
movl %ebx, (%r13)
callq 0x3738
callq 0x3a68
testb %al, %al
je 0x52f3
callq 0x20a0
testl %eax, %eax
js 0x5316
je 0x5327
leaq 0x653d1360(%rip), %rax  # 0x653d64c4
movslq (%rax), %rbx
imulq $0x30, %rbx, %rsi
leaq 0xed33b3e(%rip), %rdi   # 0xed38cb0
addq %rdi, %rsi
leaq 0x653d1344(%rip), %rax  # 0x653d64c0
movslq (%rax), %rcx
subq %rbx, %rcx
movq %rcx, -0x30(%rbp)
imulq $0x30, %rcx, %rdx
callq 0x2250
testl %ebx, %ebx
movl $0x0, %eax
cmovgl %ebx, %eax
shlq $0x4, %rax
xorl %ecx, %ecx
leaq 0x2b6f730a(%rip), %rdx  # 0x2b6fc4b0
leaq 0x34f8db03(%rip), %rsi  # 0x34f92cb0
leaq 0x3e8242fc(%rip), %rdi  # 0x3e8294b0
leaq 0x480baaf5(%rip), %r8   # 0x480bfcb0
cmpq %rcx, %rax
je 0x51de
andq $0x0, 0x8(%rdx,%rcx)
andq $0x0, 0x8(%rsi,%rcx)
andq $0x0, 0x8(%rdi,%rcx)
andq $0x0, 0x8(%r8,%rcx)
addq $0x10, %rcx
jmp 0x51bb
leaq 0x653d12f3(%rip), %rbx  # 0x653d64d8
movslq (%rbx), %rdx
shlq $0xb, %rdx
leaq 0x519512bd(%rip), %rdi  # 0x519564b0
xorl %esi, %esi
callq 0x2180
leaq 0x653d12df(%rip), %rbx  # 0x653d64e0
movslq (%rbx), %rdx
shlq $0xb, %rdx
leaq 0x540612a1(%rip), %rdi  # 0x540664b0
xorl %esi, %esi
callq 0x2180
leaq 0x653d12bf(%rip), %r12  # 0x653d64dc
movslq (%r12), %rdx
shlq $0xc, %rdx
leaq 0x56771284(%rip), %rdi  # 0x567764b0
xorl %esi, %esi
callq 0x2180
leaq 0x653d12aa(%rip), %rbx  # 0x653d64e4
movslq (%rbx), %rdx
shlq $0xc, %rdx
leaq 0x5dca1268(%rip), %rdi  # 0x5dca64b0
xorl %esi, %esi
callq 0x2180
movl $0x100000, %edx         # imm = 0x100000
leaq 0x651d1265(%rip), %rdi  # 0x651d64c0
xorl %esi, %esi
callq 0x2180
movl $0x100000, %edx         # imm = 0x100000
leaq 0x652d1252(%rip), %rdi  # 0x652d64c0
xorl %esi, %esi
callq 0x2180
leaq 0x653d1248(%rip), %rax  # 0x653d64c4
andl $0x0, (%rax)
andl $0x0, (%r12)
leaq 0x653d1235(%rip), %rax  # 0x653d64c0
movq -0x30(%rbp), %rcx
movl %ecx, (%rax)
andl $0x0, (%rbx)
leaq 0x651d1215(%rip), %rax  # 0x651d64b0
leaq 0x5195120e(%rip), %rcx  # 0x519564b0
movq %rcx, (%rax)
leaq 0x653d121c(%rip), %rax  # 0x653d64c8
andl $0x0, (%rax)
leaq 0x651d1202(%rip), %rax  # 0x651d64b8
leaq 0x540611f3(%rip), %rcx  # 0x540664b0
movq %rcx, (%rax)
leaq 0x653d1205(%rip), %rax  # 0x653d64cc
andl $0x0, (%rax)
pushq $0x1
popq %rax
leaq 0x653d1204(%rip), %rcx  # 0x653d64d8
movl %eax, (%rcx)
leaq 0x653d11f3(%rip), %rcx  # 0x653d64d0
andl $0x0, (%rcx)
leaq 0x653d11f9(%rip), %rcx  # 0x653d64e0
movl %eax, (%rcx)
leaq 0x653d11e4(%rip), %rax  # 0x653d64d4
andl $0x0, (%rax)
cmpl $0x61a7f, (%r13)        # imm = 0x61A7F
jg 0x5309
leaq 0x3fd4(%rip), %rdi      # 0x92d8
callq 0x2030
movq %r14, %rdi
callq 0x21f0
jmp 0x50cc
leaq 0x119f(%rip), %rdi      # 0x64bc
callq 0x2450
jmp 0x541e
leaq 0x6ab3a22a(%rip), %rax  # 0x6ab3f558
movq (%rax), %rax
movl $0x1, (%rax)
callq 0x4420
movq %rax, %rbx
leaq 0x10bf(%rip), %rdi      # 0x6405
leaq 0x10cb(%rip), %rsi      # 0x6418
callq 0x2330
movq %rax, %r14
movl (%rbx), %eax
pushq $0x30
popq %rcx
cltd
idivl %ecx
leaq 0x10b7(%rip), %rsi      # 0x641b
movq %r14, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0x23d0
movl %eax, -0x30(%rbp)
movl (%rbx), %edx
leaq 0x10b1(%rip), %rsi      # 0x642d
movq %r14, %rdi
xorl %eax, %eax
callq 0x23d0
movl %eax, -0x34(%rbp)
movl 0x4(%rbx), %edx
leaq 0x10b1(%rip), %rsi      # 0x6444
movq %r14, %rdi
xorl %eax, %eax
callq 0x23d0
movl %eax, %r13d
movl 0x8(%rbx), %edx
leaq 0x10b2(%rip), %rsi      # 0x645c
movq %r14, %rdi
xorl %eax, %eax
callq 0x23d0
movl %eax, %r15d
movl 0xc(%rbx), %edx
leaq 0x10b3(%rip), %rsi      # 0x6474
movq %r14, %rdi
xorl %eax, %eax
callq 0x23d0
movl %eax, %r12d
movl 0x10(%rbx), %edx
leaq 0x10b6(%rip), %rsi      # 0x648e
movq %r14, %rdi
xorl %eax, %eax
callq 0x23d0
orl -0x34(%rbp), %r13d
orl %r15d, %r13d
orl %r12d, %r13d
orl -0x30(%rbp), %r13d
orl %eax, %r13d
jns 0x5401
leaq 0x10ac(%rip), %rdi      # 0x64a8
callq 0x2450
movq %rbx, %rdi
callq 0x2340
movq %r14, %rdi
callq 0x22d0
leaq 0x6ab3a140(%rip), %rax  # 0x6ab3f558
movq (%rax), %rax
andl $0x0, (%rax)
xorl %edi, %edi
callq 0x2360
nopl (%rax)