// File: /workspace/github_cpp_makefile_star2_O1/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 0x4e55(%rip), %rbx      # 0xa2c0
leaq 0x4e42(%rip), %r12      # 0xa2b4
leaq 0x6ab0bb9f(%rip), %r14  # 0x6ab11018
movq %rbx, %rdi
callq 0x23f0
movl (%r12), %r15d
cmpl $0xf9f, %r15d           # imm = 0xF9F
jg 0x54a6
movq %r14, %rdi
movq %rbx, %rsi
callq 0x2430
movl (%r12), %r15d
cmpl $0xfa0, %r15d           # imm = 0xFA0
jl 0x548e
cmpl $0x61a80, 0x6ab0bb60(%rip) # imm = 0x61A80
jne 0x54bc
movl $0x0, 0x6ab0bb54(%rip)  # 0x6ab11010
movl 0x6ab0bb4d(%rip), %r14d # 0x6ab11010
leaq (%r14,%r14,2), %rsi
shlq $0x4, %rsi
addq 0x4b06(%rip), %rsi      # 0x9fd8
movl $0x2ee00, %edx          # imm = 0x2EE00
leaq 0x6ab0bb72(%rip), %rdi  # 0x6ab11050
callq 0x2240
leal 0xfa0(%r14), %eax
movl %eax, 0x6ab0bb20(%rip)  # 0x6ab11010
addl $0xfffff060, %r15d      # imm = 0xFFFFF060
movl %r15d, (%r12)
callq 0x37fc
callq 0x3b54
testb %al, %al
je 0x56b2
callq 0x20a0
testl %eax, %eax
js 0x56dc
je 0x56ed
leaq 0x653d1fad(%rip), %rax  # 0x653d74d4
movslq (%rax), %r15
leaq (%r15,%r15,2), %rsi
shlq $0x4, %rsi
leaq 0xed34787(%rip), %rdi   # 0xed39cc0
addq %rdi, %rsi
leaq 0x653d1f8d(%rip), %rax  # 0x653d74d0
movslq (%rax), %rax
subq %r15, %rax
movq %rax, -0x30(%rbp)
shlq $0x4, %rax
leaq (%rax,%rax,2), %rdx
callq 0x2240
testq %r15, %r15
leaq 0x2b6f7f5c(%rip), %rcx  # 0x2b6fd4c0
leaq 0x34f8e755(%rip), %rdx  # 0x34f93cc0
leaq 0x3e824f4e(%rip), %rsi  # 0x3e82a4c0
leaq 0x480bb747(%rip), %rdi  # 0x480c0cc0
jle 0x559e
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 0x5581
leaq 0x653d1f43(%rip), %r14  # 0x653d74e8
movslq (%r14), %rdx
shlq $0xb, %rdx
leaq 0x51951f0d(%rip), %rdi  # 0x519574c0
xorl %esi, %esi
callq 0x2160
leaq 0x653d1f2f(%rip), %r14  # 0x653d74f0
movslq (%r14), %rdx
shlq $0xb, %rdx
leaq 0x54061ef1(%rip), %rdi  # 0x540674c0
xorl %esi, %esi
callq 0x2160
leaq 0x653d1f0f(%rip), %r14  # 0x653d74ec
movslq (%r14), %rdx
shlq $0xc, %rdx
leaq 0x56771ed5(%rip), %rdi  # 0x567774c0
xorl %esi, %esi
callq 0x2160
leaq 0x653d1efb(%rip), %r15  # 0x653d74f4
movslq (%r15), %rdx
shlq $0xc, %rdx
leaq 0x5dca1eb9(%rip), %rdi  # 0x5dca74c0
xorl %esi, %esi
callq 0x2160
movl $0x100000, %edx         # imm = 0x100000
leaq 0x651d1eb6(%rip), %rdi  # 0x651d74d0
xorl %esi, %esi
callq 0x2160
movl $0x100000, %edx         # imm = 0x100000
leaq 0x652d1ea3(%rip), %rdi  # 0x652d74d0
xorl %esi, %esi
callq 0x2160
leaq 0x653d1e95(%rip), %rax  # 0x653d74d0
movq -0x30(%rbp), %rcx
movl %ecx, (%rax)
leaq 0x651d1e78(%rip), %rax  # 0x651d74c0
leaq 0x51951e71(%rip), %rcx  # 0x519574c0
movq %rcx, (%rax)
leaq 0x651d1e6f(%rip), %rax  # 0x651d74c8
leaq 0x54061e60(%rip), %rcx  # 0x540674c0
movq %rcx, (%rax)
leaq 0x653d1e6a(%rip), %rax  # 0x653d74d4
movl %r13d, (%rax)
movl $0x1, %eax
leaq 0x653d1e6f(%rip), %rcx  # 0x653d74e8
movl %eax, (%rcx)
leaq 0x653d1e6e(%rip), %rcx  # 0x653d74f0
movl %eax, (%rcx)
movl %r13d, (%r14)
movl %r13d, (%r15)
leaq 0x653d1e47(%rip), %rax  # 0x653d74d8
movl %r13d, (%rax)
leaq 0x653d1e41(%rip), %rax  # 0x653d74dc
movl %r13d, (%rax)
leaq 0x653d1e3b(%rip), %rax  # 0x653d74e0
movl %r13d, (%rax)
leaq 0x653d1e35(%rip), %rax  # 0x653d74e4
movl %r13d, (%rax)
cmpl $0x61a7f, (%r12)        # imm = 0x61A7F
leaq 0x6ab0b957(%rip), %r14  # 0x6ab11018
jg 0x56cf
leaq 0x4c1e(%rip), %rdi      # 0xa2e8
callq 0x2030
movq %rbx, %rdi
callq 0x21e0
jmp 0x5479
leaq 0x1e07(%rip), %rdi      # 0x74ea
callq 0x2450
jmp 0x57f4
leaq 0x6ab3ae74(%rip), %rax  # 0x6ab40568
movq (%rax), %rax
movl $0x1, (%rax)
callq 0x4794
movq %rax, %rbx
leaq 0x1d27(%rip), %rdi      # 0x7433
leaq 0x1d33(%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 0x1d10(%rip), %rsi      # 0x7449
movq %r14, %rdi
xorl %eax, %eax
callq 0x23d0
movl %eax, -0x30(%rbp)
movl (%rbx), %edx
leaq 0x1d0c(%rip), %rsi      # 0x745b
movq %r14, %rdi
xorl %eax, %eax
callq 0x23d0
movl %eax, -0x34(%rbp)
movl 0x4(%rbx), %edx
leaq 0x1d0c(%rip), %rsi      # 0x7472
movq %r14, %rdi
xorl %eax, %eax
callq 0x23d0
movl %eax, %r13d
movl 0x8(%rbx), %edx
leaq 0x1d0d(%rip), %rsi      # 0x748a
movq %r14, %rdi
xorl %eax, %eax
callq 0x23d0
movl %eax, %r15d
movl 0xc(%rbx), %edx
leaq 0x1d0e(%rip), %rsi      # 0x74a2
movq %r14, %rdi
xorl %eax, %eax
callq 0x23d0
movl %eax, %r12d
movl 0x10(%rbx), %edx
leaq 0x1d11(%rip), %rsi      # 0x74bc
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 0x57d4
leaq 0x1d07(%rip), %rdi      # 0x74d6
callq 0x2450
movq %rbx, %rdi
callq 0x2330
movq %r14, %rdi
callq 0x22c0
leaq 0x6ab3ad7d(%rip), %rax  # 0x6ab40568
movq (%rax), %rax
movl $0x0, (%rax)
xorl %edi, %edi
callq 0x2350
nop