// File: /workspace/github_c_makefile_star2_O3/mvermeulen[P]wspy/system.c
// Function: read_system
void read_system(void){
  FILE *fp;
  char buffer[1024];
  char *p,*p2;
  double value1,value5,value15;
  int runnable;
  unsigned long usertime,nicetime,systemtime,idletime,iowait,irqtime,softirqtime;
  int i;
  // loadavg
  if (system_mask & SYSTEM_LOADAVG){
    if (fp = fopen("/proc/loadavg","r")){
      if (fscanf(fp,"%lf %lf %lf %d",&value1,&value5,&value15,&runnable) == 4){
	system_state.load = value1;
	system_state.runnable = runnable;
      }
      fclose(fp);
    }
  }
  // cpu
  if (system_mask & SYSTEM_CPU){
    if (fp = fopen("/proc/stat","r")){
      while (fgets(buffer,sizeof(buffer),fp) != NULL){
	if (!strncmp(buffer,"cpu ",4)){
	  if (sscanf(&buffer[4],"%lu %lu %lu %lu %lu %lu %lu",
		     &usertime,&nicetime,&systemtime,&idletime,&iowait,&irqtime,&softirqtime) == 7){
	    system_state.cpu.prev_usertime = system_state.cpu.last_usertime;
	    system_state.cpu.prev_systemtime = system_state.cpu.last_systemtime;
	    system_state.cpu.prev_idletime = system_state.cpu.last_idletime;
	    system_state.cpu.prev_iowaittime = system_state.cpu.last_iowaittime;	    
	    system_state.cpu.prev_irqtime = system_state.cpu.last_irqtime;
	    system_state.cpu.last_usertime = usertime;
	    system_state.cpu.last_systemtime = systemtime;
	    system_state.cpu.last_idletime = idletime;
	    system_state.cpu.last_idletime = iowait;
	    system_state.cpu.last_irqtime = irqtime + softirqtime;
	    system_state.cpu.usertime = system_state.cpu.last_usertime - system_state.cpu.prev_usertime;
	    system_state.cpu.systemtime = system_state.cpu.last_systemtime - system_state.cpu.prev_systemtime;
	    system_state.cpu.idletime = system_state.cpu.last_idletime - system_state.cpu.prev_idletime;
	    system_state.cpu.iowaittime = system_state.cpu.last_iowaittime - system_state.cpu.prev_iowaittime;
	    system_state.cpu.irqtime = system_state.cpu.last_irqtime - system_state.cpu.prev_irqtime;
	    break;
	  }
	}
      }
      fclose(fp);
    }
  }
#if AMDGPU
  // gpu
  if (system_mask & SYSTEM_GPU){
    gpu_info_query(&system_state.gpu);
  }
#endif

  //
  if (system_mask & SYSTEM_NETWORK){
    unsigned long int field1,field2,field3,field4,field5,field6,field7,field8,field9;
    if (system_state.netinfo == NULL) setup_net_info();
    if (fp = fopen("/proc/net/dev","r")){
      while (fgets(buffer,sizeof(buffer),fp) != NULL){
	p = strchr(buffer,':');
	if (p == NULL) continue;
	*p = 0;
	p++;
	p2 = buffer;
	while (isspace(*p2)) p2++;
	for (i=0;i<system_state.num_net;i++){
	  if (!strcmp(p2,system_state.netinfo[i].name)){
	    if (sscanf(p,"%lu %lu %lu %lu %lu %lu %lu %lu %lu",
		       &field1,&field2,&field3,&field4,&field5,&field6,&field7,&field8,&field9) == 9){
	      system_state.netinfo[i].prev_bytes = system_state.netinfo[i].last_bytes;
	      system_state.netinfo[i].last_bytes = field1 + field9; // receive and transmit
	      system_state.netinfo[i].bytes = system_state.netinfo[i].last_bytes - system_state.netinfo[i].prev_bytes;
	      continue;
	    }
	  }
	}
      }
      fclose(fp);
    }
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4b8, %rsp            # imm = 0x4B8
testb $0x1, 0x8797(%rip)      # 0x120b8
je 0x998e
leaq 0x4250(%rip), %rdi      # 0xdb7a
leaq 0x29ed(%rip), %rsi      # 0xc31e
callq 0x3250
testq %rax, %rax
je 0x998e
movq %rax, %rbx
leaq 0x4243(%rip), %rsi      # 0xdb88
leaq 0x50(%rsp), %rdx
leaq 0xa8(%rsp), %rcx
leaq 0xa0(%rsp), %r8
leaq 0xc(%rsp), %r9
movq %rax, %rdi
xorl %eax, %eax
callq 0x3060
cmpl $0x4, %eax
jne 0x9986
movsd 0x50(%rsp), %xmm0
movsd %xmm0, 0x980c(%rip)     # 0x13188
movl 0xc(%rsp), %eax
movl %eax, 0x980a(%rip)      # 0x13190
movq %rbx, %rdi
callq 0x30a0
testb $0x2, 0x8723(%rip)      # 0x120b8
je 0x9b14
leaq 0x41f5(%rip), %rdi      # 0xdb97
leaq 0x2975(%rip), %rsi      # 0xc31e
callq 0x3250
testq %rax, %rax
je 0x9b14
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
movl $0x400, %esi            # imm = 0x400
movq %rax, %rdx
callq 0x3170
testq %rax, %rax
je 0x9b0c
leaq 0x48(%rsp), %r12
leaq 0x98(%rsp), %r13
leaq 0x40(%rsp), %rbp
leaq 0x90(%rsp), %r14
leaq 0xb0(%rsp), %r15
cmpl $0x20757063, 0xb0(%rsp) # imm = 0x20757063
jne 0x9a4b
subq $0x8, %rsp
leaq 0xbc(%rsp), %rdi
leaq 0x4190(%rip), %rsi      # 0xdbaa
movq %r12, %rdx
movq %r13, %rcx
movq %rbp, %r8
movq %r14, %r9
xorl %eax, %eax
leaq 0x30(%rsp), %r10
pushq %r10
leaq 0x40(%rsp), %r10
pushq %r10
leaq 0x50(%rsp), %r10
pushq %r10
callq 0x3210
addq $0x20, %rsp
cmpl $0x7, %eax
je 0x9a65
movq %r15, %rdi
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdx
callq 0x3170
testq %rax, %rax
jne 0x99fa
jmp 0x9b0c
movq 0x9754(%rip), %rax      # 0x131c0
movq %rax, 0x9775(%rip)      # 0x131e8
movq 0x974e(%rip), %rcx      # 0x131c8
movq %rcx, 0x976f(%rip)      # 0x131f0
movups 0x9748(%rip), %xmm0     # 0x131d0
movups %xmm0, 0x9769(%rip)     # 0x131f8
movq 0x974a(%rip), %rdx      # 0x131e0
movq %rdx, 0x976b(%rip)      # 0x13208
movq 0x48(%rsp), %rsi
movq %rsi, 0x9717(%rip)      # 0x131c0
movq 0x40(%rsp), %rdi
movq %rdi, 0x9713(%rip)      # 0x131c8
movq 0x38(%rsp), %r8
movq %r8, %r9
subq 0x970c(%rip), %r9       # 0x131d0
movq %r8, 0x9705(%rip)       # 0x131d0
movq 0x28(%rsp), %r8
addq 0x30(%rsp), %r8
movq %r8, 0x9704(%rip)       # 0x131e0
subq %rax, %rsi
movq %rsi, 0x96b2(%rip)      # 0x13198
subq %rcx, %rdi
movq %rdi, 0x96b0(%rip)      # 0x131a0
movq %r9, 0x96b1(%rip)       # 0x131a8
movq $0x0, 0x96ae(%rip) # 0x131b0
subq %rdx, %r8
movq %r8, 0x96ac(%rip)       # 0x131b8
movq %rbx, %rdi
callq 0x30a0
testb $0x8, 0x859d(%rip)      # 0x120b8
je 0x9cb8
cmpq $0x0, 0x96ef(%rip)      # 0x13218
jne 0x9b30
callq 0x97f4
leaq 0x4035(%rip), %rdi      # 0xdb6c
leaq 0x27e0(%rip), %rsi      # 0xc31e
callq 0x3250
testq %rax, %rax
je 0x9cb8
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
movl $0x400, %esi            # imm = 0x400
movq %rax, %rdx
callq 0x3170
testq %rax, %rax
je 0x9cb0
leaq 0xb0(%rsp), %r15
leaq 0x78(%rsp), %r14
movq %rbx, 0x10(%rsp)
movq %r15, %rdi
movl $0x3a, %esi
callq 0x30d0
testq %rax, %rax
je 0x9c8a
movq %rax, %rbx
movb $0x0, (%rax)
callq 0x3350
movq (%rax), %rax
leaq 0xaf(%rsp), %r15
movsbq 0x1(%r15), %rcx
incq %r15
testb $0x20, 0x1(%rax,%rcx,2)
jne 0x9bab
cmpl $0x0, 0x964f(%rip)      # 0x13210
jle 0x9c8a
incq %rbx
movq 0x9647(%rip), %rbp      # 0x13218
xorl %r12d, %r12d
xorl %r13d, %r13d
movq (%rbp,%r12), %rsi
movq %r15, %rdi
callq 0x31a0
testl %eax, %eax
jne 0x9c73
subq $0x8, %rsp
movq %rbx, %rdi
leaq 0x3fa8(%rip), %rsi      # 0xdba2
leaq 0x28(%rsp), %rdx
leaq 0x90(%rsp), %rcx
leaq 0x88(%rsp), %r8
movq %r14, %r9
xorl %eax, %eax
leaq 0x20(%rsp), %r10
pushq %r10
leaq 0x68(%rsp), %r10
pushq %r10
leaq 0x78(%rsp), %r10
pushq %r10
leaq 0x88(%rsp), %r10
pushq %r10
leaq 0x98(%rsp), %r10
pushq %r10
callq 0x3210
addq $0x30, %rsp
movq 0x95cb(%rip), %rbp      # 0x13218
cmpl $0x9, %eax
jne 0x9c73
movq 0x10(%rbp,%r12), %rax
movq %rax, 0x18(%rbp,%r12)
movq 0x18(%rsp), %rcx
addq 0x20(%rsp), %rcx
movq %rcx, 0x10(%rbp,%r12)
subq %rax, %rcx
movq %rcx, 0x8(%rbp,%r12)
incq %r13
movslq 0x9593(%rip), %rax      # 0x13210
addq $0x20, %r12
cmpq %rax, %r13
jl 0x9bd7
leaq 0xb0(%rsp), %r15
movq %r15, %rdi
movl $0x400, %esi            # imm = 0x400
movq 0x10(%rsp), %rbx
movq %rbx, %rdx
callq 0x3170
testq %rax, %rax
jne 0x9b7f
movq %rbx, %rdi
callq 0x30a0
addq $0x4b8, %rsp            # imm = 0x4B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq