// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
pushq %r14
pushq %rbx
subq $0x4f0, %rsp            # imm = 0x4F0
movl 0x6cc4(%rip), %eax      # 0x130c8
andl $0x1, %eax
cmpl $0x0, %eax
je 0xc483
leaq 0x3759(%rip), %rdi      # 0xfb6c
leaq 0x1ef4(%rip), %rsi      # 0xe30e
callq 0x3270
movq %rax, -0x18(%rbp)
cmpq $0x0, %rax
je 0xc481
movq -0x18(%rbp), %rdi
leaq 0x3746(%rip), %rsi      # 0xfb7a
leaq -0x438(%rbp), %rdx
leaq -0x440(%rbp), %rcx
leaq -0x448(%rbp), %r8
leaq -0x44c(%rbp), %r9
movb $0x0, %al
callq 0x3070
cmpl $0x4, %eax
jne 0xc478
movsd -0x438(%rbp), %xmm0
movsd %xmm0, 0x7d2c(%rip)     # 0x14198
movl -0x44c(%rbp), %eax
movl %eax, 0x7d28(%rip)      # 0x141a0
movq -0x18(%rbp), %rdi
callq 0x30b0
jmp 0xc483
movl 0x6c3f(%rip), %eax      # 0x130c8
andl $0x2, %eax
cmpl $0x0, %eax
je 0xc669
leaq 0x36ed(%rip), %rdi      # 0xfb89
leaq 0x1e6b(%rip), %rsi      # 0xe30e
callq 0x3270
movq %rax, -0x18(%rbp)
cmpq $0x0, %rax
je 0xc667
jmp 0xc4b8
leaq -0x420(%rbp), %rdi
movq -0x18(%rbp), %rdx
movl $0x400, %esi            # imm = 0x400
callq 0x3180
cmpq $0x0, %rax
je 0xc65e
leaq -0x420(%rbp), %rdi
leaq 0x36af(%rip), %rsi      # 0xfb94
movl $0x4, %edx
callq 0x3060
cmpl $0x0, %eax
jne 0xc659
leaq -0x420(%rbp), %rdi
addq $0x4, %rdi
leaq 0x3697(%rip), %rsi      # 0xfba1
leaq -0x458(%rbp), %rdx
leaq -0x460(%rbp), %rcx
leaq -0x468(%rbp), %r8
leaq -0x470(%rbp), %r9
leaq -0x478(%rbp), %r11
leaq -0x480(%rbp), %r10
leaq -0x488(%rbp), %rax
movq %r11, (%rsp)
movq %r10, 0x8(%rsp)
movq %rax, 0x10(%rsp)
movb $0x0, %al
callq 0x3230
cmpl $0x7, %eax
jne 0xc657
movq 0x7c70(%rip), %rax      # 0x141d0
movq %rax, 0x7c91(%rip)      # 0x141f8
movq 0x7c6a(%rip), %rax      # 0x141d8
movq %rax, 0x7c8b(%rip)      # 0x14200
movq 0x7c64(%rip), %rax      # 0x141e0
movq %rax, 0x7c85(%rip)      # 0x14208
movq 0x7c5e(%rip), %rax      # 0x141e8
movq %rax, 0x7c7f(%rip)      # 0x14210
movq 0x7c58(%rip), %rax      # 0x141f0
movq %rax, 0x7c79(%rip)      # 0x14218
movq -0x458(%rbp), %rax
movq %rax, 0x7c23(%rip)      # 0x141d0
movq -0x468(%rbp), %rax
movq %rax, 0x7c1d(%rip)      # 0x141d8
movq -0x470(%rbp), %rax
movq %rax, 0x7c17(%rip)      # 0x141e0
movq -0x478(%rbp), %rax
movq %rax, 0x7c09(%rip)      # 0x141e0
movq -0x480(%rbp), %rax
addq -0x488(%rbp), %rax
movq %rax, 0x7c04(%rip)      # 0x141f0
movq 0x7bdd(%rip), %rax      # 0x141d0
subq 0x7bfe(%rip), %rax      # 0x141f8
movq %rax, 0x7ba7(%rip)      # 0x141a8
movq 0x7bd0(%rip), %rax      # 0x141d8
subq 0x7bf1(%rip), %rax      # 0x14200
movq %rax, 0x7b9a(%rip)      # 0x141b0
movq 0x7bc3(%rip), %rax      # 0x141e0
subq 0x7be4(%rip), %rax      # 0x14208
movq %rax, 0x7b8d(%rip)      # 0x141b8
movq 0x7bb6(%rip), %rax      # 0x141e8
subq 0x7bd7(%rip), %rax      # 0x14210
movq %rax, 0x7b80(%rip)      # 0x141c0
movq 0x7ba9(%rip), %rax      # 0x141f0
subq 0x7bca(%rip), %rax      # 0x14218
movq %rax, 0x7b73(%rip)      # 0x141c8
jmp 0xc65e
jmp 0xc659
jmp 0xc4b8
movq -0x18(%rbp), %rdi
callq 0x30b0
jmp 0xc669
movl 0x6a59(%rip), %eax      # 0x130c8
andl $0x8, %eax
cmpl $0x0, %eax
je 0xc8dd
cmpq $0x0, 0x7ba5(%rip)      # 0x14228
jne 0xc68a
callq 0xc250
leaq 0x34cd(%rip), %rdi      # 0xfb5e
leaq 0x1c76(%rip), %rsi      # 0xe30e
callq 0x3270
movq %rax, -0x18(%rbp)
cmpq $0x0, %rax
je 0xc8db
jmp 0xc6ad
leaq -0x420(%rbp), %rdi
movq -0x18(%rbp), %rdx
movl $0x400, %esi            # imm = 0x400
callq 0x3180
cmpq $0x0, %rax
je 0xc8d2
leaq -0x420(%rbp), %rdi
movl $0x3a, %esi
callq 0x30e0
movq %rax, -0x428(%rbp)
cmpq $0x0, -0x428(%rbp)
jne 0xc6f0
jmp 0xc6ad
movq -0x428(%rbp), %rax
movb $0x0, (%rax)
movq -0x428(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x428(%rbp)
leaq -0x420(%rbp), %rax
movq %rax, -0x430(%rbp)
callq 0x3360
movq (%rax), %rax
movq -0x430(%rbp), %rcx
movsbl (%rcx), %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x2000, %eax           # imm = 0x2000
cmpl $0x0, %eax
je 0xc751
movq -0x430(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x430(%rbp)
jmp 0xc71a
movl $0x0, -0x48c(%rbp)
movl -0x48c(%rbp), %eax
cmpl 0x7ab9(%rip), %eax      # 0x14220
jge 0xc8cd
movq -0x430(%rbp), %rdi
movq 0x7aad(%rip), %rax      # 0x14228
movslq -0x48c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq (%rax), %rsi
callq 0x31b0
cmpl $0x0, %eax
jne 0xc8b7
movq -0x428(%rbp), %rdi
leaq 0x33f1(%rip), %rsi      # 0xfb99
leaq -0x498(%rbp), %rdx
leaq -0x4a0(%rbp), %rcx
leaq -0x4a8(%rbp), %r8
leaq -0x4b0(%rbp), %r9
leaq -0x4b8(%rbp), %r14
leaq -0x4c0(%rbp), %rbx
leaq -0x4c8(%rbp), %r11
leaq -0x4d0(%rbp), %r10
leaq -0x4d8(%rbp), %rax
movq %r14, (%rsp)
movq %rbx, 0x8(%rsp)
movq %r11, 0x10(%rsp)
movq %r10, 0x18(%rsp)
movq %rax, 0x20(%rsp)
movb $0x0, %al
callq 0x3230
cmpl $0x9, %eax
jne 0xc8b5
movq 0x7a12(%rip), %rax      # 0x14228
movslq -0x48c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rcx
movq 0x79f9(%rip), %rax      # 0x14228
movslq -0x48c(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rax
movq %rcx, 0x18(%rax)
movq -0x498(%rbp), %rcx
addq -0x4d8(%rbp), %rcx
movq 0x79d2(%rip), %rax      # 0x14228
movslq -0x48c(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rax
movq %rcx, 0x10(%rax)
movq 0x79b9(%rip), %rax      # 0x14228
movslq -0x48c(%rbp), %rcx
shlq $0x5, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rcx
movq 0x79a0(%rip), %rax      # 0x14228
movslq -0x48c(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rax
subq 0x18(%rax), %rcx
movq 0x7987(%rip), %rax      # 0x14228
movslq -0x48c(%rbp), %rdx
shlq $0x5, %rdx
addq %rdx, %rax
movq %rcx, 0x8(%rax)
jmp 0xc8b9
jmp 0xc8b7
jmp 0xc8b9
movl -0x48c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x48c(%rbp)
jmp 0xc75b
jmp 0xc6ad
movq -0x18(%rbp), %rdi
callq 0x30b0
jmp 0xc8dd
addq $0x4f0, %rsp            # imm = 0x4F0
popq %rbx
popq %r14
popq %rbp
retq
nopl (%rax)