// File: /workspace/github_c_makefile_star2_O0/mabdi[P]bunney/bunny-exec.c
// Function: finalize
static void finalize(void) {
  _u32 rno, i;
  struct bunny_tracedesc msg;
  MD5_CTX ctx;
  _u8 r[16];
  _u32 total_pcount = 0, total_fcount = 0;
  
  msg.exit_status  = exitflags;
  msg.thread_count = proc_cnt;

#ifdef DEBUG_TRACE
  printf("Termination status: 0x%x (",exitflags);

  if (!exitflags) printf("OK"); else {
    if (exitflags & EXITF_TIMEOUT) printf("Timeout");
    if (exitflags & EXITF_CRASH) printf("Crash");
    if (exitflags & EXITF_STUCK) printf("Stuck");
    if (exitflags & EXITF_NOCALLS) printf("Empty");
  }
  
  if (exitflags & (EXITF_TIMEOUT|EXITF_STUCK|EXITF_CRASH))
    printf(":%s",fault_loc ? fault_loc : (_u8*)"?");
  
  printf(")\nProcess count: %u\n",proc_cnt);
#endif /* DEBUG_TRACE */

  /* Report exec path cksum */
  MD5_Init(&ctx);

  for (rno=0;rno<proc_cnt;rno++) {

    for (i=0;i<procs[rno].funclist.c;i++) {
      if (procs[rno].funclist.v[i])
        MD5_Update(&ctx,procs[rno].funclist.v[i],strlen(procs[rno].funclist.v[i]));
      MD5_Update(&ctx,&procs[rno].nestlist.v[i],sizeof(_u32));
    }    

    total_fcount += procs[rno].func_count;

  }

  MD5_Final((char*)r,&ctx);
  
  msg.exec_cksum = *(_u64*)r;
  msg.func_count = total_fcount;

#ifdef DEBUG_TRACE
  printf("Exec flow checksum : %016llx (%u elem)\n",msg.exec_cksum, total_fcount);

  for (rno=0;rno<proc_cnt;rno++) {
    _s32 start = procs[rno].funclist.c - 8;

    if (start < 0) start = 0;
    
    printf("Process %u:\n",rno);
      
    printf("  Call backtrace: ... ");
      
    for (i=start;i<procs[rno].funclist.c;i++) 
      printf("%d:%s ",procs[rno].nestlist.v[i],procs[rno].funclist.v[i] ? procs[rno].funclist.v[i] : (_u8*)"<ret>");
	
    printf("\n");
      
    printf("  Parameter list (%u): ",procs[rno].paramlist.c);
      
    for (i=0;i<((procs[rno].paramlist.c > 32) ? 32 : procs[rno].paramlist.c );i++) 
      printf("0x%x ",procs[rno].paramlist.v[i]);
	
    printf("...\n");

  }
  
#endif /* DEBUG_TRACE */

  for (rno=0;rno<proc_cnt;rno++)
    total_pcount += procs[rno].paramlist.c;

  msg.param_count = total_pcount;
  msg.func_skip   = 0;

  for (i=0;i<16;i++)
    if (shmreg->func_quota[i] < 0) msg.func_skip -= shmreg->func_quota[i]; /* += - */

  if (fault_loc && (exitflags & (EXITF_TIMEOUT|EXITF_STUCK|EXITF_CRASH)))
    msg.fault_len = strlen(fault_loc); else msg.fault_len = 0;

  if (write(99, &msg, sizeof(msg)) != sizeof(msg))
    fatal("short control write to fd #99");

  if (msg.fault_len && write(99, fault_loc, msg.fault_len) != msg.fault_len)
    fatal("short fault data write to fd #99");

  for (rno=0;rno<proc_cnt;rno++) 
    if (procs[rno].paramlist.c) {
      if (sure_write(99,procs[rno].paramlist.v, procs[rno].paramlist.c * sizeof(_u32)) !=
         procs[rno].paramlist.c * sizeof(_u32)) fatal("short param write to fd #99 (%u)",procs[rno].paramlist.c);
    }
    
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0xb0, %rsp
movl $0x0, -0xa4(%rbp)
movl $0x0, -0xa8(%rbp)
movb 0x39fd(%rip), %al       # 0x6192
movb %al, -0x30(%rbp)
movl 0x39fa(%rip), %eax      # 0x6198
movl %eax, -0x1c(%rbp)
leaq -0x8c(%rbp), %rdi
callq 0x1080
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
cmpl 0x39db(%rip), %eax      # 0x6198
jae 0x28cc
movl $0x0, -0x8(%rbp)
movl -0x8(%rbp), %eax
movq 0x39cc(%rip), %rcx      # 0x61a0
movl -0x4(%rbp), %edx
shlq $0x6, %rdx
addq %rdx, %rcx
cmpl 0x18(%rcx), %eax
jae 0x289e
movq 0x39b2(%rip), %rax      # 0x61a0
movl -0x4(%rbp), %ecx
shlq $0x6, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movl -0x8(%rbp), %ecx
cmpq $0x0, (%rax,%rcx,8)
je 0x2860
movq 0x3993(%rip), %rax      # 0x61a0
movl -0x4(%rbp), %ecx
shlq $0x6, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movl -0x8(%rbp), %ecx
movq (%rax,%rcx,8), %rax
movq %rax, -0xb0(%rbp)
movq 0x3970(%rip), %rax      # 0x61a0
movl -0x4(%rbp), %ecx
shlq $0x6, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movl -0x8(%rbp), %ecx
movq (%rax,%rcx,8), %rdi
callq 0x1050
movq -0xb0(%rbp), %rsi
movq %rax, %rdx
leaq -0x8c(%rbp), %rdi
callq 0x1240
movq 0x3939(%rip), %rax      # 0x61a0
movl -0x4(%rbp), %ecx
shlq $0x6, %rcx
addq %rcx, %rax
movq 0x20(%rax), %rsi
movl -0x8(%rbp), %eax
shlq $0x2, %rax
addq %rax, %rsi
leaq -0x8c(%rbp), %rdi
movl $0x4, %edx
callq 0x1240
movl -0x8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8(%rbp)
jmp 0x27ca
movq 0x38fb(%rip), %rax      # 0x61a0
movl -0x4(%rbp), %ecx
shlq $0x6, %rcx
addq %rcx, %rax
movl 0x4(%rax), %eax
addl -0xa8(%rbp), %eax
movl %eax, -0xa8(%rbp)
movl -0x4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4(%rbp)
jmp 0x27b4
leaq -0xa0(%rbp), %rdi
leaq -0x8c(%rbp), %rsi
callq 0x1150
movq -0xa0(%rbp), %rax
movq %rax, -0x28(%rbp)
movl -0xa8(%rbp), %eax
movl %eax, -0x18(%rbp)
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
cmpl 0x3895(%rip), %eax      # 0x6198
jae 0x2930
movq 0x3894(%rip), %rax      # 0x61a0
movl -0x4(%rbp), %ecx
shlq $0x6, %rcx
addq %rcx, %rax
movl 0x38(%rax), %eax
addl -0xa4(%rbp), %eax
movl %eax, -0xa4(%rbp)
movl -0x4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4(%rbp)
jmp 0x28fa
movl -0xa4(%rbp), %eax
movl %eax, -0x14(%rbp)
movl $0x0, -0x10(%rbp)
movl $0x0, -0x8(%rbp)
cmpl $0x10, -0x8(%rbp)
jae 0x2983
movq 0x3834(%rip), %rax      # 0x6188
movl -0x8(%rbp), %ecx
movl 0x14(%rax,%rcx,4), %eax
cmpl $0x0, %eax
jge 0x2976
movq 0x3821(%rip), %rax      # 0x6188
movl -0x8(%rbp), %ecx
movl 0x14(%rax,%rcx,4), %ecx
movl -0x10(%rbp), %eax
subl %ecx, %eax
movl %eax, -0x10(%rbp)
jmp 0x2978
movl -0x8(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x8(%rbp)
jmp 0x2947
cmpq $0x0, 0x382d(%rip)      # 0x61b8
je 0x29ad
movzbl 0x37fe(%rip), %eax      # 0x6192
andl $0x7, %eax
cmpl $0x0, %eax
je 0x29ad
movq 0x3815(%rip), %rdi      # 0x61b8
callq 0x1050
movl %eax, -0x20(%rbp)
jmp 0x29b4
movl $0x0, -0x20(%rbp)
movl $0x63, %edi
leaq -0x30(%rbp), %rsi
movl $0x28, %edx
callq 0x1040
cmpq $0x28, %rax
je 0x2a1d
jmp 0x29cf
movq 0x360a(%rip), %rax      # 0x5fe0
movq (%rax), %rdi
leaq 0x195a(%rip), %rsi      # 0x433a
movb $0x0, %al
callq 0x11b0
movq 0x35f2(%rip), %rax      # 0x5fe0
movq (%rax), %rdi
leaq 0x1667(%rip), %rsi      # 0x405f
leaq 0x1970(%rip), %rdx      # 0x436f
leaq 0x166e(%rip), %rcx      # 0x4074
movl $0x165, %r8d            # imm = 0x165
movb $0x0, %al
callq 0x11b0
movl $0x1, %edi
callq 0x1130
cmpl $0x0, -0x20(%rbp)
je 0x2a91
movq 0x378e(%rip), %rsi      # 0x61b8
movl -0x20(%rbp), %eax
movl %eax, %edx
movl $0x63, %edi
callq 0x1040
movl -0x20(%rbp), %ecx
cmpq %rcx, %rax
je 0x2a91
jmp 0x2a43
movq 0x3596(%rip), %rax      # 0x5fe0
movq (%rax), %rdi
leaq 0x1924(%rip), %rsi      # 0x4378
movb $0x0, %al
callq 0x11b0
movq 0x357e(%rip), %rax      # 0x5fe0
movq (%rax), %rdi
leaq 0x15f3(%rip), %rsi      # 0x405f
leaq 0x18fc(%rip), %rdx      # 0x436f
leaq 0x15fa(%rip), %rcx      # 0x4074
movl $0x168, %r8d            # imm = 0x168
movb $0x0, %al
callq 0x11b0
movl $0x1, %edi
callq 0x1130
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
cmpl 0x36f7(%rip), %eax      # 0x6198
jae 0x2b90
movq 0x36f2(%rip), %rax      # 0x61a0
movl -0x4(%rbp), %ecx
shlq $0x6, %rcx
addq %rcx, %rax
cmpl $0x0, 0x38(%rax)
je 0x2b80
movq 0x36d7(%rip), %rax      # 0x61a0
movl -0x4(%rbp), %ecx
shlq $0x6, %rcx
addq %rcx, %rax
movq 0x30(%rax), %rsi
movq 0x36c2(%rip), %rax      # 0x61a0
movl -0x4(%rbp), %ecx
shlq $0x6, %rcx
addq %rcx, %rax
movl 0x38(%rax), %eax
shlq $0x2, %rax
movl %eax, %edx
movl $0x63, %edi
callq 0x2f60
cltq
movq 0x369c(%rip), %rcx      # 0x61a0
movl -0x4(%rbp), %edx
shlq $0x6, %rdx
addq %rdx, %rcx
movl 0x38(%rcx), %ecx
shlq $0x2, %rcx
cmpq %rcx, %rax
je 0x2b7e
jmp 0x2b1c
movq 0x34bd(%rip), %rax      # 0x5fe0
movq (%rax), %rdi
movq 0x3673(%rip), %rax      # 0x61a0
movl -0x4(%rbp), %ecx
shlq $0x6, %rcx
addq %rcx, %rax
movl 0x38(%rax), %edx
leaq 0x186f(%rip), %rsi      # 0x43b0
movb $0x0, %al
callq 0x11b0
movq 0x3491(%rip), %rax      # 0x5fe0
movq (%rax), %rdi
leaq 0x1506(%rip), %rsi      # 0x405f
leaq 0x180f(%rip), %rdx      # 0x436f
leaq 0x150d(%rip), %rcx      # 0x4074
movl $0x16d, %r8d            # imm = 0x16D
movb $0x0, %al
callq 0x11b0
movl $0x1, %edi
callq 0x1130
jmp 0x2b80
jmp 0x2b82
movl -0x4(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x4(%rbp)
jmp 0x2a98
addq $0xb0, %rsp
popq %rbp
retq
nopl (%rax)