// File: /workspace/github_c_makefile_star2_O3/redis-force[P]rpm/module/rpm.c
// Function: rpm_worker_create
static worker_process *rpm_worker_create(RedisModuleCtx *ctx, rpm_context *rpm) {
  int32_t idx;
  int status;
  time_t now;
  pid_t pid;
  worker_process *worker = RedisModule_Calloc(1, sizeof(worker_process));
  worker->request_id_to_client = hash_map_create(&int_key_map, 1024);
  worker->client_id_to_request_id = hash_map_create(&int_key_map, 1024);
  worker->num_downstreams = rpm->num_downstreams;
  worker->num_upstreams = rpm->num_upstreams;
  if ((worker->downstreams = RedisModule_Calloc(1, sizeof(worker->downstreams[0]) * worker->num_downstreams)) == NULL) {
    RedisModule_Log(ctx, "warning", "Could not allocate enough memory for downstreams, restart worker again");
    goto cleanup_exit;
  }
  for (idx = 0; idx < worker->num_downstreams; ++idx) {
    if (RedisModule_CreateClient(ctx, worker->downstreams + idx) != REDISMODULE_OK) {
      RedisModule_Log(ctx, "warning", "Could not create client for downstream %d, restart worker again", idx);
      goto cleanup_exit;
    }
  }
  if ((worker->readers = RedisModule_Calloc(1, sizeof(worker->readers[0]) * worker->num_upstreams)) == NULL) {
    RedisModule_Log(ctx, "warning", "Could not allocate enough memory for response reader, restart worker again");
    goto cleanup_exit;
  }
  if ((worker->upstreams = RedisModule_Calloc(1, sizeof(worker->upstreams[0]) * worker->num_upstreams)) == NULL) {
    RedisModule_Log(ctx, "warning", "Could not allocate enough memory for response reader, restart worker again");
    goto cleanup_exit;
  }
  for (idx = 0; idx < worker->num_upstreams; ++idx) {
    worker->readers[idx] = redis_response_reader_create(rpm->allocator, 1024 * 1024);
    if (rpm_worker_pipe_init(ctx, worker, worker->upstreams + idx, rpm_worker_upstream_read, 1)) {
      RedisModule_Log(ctx, "warning", "Could not initiate upstream pipe %d, restart worker again", idx);
      goto cleanup_exit;
    }
    worker->upstreams[idx].user_data = worker->readers[idx];
  }
  worker->active_upstreams = worker->num_upstreams;
  if (rpm_worker_pipe_init(ctx, worker, &worker->out, rpm_worker_stdout_read, 0) ||
      rpm_worker_pipe_init(ctx, worker, &worker->err, rpm_worker_stderr_read, 0)) {
    RedisModule_Log(ctx, "warning", "Could not initiate stdout and stderr pipe, restart worker again");
    goto cleanup_exit;
  }

  time(&now);
  if (now >= rpm->restart_count_reset_time && (now - rpm->restart_count_reset_time) > rpm->retry_timeout) {
    rpm->restart_count_reset_time = now;
    rpm->restart_count = 0;
  }

  if ((pid = fork()) == 0) {
    rpm_worker_process_start(worker, rpm->argv);
  } else if (pid == -1) {
    RedisModule_Log(ctx, "warning", "Could not create new worker process, restart worker again: %s", strerror(errno));
    goto cleanup_exit;
  } else {
    status = 0;
    waitpid(pid, &status, 0);
    if (WEXITSTATUS(status) != 0) {
      RedisModule_Log(ctx, "warning", "Could not daemonize worker process, restart worker again: %s", strerror(errno));
      goto cleanup_exit;
    }
    for (idx = 0; idx < worker->num_upstreams; ++idx) {
      if (worker->upstreams[idx].pipe[1] != 0) {
        close(worker->upstreams[idx].pipe[1]);
        worker->upstreams[idx].pipe[1] = 0;
      }
    }
    close(worker->out.pipe[1]);
    worker->out.pipe[1] = 0;
    close(worker->err.pipe[1]);
    worker->err.pipe[1] = 0;
    for (idx = 0; idx < worker->num_downstreams; ++idx) {
      if (worker->downstreams[idx] != 0) {
        close(worker->downstreams[idx]);
        worker->downstreams[idx] = 0;
      }
    }
  }
  return worker;
cleanup_exit:
  rpm_worker_destroy(ctx, rpm, worker);
  return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r14
movq %rdi, %r15
movq 0x7676(%rip), %r13      # 0xdc58
movl $0x1, %edi
movl $0x1000a8, %esi         # imm = 0x1000A8
callq *(%r13)
movq %rax, %rbx
leaq 0x7c6e(%rip), %r12      # 0xe268
movl $0x400, %esi            # imm = 0x400
movq %r12, %rdi
callq 0x5240
movq %rax, 0x98(%rbx)
movl $0x400, %esi            # imm = 0x400
movq %r12, %rdi
callq 0x5240
movq %rax, 0xa0(%rbx)
movslq 0x28(%r14), %rsi
movl %esi, 0x78(%rbx)
movl 0x2c(%r14), %eax
movl %eax, 0x88(%rbx)
shlq $0x2, %rsi
movl $0x1, %edi
callq *(%r13)
movq %rax, 0x80(%rbx)
testq %rax, %rax
je 0x67ad
movq %r14, (%rsp)
cmpl $0x0, 0x78(%rbx)
jle 0x668e
xorl %ebp, %ebp
movq 0x77d5(%rip), %r14      # 0xde38
xorl %r12d, %r12d
movq 0x80(%rbx), %rsi
addq %rbp, %rsi
movq %r15, %rdi
callq *(%r14)
testl %eax, %eax
jne 0x67c4
incq %r12
movslq 0x78(%rbx), %rax
addq $0x4, %rbp
cmpq %rax, %r12
jl 0x6666
movslq 0x88(%rbx), %rsi
shlq $0x3, %rsi
movl $0x1, %edi
callq *(%r13)
movq %rax, 0x70(%rbx)
testq %rax, %rax
je 0x67e6
movslq 0x88(%rbx), %rax
imulq $0x38, %rax, %rsi
movl $0x1, %edi
callq *(%r13)
movq %rax, 0x90(%rbx)
testq %rax, %rax
movq (%rsp), %r14
je 0x6808
movl 0x88(%rbx), %eax
testl %eax, %eax
jle 0x6750
xorl %ebp, %ebp
leaq 0x69c(%rip), %r13       # 0x6d86
xorl %r12d, %r12d
movq 0x20(%r14), %rdi
movl $0x100000, %esi         # imm = 0x100000
callq 0x5350
movq 0x70(%rbx), %rcx
movq %rax, (%rcx,%r12,8)
movq 0x90(%rbx), %rdx
addq %rbp, %rdx
movq %r15, %rdi
movq %rbx, %rsi
movq %r13, %rcx
movl $0x1, %r8d
callq 0x6cdd
testl %eax, %eax
jne 0x6846
movq 0x70(%rbx), %rax
movq 0x90(%rbx), %rcx
movq (%rax,%r12,8), %rax
movq %rax, 0x30(%rcx,%rbp)
incq %r12
movslq 0x88(%rbx), %rax
addq $0x38, %rbp
cmpq %rax, %r12
jl 0x66ed
movl %eax, 0x8c(%rbx)
leaq 0x8b8(%rip), %rcx       # 0x7015
movq %r15, %rdi
movq %rbx, %rsi
movq %rbx, %rdx
xorl %r8d, %r8d
callq 0x6cdd
testl %eax, %eax
jne 0x6796
movq %rbx, %rdx
addq $0x38, %rdx
leaq 0x8a1(%rip), %rcx       # 0x7021
movq %r15, %rdi
movq %rbx, %rsi
xorl %r8d, %r8d
callq 0x6cdd
testl %eax, %eax
je 0x6868
movq 0x76cb(%rip), %rcx      # 0xde68
leaq 0x4630(%rip), %rsi      # 0xadd4
leaq 0x4834(%rip), %rdx      # 0xafdf
jmp 0x681d
movq 0x76b4(%rip), %rcx      # 0xde68
leaq 0x4619(%rip), %rsi      # 0xadd4
leaq 0x4711(%rip), %rdx      # 0xaed3
jmp 0x681d
movq 0x769d(%rip), %r8       # 0xde68
leaq 0x4602(%rip), %rsi      # 0xadd4
leaq 0x4741(%rip), %rdx      # 0xaf1a
movq %r15, %rdi
movl %r12d, %ecx
xorl %eax, %eax
callq *(%r8)
jmp 0x6802
movq 0x767b(%rip), %rcx      # 0xde68
leaq 0x45e0(%rip), %rsi      # 0xadd4
leaq 0x475f(%rip), %rdx      # 0xaf5a
movq %r15, %rdi
xorl %eax, %eax
callq *(%rcx)
movq (%rsp), %r14
jmp 0x6824
movq 0x7659(%rip), %rcx      # 0xde68
leaq 0x45be(%rip), %rsi      # 0xadd4
leaq 0x473d(%rip), %rdx      # 0xaf5a
movq %r15, %rdi
xorl %eax, %eax
callq *(%rcx)
movq %r15, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x72ef
xorl %ebx, %ebx
movq %rbx, %rax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x761b(%rip), %r8       # 0xde68
leaq 0x4580(%rip), %rsi      # 0xadd4
leaq 0x474a(%rip), %rdx      # 0xafa5
movq %r15, %rdi
movl %r12d, %ecx
xorl %eax, %eax
callq *(%r8)
jmp 0x6824
leaq 0x10(%rsp), %r12
movq %r12, %rdi
callq 0x5230
movq (%r12), %rax
movq %rax, %rcx
subq 0x40(%r14), %rcx
jl 0x6897
movslq 0x34(%r14), %rdx
cmpq %rdx, %rcx
jle 0x6897
movq %rax, 0x40(%r14)
movl $0x0, 0x30(%r14)
callq 0x53d0
cmpl $-0x1, %eax
je 0x68f0
testl %eax, %eax
je 0x69cb
leaq 0xc(%rsp), %r12
movl $0x0, (%r12)
movl %eax, %edi
movq %r12, %rsi
xorl %edx, %edx
callq 0x52f0
cmpb $0x0, 0x1(%r12)
je 0x6924
movq 0x7597(%rip), %rax      # 0xde68
movq (%rax), %r14
callq 0x5030
movl (%rax), %edi
callq 0x53a0
leaq 0x44ed(%rip), %rsi      # 0xadd4
leaq 0x476f(%rip), %rdx      # 0xb05d
jmp 0x6914
movq 0x7571(%rip), %rax      # 0xde68
movq (%rax), %r14
callq 0x5030
movl (%rax), %edi
callq 0x53a0
leaq 0x44c7(%rip), %rsi      # 0xadd4
leaq 0x470b(%rip), %rdx      # 0xb01f
movq %r15, %rdi
movq %rax, %rcx
xorl %eax, %eax
callq *%r14
jmp 0x6802
movl 0x88(%rbx), %eax
testl %eax, %eax
jle 0x696f
movq 0x90(%rbx), %rcx
movl $0x28, %r14d
xorl %r15d, %r15d
movl (%rcx,%r14), %edi
testl %edi, %edi
je 0x6960
callq 0x5130
movq 0x90(%rbx), %rcx
movl $0x0, (%rcx,%r14)
movl 0x88(%rbx), %eax
incq %r15
movslq %eax, %rdx
addq $0x38, %r14
cmpq %rdx, %r15
jl 0x693e
movl 0x28(%rbx), %edi
callq 0x5130
xorl %ebp, %ebp
movl %ebp, 0x28(%rbx)
movl 0x60(%rbx), %edi
callq 0x5130
movl %ebp, 0x60(%rbx)
movl 0x78(%rbx), %eax
testl %eax, %eax
jle 0x6834
movq 0x80(%rbx), %rcx
xorl %r14d, %r14d
movl (%rcx,%r14,4), %edi
testl %edi, %edi
je 0x69bb
callq 0x5130
movq 0x80(%rbx), %rcx
movl $0x0, (%rcx,%r14,4)
movl 0x78(%rbx), %eax
incq %r14
movslq %eax, %rdx
cmpq %rdx, %r14
jl 0x699c
jmp 0x6834
movq (%r14), %rsi
movq %rbx, %rdi
callq 0x702d