// File: /workspace/github_c_makefile_star2_O2/agraf[P]kvmbench/ht.c
// Function: main
int main(int argc, char **argv)
{
    int num_cores = sysconf(_SC_NPROCESSORS_ONLN);
    int i;
    char thread_spawned[num_cores];
    pthread_t thread[num_cores];
    uint64_t result = 0;
    char thread_siblings[1024];
    char *tsp;
    int is_store = 0;

    for (i = 1; i < argc; i++) {
        if (!strcmp(argv[i], "-P")) {
            opt_pinned = 0;
        } else if (!strcmp(argv[i], "-c")) {
            opt_optimal = 0;
        } else if (!strcmp(argv[i], "-s")) {
            opt_only_store = 1;
            is_store = 1;
            opt_optimal = 0;
        } else if (!strcmp(argv[i], "-m")) {
            opt_only_mul = 1;
            opt_optimal = 0;
        } else {
            printf("Syntax: %s\n\n", argv[0]);
            printf("  -c    Pin the threads to collide\n");
            printf("  -P    Don't pin the threads\n");
            printf("  -s    Run store test on all threads\n");
            printf("  -m    Run mul test on all threads\n");
            printf("\n");
            exit(0);
        }
    }

    results = malloc(num_cores * sizeof(*results));
    memset((void*)results, 0, num_cores * sizeof(*results));
    memset(thread_spawned, 0, num_cores);

    for (i = 0; i < num_cores; i++) {
        char path[512];
        int fd;
        int offset = 0;

        if (thread_spawned[i]) continue;

        snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", i);

        memset(thread_siblings, 0, sizeof(thread_siblings));
        fd = open(path, O_RDONLY);
        if (fd < 0) {
            printf("Error opening %s\n", path);
            return 1;
        }
        read(fd, thread_siblings, sizeof(thread_siblings));
        /* Just hope we read everything */
        close(fd);

        if (strlen(thread_siblings) < 9) {
            uint32_t cur_siblings;

            sscanf(thread_siblings, "%x", &cur_siblings);
            sprintf(thread_siblings, "%08x\n", cur_siblings);
        }

        tsp = thread_siblings + strlen(thread_siblings) - 9;
        while (1) {
            uint32_t cur_siblings;
            int j;

            if (sscanf(tsp, "%08x", &cur_siblings) != 1) {
                printf("Error reading siblings from %s\n", tsp);
                exit(1);
            }

            for (j = 0; j < 32; j++) {
                if (cur_siblings & (1U << j)) {
                    int cpu = j + offset;
                    spawn_thread(&thread[cpu], &thread_spawned[cpu], is_store, cpu);
                    if (opt_optimal) {
                        is_store = (is_store + 1) & 1;
                    }
                }
            }

            if (tsp == thread_siblings) {
                break;
            }

            tsp -= 9;
            offset += 32;
            if ((uintptr_t)tsp < (uintptr_t)thread_siblings) {
                tsp = thread_siblings;
            }
        }

        if (opt_only_store) {
            is_store = 1;
        } else if (opt_only_mul) {
            is_store = 0;
        } else if (!opt_optimal) {
            is_store = (is_store + 1) & 1;
        }
    }

    if (opt_pinned) {
        printf("Running  MUL  test on CPUs ");
        for (i = 0; i < num_cores; i++) {
            if (!results[i].is_store) {
                printf("%d ", i);
            }
        }
        printf("\n");

        printf("Running STORE test on CPUs ");
        for (i = 0; i < num_cores; i++) {
            if (results[i].is_store) {
                printf("%d ", i);
            }
        }
        printf("\n");
    } else {
        printf("Running STORE and MUL tests without pinning ...\n");
    }

    running = 1;
    sleep(5);

    result = 0;
    for (i = 0; i < num_cores; i++) {
        if (!results[i].is_store) {
            result += results[i].result;
        }
    }
    printf("Total MMuls: %ld\n", result / 1000000 / 5);

    result = 0;
    for (i = 0; i < num_cores; i++) {
        if (results[i].is_store) {
            result += results[i].result;
        }
    }
    printf("Total MStore: %ld\n", result / 1000000 / 5);

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x648, %rsp            # imm = 0x648
movq %rsi, %rbx
movl %edi, %r14d
pushq $0x54
popq %rdi
callq 0x1100
movq %rax, %r12
movl %eax, %eax
movq %rsp, %rdx
leaq 0xf(%rax), %rcx
andq $-0x10, %rcx
subq %rcx, %rdx
movq %rdx, -0x40(%rbp)
movq %rdx, %rsp
movq %rsp, %rcx
leaq 0xf(,%rax,8), %rax
andq $-0x10, %rax
subq %rax, %rcx
movq %rcx, -0x60(%rbp)
movq %rcx, %rsp
movslq %r14d, %rax
xorl %r15d, %r15d
pushq $0x1
popq %rcx
leaq 0x2db2(%rip), %rdx      # 0x40b0
leaq 0x2daf(%rip), %rsi      # 0x40b4
pushq $0x1
popq %rdi
leaq 0x2db5(%rip), %r8       # 0x40c4
leaq 0x2db2(%rip), %r9       # 0x40c8
cmpq %rax, %rcx
jge 0x1395
movq (%rbx,%rcx,8), %r10
cmpb $0x2d, (%r10)
jne 0x1783
cmpb $0x50, 0x1(%r10)
jne 0x1337
cmpb $0x0, 0x2(%r10)
je 0x1384
cmpb $0x63, 0x1(%r10)
jne 0x1345
cmpb $0x0, 0x2(%r10)
je 0x1389
cmpb $0x73, 0x1(%r10)
jne 0x1359
cmpb $0x0, 0x2(%r10)
movq %r8, %r11
movl %edi, %r14d
je 0x1375
cmpb $0x6d, 0x1(%r10)
jne 0x1783
cmpb $0x0, 0x2(%r10)
movq %r9, %r11
movl %r15d, %r14d
jne 0x1783
movl $0x1, (%r11)
movq %rsi, %r10
movl %r14d, %r15d
jmp 0x138c
movq %rdx, %r10
jmp 0x138c
movq %rsi, %r10
andl $0x0, (%r10)
incq %rcx
jmp 0x1316
movslq %r12d, %rbx
movq %rbx, %rsi
shlq $0x4, %rsi
pushq $0x1
popq %rdi
callq 0x10c0
movq %rax, 0x2d22(%rip)      # 0x40d0
xorl %r14d, %r14d
movq -0x40(%rbp), %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x1090
movq %r12, %rdx
testl %edx, %edx
cmovlel %r14d, %edx
leaq -0x670(%rbp), %r12
xorl %ecx, %ecx
movq %rdx, -0x38(%rbp)
cmpq %rdx, %rcx
je 0x15e0
movq -0x40(%rbp), %rax
cmpb $0x0, (%rax,%rcx)
je 0x13f0
movl %r15d, %eax
jmp 0x15d5
movl $0x200, %esi            # imm = 0x200
leaq -0x270(%rbp), %rbx
movq %rbx, %rdi
leaq 0xc0b(%rip), %rdx       # 0x2011
movq %rcx, -0x68(%rbp)
xorl %eax, %eax
callq 0x1080
movl $0x400, %edx            # imm = 0x400
movq %r12, %rdi
xorl %esi, %esi
callq 0x1090
movq %rbx, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x10f0
testl %eax, %eax
js 0x1741
movl %eax, %ebx
movl $0x400, %edx            # imm = 0x400
movl %eax, %edi
movq %r12, %rsi
callq 0x10b0
movl %ebx, %edi
callq 0x10a0
movq %r12, %rdi
callq 0x1060
cmpq $0x8, %rax
ja 0x1483
movq %r12, %rdi
leaq 0xbf6(%rip), %rsi       # 0x205a
leaq -0x2c(%rbp), %rdx
xorl %eax, %eax
callq 0x10d0
movl -0x2c(%rbp), %edx
movq %r12, %rdi
leaq 0xbe1(%rip), %rsi       # 0x205d
xorl %eax, %eax
callq 0x1110
movq %r12, %rdi
callq 0x1060
movq %rax, %r13
leaq -0x679(%rbp), %rax
addq %rax, %r13
movq -0x60(%rbp), %rbx
movq -0x40(%rbp), %rax
movq %rax, -0x58(%rbp)
pushq $0x8
popq %rax
movq %rax, -0x50(%rbp)
xorl %edi, %edi
movq %rdi, %r14
movq %r13, %rdi
leaq 0xba9(%rip), %rsi       # 0x2063
leaq -0x2c(%rbp), %rdx
xorl %eax, %eax
callq 0x10d0
cmpl $0x1, %eax
jne 0x176a
movq %r13, -0x70(%rbp)
xorl %r12d, %r12d
xorl %r13d, %r13d
movq %r14, %rdi
movl %r15d, %ecx
movl %ecx, %r14d
notl %r14d
cmpq $0x20, %r13
je 0x1569
movl -0x2c(%rbp), %eax
btl %r13d, %eax
jae 0x155d
movq 0x2bd6(%rip), %rax      # 0x40d0
addq -0x50(%rbp), %rax
movl %ecx, (%rax,%r12,2)
leaq 0x326(%rip), %rdx       # 0x182f
movl %ecx, -0x44(%rbp)
testl %ecx, %ecx
je 0x1517
leaq 0x2bc(%rip), %rdx       # 0x17d3
leaq (%rdi,%r13), %rcx
movq %rdi, %r15
leaq (%rbx,%r12), %rdi
xorl %esi, %esi
callq 0x10e0
cmpl $0x0, 0x2b80(%rip)      # 0x40b0
je 0x153f
movq (%rbx,%r13,8), %rdi
leal (%r15,%r13), %esi
callq 0x123c
movq -0x58(%rbp), %rax
movb $0x1, (%rax,%r13)
andl $0x1, %r14d
cmpl $0x0, 0x2b61(%rip)      # 0x40b4
movl -0x44(%rbp), %ecx
cmovnel %r14d, %ecx
movq %r15, %rdi
incq %r13
addq $0x8, %r12
jmp 0x14de
movl %ecx, %r15d
movq -0x70(%rbp), %rcx
leaq -0x9(%rcx), %r13
addq $0x20, %rdi
leaq -0x670(%rbp), %r12
cmpq %r12, %r13
cmovbq %r12, %r13
addq $0x200, -0x50(%rbp)     # imm = 0x200
addq $0x20, -0x58(%rbp)
addq $0x100, %rbx            # imm = 0x100
cmpq %r12, %rcx
jne 0x14ad
cmpl $0x0, 0x2b1a(%rip)      # 0x40c4
pushq $0x1
popq %rax
movq -0x38(%rbp), %rdx
movq -0x68(%rbp), %rcx
jne 0x15d5
xorl %eax, %eax
cmpl $0x0, 0x2b08(%rip)      # 0x40c8
jne 0x15d5
cmpl $0x0, 0x2aeb(%rip)      # 0x40b4
movl %r15d, %eax
jne 0x15d5
andl $0x1, %r14d
movl %r14d, %eax
incq %rcx
movl %eax, %r15d
jmp 0x13d5
cmpl $0x0, 0x2ac9(%rip)      # 0x40b0
je 0x1699
leaq 0xa94(%rip), %rdi       # 0x2088
xorl %eax, %eax
callq 0x1070
pushq $0x8
popq %r15
leaq 0xa9e(%rip), %rbx       # 0x20a4
xorl %r14d, %r14d
movq -0x38(%rbp), %rcx
cmpq %r14, %rcx
je 0x163a
movq 0x2ab7(%rip), %rax      # 0x40d0
cmpl $0x0, (%rax,%r15)
jne 0x1631
movq %rbx, %rdi
movl %r14d, %esi
xorl %eax, %eax
callq 0x1070
movq -0x38(%rbp), %rcx
incq %r14
addq $0x10, %r15
jmp 0x160d
pushq $0xa
popq %rdi
callq 0x1030
leaq 0xa5f(%rip), %rdi       # 0x20a8
xorl %eax, %eax
callq 0x1070
movq -0x38(%rbp), %rcx
pushq $0x8
popq %r15
leaq 0xa45(%rip), %rbx       # 0x20a4
xorl %r14d, %r14d
cmpq %r14, %rcx
je 0x168f
movq 0x2a62(%rip), %rax      # 0x40d0
cmpl $0x0, (%rax,%r15)
je 0x1686
movq %rbx, %rdi
movl %r14d, %esi
xorl %eax, %eax
callq 0x1070
movq -0x38(%rbp), %rcx
incq %r14
addq $0x10, %r15
jmp 0x1662
pushq $0xa
popq %rdi
callq 0x1030
jmp 0x16a5
leaq 0xa49(%rip), %rdi       # 0x20e9
callq 0x1050
movl $0x1, 0x2a11(%rip)      # 0x40c0
pushq $0x5
popq %rdi
callq 0x1130
movq 0x2a12(%rip), %rcx      # 0x40d0
movq -0x38(%rbp), %rsi
shlq $0x4, %rsi
xorl %edx, %edx
xorl %eax, %eax
cmpq %rdx, %rsi
je 0x16e0
cmpl $0x0, 0x8(%rcx,%rdx)
jne 0x16da
addq (%rcx,%rdx), %rax
addq $0x10, %rdx
jmp 0x16ca
xorl %ebx, %ebx
movl $0x4c4b40, %ecx         # imm = 0x4C4B40
xorl %edx, %edx
divq %rcx
leaq 0x9d1(%rip), %rdi       # 0x20c4
movq %rsi, %r14
movq %rax, %rsi
xorl %eax, %eax
callq 0x1070
movq %r14, %rdx
movq 0x29c6(%rip), %rcx      # 0x40d0
xorl %eax, %eax
cmpq %rbx, %rdx
je 0x1722
cmpl $0x0, 0x8(%rcx,%rbx)
je 0x171c
addq (%rcx,%rbx), %rax
addq $0x10, %rbx
jmp 0x170c
xorl %ebx, %ebx
movl $0x4c4b40, %ecx         # imm = 0x4C4B40
xorl %edx, %edx
divq %rcx
leaq 0x9a1(%rip), %rdi       # 0x20d6
movq %rax, %rsi
xorl %eax, %eax
callq 0x1070
jmp 0x1759
leaq 0x900(%rip), %rdi       # 0x2048
leaq -0x270(%rbp), %rsi
xorl %eax, %eax
callq 0x1070
pushq $0x1
popq %rbx
movl %ebx, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x8f7(%rip), %rdi       # 0x2068
movq %r13, %rsi
xorl %eax, %eax
callq 0x1070
pushq $0x1
popq %rdi
callq 0x1120
movq (%rbx), %rsi
leaq 0x877(%rip), %rdi       # 0x2004
xorl %eax, %eax
callq 0x1070
leaq 0x97e(%rip), %rdi       # 0x2119
callq 0x1050
leaq 0x995(%rip), %rdi       # 0x213c
callq 0x1050
leaq 0x9a7(%rip), %rdi       # 0x215a
callq 0x1050
leaq 0x9c1(%rip), %rdi       # 0x2180
callq 0x1050
pushq $0xa
popq %rdi
callq 0x1030
xorl %edi, %edi
callq 0x1120