// File: /workspace/github_c_makefile_star2_O2/redis-force[P]rpm/module/rpm.c
// Function: rpm_worker_destroy
static void rpm_worker_destroy(RedisModuleCtx *ctx, rpm_context *rpm, worker_process *worker) {
  /* send worker dead error to pending requests of current worker */
  int64_t request_id;
  size_t ignored;
  int32_t idx;

  RedisModuleBlockedClient *bc = NULL;
  redis_response_reader *reader;

  hash_map_iterator *iterator = hash_map_iterator_create(worker->request_id_to_client);
  while (hash_map_iterator_next(iterator) != 0) {
    hash_map_iterator_get(iterator, (const void **) &request_id, &ignored, (const void **) &bc, &ignored);
    RedisModule_UnblockClient(bc, (void *) RPM_BAD_GATEWAY);
  }
  hash_map_iterator_destroy(iterator);
  hash_map_destroy(worker->request_id_to_client);
  hash_map_destroy(worker->client_id_to_request_id);
  if (worker->downstreams != NULL) {
    for (idx = 0; idx < worker->num_downstreams; ++idx) {
      if (worker->downstreams[idx] != 0) {
        close(worker->downstreams[idx]);
        worker->downstreams[idx] = 0;
      }
    }
    RedisModule_Free(worker->downstreams);
  }
  if (worker->upstreams != NULL) {
    for (idx = 0; idx < worker->num_upstreams; ++idx) {
      rpm_worker_close_pipe(ctx, worker->upstreams + idx);
    }
    RedisModule_Free(worker->upstreams);
  }
  if (worker->readers != NULL) {
    for (idx = 0; idx < worker->num_upstreams; ++idx) {
      if ((reader = worker->readers[idx]) != NULL) {
        redis_response_reader_free(reader);
      }
    }
    RedisModule_Free(worker->readers);
  }
  rpm_worker_close_pipe(ctx, &worker->out);
  rpm_worker_close_pipe(ctx, &worker->err);
  RedisModule_Log(ctx, "warning", "worker process is terminated prematurely, a new worker is being spawned");
  /* failed to create worker during initialization, setup a timer to try it again 10 ms later */
  RedisModule_CreateTimeEvent(ctx, 10, rpm_worker_create_later, rpm, NULL, &rpm->restart_timer);
  rpm->restart_count--;
  RedisModule_Free(worker);
  if (++rpm->restart_count >= rpm->retry_attempts) {
    RedisModule_Log(ctx, "warning", "worker has been stopped %d times within last %d seconds, shutdown redis automatically", rpm->restart_count, rpm->retry_timeout);
    rpm_context_shutdown_redis(ctx, rpm);
  }
  rpm->worker = NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movq %rdx, %r15
movq %rsi, 0x10(%rsp)
movq %rdi, 0x8(%rsp)
movq %rsp, %rax
andq $0x0, (%rax)
movq 0x98(%rdx), %rdi
callq 0x53f0
movq %rax, %r13
leaq 0x20(%rsp), %rbp
leaq 0x18(%rsp), %rbx
movq 0x6cd5(%rip), %r14      # 0xde70
movq 0x6d76(%rip), %r12      # 0xdf18
movq %r13, %rdi
callq 0x52b0
testb %al, %al
je 0x71cf
movq %r13, %rdi
movq %rbp, %rsi
movq %rbx, %rdx
movq %rsp, %rcx
movq %rbx, %r8
callq 0x5070
movq (%rsp), %rdi
movq (%r12), %rsi
callq *(%r14)
jmp 0x71a2
movq %r13, %rdi
callq 0x5340
movq 0x98(%r15), %rdi
callq 0x5120
movq 0xa0(%r15), %rdi
callq 0x5120
movq 0x80(%r15), %rdi
testq %rdi, %rdi
movq 0x10(%rsp), %r12
movq 0x8(%rsp), %r13
je 0x7237
xorl %ebx, %ebx
movslq 0x78(%r15), %rax
cmpq %rax, %rbx
jge 0x722e
movl (%rdi,%rbx,4), %eax
testl %eax, %eax
je 0x7229
movl %eax, %edi
callq 0x5140
movq 0x80(%r15), %rdi
andl $0x0, (%rdi,%rbx,4)
incq %rbx
jmp 0x7207
movq 0x69e3(%rip), %rax      # 0xdc18
callq *(%rax)
cmpq $0x0, 0x90(%r15)
je 0x727d
xorl %ebx, %ebx
xorl %r14d, %r14d
movslq 0x88(%r15), %rax
cmpq %rax, %r14
jge 0x726d
movq 0x90(%r15), %rsi
addq %rbx, %rsi
movq %r13, %rdi
callq 0x7417
incq %r14
addq $0x38, %rbx
jmp 0x7246
movq 0x69a4(%rip), %rax      # 0xdc18
movq 0x90(%r15), %rdi
callq *(%rax)
cmpq $0x0, 0x70(%r15)
je 0x72b6
xorl %ebx, %ebx
movslq 0x88(%r15), %rax
cmpq %rax, %rbx
jge 0x72a9
movq 0x70(%r15), %rax
movq (%rax,%rbx,8), %rdi
testq %rdi, %rdi
je 0x72a4
callq 0x51a0
incq %rbx
jmp 0x7286
movq 0x6968(%rip), %rax      # 0xdc18
movq 0x70(%r15), %rdi
callq *(%rax)
movq %r13, %rdi
movq %r15, %rsi
callq 0x7417
leaq 0x38(%r15), %rsi
movq %r13, %rdi
callq 0x7417
movq 0x6b94(%rip), %rbx      # 0xde68
leaq 0x3af9(%rip), %rsi      # 0xadd4
leaq 0x3f0f(%rip), %rdx      # 0xb1f1
movq %r13, %rdi
xorl %eax, %eax
callq *(%rbx)
movq 0x6ad8(%rip), %rax      # 0xddc8
leaq 0x50(%r12), %r9
leaq 0x3ad(%rip), %rdx       # 0x76a9
pushq $0xa
popq %rsi
movq %r13, %rdi
movq %r12, %rcx
xorl %r8d, %r8d
callq *(%rax)
decl 0x30(%r12)
movq 0x6902(%rip), %rax      # 0xdc18
movq %r15, %rdi
callq *(%rax)
movl 0x30(%r12), %ecx
incl %ecx
movl %ecx, 0x30(%r12)
cmpl 0x38(%r12), %ecx
jl 0x7381
movl 0x34(%r12), %r8d
leaq 0x3a9a(%rip), %rsi      # 0xadd4
leaq 0x3ef8(%rip), %rdx      # 0xb239
movq %r13, %rdi
xorl %eax, %eax
callq *(%rbx)
movq 0x48(%r12), %rax
testq %rax, %rax
leaq 0x3f38(%rip), %rsi      # 0xb28f
cmovneq %rax, %rsi
movq 0x6c8e(%rip), %rcx      # 0xdff0
leaq 0x3f90(%rip), %rdx      # 0xb2f9
movq %r13, %rdi
xorl %eax, %eax
callq *(%rcx)
testq %rax, %rax
je 0x7381
movq 0x6bc4(%rip), %rcx      # 0xdf40
movq %rax, %rdi
callq *(%rcx)
andq $0x0, 0x10(%r12)
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq