// File: /workspace/github_c_makefile_star2_O1/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, 0x8773(%rip)      # 0x120b8
je 0x99b2
leaq 0x422c(%rip), %rdi      # 0xdb7a
leaq 0x29c9(%rip), %rsi      # 0xc31e
callq 0x3250
testq %rax, %rax
je 0x99b2
movq %rax, %rbx
leaq 0x421f(%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 0x99aa
movsd 0x50(%rsp), %xmm0
movsd %xmm0, 0x97e8(%rip)     # 0x13188
movl 0xc(%rsp), %eax
movl %eax, 0x97e6(%rip)      # 0x13190
movq %rbx, %rdi
callq 0x30a0
testb $0x2, 0x86ff(%rip)      # 0x120b8
je 0x9b3f
leaq 0x41d1(%rip), %rdi      # 0xdb97
leaq 0x2951(%rip), %rsi      # 0xc31e
callq 0x3250
testq %rax, %rax
je 0x9b3f
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
movl $0x400, %esi            # imm = 0x400
movq %rax, %rdx
callq 0x3170
testq %rax, %rax
je 0x9b37
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 0x9a6f
subq $0x8, %rsp
leaq 0xbc(%rsp), %rdi
leaq 0x416c(%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 0x9a89
movq %r15, %rdi
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdx
callq 0x3170
testq %rax, %rax
jne 0x9a1e
jmp 0x9b37
movq 0x9730(%rip), %rax      # 0x131c0
movq %rax, 0x9751(%rip)      # 0x131e8
movq 0x972a(%rip), %rcx      # 0x131c8
movq %rcx, 0x974b(%rip)      # 0x131f0
movq 0x9724(%rip), %rdx      # 0x131d0
movq %rdx, 0x9745(%rip)      # 0x131f8
movq 0x971e(%rip), %rsi      # 0x131d8
movq %rsi, 0x973f(%rip)      # 0x13200
movq 0x9718(%rip), %rsi      # 0x131e0
movq %rsi, 0x9739(%rip)      # 0x13208
movq 0x48(%rsp), %rdi
movq %rdi, 0x96e5(%rip)      # 0x131c0
movq 0x40(%rsp), %r8
movq %r8, 0x96e1(%rip)       # 0x131c8
movq 0x38(%rsp), %r9
movq %r9, 0x96dd(%rip)       # 0x131d0
movq 0x28(%rsp), %r10
addq 0x30(%rsp), %r10
movq %r10, 0x96dc(%rip)      # 0x131e0
subq %rax, %rdi
movq %rdi, 0x968a(%rip)      # 0x13198
subq %rcx, %r8
movq %r8, 0x9688(%rip)       # 0x131a0
subq %rdx, %r9
movq %r9, 0x9686(%rip)       # 0x131a8
movq $0x0, 0x9683(%rip) # 0x131b0
subq %rsi, %r10
movq %r10, 0x9681(%rip)      # 0x131b8
movq %rbx, %rdi
callq 0x30a0
testb $0x8, 0x8572(%rip)      # 0x120b8
je 0x9ce5
cmpq $0x0, 0x96c4(%rip)      # 0x13218
jne 0x9b5b
callq 0x9818
leaq 0x400a(%rip), %rdi      # 0xdb6c
leaq 0x27b5(%rip), %rsi      # 0xc31e
callq 0x3250
testq %rax, %rax
je 0x9ce5
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
movl $0x400, %esi            # imm = 0x400
movq %rax, %rdx
callq 0x3170
testq %rax, %rax
je 0x9cdd
leaq 0xb0(%rsp), %r15
leaq 0x80(%rsp), %rbp
leaq 0x78(%rsp), %r14
movq %rbx, 0x10(%rsp)
movq %r15, %rdi
movl $0x3a, %esi
callq 0x30d0
testq %rax, %rax
je 0x9cb7
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 0x9bde
cmpl $0x0, 0x961c(%rip)      # 0x13210
jle 0x9cb7
incq %rbx
xorl %r12d, %r12d
xorl %r13d, %r13d
movq 0x960e(%rip), %rax      # 0x13218
movq (%rax,%r12), %rsi
movq %r15, %rdi
callq 0x31a0
testl %eax, %eax
jne 0x9ca0
subq $0x8, %rsp
movq %rbx, %rdi
leaq 0x3f76(%rip), %rsi      # 0xdba2
leaq 0x28(%rsp), %rdx
leaq 0x90(%rsp), %rcx
movq %rbp, %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
cmpl $0x9, %eax
jne 0x9ca0
movq 0x9599(%rip), %rax      # 0x13218
movq 0x10(%rax,%r12), %rcx
movq %rcx, 0x18(%rax,%r12)
movq 0x18(%rsp), %rdx
addq 0x20(%rsp), %rdx
movq %rdx, 0x10(%rax,%r12)
subq %rcx, %rdx
movq %rdx, 0x8(%rax,%r12)
incq %r13
movslq 0x9566(%rip), %rax      # 0x13210
addq $0x20, %r12
cmpq %rax, %r13
jl 0x9c03
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 0x9bb2
movq %rbx, %rdi
callq 0x30a0
addq $0x4b8, %rsp            # imm = 0x4B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq