// File: /workspace/github_c_makefile_star2_O2/google[P]multichase/multichase.c
// Function: thread_start
static void *thread_start(void *data) {
  per_thread_t *args = data;

  // ensure every thread has a different RNG
  rng_init(args->x.thread_num);

  if (set_thread_affinity) {
    // find out which cpus we can run on and move us to an appropriate cpu
    cpu_set_t cpus;
    if (sched_getaffinity(0, sizeof(cpus), &cpus)) {
      perror("sched_getaffinity");
      exit(1);
    }
    int my_cpu;
    unsigned num = args->x.thread_num;
    for (my_cpu = 0; my_cpu < CPU_SETSIZE; ++my_cpu) {
      if (!CPU_ISSET(my_cpu, &cpus)) continue;
      if (num == 0) break;
      --num;
    }
    if (my_cpu == CPU_SETSIZE) {
      fprintf(stderr, "error: more threads than cpus available\n");
      exit(1);
    }
    CPU_ZERO(&cpus);
    CPU_SET(my_cpu, &cpus);
    if (sched_setaffinity(0, sizeof(cpus), &cpus)) {
      perror("sched_setaffinity");
      exit(1);
    }
  }

  // generate chases -- using a different mixer index for every
  // thread and for every parallel chase within a thread
  unsigned parallelism = args->x.chase->parallelism;
  for (unsigned par = 0; par < parallelism; ++par) {
    if (args->x.use_longer_chase) {
      args->x.cycle[par] = generate_chase_long(args->x.genchase_args,
                                        parallelism * args->x.thread_num + par,
                                        parallelism * args->x.nr_threads);
    } else {
      args->x.cycle[par] = generate_chase(args->x.genchase_args,
                                        parallelism * args->x.thread_num + par);
    }
  }

  // handle critword2 chases
  if (strcmp(args->x.chase->name, "critword2") == 0) {
    size_t offset = strtoul(args->x.extra_args, 0, 0);
    char *p = args->x.cycle[0];
    char *q = p;
    do {
      char *next = *(char **)p;
      *(void **)(p + offset) = next + offset;
      p = next;
    } while (p != q);
  }

  // handle critword chases
  if (strcmp(args->x.chase->name, "critword") == 0) {
    size_t offset = strtoul(args->x.extra_args, 0, 0);
    char *p = args->x.cycle[0];
    char *q = p;
    do {
      char *next = *(char **)p;
      *(void **)(p + offset) = next;
      *(void **)p = p + offset;
      p = next;
    } while (p != q);
  }

  // handle branch chases
  if (!strcmp(args->x.chase->name, "branch")) {
    void *p = args->x.cycle[0];
    args->x.branch_chunk_size = convert_pointers_to_branches(p, 200);
#if defined(__aarch64__)
    __builtin___clear_cache(
        args->x.genchase_args->arena,
        args->x.genchase_args->arena + args->x.genchase_args->total_memory);
#endif
  }
  
  // now flush our caches
  if (args->x.cache_flush_size) {
    size_t nr_elts = args->x.cache_flush_size / sizeof(size_t);
    size_t *p = args->x.flush_arena;
    size_t sum = 0;
    while (nr_elts) {
      sum += *p;
      ++p;
      --nr_elts;
    }
    args->x.dummy += sum;
  }

  // wait and/or wake up everyone if we're all ready
  pthread_mutex_lock(&wait_mutex);
  if (nr_to_startup == 1) {
    if (!strcmp(args->x.chase->name, "branch")) {
      // Change buffer to executable before letting the chases run.
      make_buffer_executable(args->x.genchase_args->arena,
                             args->x.genchase_args->total_memory);
    }
  }
  --nr_to_startup;
  if (nr_to_startup) {
    pthread_cond_wait(&wait_cond, &wait_mutex);
  } else {
    pthread_cond_broadcast(&wait_cond);
  }
  pthread_mutex_unlock(&wait_mutex);

  args->x.chase->fn(data);
  return NULL;
}

// Assembly:
pushq %r15
pushq %r14
pushq %rbx
subq $0x80, %rsp
movq %rdi, %rbx
movl (%rdi), %edi
callq 0x402a7c
cmpb $0x0, 0x10787d(%rip)    # 0x50a3a0
jne 0x402bb3
movq %rsp, %rdx
movl $0x80, %esi
xorl %edi, %edi
callq 0x465dd0
testl %eax, %eax
jne 0x402da0
movl (%rbx), %eax
xorl %ecx, %ecx
cmpq $0x400, %rcx            # imm = 0x400
je 0x402d7c
movq %rcx, %rdx
shrq $0x6, %rdx
movq (%rsp,%rdx,8), %rdx
btq %rcx, %rdx
jae 0x402b68
testl %eax, %eax
je 0x402b6d
decl %eax
incq %rcx
jmp 0x402b44
xorps %xmm0, %xmm0
movq %rsp, %rdx
movaps %xmm0, 0x70(%rdx)
movaps %xmm0, 0x60(%rdx)
movaps %xmm0, 0x50(%rdx)
movaps %xmm0, 0x40(%rdx)
movaps %xmm0, 0x30(%rdx)
movaps %xmm0, 0x20(%rdx)
movaps %xmm0, 0x10(%rdx)
movaps %xmm0, (%rdx)
pushq $0x1
popq %rax
shlq %cl, %rax
shrl $0x6, %ecx
orq %rax, (%rsp,%rcx,8)
movl $0x80, %esi
xorl %edi, %edi
callq 0x465e40
testl %eax, %eax
jne 0x402da9
movq 0x78(%rbx), %rax
movl 0x2c(%rax), %r14d
xorl %r15d, %r15d
cmpq %r15, %r14
je 0x402bf7
movq 0x68(%rbx), %rdi
movl (%rbx), %esi
imull %r14d, %esi
addl %r15d, %esi
cmpb $0x1, 0x90(%rbx)
jne 0x402be8
movq 0x70(%rbx), %rdx
imulq %r14, %rdx
callq 0x407af1
jmp 0x402bed
callq 0x4078e8
movq %rax, 0x8(%rbx,%r15,8)
incq %r15
jmp 0x402bbe
movq 0x78(%rbx), %rax
movq 0x10(%rax), %r14
leaq 0xcf2e5(%rip), %rsi     # 0x4d1eeb
movq %r14, %rdi
callq 0x4010b8
testl %eax, %eax
jne 0x402c41
movq 0x58(%rbx), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4125e0
movq 0x8(%rbx), %rcx
movq %rcx, %rdx
movq (%rdx), %rsi
leaq (%rsi,%rax), %rdi
movq %rdi, (%rdx,%rax)
movq %rsi, %rdx
cmpq %rcx, %rsi
jne 0x402c26
movq 0x78(%rbx), %rax
movq 0x10(%rax), %r14
leaq 0xcf2e7(%rip), %rsi     # 0x4d1f2f
movq %r14, %rdi
callq 0x4010b8
testl %eax, %eax
jne 0x402c86
movq 0x58(%rbx), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4125e0
movq 0x8(%rbx), %rcx
movq %rcx, %rdx
movq (%rdx), %rsi
leaq (%rdx,%rax), %rdi
movq %rsi, (%rdx,%rax)
movq %rdi, (%rdx)
movq %rsi, %rdx
cmpq %rcx, %rsi
jne 0x402c68
movq 0x78(%rbx), %rax
movq 0x10(%rax), %r14
leaq 0xcef3c(%rip), %rsi     # 0x4d1bc9
movq %r14, %rdi
callq 0x4010b8
testl %eax, %eax
jne 0x402cad
movq 0x8(%rbx), %rdi
movl $0xc8, %esi
callq 0x4084e0
movl %eax, 0x94(%rbx)
movq 0x88(%rbx), %rax
testq %rax, %rax
je 0x402cd9
shrq $0x3, %rax
movq 0x80(%rbx), %rcx
xorl %edx, %edx
xorl %esi, %esi
cmpq %rdx, %rax
je 0x402cd6
addq (%rcx,%rdx,8), %rsi
incq %rdx
jmp 0x402cc8
addl %esi, 0x60(%rbx)
leaq 0x1076d0(%rip), %rdi    # 0x50a3b0
callq 0x42ebd0
movq 0x1076bc(%rip), %rax    # 0x50a3a8
cmpq $0x1, %rax
jne 0x402d2b
movq 0x78(%rbx), %rax
movq 0x10(%rax), %rdi
leaq 0xceec8(%rip), %rsi     # 0x4d1bc9
callq 0x4010b8
testl %eax, %eax
je 0x402d14
andq $0x0, 0x107696(%rip)    # 0x50a3a8
jmp 0x402d4c
movq 0x68(%rbx), %rax
movq (%rax), %rdi
movq 0x8(%rax), %rsi
callq 0x4083a4
movq 0x10767d(%rip), %rax    # 0x50a3a8
decq %rax
movq %rax, 0x107673(%rip)    # 0x50a3a8
je 0x402d4c
leaq 0x10769a(%rip), %rdi    # 0x50a3d8
leaq 0x10766b(%rip), %rsi    # 0x50a3b0
callq 0x42b230
jmp 0x402d58
leaq 0x107685(%rip), %rdi    # 0x50a3d8
callq 0x42abe0
leaq 0x107651(%rip), %rdi    # 0x50a3b0
callq 0x42fab0
movq 0x78(%rbx), %rax
movq %rbx, %rdi
callq *(%rax)
xorl %eax, %eax
addq $0x80, %rsp
popq %rbx
popq %r14
popq %r15
retq
movq $0x508708, %rax         # imm = 0x508708
movq (%rax), %rcx
leaq 0xcf239(%rip), %rdi     # 0x4d1fc6
pushq $0x28
popq %rsi
pushq $0x1
popq %rdx
callq 0x421e70
pushq $0x1
popq %rdi
callq 0x411e30
leaq 0xcf20d(%rip), %rdi     # 0x4d1fb4
jmp 0x402db0
leaq 0xcf23f(%rip), %rdi     # 0x4d1fef
callq 0x4130f0
jmp 0x402d98