// File: Stygain[P]bandwidth-monitor/src/main.cpp
// Function: initializeInterfaces()
void initializeInterfaces()
{
	FILE *fp = fopen("/proc/net/wireless", "r");
	char buf[200];
	char ifname[20];
	std::vector<char *> wirelessIfaceNames;
	
	if (fp != NULL)
	{
		// Skip first two lines
		for (int i = 0; i < 2; i++) {
			fgets(buf, 200, fp);
		}

		while (fgets(buf, 200, fp)) {
			char *wirelessInterfaceName = new char[20];
			sscanf(buf, "%s", wirelessInterfaceName);
			wirelessInterfaceName[strlen(wirelessInterfaceName) - 1] = '\0';
			wirelessIfaceNames.push_back(wirelessInterfaceName);
		}
	}
	for (size_t i = 0; i < wirelessIfaceNames.size(); ++i)
	{
		logger->Log("Wireless interface name: ");
		logger->Log(wirelessIfaceNames[i]);
		logger->Log("\n");
	}

	fp = fopen("/proc/net/dev", "r");

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

	while (fgets(buf, 200, fp)) {
		sscanf(buf, "%s", ifname);
		ifname[strlen(ifname) - 1] = '\0';
		logger->Log("Found iface name: ");
		logger->Log(ifname);
		logger->Log("\n");

		int len = strlen(ifname);
		if (len > longest)
		{
			longest = len;
		}
	}
	longest++;

	fclose(fp);


	fp = fopen("/proc/net/dev", "r");
	// skip first two lines
	for (int i = 0; i < 2; i++) {
		fgets(buf, 200, fp);
	}

	while (fgets(buf, 200, fp)) {
		sscanf(buf, "%s", ifname);
		ifname[strlen(ifname) - 1] = '\0';

		for (size_t i = 0; i < wirelessIfaceNames.size(); ++i)
		{
			if (strcmp(ifname, wirelessIfaceNames[i]) == 0)
			{
				interfaces.push_back(new Interface(ifname, longest, 1));
			}
			else
			{
				interfaces.push_back(new Interface(ifname, longest, 0));
			}
		}
	}

	wirelessIfaceNames.clear();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1d0, %rsp            # imm = 0x1D0
leaq 0x1274e(%rip), %rdi     # 0x160d0
leaq 0x12f05(%rip), %rsi     # 0x1688e
callq 0x33e0
movq %rax, -0x8(%rbp)
leaq -0x108(%rbp), %rdi
callq 0x9af0
cmpq $0x0, -0x8(%rbp)
je 0x3aa2
movl $0x0, -0x10c(%rbp)
cmpl $0x2, -0x10c(%rbp)
jge 0x39fd
movq -0x8(%rbp), %rdx
leaq -0xd0(%rbp), %rdi
movl $0xc8, %esi
callq 0x3520
jmp 0x39d3
jmp 0x39d5
movl -0x10c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x10c(%rbp)
jmp 0x39b3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x118(%rbp)
movl %eax, -0x11c(%rbp)
jmp 0x3f2c
jmp 0x39ff
movq -0x8(%rbp), %rdx
leaq -0xd0(%rbp), %rdi
movl $0xc8, %esi
callq 0x3520
movq %rax, -0x160(%rbp)
jmp 0x3a1d
movq -0x160(%rbp), %rax
cmpq $0x0, %rax
je 0x3aa0
movl $0x14, %edi
callq 0x3030
movq %rax, -0x168(%rbp)
jmp 0x3a3d
movq -0x168(%rbp), %rax
movq %rax, -0x128(%rbp)
movq -0x128(%rbp), %rdx
leaq 0x12c48(%rip), %rsi     # 0x166a1
xorl %eax, %eax
leaq -0xd0(%rbp), %rdi
callq 0x32f0
movq -0x128(%rbp), %rdi
movq %rdi, -0x170(%rbp)
callq 0x3190
movq -0x170(%rbp), %rcx
movb $0x0, -0x1(%rax,%rcx)
leaq -0x108(%rbp), %rdi
leaq -0x128(%rbp), %rsi
callq 0x9b10
jmp 0x3a9b
jmp 0x39ff
jmp 0x3aa2
movq $0x0, -0x130(%rbp)
movq -0x130(%rbp), %rax
movq %rax, -0x178(%rbp)
leaq -0x108(%rbp), %rdi
callq 0x9b80
movq %rax, %rcx
movq -0x178(%rbp), %rax
cmpq %rcx, %rax
jae 0x3b54
leaq 0x1a8eb(%rip), %rax     # 0x1e3c8
movq (%rax), %rdi
leaq 0x125fc(%rip), %rsi     # 0x160e3
callq 0x13910
jmp 0x3aee
leaq 0x1a8d3(%rip), %rax     # 0x1e3c8
movq (%rax), %rax
movq %rax, -0x180(%rbp)
movq -0x130(%rbp), %rsi
leaq -0x108(%rbp), %rdi
callq 0x9ba0
movq -0x180(%rbp), %rdi
movq (%rax), %rsi
callq 0x138e0
jmp 0x3b23
leaq 0x1a89e(%rip), %rax     # 0x1e3c8
movq (%rax), %rdi
leaq 0x1272f(%rip), %rsi     # 0x16263
callq 0x13910
jmp 0x3b3b
jmp 0x3b3d
movq -0x130(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x130(%rbp)
jmp 0x3aad
leaq 0x125a2(%rip), %rdi     # 0x160fd
leaq 0x12d2c(%rip), %rsi     # 0x1688e
callq 0x33e0
movq %rax, -0x188(%rbp)
jmp 0x3b70
movq -0x188(%rbp), %rax
movq %rax, -0x8(%rbp)
movl $0x0, -0x134(%rbp)
cmpl $0x2, -0x134(%rbp)
jge 0x3bb8
movq -0x8(%rbp), %rdx
leaq -0xd0(%rbp), %rdi
movl $0xc8, %esi
callq 0x3520
jmp 0x3ba5
jmp 0x3ba7
movl -0x134(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x134(%rbp)
jmp 0x3b85
jmp 0x3bba
movq -0x8(%rbp), %rdx
leaq -0xd0(%rbp), %rdi
movl $0xc8, %esi
callq 0x3520
movq %rax, -0x190(%rbp)
jmp 0x3bd8
movq -0x190(%rbp), %rax
cmpq $0x0, %rax
je 0x3c9d
leaq 0x12ab1(%rip), %rsi     # 0x166a1
xorl %eax, %eax
leaq -0xd0(%rbp), %rdi
leaq -0xf0(%rbp), %rdx
movq %rdx, -0x198(%rbp)
callq 0x32f0
movq -0x198(%rbp), %rdi
callq 0x3190
movq -0x198(%rbp), %rcx
movb $0x0, -0x1(%rax,%rcx)
leaq 0x1a79d(%rip), %rax     # 0x1e3c8
movq (%rax), %rdi
leaq 0x124d6(%rip), %rsi     # 0x1610b
callq 0x13910
jmp 0x3c3c
leaq 0x1a785(%rip), %rax     # 0x1e3c8
movq (%rax), %rdi
leaq -0xf0(%rbp), %rsi
callq 0x138e0
jmp 0x3c54
leaq 0x1a76d(%rip), %rax     # 0x1e3c8
movq (%rax), %rdi
leaq 0x125fe(%rip), %rsi     # 0x16263
callq 0x13910
jmp 0x3c6c
leaq -0xf0(%rbp), %rdi
callq 0x3190
movl %eax, -0x138(%rbp)
movl -0x138(%rbp), %eax
cmpl 0x1a6a6(%rip), %eax     # 0x1e330
jle 0x3c98
movl -0x138(%rbp), %eax
movl %eax, 0x1a698(%rip)     # 0x1e330
jmp 0x3bba
movl 0x1a68d(%rip), %eax     # 0x1e330
incl %eax
movl %eax, 0x1a685(%rip)     # 0x1e330
movq -0x8(%rbp), %rdi
callq 0x3310
jmp 0x3cb6
leaq 0x12440(%rip), %rdi     # 0x160fd
leaq 0x12bca(%rip), %rsi     # 0x1688e
callq 0x33e0
movq %rax, -0x1a0(%rbp)
jmp 0x3cd2
movq -0x1a0(%rbp), %rax
movq %rax, -0x8(%rbp)
movl $0x0, -0x13c(%rbp)
cmpl $0x2, -0x13c(%rbp)
jge 0x3d1a
movq -0x8(%rbp), %rdx
leaq -0xd0(%rbp), %rdi
movl $0xc8, %esi
callq 0x3520
jmp 0x3d07
jmp 0x3d09
movl -0x13c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x13c(%rbp)
jmp 0x3ce7
jmp 0x3d1c
movq -0x8(%rbp), %rdx
leaq -0xd0(%rbp), %rdi
movl $0xc8, %esi
callq 0x3520
movq %rax, -0x1a8(%rbp)
jmp 0x3d3a
movq -0x1a8(%rbp), %rax
cmpq $0x0, %rax
je 0x3f0b
leaq -0xd0(%rbp), %rdi
leaq -0xf0(%rbp), %rdx
leaq 0x12941(%rip), %rsi     # 0x166a1
movb $0x0, %al
callq 0x32f0
leaq -0xf0(%rbp), %rdi
callq 0x3190
subq $0x1, %rax
movb $0x0, -0xf0(%rbp,%rax)
movq $0x0, -0x148(%rbp)
movq -0x148(%rbp), %rax
movq %rax, -0x1b0(%rbp)
leaq -0x108(%rbp), %rdi
callq 0x9b80
movq %rax, %rcx
movq -0x1b0(%rbp), %rax
cmpq %rcx, %rax
jae 0x3f06
leaq -0xf0(%rbp), %rax
movq %rax, -0x1b8(%rbp)
movq -0x148(%rbp), %rsi
leaq -0x108(%rbp), %rdi
callq 0x9ba0
movq -0x1b8(%rbp), %rdi
movq (%rax), %rsi
callq 0x3460
cmpl $0x0, %eax
jne 0x3e73
movl $0xb8, %edi
callq 0x3320
movq %rax, -0x1c0(%rbp)
jmp 0x3e03
movq -0x1c0(%rbp), %rdi
movl 0x1a520(%rip), %edx     # 0x1e330
leaq -0xf0(%rbp), %rsi
movl $0x1, %ecx
callq 0x124e0
jmp 0x3e23
movq -0x1c0(%rbp), %rax
movq %rax, -0x150(%rbp)
leaq 0x1a538(%rip), %rdi     # 0x1e370
leaq -0x150(%rbp), %rsi
callq 0x9bc0
jmp 0x3e46
jmp 0x3eed
movq -0x1c0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x118(%rbp)
movl %eax, -0x11c(%rbp)
movl $0xb8, %esi
callq 0x3330
jmp 0x3f2c
movl $0xb8, %edi
callq 0x3320
movq %rax, -0x1c8(%rbp)
jmp 0x3e86
movq -0x1c8(%rbp), %rdi
movl 0x1a49d(%rip), %edx     # 0x1e330
leaq -0xf0(%rbp), %rsi
xorl %ecx, %ecx
callq 0x124e0
jmp 0x3ea3
movq -0x1c8(%rbp), %rax
movq %rax, -0x158(%rbp)
leaq 0x1a4b8(%rip), %rdi     # 0x1e370
leaq -0x158(%rbp), %rsi
callq 0x9bc0
jmp 0x3ec6
jmp 0x3eed
movq -0x1c8(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x118(%rbp)
movl %eax, -0x11c(%rbp)
movl $0xb8, %esi
callq 0x3330
jmp 0x3f2c
jmp 0x3eef
movq -0x148(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x148(%rbp)
jmp 0x3d8a
jmp 0x3d1c
leaq -0x108(%rbp), %rdi
callq 0x9bf0
leaq -0x108(%rbp), %rdi
callq 0x9c10
addq $0x1d0, %rsp            # imm = 0x1D0
popq %rbp
retq
leaq -0x108(%rbp), %rdi
callq 0x9c10
movq -0x118(%rbp), %rdi
callq 0x35c0
nopw %cs:(%rax,%rax)