// File: mukul-mehta[P]meta/builtins.cc
// Function: metash_fetch(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>>>)
int metash_fetch(unused vector<string> tokens) {
    // Fetch username, hostname and OS Name
    unused string username = getUsername();
    unused string hostname = getHostname();
    unused string OSname = getOSName();

    string kernel_name, kernel_version, machine;

    long uptime = -1;
    long total_memory = -1;
    long free_memory = -1;
    long consumed_memory = -1;

    struct utsname uts;
    // The `uname` call populates the uts struct with system information. Also implemented as the `uname` executable
    if (uname(&uts) < 0) {
        perror("uname error");
    } else {
        kernel_name = uts.sysname;
        kernel_version = uts.release;
        machine = uts.machine;
    }

    struct sysinfo info;
    // The `sysinfo` call populates the `sysinfo` struct. Fills with uptime and memory usage information
    if (sysinfo(&info) < 0)
        perror("sysinfo() error");
    else {
        total_memory = info.totalram;
        free_memory = info.freeram;
        consumed_memory = total_memory - free_memory;
        uptime = info.uptime;
    }

    char* user_host_string = (char*)malloc(BUFSIZE * sizeof(char));
    sprintf(user_host_string, "%s%s@%s%s\n", BLUE, username.c_str(), hostname.c_str(), NORM);
    size_t len = username.size() + hostname.size();

    printf("%s", user_host_string);
    for (size_t i = 0; i <= len; i++)
        printf("-");
    printf("\n");

    printf("%sOS%s:       %s\n", BLUE, NORM, OSname.c_str());
    printf("%sKernel%s:   %s %s\n", BLUE, NORM, kernel_name.c_str(), kernel_version.c_str());
    printf("%sPlatform%s: %s\n", BLUE, NORM, machine.c_str());
    printf("%sMemory%s:   %s / %s\n", BLUE, NORM, parse_memory(consumed_memory),
           parse_memory(total_memory));
    printf("%sUptime%s:   %s\n", BLUE, NORM, parse_time(uptime));
    printf("%sShell%s:    %s %s\n", BLUE, NORM, SHELL, VERSION);
    printf("%sAuthors%s:  %s\n", BLUE, NORM, AUTHORS);

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x360, %rsp            # imm = 0x360
movq %rdi, -0x8(%rbp)
leaq -0x28(%rbp), %rdi
callq 0x89c0
leaq -0x48(%rbp), %rdi
callq 0x8a70
jmp 0x9243
leaq -0x78(%rbp), %rdi
callq 0x8b10
jmp 0x924e
leaq -0x98(%rbp), %rdi
callq 0x34a0
leaq -0xb8(%rbp), %rdi
callq 0x34a0
leaq -0xd8(%rbp), %rdi
callq 0x34a0
movq $-0x1, -0xe0(%rbp)
movq $-0x1, -0xe8(%rbp)
movq $-0x1, -0xf0(%rbp)
movq $-0x1, -0xf8(%rbp)
leaq -0x27e(%rbp), %rdi
callq 0x3070
cmpl $0x0, %eax
jge 0x9322
leaq 0x25d1(%rip), %rdi      # 0xb887
callq 0x32a0
jmp 0x92bd
jmp 0x9363
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x96ad
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x96a4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0xd8(%rbp), %rdi
callq 0x31e0
leaq -0xb8(%rbp), %rdi
callq 0x31e0
leaq -0x98(%rbp), %rdi
callq 0x31e0
leaq -0x78(%rbp), %rdi
callq 0x31e0
jmp 0x96a4
leaq -0x98(%rbp), %rdi
leaq -0x27e(%rbp), %rsi
callq 0x3470
jmp 0x9337
leaq -0x1fc(%rbp), %rsi
leaq -0xb8(%rbp), %rdi
callq 0x3470
jmp 0x934c
leaq -0x17a(%rbp), %rsi
leaq -0xd8(%rbp), %rdi
callq 0x3470
jmp 0x9361
jmp 0x9363
leaq -0x2f0(%rbp), %rdi
callq 0x3440
cmpl $0x0, %eax
jge 0x9384
leaq 0x2518(%rip), %rdi      # 0xb893
callq 0x32a0
jmp 0x9382
jmp 0x93c3
movq -0x2d0(%rbp), %rax
movq %rax, -0xe8(%rbp)
movq -0x2c8(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq -0xe8(%rbp), %rax
subq -0xf0(%rbp), %rax
movq %rax, -0xf8(%rbp)
movq -0x2f0(%rbp), %rax
movq %rax, -0xe0(%rbp)
movl $0x1000, %edi           # imm = 0x1000
callq 0x3460
movq %rax, -0x2f8(%rbp)
movq -0x2f8(%rbp), %rax
movq %rax, -0x338(%rbp)
leaq -0x28(%rbp), %rdi
movq %rdi, -0x328(%rbp)
callq 0x3120
movq %rax, -0x330(%rbp)
leaq -0x48(%rbp), %rdi
movq %rdi, -0x320(%rbp)
callq 0x3120
movq -0x338(%rbp), %rdi
movq -0x330(%rbp), %rcx
movq %rax, %r8
leaq 0x2482(%rip), %rsi      # 0xb8a3
leaq 0x2353(%rip), %rdx      # 0xb77b
leaq 0x1d83(%rip), %r9       # 0xb1b2
xorl %eax, %eax
movb %al, -0x309(%rbp)
callq 0x3090
movq -0x328(%rbp), %rdi
callq 0x3200
movq -0x320(%rbp), %rdi
movq %rax, -0x318(%rbp)
callq 0x3200
movq -0x318(%rbp), %rcx
movq %rax, %rdx
movb -0x309(%rbp), %al
addq %rdx, %rcx
movq %rcx, -0x300(%rbp)
movq -0x2f8(%rbp), %rsi
leaq 0x1eb5(%rip), %rdi      # 0xb338
callq 0x3040
jmp 0x948a
movq $0x0, -0x308(%rbp)
movq -0x308(%rbp), %rax
cmpq -0x300(%rbp), %rax
ja 0x94cb
leaq 0x2402(%rip), %rdi      # 0xb8ae
xorl %eax, %eax
callq 0x3040
jmp 0x94b5
jmp 0x94b7
movq -0x308(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x308(%rbp)
jmp 0x9495
leaq 0x23ee(%rip), %rdi      # 0xb8c0
xorl %eax, %eax
callq 0x3040
jmp 0x94db
leaq -0x78(%rbp), %rdi
callq 0x3120
movq %rax, %rcx
leaq 0x23c2(%rip), %rdi      # 0xb8b0
leaq 0x2286(%rip), %rsi      # 0xb77b
leaq 0x1cb6(%rip), %rdx      # 0xb1b2
xorl %eax, %eax
callq 0x3040
jmp 0x9505
leaq -0x98(%rbp), %rdi
callq 0x3120
movq %rax, -0x340(%rbp)
leaq -0xb8(%rbp), %rdi
callq 0x3120
movq -0x340(%rbp), %rcx
movq %rax, %r8
leaq 0x238d(%rip), %rdi      # 0xb8c2
leaq 0x223f(%rip), %rsi      # 0xb77b
leaq 0x1c6f(%rip), %rdx      # 0xb1b2
xorl %eax, %eax
callq 0x3040
jmp 0x954c
leaq -0xd8(%rbp), %rdi
callq 0x3120
movq %rax, %rcx
leaq 0x2375(%rip), %rdi      # 0xb8d7
leaq 0x2212(%rip), %rsi      # 0xb77b
leaq 0x1c42(%rip), %rdx      # 0xb1b2
xorl %eax, %eax
callq 0x3040
jmp 0x9579
movq -0xf8(%rbp), %rdi
callq 0x8760
movq %rax, -0x348(%rbp)
jmp 0x958e
movq -0xe8(%rbp), %rdi
callq 0x8760
movq %rax, -0x350(%rbp)
jmp 0x95a3
movq -0x350(%rbp), %r8
movq -0x348(%rbp), %rcx
leaq 0x2331(%rip), %rdi      # 0xb8e9
leaq 0x21bc(%rip), %rsi      # 0xb77b
leaq 0x1bec(%rip), %rdx      # 0xb1b2
xorl %eax, %eax
callq 0x3040
jmp 0x95cf
movq -0xe0(%rbp), %rdi
callq 0x8820
movq %rax, -0x358(%rbp)
jmp 0x95e4
movq -0x358(%rbp), %rcx
leaq 0x230e(%rip), %rdi      # 0xb900
leaq 0x2182(%rip), %rsi      # 0xb77b
leaq 0x1bb2(%rip), %rdx      # 0xb1b2
xorl %eax, %eax
callq 0x3040
jmp 0x9609
leaq 0x2302(%rip), %rdi      # 0xb912
leaq 0x2164(%rip), %rsi      # 0xb77b
leaq 0x1b94(%rip), %rdx      # 0xb1b2
leaq 0x2302(%rip), %rcx      # 0xb927
leaq 0x2303(%rip), %r8       # 0xb92f
xorl %eax, %eax
callq 0x3040
jmp 0x9635
leaq 0x22f7(%rip), %rdi      # 0xb933
leaq 0x2138(%rip), %rsi      # 0xb77b
leaq 0x1b68(%rip), %rdx      # 0xb1b2
leaq 0x22f4(%rip), %rcx      # 0xb945
xorl %eax, %eax
callq 0x3040
jmp 0x965a
leaq -0xd8(%rbp), %rdi
callq 0x31e0
leaq -0xb8(%rbp), %rdi
callq 0x31e0
leaq -0x98(%rbp), %rdi
callq 0x31e0
leaq -0x78(%rbp), %rdi
callq 0x31e0
leaq -0x48(%rbp), %rdi
callq 0x31e0
leaq -0x28(%rbp), %rdi
callq 0x31e0
xorl %eax, %eax
addq $0x360, %rsp            # imm = 0x360
popq %rbp
retq
leaq -0x48(%rbp), %rdi
callq 0x31e0
leaq -0x28(%rbp), %rdi
callq 0x31e0
movq -0x50(%rbp), %rdi
callq 0x3570
nop