// File: /workspace/github_cpp_makefile_star2_O3/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 $0x1d8, %rsp            # imm = 0x1D8
movq %rdi, %r12
leaq 0x2ad4(%rip), %rdi      # 0xc15d
leaq 0x328a(%rip), %rsi      # 0xc91a
callq 0x3370
testq %rax, %rax
movq %r12, 0x20(%rsp)
je 0x97c6
movq %rax, %r14
leaq 0x110(%rsp), %r15
movq %r15, %rdi
movl $0xc8, %esi
movq %rax, %rdx
callq 0x34b0
movq %r15, %rdi
movl $0xc8, %esi
movq %r14, %rdx
callq 0x34b0
leaq 0xf0(%rsp), %r13
leaq 0x78(%rsp), %rbp
leaq 0x68(%rsp), %rbx
leaq 0x70(%rsp), %r12
movq %r15, %rdi
movl $0xc8, %esi
movq %r14, %rdx
callq 0x34b0
testq %rax, %rax
je 0x97b9
leaq 0x60(%rsp), %rax
movq %rax, (%rsp)
movq %r15, %rdi
leaq 0x2f42(%rip), %rsi      # 0xc653
movq %r13, %rdx
movq %rbp, %rcx
movq %rbx, %r8
movq %r12, %r9
xorl %eax, %eax
callq 0x32a0
movq %r13, %rdi
callq 0x3170
movb $0x0, -0x1(%rax,%r13)
movq 0x20(%rsp), %rax
movq (%rax), %rsi
movq %r13, %rdi
callq 0x33f0
testl %eax, %eax
jne 0x96e5
movq 0x20(%rsp), %r8
movq 0x8(%r8), %rax
movq 0x10(%r8), %rcx
movq %rcx, 0x18(%r8)
movq %rax, 0x10(%r8)
movq 0x78(%rsp), %rax
movq %rax, 0x8(%r8)
movq 0x30(%r8), %rdx
movq %rdx, 0x38(%r8)
movq 0x28(%r8), %rsi
movq %rsi, 0x30(%r8)
movq 0x70(%rsp), %rsi
movq %rsi, 0x28(%r8)
movq 0x48(%r8), %rdi
movq %rdi, 0x50(%r8)
movq 0x68(%rsp), %rdi
movq %rdi, 0x48(%r8)
movq 0x60(%r8), %rdi
movq %rdi, 0x68(%r8)
movq 0x60(%rsp), %rdi
movq %rdi, 0x60(%r8)
subq %rcx, %rax
shrq %rax
movl %eax, 0x9c(%r8)
subq %rdx, %rsi
shrq %rsi
movl %esi, 0xa0(%r8)
movq %r14, %rdi
callq 0x32b0
movq 0x20(%rsp), %r12
movaps 0x2fb3(%rip), %xmm0     # 0xc780
leaq 0xc0(%rsp), %rbx
movaps %xmm0, (%rbx)
movq (%r12), %rsi
movq %rbx, %rdi
callq 0x3560
movq %rbx, %rdi
callq 0x3170
movabsq $0x6174737265706f2f, %rcx # imm = 0x6174737265706F2F
movq %rcx, 0xc0(%rsp,%rax)
movl $0x657461, 0xc7(%rsp,%rax) # imm = 0x657461
leaq 0x310a(%rip), %rsi      # 0xc91a
movq %rbx, %rdi
callq 0x3370
movq %rax, %r14
leaq 0x6b5e(%rip), %r13      # 0x10380
movq (%r13), %rdi
leaq 0x2e62(%rip), %rsi      # 0xc68f
callq 0xa57c
movq (%r13), %rdi
movq %rbx, %rsi
callq 0xa52e
movq (%r13), %rdi
leaq 0x2a7a(%rip), %rsi      # 0xc2c3
callq 0xa57c
testq %r14, %r14
je 0x98b8
leaq 0x110(%rsp), %rbx
movq %rbx, %rdi
movl $0xc8, %esi
movq %r14, %rdx
callq 0x34b0
leaq 0x78(%r12), %r15
leaq 0x2ea7(%rip), %rsi      # 0xc71e
movq %rbx, %rdi
movq %r15, %rdx
xorl %eax, %eax
callq 0x32a0
movq (%r13), %rdi
leaq 0x2e0d(%rip), %rsi      # 0xc69c
callq 0xa57c
movq (%r13), %rdi
movq %r15, %rsi
callq 0xa52e
movq (%r13), %rdi
leaq 0x2a18(%rip), %rsi      # 0xc2c3
callq 0xa57c
movq %r14, %rdi
callq 0x32b0
cmpb $0x1, 0x8c(%r12)
jne 0x9ca0
leaq 0x2862(%rip), %rdi      # 0xc130
leaq 0x3045(%rip), %rsi      # 0xc91a
callq 0x3370
testq %rax, %rax
je 0x9ca0
movq %rax, %r15
movq (%r13), %rdi
leaq 0x2dbc(%rip), %rsi      # 0xc6ad
callq 0xa57c
leaq 0x110(%rsp), %rbx
movq %rbx, %rdi
movl $0xc8, %esi
movq %r15, %rdx
callq 0x34b0
movq %rbx, %rdi
movl $0xc8, %esi
movq %r15, %rdx
callq 0x34b0
movq %rbx, %rdi
movl $0xc8, %esi
movq %r15, %rdx
callq 0x34b0
testq %rax, %rax
je 0x9ca0
leaq 0x40(%rsp), %rax
movq %rax, 0x28(%rsp)
leaq 0x2d7c(%rip), %r14      # 0xc6c4
leaq 0x110(%rsp), %rbp
movq %r15, 0x50(%rsp)
movq %rbp, %rdi
movq %r14, %rsi
leaq 0xd0(%rsp), %rbx
movq %rbx, %rdx
leaq 0x1c(%rsp), %rcx
leaq 0x18(%rsp), %r8
leaq 0x14(%rsp), %r9
xorl %eax, %eax
callq 0x32a0
movq %rbx, %rdi
callq 0x3170
movb $0x0, -0x1(%rax,%rbx)
movq (%r12), %rsi
movq %rbx, %rdi
callq 0x33f0
testl %eax, %eax
jne 0x9c87
movq (%r13), %rdi
leaq 0x2d2f(%rip), %rsi      # 0xc6d7
callq 0xa57c
movl 0x1c(%rsp), %r12d
movl %r12d, %r15d
negl %r15d
cmovsl %r12d, %r15d
movl $0x1, %ebp
cmpl $0xa, %r15d
leaq 0x40(%rsp), %r13
leaq 0xb0(%rsp), %r14
jb 0x9a16
movl $0x4, %ebp
movl %r15d, %eax
movl $0xd1b71759, %esi       # imm = 0xD1B71759
cmpl $0x63, %eax
jbe 0x9a0f
cmpl $0x3e7, %eax            # imm = 0x3E7
jbe 0x9a14
cmpl $0x2710, %eax           # imm = 0x2710
jb 0x9a16
movl %eax, %ecx
imulq %rsi, %rcx
shrq $0x2d, %rcx
addl $0x4, %ebp
cmpl $0x1869f, %eax          # imm = 0x1869F
movl %ecx, %eax
ja 0x99e1
addl $-0x3, %ebp
jmp 0x9a16
addl $-0x2, %ebp
jmp 0x9a16
decl %ebp
leaq 0x6963(%rip), %rax      # 0x10380
movq (%rax), %rbx
shrl $0x1f, %r12d
movl %r12d, %esi
addl %ebp, %esi
movq %r13, 0x30(%rsp)
leaq 0x30(%rsp), %rdi
movl $0x2d, %edx
callq 0x3340
movq 0x30(%rsp), %rdi
addq %r12, %rdi
movl %ebp, %esi
movl %r15d, %edx
callq 0x79ca
movq %rbx, %rdi
leaq 0x30(%rsp), %rsi
callq 0xa5ca
movq 0x30(%rsp), %rdi
cmpq %r13, %rdi
je 0x9a73
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x32d0
leaq 0x6906(%rip), %rbx      # 0x10380
movq (%rbx), %rdi
leaq 0x2c5a(%rip), %rsi      # 0xc6de
callq 0xa57c
movq (%rbx), %rax
movq %rax, 0x58(%rsp)
movl 0x18(%rsp), %ebx
movl %ebx, %ebp
negl %ebp
cmovsl %ebx, %ebp
movl $0x1, %r12d
cmpl $0xa, %ebp
leaq 0x90(%rsp), %r15
jb 0x9af5
movl $0x4, %r12d
movl %ebp, %eax
movl $0xd1b71759, %edx       # imm = 0xD1B71759
cmpl $0x63, %eax
jbe 0x9aec
cmpl $0x3e7, %eax            # imm = 0x3E7
jbe 0x9af2
cmpl $0x2710, %eax           # imm = 0x2710
jb 0x9af5
movl %eax, %ecx
imulq %rdx, %rcx
shrq $0x2d, %rcx
addl $0x4, %r12d
cmpl $0x1869f, %eax          # imm = 0x1869F
movl %ecx, %eax
ja 0x9abc
addl $-0x3, %r12d
jmp 0x9af5
addl $-0x2, %r12d
jmp 0x9af5
decl %r12d
shrl $0x1f, %ebx
movl %ebx, %esi
addl %r12d, %esi
movq %r14, 0xa0(%rsp)
movq %r14, %r13
leaq 0xa0(%rsp), %r14
movq %r14, %rdi
movl $0x2d, %edx
callq 0x3340
movq 0xa0(%rsp), %rdi
addq %rbx, %rdi
movl %r12d, %esi
movl %ebp, %edx
callq 0x79ca
movq 0x58(%rsp), %rdi
movq %r14, %rsi
callq 0xa5ca
movq 0xa0(%rsp), %rdi
cmpq %r13, %rdi
leaq 0x682f(%rip), %rbx      # 0x10380
je 0x9b63
movq 0xb0(%rsp), %rsi
incq %rsi
callq 0x32d0
movq (%rbx), %rdi
leaq 0x2b71(%rip), %rsi      # 0xc6de
callq 0xa57c
movq (%rbx), %r13
movl 0x14(%rsp), %ebx
movl %ebx, %ebp
negl %ebp
cmovsl %ebx, %ebp
movl $0x1, %r12d
cmpl $0xa, %ebp
jb 0x9bd1
movl $0x4, %r12d
movl %ebp, %eax
movl $0xd1b71759, %edx       # imm = 0xD1B71759
cmpl $0x63, %eax
jbe 0x9bc8
cmpl $0x3e7, %eax            # imm = 0x3E7
jbe 0x9bce
cmpl $0x2710, %eax           # imm = 0x2710
jb 0x9bd1
movl %eax, %ecx
imulq %rdx, %rcx
shrq $0x2d, %rcx
addl $0x4, %r12d
cmpl $0x1869f, %eax          # imm = 0x1869F
movl %ecx, %eax
ja 0x9b98
addl $-0x3, %r12d
jmp 0x9bd1
addl $-0x2, %r12d
jmp 0x9bd1
decl %r12d
shrl $0x1f, %ebx
movl %ebx, %esi
addl %r12d, %esi
movq %r15, 0x80(%rsp)
leaq 0x80(%rsp), %r14
movq %r14, %rdi
movl $0x2d, %edx
callq 0x3340
movq 0x80(%rsp), %rdi
addq %rbx, %rdi
movl %r12d, %esi
movl %ebp, %edx
callq 0x79ca
movq %r13, %rdi
movq %r14, %rsi
callq 0xa5ca
movq 0x80(%rsp), %rdi
cmpq %r15, %rdi
movq 0x20(%rsp), %r12
leaq 0x6753(%rip), %r13      # 0x10380
movq 0x50(%rsp), %r15
leaq 0x2a8b(%rip), %r14      # 0xc6c4
leaq 0x110(%rsp), %rbp
je 0x9c53
movq 0x90(%rsp), %rsi
incq %rsi
callq 0x32d0
movq (%r13), %rdi
leaq 0x2665(%rip), %rsi      # 0xc2c3
callq 0xa57c
movl 0x1c(%rsp), %eax
movl %eax, 0x90(%r12)
movl 0x18(%rsp), %eax
movl %eax, 0x94(%r12)
movl 0x14(%rsp), %eax
movl %eax, 0x98(%r12)
movq %rbp, %rdi
movl $0xc8, %esi
movq %r15, %rdx
callq 0x34b0
testq %rax, %rax
jne 0x9955
addq $0x1d8, %rsp            # imm = 0x1D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, %rbx
movq 0xa0(%rsp), %rdi
movq %r13, 0x28(%rsp)
jmp 0x9ccc
movq %rax, %rbx
movq 0x30(%rsp), %rdi
cmpq %r13, %rdi
je 0x9ce1
movq 0x28(%rsp), %rax
movq (%rax), %rsi
incq %rsi
callq 0x32d0
movq %rbx, %rdi
callq 0x3550
movq %rax, %rbx
movq 0x80(%rsp), %rdi
movq %r15, 0x28(%rsp)
cmpq %r15, %rdi
jne 0x9cd1
jmp 0x9ce1