// File: /workspace/github_c_makefile_star2_O3/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, %r14
movl %edi, %ebp
callq 0x10d0
movl %eax, %ebx
cmpl $0x2, %ebp
jl 0x1352
movl %ebp, %eax
movl $0x1, %esi
movl $0x1, %ecx
movl $0x1, %edi
movl $0x1, %edx
movq (%r14,%rcx,8), %r8
cmpb $0x2d, (%r8)
jne 0x1553
cmpb $0x75, 0x1(%r8)
jne 0x1308
cmpb $0x0, 0x2(%r8)
je 0x1330
cmpb $0x4e, 0x1(%r8)
jne 0x1316
cmpb $0x0, 0x2(%r8)
je 0x1334
cmpb $0x30, 0x1(%r8)
jne 0x1553
cmpb $0x0, 0x2(%r8)
jne 0x1553
xorl %esi, %esi
jmp 0x1336
xorl %edx, %edx
jmp 0x1336
xorl %edi, %edi
incq %rcx
cmpq %rcx, %rax
jne 0x12ec
testl %edi, %edi
sete %r15b
testl %edx, %edx
sete %r12b
testl %esi, %esi
sete %r13b
jmp 0x135b
xorl %r12d, %r12d
xorl %r15d, %r15d
xorl %r13d, %r13d
leal 0x1(%rbx), %ebp
xorl %r14d, %r14d
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, 0x2d59(%rip)      # 0x40e0
leaq 0xc83(%rip), %rdi       # 0x2011
movl %ebp, %esi
xorl %eax, %eax
callq 0x1080
testb %r15b, %r15b
movl %r15d, 0xc(%rsp)
je 0x13af
leaq 0xd05(%rip), %rdi       # 0x20ad
callq 0x1050
jmp 0x13f5
leaq 0xd0f(%rip), %rdi       # 0x20c5
callq 0x1050
leaq 0xd00(%rip), %rax       # 0x20c2
leaq 0xd11(%rip), %rsi       # 0x20da
testb %r12b, %r12b
cmovneq %rax, %rsi
leaq 0xc6d(%rip), %rdi       # 0x2044
xorl %eax, %eax
callq 0x1080
testb %r13b, %r13b
cmovnel %r14d, %ebx
leaq 0xc71(%rip), %rdi       # 0x205d
movl %ebx, %esi
xorl %eax, %eax
callq 0x1080
movl $0xa, %edi
callq 0x1030
xorl %r15d, %r15d
xorl %r14d, %r14d
xorl %ebx, %ebx
testb $0x1, %r14b
jne 0x1428
leaq 0x4(%rsp), %rdi
callq 0x10b0
testl %eax, %eax
jne 0x1599
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 0x14a2
movl %eax, 0x10(%rsp,%r14,4)
testb %r12b, %r12b
je 0x144f
incl %ebx
movl %ebx, %eax
cltd
idivl %ebp
movl %edx, %ebx
incq %r14
addq $0x40, %r15
cmpq $0x64, %r14
jne 0x1407
movl $0x5, %edi
callq 0x1170
xorl %ecx, %ecx
movq 0x2c71(%rip), %rdx      # 0x40e0
xorl %eax, %eax
addq (%rdx,%rcx), %rax
addq $0x40, %rcx
cmpq $0x1900, %rcx           # imm = 0x1900
jne 0x1471
movabsq $-0x3333333333333333, %rcx # imm = 0xCCCCCCCCCCCCCCCD
mulq %rcx
shrq $0x16, %rdx
leaq 0xbfd(%rip), %rdi       # 0x2097
movq %rdx, %rsi
jmp 0x15af
cmpb $0x0, 0xc(%rsp)
jne 0x14b7
movl %ebx, %edi
callq 0x1140
movl %ebx, %edi
callq 0x1110
callq 0x1130
movl %eax, %edi
callq 0x1120
movq 0x2c16(%rip), %rcx      # 0x40e0
movl %eax, 0x8(%rcx,%r15)
testb $0x1, %r14b
jne 0x1516
movl 0x8(%rsp), %edi
callq 0x10a0
movl 0x4(%rsp), %ebx
movq 0x2bf7(%rip), %r12      # 0x40e0
movl $0x1, %edi
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 0x14fe
movl 0x4(%rsp), %edi
callq 0x10a0
movl 0x8(%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 0x1530
movq (%r14), %rsi
leaq 0xaa7(%rip), %rdi       # 0x2004
xorl %eax, %eax
callq 0x1080
leaq 0xb73(%rip), %rdi       # 0x20de
callq 0x1050
leaq 0xb95(%rip), %rdi       # 0x210c
callq 0x1050
leaq 0xba8(%rip), %rdi       # 0x212b
callq 0x1050
movl $0xa, %edi
callq 0x1030
xorl %edi, %edi
callq 0x1150
callq 0x1040
movl (%rax), %edi
callq 0x1160
leaq 0xacc(%rip), %rdi       # 0x2078
movq %rax, %rsi
xorl %eax, %eax
callq 0x1080
leaq 0x10(%rsp), %rdi
callq 0x128c