// File: /workspace/github_c_makefile_star2_O1/agraf[P]kvmbench/numa.c
// Function: main
int main(int argc, char **argv)
{
    int childpid[NR_CHILDREN];
    int pipefd[2];
    int i;
    uint64_t all_bytes = 0;
    int numa_node = 0;
    int numa_max = numa_max_node() + 1;
    int opt_aligned = 1;
    int opt_numactl = 1;
    int opt_rotate = 1;

    for (i = 1; i < argc; i++) {
        if (!strcmp(argv[i], "-u")) {
            opt_aligned = 0;
        } else if (!strcmp(argv[i], "-N")) {
            opt_numactl = 0;
        } else if (!strcmp(argv[i], "-0")) {
            opt_rotate = 0;
        } else {
            printf("Syntax: %s\n\n", argv[0]);
            printf("  -u    Misalign NUMA nodes for reader/writer\n");
            printf("  -N    Disable NUMA awareness\n");
            printf("  -0    Always use NUMA node 0\n");
            printf("\n");
            exit(0);
        }
    }

    /*
     * We create reader and writer processes. Both are connected via pipes.
     * Writers keep writing 100MB chunks of garbage to the pipe. Readers read
     * that garbage and add up the number of bytes read.
     *
     * To account to full number of bytes read at the end, account them in
     * a shared memory range.
     */

    results = mmap(NULL, sizeof(*results) * NR_CHILDREN, PROT_READ | PROT_WRITE, 
                    MAP_SHARED | MAP_ANONYMOUS, -1, 0);

    printf("Benchmarking message passing on %d NUMA nodes ...\n", numa_max);
    if (opt_numactl) {
        printf("  NUMA aware:        yes\n");
        printf("  NUMA aligned:      %s\n", opt_aligned ? "yes" : "no");
        printf("  NUMA nodes used:   0-%d\n", opt_rotate ? numa_max-1 : 0);
    } else {
        printf("  NUMA aware:        no\n");
    }
    printf("\n");

    for (i = 0; i < NR_CHILDREN; i++) {
        int cpid;
        int new_pair = ((i & 1) == CHILD_READER);

        if (new_pair) {
            if (pipe(pipefd)) {
                printf("Error while creating pipe: %s\n", strerror(errno));
                exit_all(childpid);
            }

            /*
             * Move to the next NUMA node, so we pin reader and writer to
             * the same node
             */
            numa_node++;
            numa_node %= numa_max;
        }

        if (!opt_rotate) {
            numa_node = 0;
        }

        cpid = fork();
        if (!cpid) {
            /*
             * We're a child now, figure out what we should do, but
             * first pin us to the numa node.
             */

            if (opt_numactl) {
                numa_set_preferred(numa_node);
                numa_run_on_node(numa_node);
            }

            results[i].node = numa_node_of_cpu(sched_getcpu());

            if ((i & 1) == CHILD_READER) {
                /* Reader */
                close(pipefd[CHILD_WRITER]);
                child_reader(pipefd[CHILD_READER], &results[i].bytes);
            } else {
                /* Writer */
                close(pipefd[CHILD_READER]);
                child_writer(pipefd[CHILD_WRITER]);
            }
        } else {
            /* Parent, remember child pid */
            childpid[i] = cpid;

            if (!opt_aligned) {
                numa_node++;
                numa_node %= numa_max;
            }
        }
    }

    /* Wait n seconds */
    sleep(RUN_SECS);

    for (i = 0; i < NR_CHILDREN; i++) {
        all_bytes += results[i].bytes;
    }

    printf("Total MB/s read: %ld\n", all_bytes / MB / RUN_SECS);

    /* Then quit */
    exit_all(childpid);

    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1b8, %rsp            # imm = 0x1B8
movq %rsi, %r14
movl %edi, %ebp
callq 0x10d0
movl %eax, %r15d
cmpl $0x2, %ebp
jl 0x1381
movq %r15, 0x18(%rsp)
movl %ebp, %ebx
movl $0x1, 0x8(%rsp)
movl $0x1, %r13d
leaq 0xd0d(%rip), %r15       # 0x2004
movl $0x1, 0xc(%rsp)
movl $0x1, %r12d
movq (%r14,%r13,8), %rbp
movq %rbp, %rdi
movq %r15, %rsi
callq 0x10e0
testl %eax, %eax
je 0x134c
movq %rbp, %rdi
leaq 0xce5(%rip), %rsi       # 0x2007
callq 0x10e0
testl %eax, %eax
je 0x1351
movq %rbp, %rdi
leaq 0xcd5(%rip), %rsi       # 0x200a
callq 0x10e0
testl %eax, %eax
jne 0x15b4
movl $0x0, 0x8(%rsp)
jmp 0x1359
xorl %r12d, %r12d
jmp 0x1359
movl $0x0, 0xc(%rsp)
incq %r13
cmpq %r13, %rbx
jne 0x1305
cmpl $0x0, 0xc(%rsp)
sete %r14b
testl %r12d, %r12d
sete %r12b
cmpl $0x0, 0x8(%rsp)
sete %r13b
movq 0x18(%rsp), %r15
jmp 0x138a
xorl %r12d, %r12d
xorl %r14d, %r14d
xorl %r13d, %r13d
leal 0x1(%r15), %ebp
xorl %ebx, %ebx
movl $0x1900, %esi           # imm = 0x1900
xorl %edi, %edi
movl $0x3, %edx
movl $0x21, %ecx
movl $0xffffffff, %r8d       # imm = 0xFFFFFFFF
xorl %r9d, %r9d
callq 0x1070
movq %rax, 0x2d2a(%rip)      # 0x40e0
leaq 0xc5d(%rip), %rdi       # 0x201a
movl %ebp, %esi
xorl %eax, %eax
callq 0x1080
testb %r14b, %r14b
movl %r14d, 0x8(%rsp)
je 0x13de
leaq 0xcdf(%rip), %rdi       # 0x20b6
callq 0x1050
jmp 0x1425
leaq 0xce9(%rip), %rdi       # 0x20ce
callq 0x1050
leaq 0xcda(%rip), %rax       # 0x20cb
leaq 0xceb(%rip), %rsi       # 0x20e3
testb %r12b, %r12b
cmovneq %rax, %rsi
leaq 0xc47(%rip), %rdi       # 0x204d
xorl %eax, %eax
callq 0x1080
testb %r13b, %r13b
cmovnel %ebx, %r15d
leaq 0xc4b(%rip), %rdi       # 0x2066
movl %r15d, %esi
xorl %eax, %eax
callq 0x1080
movl $0xa, %edi
callq 0x1030
xorl %r15d, %r15d
xorl %r14d, %r14d
xorl %ebx, %ebx
testb $0x1, %r14b
jne 0x1458
leaq 0x10(%rsp), %rdi
callq 0x10b0
testl %eax, %eax
jne 0x158d
incl %ebx
movl %ebx, %eax
cltd
idivl %ebp
movl %edx, %ebx
testb %r13b, %r13b
movl $0x0, %eax
cmovnel %eax, %ebx
callq 0x1180
testl %eax, %eax
je 0x14d2
movl %eax, 0x20(%rsp,%r14,4)
testb %r12b, %r12b
je 0x147f
incl %ebx
movl %ebx, %eax
cltd
idivl %ebp
movl %edx, %ebx
incq %r14
addq $0x40, %r15
cmpq $0x64, %r14
jne 0x1437
movl $0x5, %edi
callq 0x1170
xorl %ecx, %ecx
movq 0x2c41(%rip), %rdx      # 0x40e0
xorl %eax, %eax
addq (%rdx,%rcx), %rax
addq $0x40, %rcx
cmpq $0x1900, %rcx           # imm = 0x1900
jne 0x14a1
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
mulq %rcx
shrq $0x16, %rdx
leaq 0xbd6(%rip), %rdi       # 0x20a0
movq %rdx, %rsi
jmp 0x15a3
cmpb $0x0, 0x8(%rsp)
jne 0x14e7
movl %ebx, %edi
callq 0x1140
movl %ebx, %edi
callq 0x1110
callq 0x1130
movl %eax, %edi
callq 0x1120
movq 0x2be6(%rip), %rcx      # 0x40e0
movl %eax, 0x8(%rcx,%r15)
testb $0x1, %r14b
jne 0x1550
movl 0x14(%rsp), %edi
callq 0x10a0
movl 0x10(%rsp), %ebx
movq 0x2bc7(%rip), %r12      # 0x40e0
movl $0x6400000, %edi        # imm = 0x6400000
callq 0x1100
movq %rax, %r14
xorl %r13d, %r13d
movl $0x6400000, %edx        # imm = 0x6400000
movq %rax, %rdi
xorl %esi, %esi
callq 0x1090
movl $0x6400000, %edx        # imm = 0x6400000
movl %ebx, %edi
movq %r14, %rsi
callq 0x10c0
addq %rax, %r13
movq %r13, (%r12,%r15)
jmp 0x1538
movl 0x10(%rsp), %edi
callq 0x10a0
movl 0x14(%rsp), %ebx
movl $0x6400000, %edi        # imm = 0x6400000
callq 0x1100
movq %rax, %r14
movl $0x6400000, %edx        # imm = 0x6400000
movq %r14, %rdi
movl $0x20, %esi
callq 0x1090
movl $0x6400000, %edx        # imm = 0x6400000
movl %ebx, %edi
movq %r14, %rsi
callq 0x1060
jmp 0x156a
callq 0x1040
movl (%rax), %edi
callq 0x1160
leaq 0xae1(%rip), %rdi       # 0x2081
movq %rax, %rsi
xorl %eax, %eax
callq 0x1080
leaq 0x20(%rsp), %rdi
callq 0x128c
movq (%r14), %rsi
leaq 0xa4f(%rip), %rdi       # 0x200d
xorl %eax, %eax
callq 0x1080
leaq 0xb1b(%rip), %rdi       # 0x20e7
callq 0x1050
leaq 0xb3d(%rip), %rdi       # 0x2115
callq 0x1050
leaq 0xb50(%rip), %rdi       # 0x2134
callq 0x1050
movl $0xa, %edi
callq 0x1030
xorl %edi, %edi
callq 0x1150