// File: /workspace/github_cpp_makefile_star2_O3/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
xorl %r13d, %r13d
movl $0x1, %edi
xorl %esi, %esi
callq 0x20c0
leaq 0x4edd(%rip), %rbx      # 0xa2b0
leaq 0x4eca(%rip), %r12      # 0xa2a4
leaq 0x6ab0bc27(%rip), %r14  # 0x6ab11008
movq %rbx, %rdi
callq 0x23e0
movl (%r12), %r15d
cmpl $0xf9f, %r15d           # imm = 0xF9F
jg 0x540e
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2420
movl (%r12), %r15d
cmpl $0xfa0, %r15d           # imm = 0xFA0
jl 0x53f6
movl 0x6ab0bbeb(%rip), %r14d # 0x6ab11000
cmpl $0x61a80, %r14d         # imm = 0x61A80
cmovel %r13d, %r14d
leaq (%r14,%r14,2), %rsi
shlq $0x4, %rsi
addq 0x4ba9(%rip), %rsi      # 0x9fd8
movl $0x2ee00, %edx          # imm = 0x2EE00
leaq 0x6ab0bc05(%rip), %rdi  # 0x6ab11040
callq 0x2240
addl $0xfa0, %r14d           # imm = 0xFA0
movl %r14d, 0x6ab0bbb2(%rip) # 0x6ab11000
addl $0xfffff060, %r15d      # imm = 0xFFFFF060
movl %r15d, (%r12)
callq 0x37fc
callq 0x3b54
testb %al, %al
je 0x5610
callq 0x20a0
testl %eax, %eax
js 0x5633
je 0x5644
leaq 0x653d203f(%rip), %rax  # 0x653d74c4
movslq (%rax), %r15
leaq (%r15,%r15,2), %rsi
shlq $0x4, %rsi
leaq 0xed34819(%rip), %rdi   # 0xed39cb0
addq %rdi, %rsi
leaq 0x653d201f(%rip), %rax  # 0x653d74c0
movslq (%rax), %rax
subq %r15, %rax
movq %rax, -0x30(%rbp)
shlq $0x4, %rax
leaq (%rax,%rax,2), %rdx
callq 0x2240
testq %r15, %r15
leaq 0x2b6f7fee(%rip), %rcx  # 0x2b6fd4b0
leaq 0x34f8e7e7(%rip), %rdx  # 0x34f93cb0
leaq 0x3e824fe0(%rip), %rsi  # 0x3e82a4b0
leaq 0x480bb7d9(%rip), %rdi  # 0x480c0cb0
jle 0x54fc
shlq $0x4, %r15
xorl %eax, %eax
movq %r13, 0x8(%rcx,%rax)
movq %r13, 0x8(%rdx,%rax)
movq %r13, 0x8(%rsi,%rax)
movq %r13, 0x8(%rdi,%rax)
addq $0x10, %rax
cmpq %rax, %r15
jne 0x54df
leaq 0x653d1fd5(%rip), %r14  # 0x653d74d8
movslq (%r14), %rdx
shlq $0xb, %rdx
leaq 0x51951f9f(%rip), %rdi  # 0x519574b0
xorl %esi, %esi
callq 0x2160
leaq 0x653d1fc1(%rip), %r14  # 0x653d74e0
movslq (%r14), %rdx
shlq $0xb, %rdx
leaq 0x54061f83(%rip), %rdi  # 0x540674b0
xorl %esi, %esi
callq 0x2160
leaq 0x653d1fa1(%rip), %r14  # 0x653d74dc
movslq (%r14), %rdx
shlq $0xc, %rdx
leaq 0x56771f67(%rip), %rdi  # 0x567774b0
xorl %esi, %esi
callq 0x2160
leaq 0x653d1f8d(%rip), %r15  # 0x653d74e4
movslq (%r15), %rdx
shlq $0xc, %rdx
leaq 0x5dca1f4b(%rip), %rdi  # 0x5dca74b0
xorl %esi, %esi
callq 0x2160
movl $0x100000, %edx         # imm = 0x100000
leaq 0x651d1f48(%rip), %rdi  # 0x651d74c0
xorl %esi, %esi
callq 0x2160
movl $0x100000, %edx         # imm = 0x100000
leaq 0x652d1f35(%rip), %rdi  # 0x652d74c0
xorl %esi, %esi
callq 0x2160
leaq 0x653d1f27(%rip), %rax  # 0x653d74c0
movq -0x30(%rbp), %rcx
movl %ecx, (%rax)
leaq 0x651d1f0a(%rip), %rax  # 0x651d74b0
leaq 0x51951f03(%rip), %rcx  # 0x519574b0
movq %rcx, (%rax)
leaq 0x651d1f01(%rip), %rax  # 0x651d74b8
leaq 0x54061ef2(%rip), %rcx  # 0x540674b0
movq %rcx, (%rax)
leaq 0x653d1efc(%rip), %rax  # 0x653d74c4
movl %r13d, (%rax)
movl $0x1, %eax
leaq 0x653d1f01(%rip), %rcx  # 0x653d74d8
movl %eax, (%rcx)
leaq 0x653d1f00(%rip), %rcx  # 0x653d74e0
movl %eax, (%rcx)
movl %r13d, (%r14)
movl %r13d, (%r15)
leaq 0x653d1ed9(%rip), %rax  # 0x653d74c8
movl %r13d, (%rax)
leaq 0x653d1ed3(%rip), %rax  # 0x653d74cc
movl %r13d, (%rax)
leaq 0x653d1ecd(%rip), %rax  # 0x653d74d0
movl %r13d, (%rax)
leaq 0x653d1ec7(%rip), %rax  # 0x653d74d4
movl %r13d, (%rax)
cmpl $0x61a7f, (%r12)        # imm = 0x61A7F
jg 0x5626
leaq 0x4cb7(%rip), %rdi      # 0xa2d8
callq 0x2030
movq %rbx, %rdi
callq 0x21e0
jmp 0x53da
leaq 0x1eb0(%rip), %rdi      # 0x74ea
callq 0x2440
jmp 0x574b
leaq 0x6ab3af0d(%rip), %rax  # 0x6ab40558
movq (%rax), %rax
movl $0x1, (%rax)
callq 0x4788
movq %rax, %rbx
leaq 0x1dd0(%rip), %rdi      # 0x7433
leaq 0x1ddc(%rip), %rsi      # 0x7446
callq 0x2320
movq %rax, %r14
movslq (%rbx), %rax
imulq $0x2aaaaaab, %rax, %rdx # imm = 0x2AAAAAAB
movq %rdx, %rax
shrq $0x3f, %rax
sarq $0x23, %rdx
addl %eax, %edx
leaq 0x1db9(%rip), %rsi      # 0x7449
movq %r14, %rdi
xorl %eax, %eax
callq 0x23c0
movl %eax, -0x30(%rbp)
movl (%rbx), %edx
leaq 0x1db5(%rip), %rsi      # 0x745b
movq %r14, %rdi
xorl %eax, %eax
callq 0x23c0
movl %eax, -0x34(%rbp)
movl 0x4(%rbx), %edx
leaq 0x1db5(%rip), %rsi      # 0x7472
movq %r14, %rdi
xorl %eax, %eax
callq 0x23c0
movl %eax, %r13d
movl 0x8(%rbx), %edx
leaq 0x1db6(%rip), %rsi      # 0x748a
movq %r14, %rdi
xorl %eax, %eax
callq 0x23c0
movl %eax, %r15d
movl 0xc(%rbx), %edx
leaq 0x1db7(%rip), %rsi      # 0x74a2
movq %r14, %rdi
xorl %eax, %eax
callq 0x23c0
movl %eax, %r12d
movl 0x10(%rbx), %edx
leaq 0x1dba(%rip), %rsi      # 0x74bc
movq %r14, %rdi
xorl %eax, %eax
callq 0x23c0
orl -0x34(%rbp), %r13d
orl %r15d, %r13d
orl %r12d, %r13d
orl -0x30(%rbp), %r13d
orl %eax, %r13d
jns 0x572b
leaq 0x1db0(%rip), %rdi      # 0x74d6
callq 0x2440
movq %rbx, %rdi
callq 0x2330
movq %r14, %rdi
callq 0x22c0
leaq 0x6ab3ae16(%rip), %rax  # 0x6ab40558
movq (%rax), %rax
movl $0x0, (%rax)
xorl %edi, %edi
callq 0x2350
nop