// File: /workspace/github_c_makefile_star2_O2/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 $0x1a8, %rsp            # imm = 0x1A8
movq %rsi, %r15
movl %edi, %ebp
callq 0x10d0
movl %eax, %r14d
leal 0x1(%r14), %ebx
movslq %ebp, %rax
pushq $0x1
popq %r13
pushq $0x1
popq %rcx
movl %r13d, %ebp
movl %r13d, %r12d
cmpq %rax, %rcx
jge 0x1337
movq (%r15,%rcx,8), %rdx
cmpb $0x2d, (%rdx)
jne 0x14da
cmpb $0x75, 0x1(%rdx)
jne 0x1306
cmpb $0x0, 0x2(%rdx)
je 0x132b
cmpb $0x4e, 0x1(%rdx)
jne 0x1312
cmpb $0x0, 0x2(%rdx)
je 0x1330
cmpb $0x30, 0x1(%rdx)
jne 0x14da
cmpb $0x0, 0x2(%rdx)
jne 0x14da
xorl %r12d, %r12d
jmp 0x1332
xorl %r13d, %r13d
jmp 0x1332
xorl %ebp, %ebp
incq %rcx
jmp 0x12e8
pushq $0x3
popq %rdx
pushq $0x21
popq %rcx
pushq $-0x1
popq %r8
movl $0x1900, %esi           # imm = 0x1900
xorl %edi, %edi
xorl %r9d, %r9d
callq 0x1070
movq %rax, 0x2d89(%rip)      # 0x40e0
leaq 0xcb3(%rip), %rdi       # 0x2011
movl %ebx, %esi
xorl %eax, %eax
callq 0x1080
testl %ebp, %ebp
movl %ebp, 0xc(%rsp)
je 0x13b8
leaq 0xd4f(%rip), %rdi       # 0x20c5
callq 0x1050
testl %r13d, %r13d
leaq 0xd3d(%rip), %rax       # 0x20c2
leaq 0xd4e(%rip), %rsi       # 0x20da
cmoveq %rax, %rsi
leaq 0xcad(%rip), %rdi       # 0x2044
xorl %eax, %eax
callq 0x1080
testl %r12d, %r12d
cmovel %r12d, %r14d
leaq 0xcb1(%rip), %rdi       # 0x205d
movl %r14d, %esi
xorl %eax, %eax
callq 0x1080
jmp 0x13c4
leaq 0xcee(%rip), %rdi       # 0x20ad
callq 0x1050
pushq $0xa
popq %rdi
callq 0x1030
xorl %r15d, %r15d
xorl %r14d, %r14d
xorl %ebp, %ebp
cmpq $0x64, %r14
je 0x151e
testb $0x1, %r14b
jne 0x13ff
leaq 0x4(%rsp), %rdi
callq 0x10b0
testl %eax, %eax
jne 0x1557
incl %ebp
movl %ebp, %eax
cltd
idivl %ebx
movl %edx, %ebp
testl %r12d, %r12d
cmovel %r12d, %ebp
callq 0x1180
testl %eax, %eax
je 0x142b
movl %eax, 0x10(%rsp,%r14,4)
testl %r13d, %r13d
jne 0x1422
incl %ebp
movl %ebp, %eax
cltd
idivl %ebx
movl %edx, %ebp
incq %r14
addq $0x40, %r15
jmp 0x13d4
cmpl $0x0, 0xc(%rsp)
je 0x1440
movl %ebp, %edi
callq 0x1140
movl %ebp, %edi
callq 0x1110
callq 0x1130
movl %eax, %edi
callq 0x1120
movq 0x2c8d(%rip), %rcx      # 0x40e0
movl %eax, 0x8(%rcx,%r15)
testb $0x1, %r14b
jne 0x149d
movl 0x8(%rsp), %edi
callq 0x10a0
movl 0x4(%rsp), %ebx
movq 0x2c6e(%rip), %r12      # 0x40e0
pushq $0x1
popq %rdi
movl $0x6400000, %esi        # imm = 0x6400000
callq 0x10e0
movq %rax, %r14
xorl %r13d, %r13d
movl $0x6400000, %edx        # imm = 0x6400000
movl %ebx, %edi
movq %r14, %rsi
callq 0x10c0
addq %rax, %r13
movq %r13, (%r12,%r15)
jmp 0x1485
movl 0x4(%rsp), %edi
callq 0x10a0
movl 0x8(%rsp), %ebx
movl $0x6400000, %edi        # imm = 0x6400000
callq 0x1100
movq %rax, %r14
pushq $0x20
popq %rbp
movl $0x6400000, %edx        # imm = 0x6400000
movq %r14, %rdi
movl %ebp, %esi
callq 0x1090
movl $0x6400000, %edx        # imm = 0x6400000
movl %ebx, %edi
movq %r14, %rsi
callq 0x1060
jmp 0x14ba
movq (%r15), %rsi
leaq 0xb20(%rip), %rdi       # 0x2004
xorl %eax, %eax
callq 0x1080
leaq 0xbec(%rip), %rdi       # 0x20de
callq 0x1050
leaq 0xc0e(%rip), %rdi       # 0x210c
callq 0x1050
leaq 0xc21(%rip), %rdi       # 0x212b
callq 0x1050
pushq $0xa
popq %rdi
callq 0x1030
xorl %edi, %edi
callq 0x1150
pushq $0x5
popq %rdi
callq 0x1170
xorl %ecx, %ecx
movq 0x2bb1(%rip), %rdx      # 0x40e0
xorl %eax, %eax
cmpq $0x1900, %rcx           # imm = 0x1900
je 0x1544
addq (%rdx,%rcx), %rax
addq $0x40, %rcx
jmp 0x1531
movl $0x500000, %ecx         # imm = 0x500000
xorl %edx, %edx
divq %rcx
leaq 0xb42(%rip), %rdi       # 0x2097
jmp 0x156a
callq 0x1040
movl (%rax), %edi
callq 0x1160
leaq 0xb0e(%rip), %rdi       # 0x2078
movq %rax, %rsi
xorl %eax, %eax
callq 0x1080
leaq 0x10(%rsp), %rdi
callq 0x128c