// File: /workspace/github_cpp_makefile_star2_O2/Stygain[P]bandwidth-monitor/src/interface.cpp
// Function: Interface::Update()
void Interface::Update()
{
	extern Logger *logger;
	FILE *fp = fopen("/proc/net/dev", "r");
	char buf[200];
	char ifname[20];
	unsigned long int r_bytes;
	unsigned long int t_bytes;
	unsigned long int r_packets;
	unsigned long int t_packets;

	if (fp != NULL)
	{
		// skip first two lines
		for (int i = 0; i < 2; i++) {
			fgets(buf, 200, fp);
		}

		while (fgets(buf, 200, fp)) {
			sscanf(buf, "%s %lu %lu %*lu %*lu %*lu %*lu %*lu %*lu %lu %lu",
					ifname, &r_bytes, &r_packets, &t_bytes, &t_packets);
			ifname[strlen(ifname) - 1] = '\0';

			if (strcmp(ifname, this->name) == 0)
			{
				this->r_bytesLastLast = this->r_bytesLast;
				this->r_bytesLast = this->r_bytes;
				this->r_bytes = r_bytes;
				this->t_bytesLastLast = this->t_bytesLast;
				this->t_bytesLast = this->t_bytes;
				this->t_bytes = t_bytes;
				this->r_packetsLast = this->r_packets;
				this->r_packets = r_packets;
				this->t_packetsLast = this->t_packets;
				this->t_packets = t_packets;

				if (((this->r_bytes - this->r_bytesLast) + (this->r_bytesLast - this->r_bytesLastLast)) == 0)
				{
					this->r_bps = 0;
				}
				else
				{
					this->r_bps = (int)(((this->r_bytes - this->r_bytesLast) + (this->r_bytesLast - this->r_bytesLastLast)) / 2);
				}
				if (((this->t_bytes - this->t_bytesLast) + (this->t_bytesLast - this->t_bytesLastLast)) == 0)
				{
					this->t_bps = 0;
				}
				else
				{
					this->t_bps = (int)(((this->t_bytes - this->t_bytesLast) + (this->t_bytesLast - this->t_bytesLastLast)) / 2);
				}
				break;
			}
		}

		fclose(fp);
	}

	char ifaceType[] = "/sys/class/net/";
	strcat(ifaceType, this->name);
	strcat(ifaceType, "/operstate");
	fp = fopen(ifaceType, "r");

	logger->Log("Iface file: ");
	logger->Log(ifaceType);
	logger->Log("\n");
	if (fp != NULL)
	{
		fgets(buf, 200, fp);
		sscanf(buf, "%s", this->operstate);
		logger->Log("This operstate: ");
		logger->Log(this->operstate);
		logger->Log("\n");

		fclose(fp);
	}

	if (this->wireless)
	{
		fp = fopen("/proc/net/wireless", "r");

		if (fp != NULL)
		{
			logger->Log("In /proc/net/wireless\n");
			// Skip first two lines
			for (int i = 0; i < 2; i++) {
				fgets(buf, 200, fp);
			}

			int linkQual;
			int levelQual;
			int noiseQual;

			char wirelessInterfaceName[20];
			while (fgets(buf, 200, fp)) {
				sscanf(buf, "%s %*lu %d. %d. %d",
						wirelessInterfaceName, &linkQual, &levelQual, &noiseQual);
				wirelessInterfaceName[strlen(wirelessInterfaceName) - 1] = '\0';

				if (strcmp(wirelessInterfaceName, this->name) == 0)
				{
					logger->Log("Read: ");
					logger->Log(std::to_string(linkQual));
					logger->Log(", ");
					logger->Log(std::to_string(levelQual));
					logger->Log(", ");
					logger->Log(std::to_string(noiseQual));
					logger->Log("\n");
					this->linkQual = linkQual;
					this->levelQual = levelQual;
					this->noiseQual = noiseQual;
				}
			}
		}
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1c8, %rsp            # imm = 0x1C8
movq %rdi, %rbx
leaq 0x256e(%rip), %rdi      # 0xb15d
leaq 0x2d24(%rip), %rsi      # 0xb91a
callq 0x3360
testq %rax, %rax
je 0x8d15
movq %rax, %r14
pushq $0x2
popq %rbp
leaq 0x100(%rsp), %r15
subl $0x1, %ebp
jb 0x8c29
movq %r15, %rdi
movl $0xc8, %esi
movq %r14, %rdx
callq 0x34b0
jmp 0x8c12
leaq 0x100(%rsp), %r15
leaq 0xa0(%rsp), %r13
leaq 0x38(%rsp), %r12
leaq 0x40(%rsp), %rbp
movq %r15, %rdi
movl $0xc8, %esi
movq %r14, %rdx
callq 0x34b0
testq %rax, %rax
je 0x8d0d
leaq 0x30(%rsp), %rax
movq %rax, (%rsp)
movq %r15, %rdi
leaq 0x29e4(%rip), %rsi      # 0xb653
movq %r13, %rdx
leaq 0x48(%rsp), %rcx
movq %r12, %r8
movq %rbp, %r9
xorl %eax, %eax
callq 0x3290
movq %r13, %rdi
callq 0x3160
movb $0x0, -0x1(%rax,%r13)
movq (%rbx), %rsi
movq %r13, %rdi
callq 0x33f0
testl %eax, %eax
jne 0x8c43
movq 0x8(%rbx), %rax
movq 0x10(%rbx), %rcx
movq %rcx, 0x18(%rbx)
movq %rax, 0x10(%rbx)
movq 0x48(%rsp), %rax
movq %rax, 0x8(%rbx)
movq 0x30(%rbx), %rdx
movq %rdx, 0x38(%rbx)
movq 0x28(%rbx), %rsi
movq %rsi, 0x30(%rbx)
movq 0x40(%rsp), %rsi
movq %rsi, 0x28(%rbx)
movq 0x48(%rbx), %rdi
movq %rdi, 0x50(%rbx)
movq 0x38(%rsp), %rdi
movq %rdi, 0x48(%rbx)
movq 0x60(%rbx), %rdi
movq %rdi, 0x68(%rbx)
movq 0x30(%rsp), %rdi
movq %rdi, 0x60(%rbx)
subq %rcx, %rax
shrq %rax
movl %eax, 0x9c(%rbx)
subq %rdx, %rsi
shrq %rsi
movl %esi, 0xa0(%rbx)
movq %r14, %rdi
callq 0x32b0
movaps 0x2a64(%rip), %xmm0     # 0xb780
leaq 0x50(%rsp), %r15
movaps %xmm0, (%r15)
movq (%rbx), %rsi
movq %r15, %rdi
callq 0x3550
movq %r15, %rdi
callq 0x3160
movabsq $0x6174737265706f2f, %rcx # imm = 0x6174737265706F2F
movq %rcx, 0x50(%rsp,%rax)
movl $0x657461, 0x57(%rsp,%rax) # imm = 0x657461
leaq 0x2bc4(%rip), %rsi      # 0xb91a
movq %r15, %rdi
callq 0x3360
movq %rax, %r14
leaq 0x7610(%rip), %r13      # 0x10378
movq (%r13), %rdi
leaq 0x291c(%rip), %rsi      # 0xb68f
callq 0x97a6
movq (%r13), %rdi
movq %r15, %rsi
callq 0x9794
movq (%r13), %rdi
leaq 0x2534(%rip), %rsi      # 0xb2c3
callq 0x97a6
testq %r14, %r14
je 0x8dfd
leaq 0x100(%rsp), %r15
movq %r15, %rdi
movl $0xc8, %esi
movq %r14, %rdx
callq 0x34b0
leaq 0x78(%rbx), %r12
leaq 0x2962(%rip), %rsi      # 0xb71e
movq %r15, %rdi
movq %r12, %rdx
xorl %eax, %eax
callq 0x3290
movq (%r13), %rdi
leaq 0x28c8(%rip), %rsi      # 0xb69c
callq 0x97a6
movq (%r13), %rdi
movq %r12, %rsi
callq 0x9794
movq (%r13), %rdi
leaq 0x24d3(%rip), %rsi      # 0xb2c3
callq 0x97a6
movq %r14, %rdi
callq 0x32b0
cmpb $0x1, 0x8c(%rbx)
jne 0x8fdb
leaq 0x231f(%rip), %rdi      # 0xb130
leaq 0x2b02(%rip), %rsi      # 0xb91a
callq 0x3360
testq %rax, %rax
je 0x8fdb
movq %rax, %r14
movq (%r13), %rdi
leaq 0x2879(%rip), %rsi      # 0xb6ad
callq 0x97a6
pushq $0x2
popq %rbp
leaq 0x100(%rsp), %r15
subl $0x1, %ebp
jb 0x8e5b
movq %r15, %rdi
movl $0xc8, %esi
movq %r14, %rdx
callq 0x34b0
jmp 0x8e44
leaq 0x100(%rsp), %r12
leaq 0x285a(%rip), %r15      # 0xb6c4
leaq 0x60(%rsp), %rbp
leaq 0x80(%rsp), %rax
movq %rax, 0x20(%rsp)
movq %r14, 0x28(%rsp)
movq %r12, %rdi
movl $0xc8, %esi
movq %r14, %rdx
callq 0x34b0
testq %rax, %rax
je 0x8fdb
movq %r12, %rdi
movq %r15, %rsi
movq %rbp, %rdx
leaq 0x1c(%rsp), %rcx
leaq 0x18(%rsp), %r8
leaq 0x14(%rsp), %r9
xorl %eax, %eax
callq 0x3290
movq %rbp, %rdi
callq 0x3160
movb $0x0, -0x1(%rax,%rbp)
movq (%rbx), %rsi
movq %rbp, %rdi
callq 0x33f0
testl %eax, %eax
jne 0x8e81
movq %rbx, %r14
movq %r12, %r15
movq (%r13), %rdi
leaq 0x27f1(%rip), %rsi      # 0xb6d7
callq 0x97a6
movq (%r13), %r12
movl 0x1c(%rsp), %esi
leaq 0x80(%rsp), %rbx
movq %rbx, %rdi
callq 0x6ce9
movq %r12, %rdi
movq %rbx, %rsi
callq 0x97b8
movq %rbx, %rdi
callq 0x3170
movq (%r13), %rdi
leaq 0x27bd(%rip), %rbp      # 0xb6de
movq %rbp, %rsi
callq 0x97a6
movq (%r13), %r12
movl 0x18(%rsp), %esi
leaq 0xe0(%rsp), %rbx
movq %rbx, %rdi
callq 0x6ce9
movq %r12, %rdi
movq %rbx, %rsi
callq 0x97b8
movq %rbx, %rdi
callq 0x3170
movq (%r13), %rdi
movq %rbp, %rsi
callq 0x97a6
movq (%r13), %r12
movl 0x14(%rsp), %esi
leaq 0xc0(%rsp), %rbx
movq %rbx, %rdi
callq 0x6ce9
movq %r12, %rdi
movq %rbx, %r12
movq %rbx, %rsi
callq 0x97b8
movq %r14, %rbx
movq %r12, %rdi
callq 0x3170
movq (%r13), %rdi
leaq 0x2327(%rip), %rsi      # 0xb2c3
callq 0x97a6
movl 0x1c(%rsp), %eax
movl %eax, 0x90(%r14)
movl 0x18(%rsp), %eax
movl %eax, 0x94(%r14)
movl 0x14(%rsp), %eax
movl %eax, 0x98(%r14)
movq %r15, %r12
movq 0x28(%rsp), %r14
leaq 0x26f3(%rip), %r15      # 0xb6c4
leaq 0x60(%rsp), %rbp
jmp 0x8e81
addq $0x1c8, %rsp            # imm = 0x1C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rbx, %rcx
movq %rax, %rbx
movq %rcx, 0x20(%rsp)
jmp 0x9007
movq %rax, %rbx
jmp 0x9007
movq %rax, %rbx
movq %r12, 0x20(%rsp)
movq 0x20(%rsp), %rdi
callq 0x3170
movq %rbx, %rdi
callq 0x3540
nop