// File: /workspace/github_c_makefile_star2_O3/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 0xdcf8(%rip), %rbp      # 0x120f0
leaq 0x18(%rsp), %r13
leaq 0x30(%rsp), %rbx
leaq 0x83a9(%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 0xed01(%rip), %rsi      # 0x13140
callq 0x3070
xorps %xmm0, %xmm0
cvtsi2sdq 0xecf0(%rip), %xmm0 # 0x13140
movsd %xmm0, 0x40(%rsp)
xorps %xmm0, %xmm0
cvtsi2sdq 0xece6(%rip), %xmm0 # 0x13148
movsd %xmm0, 0x8(%rsp)
xorps %xmm0, %xmm0
cvtsi2sdq 0xecdc(%rip), %xmm0 # 0x13150
movsd %xmm0, 0x38(%rsp)
xorps %xmm0, %xmm0
cvtsi2sdq 0xecd2(%rip), %xmm0 # 0x13158
movsd %xmm0, 0x48(%rsp)
movl 0x10(%rsp), %edx
leaq 0x8280(%rip), %rdi      # 0xc717
movl %r15d, %esi
xorl %eax, %eax
callq 0x96db
movsd 0x8(%rsp), %xmm2
cmpl $-0x1, %r15d
jne 0x44e1
callq 0x3040
movl (%rax), %r12d
cmpl $0xa, %r12d
je 0x4b23
movl %r12d, %edi
callq 0x32f0
leaq 0x8262(%rip), %rdi      # 0xc730
movl %r12d, %esi
movq %rax, %rdx
xorl %eax, %eax
callq 0x9230
movsd 0x8(%rsp), %xmm2
movsd 0x6cff(%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 0x465a
shll $0x18, %eax
addl $0x1000000, %eax        # imm = 0x1000000
cmpl $0x2000000, %eax        # imm = 0x2000000
jl 0x4698
movsd %xmm0, 0x8(%rsp)
movl $0x80, %esi
leaq 0x50(%rsp), %r12
movq %r12, %rdi
leaq 0x8229(%rip), %rdx      # 0xc770
movl %r15d, %ecx
xorl %eax, %eax
callq 0x30f0
movq %r12, %rdi
leaq 0x7dc3(%rip), %rsi      # 0xc31e
callq 0x3250
testq %rax, %rax
je 0x45bb
movq %rax, %r12
leaq 0x240(%rsp), %r14
movq %r14, %rdi
movl $0x400, %esi            # imm = 0x400
movq %rax, %rdx
callq 0x3170
testq %rax, %rax
je 0x45ac
movq (%rbp), %rdi
leaq 0x81ee(%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 0x81f7(%rip), %r14      # 0xc7b2
leaq 0xdb22(%rip), %rax      # 0x120e4
cmpl $0x0, (%rax)
je 0x475f
movl $0x80, %esi
leaq 0x50(%rsp), %r12
movq %r12, %rdi
leaq 0x81ae(%rip), %rdx      # 0xc78d
movl %r15d, %ecx
xorl %eax, %eax
callq 0x30f0
movq (%rbp), %rdi
leaq 0x81aa(%rip), %rsi      # 0xc79e
movsd 0x8(%rsp), %xmm0
movl %r15d, %edx
movb $0x1, %al
callq 0x31c0
movq %r12, %rdi
leaq 0x81a1(%rip), %rsi      # 0xc7af
callq 0x3250
testq %rax, %rax
je 0x4751
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 0x4749
movq (%rbp), %rdi
movq 0x18(%rsp), %rdx
movq %r14, %rsi
xorl %eax, %eax
callq 0x31c0
jmp 0x462b
movq (%rbp), %rdi
leaq 0x80ef(%rip), %rsi      # 0xc754
movl %r15d, %edx
movb $0x1, %al
callq 0x31c0
movq (%rbp), %rdi
callq 0x3200
leaq 0x80e6(%rip), %rdi      # 0xc765
xorl %eax, %eax
callq 0x96db
cmpl 0xda9b(%rip), %r15d     # 0x12128
jne 0x441a
jmp 0x4b23
cmpb $0x7f, %cl
jne 0x47ed
movl %ecx, %eax
shrl $0x8, %eax
cmpb $0x5, %al
je 0x46c6
movzbl %al, %ecx
cmpl $0x13, %ecx
jne 0x481c
leaq 0x81ba(%rip), %rdi      # 0xc877
xorl %eax, %eax
callq 0x96db
jmp 0x471f
movsd %xmm0, 0x8(%rsp)
movl %ecx, %eax
sarl $0x10, %eax
leal -0x1(%rax), %edx
cmpl $0x3, %edx
jb 0x479d
cmpl $0x6, %eax
je 0x492f
movq (%rbp), %rdi
leaq 0x814e(%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 0x813f(%rip), %rdi      # 0xc854
movl %r15d, %esi
xorl %eax, %eax
callq 0x96db
leaq 0xd9c6(%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 0x441a
movq %r12, %rdi
callq 0x30a0
movq (%rbp), %rsi
movl $0xa, %edi
callq 0x3150
movq (%rbp), %rdi
movl 0x10(%rsp), %ecx
andl $0x7f, %ecx
leaq 0x8045(%rip), %rsi      # 0xc7b6
movsd 0x8(%rsp), %xmm0
movl %r15d, %edx
movb $0x1, %al
callq 0x31c0
movq (%rbp), %rdi
callq 0x3200
leaq 0x8039(%rip), %rdi      # 0xc7ca
xorl %eax, %eax
callq 0x96db
jmp 0x441a
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 0x8014(%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 0x8002(%rip), %rdi      # 0xc7ea
jmp 0x4b17
cmpl $0xffff, %ecx           # imm = 0xFFFF
jne 0x471f
movq (%rbp), %rdi
leaq 0x80a5(%rip), %rsi      # 0xc8a9
movl %r15d, %edx
movb $0x1, %al
callq 0x31c0
movq (%rbp), %rdi
callq 0x3200
jmp 0x471f
cmpl $0x85, %ecx
jne 0x48e1
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 0xd876(%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 0x48c1
movq 0x140(%rsp), %rsi
movl %r15d, %edi
callq 0x4339
movq (%rbp), %rdi
leaq 0x7fd5(%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 0x471f
movzbl %al, %ecx
movq (%rbp), %rdi
leaq 0x7fa6(%rip), %rsi      # 0xc895
movl %r15d, %edx
movb $0x1, %al
callq 0x31c0
movq (%rbp), %rdi
callq 0x3200
leaq 0xd7e3(%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 0x471f
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 0x7e17(%rip), %rdx      # 0xc770
movl %r15d, %ecx
xorl %eax, %eax
callq 0x30f0
movq %r12, %rdi
leaq 0x79b1(%rip), %rsi      # 0xc31e
callq 0x3250
testq %rax, %rax
je 0x49cd
movq %rax, %r12
leaq 0x240(%rsp), %r14
movq %r14, %rdi
movl $0x400, %esi            # imm = 0x400
movq %rax, %rdx
callq 0x3170
testq %rax, %rax
je 0x49be
movq (%rbp), %rdi
leaq 0x7ddc(%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 0x7de5(%rip), %r14      # 0xc7b2
leaq 0xd710(%rip), %rax      # 0x120e4
cmpl $0x0, (%rax)
je 0x4a7a
movl $0x80, %esi
leaq 0x50(%rsp), %r12
movq %r12, %rdi
leaq 0x7d9c(%rip), %rdx      # 0xc78d
movl %r15d, %ecx
xorl %eax, %eax
callq 0x30f0
movq (%rbp), %rdi
leaq 0x7d98(%rip), %rsi      # 0xc79e
movsd 0x8(%rsp), %xmm0
movl %r15d, %edx
movb $0x1, %al
callq 0x31c0
movq %r12, %rdi
leaq 0x7d8f(%rip), %rsi      # 0xc7af
callq 0x3250
movq %rax, %r12
testq %rax, %rax
je 0x4a64
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 0x4a64
movq (%rbp), %rdi
movq 0x18(%rsp), %rdx
movq %r14, %rsi
xorl %eax, %eax
callq 0x31c0
jmp 0x4a39
movq (%rbp), %rsi
movl $0xa, %edi
callq 0x3150
movq %r12, %rdi
callq 0x30a0
movl $0x80, %esi
leaq 0x50(%rsp), %r12
movq %r12, %rdi
leaq 0x7d7a(%rip), %rdx      # 0xc808
movl %r15d, %ecx
xorl %eax, %eax
callq 0x30f0
movq %r12, %rdi
leaq 0x787c(%rip), %rsi      # 0xc31e
callq 0x3250
testq %rax, %rax
je 0x4b0b
movq %rax, %r12
leaq 0x240(%rsp), %r14
movq %r14, %rdi
movl $0x400, %esi            # imm = 0x400
movq %rax, %rdx
callq 0x3170
testq %rax, %rax
je 0x4afc
movq (%rbp), %rdi
leaq 0x7d3f(%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 0x7ca7(%rip), %r14      # 0xc7b2
movq 0x20(%rsp), %rsi
leaq 0x7d0e(%rip), %rdi      # 0xc825
xorl %eax, %eax
callq 0x96db
jmp 0x471f
addq $0x648, %rsp            # imm = 0x648
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq