// File: /workspace/github_c_makefile_star2_O3/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
movl $0x54, %edi
callq 0x1100
movq %rax, -0x38(%rbp)
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
xorl %r12d, %r12d
cmpl $0x2, %r14d
jl 0x13a3
movl %r14d, %eax
xorl %r12d, %r12d
movl $0x1, %ecx
leaq 0x2d9e(%rip), %rdx      # 0x40b0
leaq 0x2d9b(%rip), %rsi      # 0x40b4
leaq 0x2da4(%rip), %rdi      # 0x40c4
leaq 0x2da1(%rip), %r8       # 0x40c8
movq (%rbx,%rcx,8), %r9
cmpb $0x2d, (%r9)
jne 0x181b
cmpb $0x50, 0x1(%r9)
jne 0x1343
cmpb $0x0, 0x2(%r9)
je 0x1393
cmpb $0x63, 0x1(%r9)
jne 0x1351
cmpb $0x0, 0x2(%r9)
je 0x137f
cmpb $0x73, 0x1(%r9)
jne 0x135f
cmpb $0x0, 0x2(%r9)
je 0x1398
cmpb $0x6d, 0x1(%r9)
jne 0x181b
cmpb $0x0, 0x2(%r9)
movq %r8, %r9
jne 0x181b
movl $0x1, (%r9)
movq %rsi, %r9
movl $0x0, (%r9)
incq %rcx
cmpq %rcx, %rax
jne 0x1327
jmp 0x13a3
movq %rdx, %r9
jmp 0x1382
movl $0x1, %r12d
movq %rdi, %r9
jmp 0x1378
movq -0x38(%rbp), %r14
movslq %r14d, %rbx
movq %rbx, %rsi
shlq $0x4, %rsi
movl $0x1, %edi
callq 0x10c0
movq %rax, 0x2d0e(%rip)      # 0x40d0
movq -0x40(%rbp), %rdi
xorl %esi, %esi
movq %rbx, %rdx
callq 0x1090
testl %r14d, %r14d
jle 0x1650
andl $0x7fffffff, %r14d      # imm = 0x7FFFFFFF
leaq -0x670(%rbp), %r15
xorl %ecx, %ecx
movq %r14, -0x58(%rbp)
movq -0x40(%rbp), %rax
cmpb $0x0, (%rax,%rcx)
je 0x13ff
movl %r12d, %eax
jmp 0x15b7
movl $0x200, %esi            # imm = 0x200
leaq -0x270(%rbp), %rbx
movq %rbx, %rdi
leaq 0xbfc(%rip), %rdx       # 0x2011
movq %rcx, -0x68(%rbp)
xorl %eax, %eax
callq 0x1080
movl $0x400, %edx            # imm = 0x400
movq %r15, %rdi
xorl %esi, %esi
callq 0x1090
movq %rbx, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x10f0
testl %eax, %eax
js 0x16e2
movl %eax, %ebx
movl $0x400, %edx            # imm = 0x400
movl %eax, %edi
movq %r15, %rsi
callq 0x10b0
movl %ebx, %edi
callq 0x10a0
movq %r15, %rdi
callq 0x1060
cmpq $0x8, %rax
ja 0x1492
movq %r15, %rdi
leaq 0xbe7(%rip), %rsi       # 0x205a
leaq -0x2c(%rbp), %rdx
xorl %eax, %eax
callq 0x10d0
movl -0x2c(%rbp), %edx
movq %r15, %rdi
leaq 0xbd2(%rip), %rsi       # 0x205d
xorl %eax, %eax
callq 0x1110
movq %r15, %rdi
callq 0x1060
movq %rax, %r13
leaq -0x679(%rbp), %rax
addq %rax, %r13
movl $0x8, %eax
movq %rax, -0x50(%rbp)
movq -0x60(%rbp), %r14
movq -0x40(%rbp), %rax
movq %rax, -0x48(%rbp)
xorl %ebx, %ebx
movq %r13, %rdi
leaq 0xb9b(%rip), %rsi       # 0x2063
leaq -0x2c(%rbp), %rdx
xorl %eax, %eax
callq 0x10d0
cmpl $0x1, %eax
jne 0x1800
movq %r13, -0x70(%rbp)
xorl %r13d, %r13d
xorl %r15d, %r15d
movl -0x2c(%rbp), %eax
btl %r15d, %eax
jae 0x1552
movq 0x2bda(%rip), %rax      # 0x40d0
addq -0x50(%rbp), %rax
movl %r12d, (%rax,%r13,2)
leaq 0x3c3(%rip), %rdx       # 0x18c8
testl %r12d, %r12d
je 0x1511
leaq 0x35c(%rip), %rdx       # 0x186d
leaq (%rbx,%r15), %rcx
leaq (%r14,%r13), %rdi
xorl %esi, %esi
callq 0x10e0
cmpl $0x0, 0x2b89(%rip)      # 0x40b0
je 0x1536
movq (%r14,%r15,8), %rdi
leal (%rbx,%r15), %esi
callq 0x123c
movq -0x48(%rbp), %rax
movb $0x1, (%rax,%r15)
movl %r12d, %eax
notl %eax
andl $0x1, %eax
cmpl $0x0, 0x2b66(%rip)      # 0x40b4
cmovnel %eax, %r12d
incq %r15
addq $0x8, %r13
cmpq $0x20, %r15
jne 0x14e6
movq -0x70(%rbp), %rcx
leaq -0x9(%rcx), %r13
addq $0x20, %rbx
leaq -0x670(%rbp), %r15
cmpq %r15, %r13
cmovbq %r15, %r13
addq $0x200, -0x50(%rbp)     # imm = 0x200
addq $0x20, -0x48(%rbp)
addq $0x100, %r14            # imm = 0x100
cmpq %r15, %rcx
jne 0x14be
movl $0x1, %eax
cmpl $0x0, 0x2b22(%rip)      # 0x40c4
movq -0x58(%rbp), %r14
movq -0x68(%rbp), %rcx
jne 0x15b7
xorl %eax, %eax
cmpl $0x0, 0x2b13(%rip)      # 0x40c8
je 0x15c8
incq %rcx
movl %eax, %r12d
cmpq %r14, %rcx
jne 0x13ed
jmp 0x15e1
cmpl $0x0, 0x2ae5(%rip)      # 0x40b4
jne 0x13f7
notl %r12d
andl $0x1, %r12d
jmp 0x13f7
cmpl $0x0, 0x2ac8(%rip)      # 0x40b0
je 0x16d4
leaq 0xa93(%rip), %rdi       # 0x2088
xorl %eax, %eax
callq 0x1070
movq -0x38(%rbp), %r15
testl %r15d, %r15d
jle 0x1702
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
movl $0x8, %r12d
movq 0x2ab3(%rip), %rax      # 0x40d0
leaq 0xa80(%rip), %rbx       # 0x20a4
xorl %r14d, %r14d
cmpl $0x0, (%rax,%r12)
jne 0x1642
movq %rbx, %rdi
movl %r14d, %esi
xorl %eax, %eax
callq 0x1070
movq 0x2a8e(%rip), %rax      # 0x40d0
incq %r14
addq $0x10, %r12
cmpq %r14, %r15
jne 0x1627
jmp 0x1667
cmpl $0x0, 0x2a59(%rip)      # 0x40b0
je 0x16d4
leaq 0xa28(%rip), %rdi       # 0x2088
xorl %eax, %eax
callq 0x1070
movl $0xa, %edi
callq 0x1030
leaq 0xa30(%rip), %rdi       # 0x20a8
xorl %eax, %eax
callq 0x1070
cmpl $0x0, -0x38(%rbp)
jle 0x171a
movq -0x38(%rbp), %r15
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
movl $0x8, %r12d
movq 0x2a2f(%rip), %rax      # 0x40d0
leaq 0x9fc(%rip), %rbx       # 0x20a4
xorl %r14d, %r14d
cmpl $0x0, (%rax,%r12)
je 0x16c6
movq %rbx, %rdi
movl %r14d, %esi
xorl %eax, %eax
callq 0x1070
movq 0x2a0a(%rip), %rax      # 0x40d0
incq %r14
addq $0x10, %r12
cmpq %r14, %r15
jne 0x16ab
jmp 0x171a
leaq 0xa0e(%rip), %rdi       # 0x20e9
callq 0x1050
jmp 0x1724
leaq 0x95f(%rip), %rdi       # 0x2048
leaq -0x270(%rbp), %rsi
xorl %eax, %eax
callq 0x1070
movl $0x1, %r14d
jmp 0x17ee
movl $0xa, %edi
callq 0x1030
leaq 0x995(%rip), %rdi       # 0x20a8
xorl %eax, %eax
callq 0x1070
movl $0xa, %edi
callq 0x1030
movl $0x1, 0x2992(%rip)      # 0x40c0
movl $0x5, %edi
callq 0x1130
movq -0x38(%rbp), %r15
testl %r15d, %r15d
jle 0x17c8
movq 0x2984(%rip), %rcx      # 0x40d0
movl %r15d, %edx
andl $0x7fffffff, %edx       # imm = 0x7FFFFFFF
addq $0x8, %rcx
xorl %eax, %eax
cmpl $0x0, (%rcx)
jne 0x1764
addq -0x8(%rcx), %rax
addq $0x10, %rcx
decq %rdx
jne 0x175b
movabsq $-0x29406b2a1a85bd43, %r14 # imm = 0xD6BF94D5E57A42BD
mulq %r14
shrq $0x16, %rdx
leaq 0x93f(%rip), %rdi       # 0x20c4
xorl %ebx, %ebx
movq %rdx, %rsi
xorl %eax, %eax
callq 0x1070
testl %r15d, %r15d
jle 0x17da
movq 0x2933(%rip), %rcx      # 0x40d0
andl $0x7fffffff, %r15d      # imm = 0x7FFFFFFF
addq $0x8, %rcx
xorl %eax, %eax
cmpl $0x0, (%rcx)
je 0x17b3
addq -0x8(%rcx), %rax
addq $0x10, %rcx
decq %r15
jne 0x17aa
mulq %r14
movq %rdx, %rbx
shrq $0x16, %rbx
jmp 0x17da
leaq 0x8f5(%rip), %rdi       # 0x20c4
xorl %ebx, %ebx
xorl %esi, %esi
xorl %eax, %eax
callq 0x1070
leaq 0x8f5(%rip), %rdi       # 0x20d6
xorl %r14d, %r14d
movq %rbx, %rsi
xorl %eax, %eax
callq 0x1070
movl %r14d, %eax
leaq -0x28(%rbp), %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x861(%rip), %rdi       # 0x2068
movq %r13, %rsi
xorl %eax, %eax
callq 0x1070
movl $0x1, %edi
callq 0x1120
movq (%rbx), %rsi
leaq 0x7df(%rip), %rdi       # 0x2004
xorl %eax, %eax
callq 0x1070
leaq 0x8e6(%rip), %rdi       # 0x2119
callq 0x1050
leaq 0x8fd(%rip), %rdi       # 0x213c
callq 0x1050
leaq 0x90f(%rip), %rdi       # 0x215a
callq 0x1050
leaq 0x929(%rip), %rdi       # 0x2180
callq 0x1050
movl $0xa, %edi
callq 0x1030
xorl %edi, %edi
callq 0x1120