// File: /workspace/github_c_makefile_star2_O2/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, 0x79dc(%rip)      # 0x110c8
je 0x9759
leaq 0x3485(%rip), %rdi      # 0xcb7a
leaq 0x1c22(%rip), %rsi      # 0xb31e
callq 0x3260
testq %rax, %rax
je 0x9759
movq %rax, %rbx
leaq 0x3478(%rip), %rsi      # 0xcb88
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 0x9751
movsd 0x50(%rsp), %xmm0
movsd %xmm0, 0x8a51(%rip)     # 0x12198
movl 0xc(%rsp), %eax
movl %eax, 0x8a4f(%rip)      # 0x121a0
movq %rbx, %rdi
callq 0x30a0
testb $0x2, 0x7968(%rip)      # 0x110c8
je 0x98bd
leaq 0x342a(%rip), %rdi      # 0xcb97
leaq 0x1baa(%rip), %rsi      # 0xb31e
callq 0x3260
testq %rax, %rax
je 0x98bd
movq %rax, %rbx
leaq 0xb0(%rsp), %r15
leaq 0x48(%rsp), %r13
leaq 0x98(%rsp), %rbp
leaq 0x40(%rsp), %r14
leaq 0x90(%rsp), %r12
movq %r15, %rdi
movl $0x400, %esi            # imm = 0x400
movq %rbx, %rdx
callq 0x3170
testq %rax, %rax
je 0x98b5
cmpl $0x20757063, 0xb0(%rsp) # imm = 0x20757063
jne 0x97a7
subq $0x8, %rsp
leaq 0xbc(%rsp), %rdi
leaq 0x33ca(%rip), %rsi      # 0xcbaa
movq %r13, %rdx
movq %rbp, %rcx
movq %r14, %r8
movq %r12, %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
jne 0x97a7
movq 0x89b8(%rip), %rax      # 0x121d0
movq %rax, 0x89d9(%rip)      # 0x121f8
movq 0x89b2(%rip), %rcx      # 0x121d8
movq %rcx, 0x89d3(%rip)      # 0x12200
movups 0x89ac(%rip), %xmm0     # 0x121e0
movups %xmm0, 0x89cd(%rip)     # 0x12208
movq 0x89ae(%rip), %rdx      # 0x121f0
movq %rdx, 0x89cf(%rip)      # 0x12218
movq 0x48(%rsp), %rsi
movq %rsi, 0x897b(%rip)      # 0x121d0
movq 0x40(%rsp), %rdi
movq %rdi, 0x8977(%rip)      # 0x121d8
movq 0x38(%rsp), %r8
movq %r8, %r9
subq 0x8970(%rip), %r9       # 0x121e0
movq %r8, 0x8969(%rip)       # 0x121e0
movq 0x28(%rsp), %r8
addq 0x30(%rsp), %r8
movq %r8, 0x8968(%rip)       # 0x121f0
subq %rax, %rsi
movq %rsi, 0x8916(%rip)      # 0x121a8
subq %rcx, %rdi
movq %rdi, 0x8914(%rip)      # 0x121b0
movq %r9, 0x8915(%rip)       # 0x121b8
andq $0x0, 0x8915(%rip)      # 0x121c0
subq %rdx, %r8
movq %r8, 0x8913(%rip)       # 0x121c8
movq %rbx, %rdi
callq 0x30a0
testb $0x8, 0x7804(%rip)      # 0x110c8
je 0x9a2c
cmpq $0x0, 0x8956(%rip)      # 0x12228
jne 0x98d9
callq 0x95d0
leaq 0x328c(%rip), %rdi      # 0xcb6c
leaq 0x1a37(%rip), %rsi      # 0xb31e
callq 0x3260
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x9a2c
leaq 0x78(%rsp), %r14
leaq 0xb0(%rsp), %rbx
movq %rbx, %rdi
movl $0x400, %esi            # imm = 0x400
movq 0x10(%rsp), %rdx
callq 0x3170
testq %rax, %rax
je 0x9a22
movq %rbx, %rdi
pushq $0x3a
popq %rsi
callq 0x30d0
testq %rax, %rax
je 0x98ff
movq %rax, %rbx
movb $0x0, (%rax)
callq 0x3360
movq (%rax), %rax
leaq 0xaf(%rsp), %r15
movsbq 0x1(%r15), %rcx
incq %r15
testb $0x20, 0x1(%rax,%rcx,2)
jne 0x9948
incq %rbx
movl 0x88c0(%rip), %ebp      # 0x12220
xorl %r12d, %r12d
xorl %r13d, %r13d
movslq %ebp, %rax
cmpq %rax, %r13
jge 0x98ff
movq 0x88b3(%rip), %rax      # 0x12228
movq (%rax,%r12), %rsi
movq %r15, %rdi
callq 0x31a0
testl %eax, %eax
jne 0x9a16
subq $0x8, %rsp
movq %rbx, %rdi
leaq 0x320b(%rip), %rsi      # 0xcba2
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
movl 0x8837(%rip), %ebp      # 0x12220
cmpl $0x9, %eax
jne 0x9a16
movq 0x8833(%rip), %rax      # 0x12228
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
addq $0x20, %r12
jmp 0x9966
movq 0x10(%rsp), %rdi
callq 0x30a0
addq $0x4b8, %rsp            # imm = 0x4B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq