// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xa8, %rsp
movl %r9d, %r15d
movl %r8d, %ebp
movl %ecx, %r13d
movl %esi, 0x2c(%rsp)
movl %edi, %ebx
testl %edx, %edx
jne 0x40291c
movq $0x597868, %rax         # imm = 0x597868
cmpb $0x1, (%rax)
jne 0x4028fe
pushq $0x64
popq %rdi
callq 0x495830
movq %rax, 0x194f5a(%rip)    # 0x597830
movups 0x135981(%rip), %xmm0   # 0x53825e
movups %xmm0, (%rax)
movups 0x135987(%rip), %xmm0   # 0x53826e
movups %xmm0, 0x10(%rax)
movabsq $0x29657572742073, %rcx # imm = 0x29657572742073
movq %rcx, 0x1d(%rax)
jmp 0x402bba
movq 0x194eeb(%rip), %rdi    # 0x5977f0
callq 0x4103ae
andq $0x0, 0x194ede(%rip)    # 0x5977f0
callq 0x4025f3
callq 0x402802
cmpl %r13d, 0x194ed9(%rip)   # 0x5977fc
je 0x402952
pushq $0x64
popq %r14
movq %r14, %rdi
callq 0x495830
movq %rax, %r12
leaq 0x135948(%rip), %rdx    # 0x538283
movq %rax, %rdi
movq %r14, %rsi
movl %r13d, %ecx
xorl %eax, %eax
callq 0x474e90
movq %r12, 0x194ede(%rip)    # 0x597830
movl %r13d, 0x14(%rsp)
leaq 0x38(%rsp), %r14
andq $0x0, (%r14)
leaq 0x135974(%rip), %rax    # 0x5382db
leaq 0x135987(%rip), %rsi    # 0x5382f5
testb %bl, %bl
cmovneq %rax, %rsi
movq 0x194e75(%rip), %rdi    # 0x5977f0
xorl %edx, %edx
xorl %ecx, %ecx
movq %r14, %r8
callq 0x40d53b
movq (%r14), %rsi
movl %eax, %edi
callq 0x402df4
movq 0x194e58(%rip), %rdi    # 0x5977f0
leaq 0x1358ed(%rip), %rsi    # 0x53828c
pushq $-0x1
popq %rbx
leaq 0x8(%rsp), %rcx
movl %ebx, %edx
xorl %r8d, %r8d
callq 0x40d861
movl %eax, %edi
callq 0x401e32
movq 0x194e31(%rip), %rdi    # 0x5977f0
leaq 0x1358f3(%rip), %rsi    # 0x5382b9
leaq 0x20(%rsp), %rcx
movl %ebx, %edx
xorl %r8d, %r8d
callq 0x40d861
movl %eax, %edi
callq 0x401e32
movq 0x194e0d(%rip), %rdi    # 0x5977f0
leaq 0x1358e1(%rip), %rsi    # 0x5382cb
leaq 0x18(%rsp), %rcx
movl %ebx, %edx
xorl %r8d, %r8d
callq 0x40d861
movl %eax, %edi
callq 0x401e32
movslq %ebp, %r13
testl %r15d, %r15d
movl $0x0, %r14d
cmovgl %r15d, %r14d
leaq 0x40(%rsp), %rbx
xorl %eax, %eax
movl %r15d, 0x28(%rsp)
cmpl 0x14(%rsp), %eax
jge 0x402b87
movq %rax, 0x30(%rsp)
cmpl $0x2, %r15d
jl 0x402a5f
movq $0x597869, %rax         # imm = 0x597869
cmpb $0x0, (%rax)
je 0x402a5f
movq 0x20(%rsp), %rdi
callq 0x40a229
movl %eax, %edi
callq 0x402e28
movq 0x20(%rsp), %rdi
callq 0x409808
movl %eax, %edi
callq 0x401e32
xorl %r15d, %r15d
cmpl %r15d, %r14d
je 0x402b41
leaq 0x194d9e(%rip), %rdi    # 0x597810
movl %ebp, %esi
callq 0x403b0f
movq %rax, %r12
cmpl $0x0, 0x2c(%rsp)
je 0x402a99
leaq 0x194d9e(%rip), %rdi    # 0x597828
callq 0x4039a8
xorl %edx, %edx
divl 0x14(%rsp)
movl %edx, %ecx
jmp 0x402aa2
movq 0x30(%rsp), %rax
leal (%rax,%r15), %ecx
movq %rbx, %rdi
pushq $0x64
popq %rsi
leaq 0x13585f(%rip), %rdx    # 0x53830e
xorl %eax, %eax
callq 0x474e90
movq 0x8(%rsp), %rdi
pushq $0x1
popq %rsi
movq %rbx, %rdx
pushq $0x10
popq %rcx
xorl %r8d, %r8d
callq 0x40b284
movl %eax, %edi
callq 0x401e32
movq 0x8(%rsp), %rdi
pushq $0x2
popq %rsi
movq %r12, %rdx
movl %ebp, %ecx
xorl %r8d, %r8d
callq 0x40b284
movl %eax, %edi
callq 0x401e32
movq %rbx, %rdi
callq 0x401100
addq %r13, %rax
addq %rax, 0x194d07(%rip)    # 0x597808
movq 0x8(%rsp), %rdi
callq 0x40a229
movl %eax, %edi
callq 0x402e28
movq 0x8(%rsp), %rdi
callq 0x409934
movl %eax, %edi
callq 0x401e32
movq 0x8(%rsp), %rdi
callq 0x409808
movl %eax, %edi
callq 0x401e32
callq 0x401b98
incl %r15d
jmp 0x402a62
movl 0x28(%rsp), %r15d
cmpl $0x2, %r15d
jl 0x402b7a
movq $0x597869, %rax         # imm = 0x597869
cmpb $0x0, (%rax)
je 0x402b7a
movq 0x18(%rsp), %rdi
callq 0x40a229
movl %eax, %edi
callq 0x402e28
movq 0x18(%rsp), %rdi
callq 0x409808
movl %eax, %edi
callq 0x401e32
movq 0x30(%rsp), %rax
addl %r15d, %eax
jmp 0x402a1c
movq 0x8(%rsp), %rdi
callq 0x40969e
movl %eax, %edi
callq 0x401e32
movq 0x20(%rsp), %rdi
callq 0x40969e
movl %eax, %edi
callq 0x401e32
movq 0x18(%rsp), %rdi
callq 0x40969e
movl %eax, %edi
callq 0x401e32
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq