// File: /workspace/github_c_makefile_star2_O0/ukontainer[P]sqlite-bench/benchmark.c
// Function: benchmark_write
void benchmark_write(bool write_sync, int order, int state,
                  int num_entries, int value_size, int entries_per_batch) {
  /* Create new database if state == FRESH */
  if (state == FRESH) {
    if (FLAGS_use_existing_db) {
      message_ = malloc(sizeof(char) * 100);
      strcpy(message_, "skipping (--use_existing_db is true)");
      return;
    }
    sqlite3_close(db_);
    db_ = NULL;
    benchmark_open();
    start();
  }

  if (num_entries != num_) {
    char* msg = malloc(sizeof(char) * 100);
    snprintf(msg, 100, "(%d ops)", num_entries);
    message_ = msg;
  }

  char* err_msg = NULL;
  int status;

  sqlite3_stmt *replace_stmt, *begin_trans_stmt, *end_trans_stmt;
  char* replace_str = "REPLACE INTO test (key, value) VALUES (?, ?)";
  char* begin_trans_str = "BEGIN TRANSACTION";
  char* end_trans_str = "END TRANSACTION";

  /* Check for synchronous flag in options */
  char* sync_stmt = (write_sync) ? "PRAGMA synchronous = FULL" :
                                    "PRAGMA synchronous = OFF";
  status = sqlite3_exec(db_, sync_stmt, NULL, NULL, &err_msg);
  exec_error_check(status, err_msg);

  /* Preparing sqlite3 statements */
  status = sqlite3_prepare_v2(db_, replace_str, -1,
                              &replace_stmt, NULL);
  error_check(status);
  status = sqlite3_prepare_v2(db_, begin_trans_str, -1,
                              &begin_trans_stmt, NULL);
  error_check(status);
  status = sqlite3_prepare_v2(db_, end_trans_str, -1,
                              &end_trans_stmt, NULL);
  error_check(status);

  bool transaction = (entries_per_batch > 1);
  for (int i = 0; i < num_entries; i += entries_per_batch) {
    /* Begin write transaction */
    if (FLAGS_transaction && transaction) {
      status = sqlite3_step(begin_trans_stmt);
      step_error_check(status);
      status = sqlite3_reset(begin_trans_stmt);
      error_check(status);
    }

    /* Create and execute SQL statements */
    for (int j = 0; j < entries_per_batch; j++) {
      const char* value = rand_gen_generate(&gen_, value_size);

      /* Create values for key-value pair */
      const int k = (order == SEQUENTIAL) ? i + j :
                    (rand_next(&rand_) % num_entries);
      char key[100];
      snprintf(key, sizeof(key), "%016d", k);

      /* Bind KV values into replace_stmt */
      status = sqlite3_bind_blob(replace_stmt, 1, key, 16, SQLITE_STATIC);
      error_check(status);
      status = sqlite3_bind_blob(replace_stmt, 2, value,
                                  value_size, SQLITE_STATIC);
      error_check(status);

      /* Execute replace_stmt */
      bytes_ += value_size + strlen(key);
      status = sqlite3_step(replace_stmt);
      step_error_check(status);

      /* Reset SQLite statement for another use */
      status = sqlite3_clear_bindings(replace_stmt);
      error_check(status);
      status = sqlite3_reset(replace_stmt);
      error_check(status);

      finished_single_op();
    }

    /* End write transaction */
    if (FLAGS_transaction && transaction) {
      status = sqlite3_step(end_trans_stmt);
      step_error_check(status);
      status = sqlite3_reset(end_trans_stmt);
      error_check(status);
    }
  }

  status = sqlite3_finalize(replace_stmt);
  error_check(status);
  status = sqlite3_finalize(begin_trans_stmt);
  error_check(status);
  status = sqlite3_finalize(end_trans_stmt);
  error_check(status);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x100, %rsp            # imm = 0x100
movb %dil, %al
andb $0x1, %al
movb %al, -0x1(%rbp)
movl %esi, -0x8(%rbp)
movl %edx, -0xc(%rbp)
movl %ecx, -0x10(%rbp)
movl %r8d, -0x14(%rbp)
movl %r9d, -0x18(%rbp)
cmpl $0x0, -0xc(%rbp)
jne 0x402940
movq $0x5d2968, %rax         # imm = 0x5D2968
testb $0x1, (%rax)
je 0x40291f
movl $0x64, %edi
callq 0x4d4850
movq %rax, 0x1d0029(%rip)    # 0x5d2930
movq 0x1d0022(%rip), %rdi    # 0x5d2930
leaq 0x174949(%rip), %rsi    # 0x57725e
callq 0x401020
jmp 0x402c94
movq 0x1cffca(%rip), %rdi    # 0x5d28f0
callq 0x416bd0
movq $0x0, 0x1cffba(%rip) # 0x5d28f0
callq 0x402540
callq 0x402800
movl -0x10(%rbp), %eax
cmpl 0x1cffb3(%rip), %eax    # 0x5d28fc
je 0x40297e
movl $0x64, %edi
callq 0x4d4850
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rdi
movl -0x10(%rbp), %ecx
movl $0x64, %esi
leaq 0x174917(%rip), %rdx    # 0x577283
movb $0x0, %al
callq 0x4b4180
movq -0x20(%rbp), %rax
movq %rax, 0x1cffb2(%rip)    # 0x5d2930
movq $0x0, -0x28(%rbp)
leaq 0x1748ff(%rip), %rax    # 0x57728c
movq %rax, -0x50(%rbp)
leaq 0x174921(%rip), %rax    # 0x5772b9
movq %rax, -0x58(%rbp)
leaq 0x174928(%rip), %rax    # 0x5772cb
movq %rax, -0x60(%rbp)
movb -0x1(%rbp), %dl
leaq 0x174944(%rip), %rax    # 0x5772f5
leaq 0x174923(%rip), %rcx    # 0x5772db
testb $0x1, %dl
cmovneq %rcx, %rax
movq %rax, -0x68(%rbp)
movq 0x1cff26(%rip), %rdi    # 0x5d28f0
movq -0x68(%rbp), %rsi
xorl %eax, %eax
movl %eax, %ecx
leaq -0x28(%rbp), %r8
movq %rcx, %rdx
callq 0x412850
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %edi
movq -0x28(%rbp), %rsi
callq 0x4031e0
movq 0x1cfefc(%rip), %rdi    # 0x5d28f0
movq -0x50(%rbp), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
leaq -0x38(%rbp), %rcx
xorl %eax, %eax
movl %eax, %r8d
callq 0x412c70
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %edi
callq 0x401f20
movq 0x1cfed3(%rip), %rdi    # 0x5d28f0
movq -0x58(%rbp), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
leaq -0x40(%rbp), %rcx
xorl %eax, %eax
movl %eax, %r8d
callq 0x412c70
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %edi
callq 0x401f20
movq 0x1cfeaa(%rip), %rdi    # 0x5d28f0
movq -0x60(%rbp), %rsi
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
leaq -0x48(%rbp), %rcx
xorl %eax, %eax
movl %eax, %r8d
callq 0x412c70
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %edi
callq 0x401f20
cmpl $0x1, -0x18(%rbp)
setg %al
andb $0x1, %al
movb %al, -0x69(%rbp)
movl $0x0, -0x70(%rbp)
movl -0x70(%rbp), %eax
cmpl -0x10(%rbp), %eax
jge 0x402c58
movq $0x5d2969, %rax         # imm = 0x5D2969
testb $0x1, (%rax)
je 0x402ac1
testb $0x1, -0x69(%rbp)
je 0x402ac1
movq -0x40(%rbp), %rdi
callq 0x40e590
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %edi
callq 0x403230
movq -0x40(%rbp), %rdi
callq 0x40cda0
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %edi
callq 0x401f20
movl $0x0, -0x74(%rbp)
movl -0x74(%rbp), %eax
cmpl -0x18(%rbp), %eax
jge 0x402c0e
movl -0x14(%rbp), %esi
leaq 0x1cfe32(%rip), %rdi    # 0x5d2910
callq 0x404960
movq %rax, -0x80(%rbp)
cmpl $0x0, -0x8(%rbp)
jne 0x402afb
movl -0x70(%rbp), %edx
addl -0x74(%rbp), %edx
movl %edx, -0xf4(%rbp)
jmp 0x402b12
leaq 0x1cfe26(%rip), %rdi    # 0x5d2928
callq 0x404740
xorl %edx, %edx
divl -0x10(%rbp)
movl %edx, -0xf4(%rbp)
movl -0xf4(%rbp), %eax
movl %eax, -0x84(%rbp)
leaq -0xf0(%rbp), %rdi
movl -0x84(%rbp), %ecx
movl $0x64, %esi
leaq 0x1747d7(%rip), %rdx    # 0x57730e
movb $0x0, %al
callq 0x4b4180
movq -0x38(%rbp), %rdi
leaq -0xf0(%rbp), %rdx
movl $0x1, %esi
movl $0x10, %ecx
xorl %eax, %eax
movl %eax, %r8d
callq 0x40f4a0
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %edi
callq 0x401f20
movq -0x38(%rbp), %rdi
movq -0x80(%rbp), %rdx
movl -0x14(%rbp), %ecx
movl $0x2, %esi
xorl %eax, %eax
movl %eax, %r8d
callq 0x40f4a0
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %edi
callq 0x401f20
movslq -0x14(%rbp), %rax
movq %rax, -0x100(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x4010f8
movq %rax, %rcx
movq -0x100(%rbp), %rax
addq %rcx, %rax
addq 0x1cfd50(%rip), %rax    # 0x5d2908
movq %rax, 0x1cfd49(%rip)    # 0x5d2908
movq -0x38(%rbp), %rdi
callq 0x40e590
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %edi
callq 0x403230
movq -0x38(%rbp), %rdi
callq 0x40cfd0
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %edi
callq 0x401f20
movq -0x38(%rbp), %rdi
callq 0x40cda0
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %edi
callq 0x401f20
callq 0x401b60
movl -0x74(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x74(%rbp)
jmp 0x402ac8
movq $0x5d2969, %rax         # imm = 0x5D2969
testb $0x1, (%rax)
je 0x402c48
testb $0x1, -0x69(%rbp)
je 0x402c48
movq -0x48(%rbp), %rdi
callq 0x40e590
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %edi
callq 0x403230
movq -0x48(%rbp), %rdi
callq 0x40cda0
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %edi
callq 0x401f20
jmp 0x402c4a
movl -0x18(%rbp), %eax
addl -0x70(%rbp), %eax
movl %eax, -0x70(%rbp)
jmp 0x402a7b
movq -0x38(%rbp), %rdi
callq 0x40cb40
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %edi
callq 0x401f20
movq -0x40(%rbp), %rdi
callq 0x40cb40
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %edi
callq 0x401f20
movq -0x48(%rbp), %rdi
callq 0x40cb40
movl %eax, -0x2c(%rbp)
movl -0x2c(%rbp), %edi
callq 0x401f20
addq $0x100, %rsp            # imm = 0x100
popq %rbp
retq
nopl (%rax)