// File: /workspace/github_c_makefile_star2_O2/google[P]multichase/multiload.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);
    }
  }
  if (args->x.run_test_type == RUN_CHASE) {
    // 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);
      } 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);
    }
    // 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;
    }
  } else {
    if (verbosity > 2)
      printf("thread_start(%d) memload generate buffers\n", args->x.thread_num);
    // generate buffers
    args->x.load_arena = (char *)alloc_arena_mmap(
                             page_size, use_thp,
                             args->x.load_total_memory + args->x.load_offset,
                             -1) + args->x.load_offset;
    memset(args->x.load_arena, 1,
           args->x.load_total_memory);  // ensure pages are mapped
  }

  if (verbosity > 2)
    printf("thread_start(%d) wait and/or wake up everyone\n",
           args->x.thread_num);
  // wait and/or wake up everyone if we're all ready
  pthread_mutex_lock(&wait_mutex);
  --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);

  if (args->x.run_test_type == RUN_CHASE) {
    if (verbosity > 2) printf("thread_start: C(%d)\n", args->x.thread_num);
    args->x.chase->fn(data);
  } else {
    if (verbosity > 2) printf("thread_start: M(%d)\n", args->x.thread_num);
    args->x.memload->fn(data);
  }
  return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x88, %rsp
movq %rdi, %rbx
movl (%rdi), %edi
callq 0x4032a9
cmpb $0x0, 0x10b07f(%rip)    # 0x50e3d0
jne 0x4033e1
movq %rsp, %rdx
movl $0x80, %esi
xorl %edi, %edi
callq 0x468090
testl %eax, %eax
jne 0x403619
movl (%rbx), %eax
xorl %ecx, %ecx
cmpq $0x400, %rcx            # imm = 0x400
je 0x4035f5
movq %rcx, %rdx
shrq $0x6, %rdx
movq (%rsp,%rdx,8), %rdx
btq %rcx, %rdx
jae 0x403396
testl %eax, %eax
je 0x40339b
decl %eax
incq %rcx
jmp 0x403372
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 0x468100
testl %eax, %eax
jne 0x403622
cmpl $0x0, 0x9c(%rbx)
je 0x40344c
cmpl $0x3, 0x10af4f(%rip)    # 0x50e340
jl 0x403403
movl (%rbx), %esi
leaq 0xd1298(%rip), %rdi     # 0x4d4694
xorl %eax, %eax
callq 0x415070
movq 0x10af26(%rip), %rdi    # 0x50e330
movzbl 0x10af33(%rip), %esi    # 0x50e344
movq 0xb8(%rbx), %rdx
addq 0xb0(%rbx), %rdx
pushq $-0x1
popq %rcx
callq 0x4091c8
addq 0xb8(%rbx), %rax
movq %rax, 0xa8(%rbx)
movq 0xb0(%rbx), %rdx
pushq $0x1
popq %rsi
movq %rax, %rdi
callq 0x4010b0
jmp 0x403547
movq 0x80(%rbx), %rax
movl 0x2c(%rax), %r14d
xorl %r15d, %r15d
cmpq %r15, %r14
je 0x40348e
movq 0x70(%rbx), %rdi
movl (%rbx), %esi
imull %r14d, %esi
addl %r15d, %esi
cmpb $0x1, 0x98(%rbx)
jne 0x40347f
movq %r14, %rdx
callq 0x408d1d
jmp 0x403484
callq 0x408b14
movq %rax, 0x10(%rbx,%r15,8)
incq %r15
jmp 0x40345a
movq 0x80(%rbx), %rax
movq 0x10(%rax), %r14
leaq 0xd0f10(%rip), %rsi     # 0x4d43b0
movq %r14, %rdi
callq 0x4010c0
testl %eax, %eax
jne 0x4034de
movq 0x60(%rbx), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x414790
movq 0x10(%rbx), %rcx
movq %rcx, %rdx
movq (%rdx), %rsi
leaq (%rsi,%rax), %rdi
movq %rdi, (%rdx,%rax)
movq %rsi, %rdx
cmpq %rcx, %rsi
jne 0x4034c0
movq 0x80(%rbx), %rax
movq 0x10(%rax), %r14
leaq 0xd0f0f(%rip), %rsi     # 0x4d43f4
movq %r14, %rdi
callq 0x4010c0
testl %eax, %eax
jne 0x40351b
movq 0x60(%rbx), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x414790
movq 0x10(%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 0x403505
movq 0x90(%rbx), %rax
testq %rax, %rax
je 0x403547
shrq $0x3, %rax
movq 0x88(%rbx), %rcx
xorl %edx, %edx
xorl %esi, %esi
cmpq %rdx, %rax
je 0x403544
addq (%rcx,%rdx,8), %rsi
incq %rdx
jmp 0x403536
addl %esi, 0x68(%rbx)
cmpl $0x3, 0x10adf2(%rip)    # 0x50e340
jl 0x403560
movl (%rbx), %esi
leaq 0xd1166(%rip), %rdi     # 0x4d46bf
xorl %eax, %eax
callq 0x415070
leaq 0x10ae79(%rip), %rdi    # 0x50e3e0
callq 0x430e90
decq 0x10ae65(%rip)          # 0x50e3d8
je 0x40358a
leaq 0x10ae8c(%rip), %rdi    # 0x50e408
leaq 0x10ae5d(%rip), %rsi    # 0x50e3e0
callq 0x42d4f0
jmp 0x403596
leaq 0x10ae77(%rip), %rdi    # 0x50e408
callq 0x42cea0
leaq 0x10ae43(%rip), %rdi    # 0x50e3e0
callq 0x431d70
movl 0x9c(%rbx), %ebp
cmpl $0x3, 0x10ad91(%rip)    # 0x50e340
jl 0x4035ce
testl %ebp, %ebp
leaq 0xd1134(%rip), %rax     # 0x4d46ee
leaq 0xd1142(%rip), %rdi     # 0x4d4703
cmoveq %rax, %rdi
movl (%rbx), %esi
xorl %eax, %eax
callq 0x415070
xorl %eax, %eax
testl %ebp, %ebp
setne %al
shll $0x5, %eax
movq 0x80(%rbx,%rax), %rax
movq %rbx, %rdi
callq *(%rax)
xorl %eax, %eax
addq $0x88, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq $0x50c728, %rax         # imm = 0x50C728
movq (%rax), %rcx
leaq 0xd1053(%rip), %rdi     # 0x4d4659
pushq $0x28
popq %rsi
pushq $0x1
popq %rdx
callq 0x424020
pushq $0x1
popq %rdi
callq 0x413fe0
leaq 0xd1027(%rip), %rdi     # 0x4d4647
jmp 0x403629
leaq 0xd1059(%rip), %rdi     # 0x4d4682
callq 0x4152a0
jmp 0x403611