// File: /workspace/github_c_makefile_star2_O1/mvermeulen[P]wspy/topdown.c
// Function: ptrace_loop
void ptrace_loop(void){
  pid_t pid,child;
  int status;
  int i;
  int last_syscall = 0;
  int syscall_entry = 0;
  unsigned long data;
  char buffer[1024];
  char stat_name[128];
  struct stat statbuf;
  FILE *stat_file;
  struct user_regs_struct regs;
  struct rusage rusage;
  double elapsed;
  char *filename;

  while(1){
    pid = wait4(-1,&status,0,&rusage);
    clock_gettime(CLOCK_REALTIME,&finish_time);
    elapsed = finish_time.tv_sec + finish_time.tv_nsec / 1000000000.0 -
      start_time.tv_sec - start_time.tv_nsec / 1000000000.0;
    debug2("event: pid=%d status=%x\n",pid,status);
    if (pid == -1){
      if (errno == ECHILD){
	break; // no more children to wait
      } else {
	error("wait returns -1 with errno %d - %s\n",errno,strerror(errno));
      }
    }
    if (WIFEXITED(status)){
      fprintf(treefile,"%5.3f %d exited\n",elapsed,pid);
      fflush(treefile);
      debug2("   exited\n");
      if (pid == child_pid) break;
      continue;
    } else if (WIFSIGNALED(status)){
      // dump contents of proc/<pid>/comm
      snprintf(stat_name,sizeof(stat_name),"/proc/%d/comm",pid);
      if ((stat_file = fopen(stat_name,"r")) != NULL){
	if (fgets(buffer,sizeof(buffer),stat_file) != NULL){
	  fprintf(treefile,"%5.3f %d comm ",elapsed,pid);
	  fputs(buffer,treefile);
	}
	fclose(stat_file);
      }
      // dump the full command line
      if (tree_cmdline){
	snprintf(stat_name,sizeof(stat_name),"/proc/%d/cmdline",pid);
	fprintf(treefile,"%5.3f %d cmdline",elapsed,pid);
	if ((stat_file = fopen(stat_name,"rb")) != NULL){
	  char *arg = 0;
	  size_t size = 0;
	  while (getdelim(&arg,&size,0,stat_file) != -1){
	    fprintf(treefile," %s",arg);
	  }
	  fclose(stat_file);
	}
	fprintf(treefile,"\n");
      }
      
      fprintf(treefile,"%5.3f %d signal %u\n",elapsed,pid,WTERMSIG(status));
      fflush(treefile);
      debug2("   signaled\n");
      continue;
    } else if (WIFSTOPPED(status)){
      if (WSTOPSIG(status) == SIGTRAP){
	// we have an event!
	switch(status>>16){
	case PTRACE_EVENT_CLONE:
	case PTRACE_EVENT_FORK:
	case PTRACE_EVENT_VFORK:
	  ptrace(PTRACE_GETEVENTMSG,pid,NULL,&data);
	  fprintf(treefile,"%5.3f %d fork %lu\n",elapsed,pid,data);
	  fflush(treefile);
	  debug2("   clone/fork/vfork - pid=%d\n",data);
	  break;
	case PTRACE_EVENT_EXIT:
	  ptrace(PTRACE_GETEVENTMSG,pid,NULL,&data);
	  // dump contents of proc/<pid>/comm
	  snprintf(stat_name,sizeof(stat_name),"/proc/%d/comm",pid);
	  if ((stat_file = fopen(stat_name,"r")) != NULL){
	    if (fgets(buffer,sizeof(buffer),stat_file) != NULL){
	      fprintf(treefile,"%5.3f %d comm ",elapsed,pid);
	      fputs(buffer,treefile);
	    }
	    fclose(stat_file);
	  }
	  // dump the full command line
	  if (tree_cmdline){
	    snprintf(stat_name,sizeof(stat_name),"/proc/%d/cmdline",pid);
	    fprintf(treefile,"%5.3f %d cmdline",elapsed,pid);
	    if ((stat_file = fopen(stat_name,"rb")) != NULL){
	      char *arg = 0;
	      size_t size = 0;
	      while (getdelim(&arg,&size,0,stat_file) != -1){
		fprintf(treefile," %s",arg);
	      }
	    }
	    fprintf(treefile,"\n");
	    fclose(stat_file);
	  }

	  // dump contents of proc/<pid>/stat
	  snprintf(stat_name,sizeof(stat_name),"/proc/%d/stat",pid);
	  if ((stat_file = fopen(stat_name,"r")) != NULL){
	    if (fgets(buffer,sizeof(buffer),stat_file) != NULL){
	      fprintf(treefile,"%5.3f %d exit ",elapsed,pid);
	      fputs(buffer,treefile);
	      fflush(treefile);
	    }
	    fclose(stat_file);
	  }
	  debug2("   exit - exit status=%d\n",data);
	  break;
	default:
	  // normal SIGTRAP - not sure how we got here, but continue without it.
	  // we seem to get these after a process has exited...
	  fprintf(treefile,"%5.3f %d unknown %x\n",elapsed,pid,status);
	  fflush(treefile);
	  debug2("   unknown event: %d status=%x %d\n",pid,status);
	  ptrace(trace_syscall?PTRACE_SYSCALL:PTRACE_CONT,pid,NULL,NULL);
	  continue;
	}
      } else if (WSTOPSIG(status) == SIGSTOP){
	// newly created process after fork/vfork/clone, continue without passing along signal
	debug2("   new pid\n");
	ptrace(trace_syscall?PTRACE_SYSCALL:PTRACE_CONT,pid,NULL,NULL);
	continue;
      } else if (WSTOPSIG(status) == (SIGTRAP | 0x80)){
	// stopped because of a system call
	ptrace(PTRACE_GETREGS,pid,0,&regs);
	if (last_syscall != regs.orig_rax){
	  syscall_entry = 1;
	} else {
	  syscall_entry = (1-syscall_entry);
	}
	last_syscall = regs.orig_rax;
	if (tree_open && (syscall_entry == 0) && (last_syscall == SYS_openat)){
	  filename = ptrace_read_null_terminated_string(pid, regs.rsi);
	  fprintf(treefile,"%5.3f %d open %s\n",elapsed,pid,filename);
	}
	ptrace(PTRACE_SYSCALL,pid,NULL,NULL,NULL);
      } else {
	// pass other signals to the child
	fprintf(treefile,"%5.3f %d signal %d\n",elapsed,pid,WSTOPSIG(status));
	fflush(treefile);
	ptrace(trace_syscall?PTRACE_SYSCALL:PTRACE_CONT,pid,NULL,WSTOPSIG(status));
      }
    } else if (WIFCONTINUED(status)){
      fprintf(treefile,"%5.3f %d continued\n",elapsed,pid);
      fflush(treefile);      
      // nothing here
    }
    // let the child go to the next event
    ptrace(trace_syscall?PTRACE_SYSCALL:PTRACE_CONT,pid,NULL,NULL);
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x648, %rsp            # imm = 0x648
leaq 0xdcf9(%rip), %rbp      # 0x120f0
leaq 0x18(%rsp), %r13
leaq 0x30(%rsp), %rbx
leaq 0x83aa(%rip), %r14      # 0xc7b2
movl $0x0, 0x14(%rsp)
movq $0x0, 0x28(%rsp)
movl $0xffffffff, %edi       # imm = 0xFFFFFFFF
leaq 0x10(%rsp), %rsi
xorl %edx, %edx
leaq 0x1b0(%rsp), %rcx
callq 0x3230
movl %eax, %r15d
xorl %edi, %edi
leaq 0xed02(%rip), %rsi      # 0x13140
callq 0x3070
xorps %xmm0, %xmm0
cvtsi2sdq 0xecf1(%rip), %xmm0 # 0x13140
movsd %xmm0, 0x40(%rsp)
xorps %xmm0, %xmm0
cvtsi2sdq 0xece7(%rip), %xmm0 # 0x13148
movsd %xmm0, 0x8(%rsp)
xorps %xmm0, %xmm0
cvtsi2sdq 0xecdd(%rip), %xmm0 # 0x13150
movsd %xmm0, 0x38(%rsp)
xorps %xmm0, %xmm0
cvtsi2sdq 0xecd3(%rip), %xmm0 # 0x13158
movsd %xmm0, 0x48(%rsp)
movl 0x10(%rsp), %edx
leaq 0x8281(%rip), %rdi      # 0xc717
movl %r15d, %esi
xorl %eax, %eax
callq 0x9701
movsd 0x8(%rsp), %xmm2
cmpl $-0x1, %r15d
jne 0x44e0
callq 0x3040
movl (%rax), %r12d
cmpl $0xa, %r12d
je 0x4b22
movl %r12d, %edi
callq 0x32f0
leaq 0x8263(%rip), %rdi      # 0xc730
movl %r12d, %esi
movq %rax, %rdx
xorl %eax, %eax
callq 0x9256
movsd 0x8(%rsp), %xmm2
movsd 0x6d00(%rip), %xmm0     # 0xb1e8
divsd %xmm0, %xmm2
addsd 0x40(%rsp), %xmm2
subsd 0x38(%rsp), %xmm2
movsd 0x48(%rsp), %xmm1
divsd %xmm0, %xmm1
subsd %xmm1, %xmm2
movl 0x10(%rsp), %ecx
movl %ecx, %eax
andl $0x7f, %eax
movapd %xmm2, %xmm0
je 0x4659
shll $0x18, %eax
addl $0x1000000, %eax        # imm = 0x1000000
cmpl $0x2000000, %eax        # imm = 0x2000000
jl 0x4697
movsd %xmm0, 0x8(%rsp)
movl $0x80, %esi
leaq 0x50(%rsp), %r12
movq %r12, %rdi
leaq 0x822a(%rip), %rdx      # 0xc770
movl %r15d, %ecx
xorl %eax, %eax
callq 0x30f0
movq %r12, %rdi
leaq 0x7dc4(%rip), %rsi      # 0xc31e
callq 0x3250
testq %rax, %rax
je 0x45ba
movq %rax, %r12
leaq 0x240(%rsp), %r14
movq %r14, %rdi
movl $0x400, %esi            # imm = 0x400
movq %rax, %rdx
callq 0x3170
testq %rax, %rax
je 0x45ab
movq (%rbp), %rdi
leaq 0x81ef(%rip), %rsi      # 0xc77e
movsd 0x8(%rsp), %xmm0
movl %r15d, %edx
movb $0x1, %al
callq 0x31c0
movq (%rbp), %rsi
movq %r14, %rdi
callq 0x3100
movq %r12, %rdi
callq 0x30a0
leaq 0x81f8(%rip), %r14      # 0xc7b2
leaq 0xdb23(%rip), %rax      # 0x120e4
cmpl $0x0, (%rax)
je 0x475e
movl $0x80, %esi
leaq 0x50(%rsp), %r12
movq %r12, %rdi
leaq 0x81af(%rip), %rdx      # 0xc78d
movl %r15d, %ecx
xorl %eax, %eax
callq 0x30f0
movq (%rbp), %rdi
leaq 0x81ab(%rip), %rsi      # 0xc79e
movsd 0x8(%rsp), %xmm0
movl %r15d, %edx
movb $0x1, %al
callq 0x31c0
movq %r12, %rdi
leaq 0x81a2(%rip), %rsi      # 0xc7af
callq 0x3250
testq %rax, %rax
je 0x4750
movq %rax, %r12
xorl %eax, %eax
movq %rax, 0x18(%rsp)
movq %rax, 0x30(%rsp)
movq %r13, %rdi
movq %rbx, %rsi
xorl %edx, %edx
movq %r12, %rcx
callq 0x3320
cmpq $-0x1, %rax
je 0x4748
movq (%rbp), %rdi
movq 0x18(%rsp), %rdx
movq %r14, %rsi
xorl %eax, %eax
callq 0x31c0
jmp 0x462a
movq (%rbp), %rdi
leaq 0x80f0(%rip), %rsi      # 0xc754
movl %r15d, %edx
movb $0x1, %al
callq 0x31c0
movq (%rbp), %rdi
callq 0x3200
leaq 0x80e7(%rip), %rdi      # 0xc765
xorl %eax, %eax
callq 0x9701
cmpl 0xda9c(%rip), %r15d     # 0x12128
jne 0x4419
jmp 0x4b22
cmpb $0x7f, %cl
jne 0x47ec
movl %ecx, %eax
shrl $0x8, %eax
cmpb $0x5, %al
je 0x46c5
movzbl %al, %ecx
cmpl $0x13, %ecx
jne 0x481b
leaq 0x81bb(%rip), %rdi      # 0xc877
xorl %eax, %eax
callq 0x9701
jmp 0x471e
movsd %xmm0, 0x8(%rsp)
movl %ecx, %eax
sarl $0x10, %eax
leal -0x1(%rax), %edx
cmpl $0x3, %edx
jb 0x479c
cmpl $0x6, %eax
je 0x492e
movq (%rbp), %rdi
leaq 0x814f(%rip), %rsi      # 0xc83f
movsd 0x8(%rsp), %xmm0
movl %r15d, %edx
movb $0x1, %al
callq 0x31c0
movq (%rbp), %rdi
callq 0x3200
movl 0x10(%rsp), %edx
leaq 0x8140(%rip), %rdi      # 0xc854
movl %r15d, %esi
xorl %eax, %eax
callq 0x9701
leaq 0xd9c7(%rip), %rax      # 0x120ec
cmpl $0x0, (%rax)
movl $0x18, %edi
movl $0x7, %eax
cmovel %eax, %edi
movl %r15d, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %eax, %eax
callq 0x3220
jmp 0x4419
movq %r12, %rdi
callq 0x30a0
movq (%rbp), %rsi
movl $0xa, %edi
callq 0x3150
movq (%rbp), %rdi
movl 0x10(%rsp), %ecx
andl $0x7f, %ecx
leaq 0x8046(%rip), %rsi      # 0xc7b6
movsd 0x8(%rsp), %xmm0
movl %r15d, %edx
movb $0x1, %al
callq 0x31c0
movq (%rbp), %rdi
callq 0x3200
leaq 0x803a(%rip), %rdi      # 0xc7ca
xorl %eax, %eax
callq 0x9701
jmp 0x4419
movl $0x4201, %edi           # imm = 0x4201
movl %r15d, %esi
xorl %edx, %edx
leaq 0x20(%rsp), %rcx
xorl %eax, %eax
callq 0x3220
movq (%rbp), %rdi
movq 0x20(%rsp), %rcx
leaq 0x8015(%rip), %rsi      # 0xc7d7
movsd 0x8(%rsp), %xmm0
movl %r15d, %edx
movb $0x1, %al
callq 0x31c0
movq (%rbp), %rdi
callq 0x3200
movq 0x20(%rsp), %rsi
leaq 0x8003(%rip), %rdi      # 0xc7ea
jmp 0x4b16
cmpl $0xffff, %ecx           # imm = 0xFFFF
jne 0x471e
movq (%rbp), %rdi
leaq 0x80a6(%rip), %rsi      # 0xc8a9
movl %r15d, %edx
movb $0x1, %al
callq 0x31c0
movq (%rbp), %rdi
callq 0x3200
jmp 0x471e
cmpl $0x85, %ecx
jne 0x48e0
movsd %xmm0, 0x8(%rsp)
movl $0xc, %edi
movl %r15d, %esi
xorl %edx, %edx
leaq 0xd8(%rsp), %rcx
xorl %eax, %eax
callq 0x3220
movslq 0x28(%rsp), %rax
movq 0x150(%rsp), %rdx
movl $0x1, %r12d
subl 0x14(%rsp), %r12d
cmpq %rax, %rdx
movl $0x1, %eax
cmovnel %eax, %r12d
leaq 0xd877(%rip), %rax      # 0x120e8
cmpl $0x0, (%rax)
setne %al
movq %rdx, %rcx
movq %rdx, 0x28(%rsp)
xorl $0x101, %ecx            # imm = 0x101
orl %r12d, %ecx
sete %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x48c0
movq 0x140(%rsp), %rsi
movl %r15d, %edi
callq 0x4331
movq (%rbp), %rdi
leaq 0x7fd6(%rip), %rsi      # 0xc883
movsd 0x8(%rsp), %xmm0
movl %r15d, %edx
movq %rax, %rcx
movb $0x1, %al
callq 0x31c0
movl $0x18, %edi
movl %r15d, %esi
xorl %edx, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %eax, %eax
callq 0x3220
movl %r12d, 0x14(%rsp)
jmp 0x471e
movzbl %al, %ecx
movq (%rbp), %rdi
leaq 0x7fa7(%rip), %rsi      # 0xc895
movl %r15d, %edx
movb $0x1, %al
callq 0x31c0
movq (%rbp), %rdi
callq 0x3200
leaq 0xd7e4(%rip), %rax      # 0x120ec
cmpl $0x0, (%rax)
movl $0x18, %edi
movl $0x7, %eax
cmovel %eax, %edi
movzbl 0x11(%rsp), %ecx
movl %r15d, %esi
xorl %edx, %edx
xorl %eax, %eax
callq 0x3220
jmp 0x471e
movl $0x4201, %edi           # imm = 0x4201
movl %r15d, %esi
xorl %edx, %edx
leaq 0x20(%rsp), %rcx
xorl %eax, %eax
callq 0x3220
movl $0x80, %esi
leaq 0x50(%rsp), %r12
movq %r12, %rdi
leaq 0x7e18(%rip), %rdx      # 0xc770
movl %r15d, %ecx
xorl %eax, %eax
callq 0x30f0
movq %r12, %rdi
leaq 0x79b2(%rip), %rsi      # 0xc31e
callq 0x3250
testq %rax, %rax
je 0x49cc
movq %rax, %r12
leaq 0x240(%rsp), %r14
movq %r14, %rdi
movl $0x400, %esi            # imm = 0x400
movq %rax, %rdx
callq 0x3170
testq %rax, %rax
je 0x49bd
movq (%rbp), %rdi
leaq 0x7ddd(%rip), %rsi      # 0xc77e
movsd 0x8(%rsp), %xmm0
movl %r15d, %edx
movb $0x1, %al
callq 0x31c0
movq (%rbp), %rsi
movq %r14, %rdi
callq 0x3100
movq %r12, %rdi
callq 0x30a0
leaq 0x7de6(%rip), %r14      # 0xc7b2
leaq 0xd711(%rip), %rax      # 0x120e4
cmpl $0x0, (%rax)
je 0x4a79
movl $0x80, %esi
leaq 0x50(%rsp), %r12
movq %r12, %rdi
leaq 0x7d9d(%rip), %rdx      # 0xc78d
movl %r15d, %ecx
xorl %eax, %eax
callq 0x30f0
movq (%rbp), %rdi
leaq 0x7d99(%rip), %rsi      # 0xc79e
movsd 0x8(%rsp), %xmm0
movl %r15d, %edx
movb $0x1, %al
callq 0x31c0
movq %r12, %rdi
leaq 0x7d90(%rip), %rsi      # 0xc7af
callq 0x3250
movq %rax, %r12
testq %rax, %rax
je 0x4a63
xorl %eax, %eax
movq %rax, 0x18(%rsp)
movq %rax, 0x30(%rsp)
movq %r13, %rdi
movq %rbx, %rsi
xorl %edx, %edx
movq %r12, %rcx
callq 0x3320
cmpq $-0x1, %rax
je 0x4a63
movq (%rbp), %rdi
movq 0x18(%rsp), %rdx
movq %r14, %rsi
xorl %eax, %eax
callq 0x31c0
jmp 0x4a38
movq (%rbp), %rsi
movl $0xa, %edi
callq 0x3150
movq %r12, %rdi
callq 0x30a0
movl $0x80, %esi
leaq 0x50(%rsp), %r12
movq %r12, %rdi
leaq 0x7d7b(%rip), %rdx      # 0xc808
movl %r15d, %ecx
xorl %eax, %eax
callq 0x30f0
movq %r12, %rdi
leaq 0x787d(%rip), %rsi      # 0xc31e
callq 0x3250
testq %rax, %rax
je 0x4b0a
movq %rax, %r12
leaq 0x240(%rsp), %r14
movq %r14, %rdi
movl $0x400, %esi            # imm = 0x400
movq %rax, %rdx
callq 0x3170
testq %rax, %rax
je 0x4afb
movq (%rbp), %rdi
leaq 0x7d40(%rip), %rsi      # 0xc816
movsd 0x8(%rsp), %xmm0
movl %r15d, %edx
movb $0x1, %al
callq 0x31c0
movq (%rbp), %rsi
movq %r14, %rdi
callq 0x3100
movq (%rbp), %rdi
callq 0x3200
movq %r12, %rdi
callq 0x30a0
leaq 0x7ca8(%rip), %r14      # 0xc7b2
movq 0x20(%rsp), %rsi
leaq 0x7d0f(%rip), %rdi      # 0xc825
xorl %eax, %eax
callq 0x9701
jmp 0x471e
addq $0x648, %rsp            # imm = 0x648
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq