// File: travisdowns[P]avx-turbo/avx-turbo.cpp
// Function: filter_cpus(std::vector<int, std::allocator<int>>)
std::vector<int> filter_cpus(std::vector<int> cpus) {
    int shift = get_smt_shift();
    if (shift == -1) {
        printf("Can't use cpuid leaf 0xb to filter out hyperthreads, CPU too old or AMD\n");
        return cpus;
    }
    cpu_set_t original_set;
    if (sched_getaffinity(0, sizeof(original_set), &original_set)) {
        err(EXIT_FAILURE, "failed while getting cpu affinity");
    }
    std::vector<int> filtered_cpus;
    std::set<uint32_t> coreid_set;
    for (int cpu : cpus) {
        cpu_set_t cpuset;
        CPU_ZERO(&cpuset);
        CPU_SET(cpu, &cpuset);
        if (sched_setaffinity(0, sizeof(cpu_set_t), &cpuset)) {
            err(EXIT_FAILURE, "failed to sched_setaffinity in filter_cpus");
        }
        cpuid_result leafb = cpuid(0xb);
        uint32_t apicid = leafb.edx, coreid = apicid >> shift;
        if (verbose) printf("cpu %d has x2apic ID %u, coreid %u\n", cpu, apicid, coreid);
        if (coreid_set.insert(coreid).second) {
            filtered_cpus.push_back(cpu);
        }
    }
    // restore original affinity
    sched_setaffinity(0, sizeof(cpu_set_t), &original_set);
    return filtered_cpus;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x210, %rsp            # imm = 0x210
movq %rsi, -0x1e0(%rbp)
movq %rdi, -0x1d8(%rbp)
movq %rdi, %rax
movq %rax, -0x1d0(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
callq 0x36a30
movl %eax, -0x14(%rbp)
cmpl $-0x1, -0x14(%rbp)
jne 0xcaaf
leaq 0x33d56(%rip), %rdi     # 0x407e6
movb $0x0, %al
callq 0x7080
movq -0x1d8(%rbp), %rdi
movq -0x1e0(%rbp), %rsi
callq 0x12680
jmp 0xcd85
xorl %edi, %edi
movl $0x80, %esi
leaq -0x98(%rbp), %rdx
callq 0x76c0
cmpl $0x0, %eax
je 0xcada
movl $0x1, %edi
leaq 0x33cf1(%rip), %rsi     # 0x407c4
movb $0x0, %al
callq 0x7400
movq -0x1d8(%rbp), %rdi
movb $0x0, -0x99(%rbp)
callq 0x125f0
leaq -0xd0(%rbp), %rdi
callq 0x126b0
movq -0x1e0(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rdi
callq 0x126d0
movq %rax, -0xe0(%rbp)
movq -0xd8(%rbp), %rdi
callq 0x12700
movq %rax, -0xe8(%rbp)
leaq -0xe0(%rbp), %rdi
leaq -0xe8(%rbp), %rsi
callq 0x12730
testb $0x1, %al
jne 0xcb49
jmp 0xcd48
leaq -0xe0(%rbp), %rdi
callq 0x12770
movl (%rax), %eax
movl %eax, -0xec(%rbp)
leaq -0x170(%rbp), %rdi
xorl %esi, %esi
movl $0x80, %edx
callq 0x7230
jmp 0xcb72
movslq -0xec(%rbp), %rax
movq %rax, -0x178(%rbp)
movq -0x178(%rbp), %rax
shrq $0x3, %rax
cmpq $0x80, %rax
jae 0xcbca
movq -0x178(%rbp), %rcx
andq $0x3f, %rcx
movl $0x1, %eax
shlq %cl, %rax
movq -0x178(%rbp), %rcx
shrq $0x6, %rcx
orq -0x170(%rbp,%rcx,8), %rax
movq %rax, -0x170(%rbp,%rcx,8)
movq %rax, -0x1e8(%rbp)
jmp 0xcbd5
xorl %eax, %eax
movq %rax, -0x1e8(%rbp)
jmp 0xcbd5
movq -0x1e8(%rbp), %rax
movq %rax, -0x180(%rbp)
xorl %edi, %edi
movl $0x80, %esi
leaq -0x170(%rbp), %rdx
callq 0x73a0
cmpl $0x0, %eax
je 0xcc3f
leaq 0x33c2d(%rip), %rsi     # 0x4082f
xorl %eax, %eax
movl $0x1, %edi
callq 0x7400
jmp 0xcc10
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x188(%rbp)
movl %eax, -0x18c(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x12800
movq -0x1d8(%rbp), %rdi
callq 0x11940
jmp 0xcd95
movl $0xb, %edi
callq 0x36530
movq %rdx, -0x1f8(%rbp)
movq %rax, -0x1f0(%rbp)
jmp 0xcc59
movq -0x1f8(%rbp), %rax
movq -0x1f0(%rbp), %rcx
movq %rcx, -0x19c(%rbp)
movq %rax, -0x194(%rbp)
movl -0x190(%rbp), %eax
movl %eax, -0x1a0(%rbp)
movl -0x1a0(%rbp), %eax
movl -0x14(%rbp), %ecx
shrl %cl, %eax
movl %eax, -0x1a4(%rbp)
testb $0x1, 0x4f6cf(%rip)     # 0x5c368
je 0xccbf
movl -0xec(%rbp), %esi
movl -0x1a0(%rbp), %edx
movl -0x1a4(%rbp), %ecx
leaq 0x33ba6(%rip), %rdi     # 0x4085a
xorl %eax, %eax
callq 0x7080
jmp 0xccbd
jmp 0xccbf
leaq -0xd0(%rbp), %rdi
leaq -0x1a4(%rbp), %rsi
callq 0x12790
movb %dl, -0x201(%rbp)
movq %rax, -0x200(%rbp)
jmp 0xcce1
movb -0x201(%rbp), %al
movq -0x200(%rbp), %rcx
movq %rcx, -0x1c8(%rbp)
movb %al, -0x1c0(%rbp)
movq -0x1c8(%rbp), %rax
movq %rax, -0x1b8(%rbp)
movb -0x1c0(%rbp), %al
movb %al, -0x1b0(%rbp)
testb $0x1, -0x1b0(%rbp)
je 0xcd35
movq -0x1d8(%rbp), %rdi
leaq -0xec(%rbp), %rsi
callq 0x12610
jmp 0xcd33
jmp 0xcd35
jmp 0xcd37
leaq -0xe0(%rbp), %rdi
callq 0x127e0
jmp 0xcb2d
xorl %edi, %edi
movl $0x80, %esi
leaq -0x98(%rbp), %rdx
callq 0x73a0
movb $0x1, -0x99(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x12800
testb $0x1, -0x99(%rbp)
jne 0xcd83
movq -0x1d8(%rbp), %rdi
callq 0x11940
jmp 0xcd85
movq -0x1d0(%rbp), %rax
addq $0x210, %rsp            # imm = 0x210
popq %rbp
retq
movq -0x188(%rbp), %rdi
callq 0x77b0
nopw %cs:(%rax,%rax)