// File: /workspace/github_c_makefile_star2_O1/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, %r13d
movl %r8d, %ebp
movl %ecx, %r15d
movl %esi, 0x2c(%rsp)
movl %edi, %r14d
testl %edx, %edx
jne 0x4031b4
movq $0x5a4868, %rax         # imm = 0x5A4868
cmpb $0x1, (%rax)
jne 0x403193
movl $0x64, %edi
callq 0x4a7d10
movq %rax, 0x1a16c5(%rip)    # 0x5a4830
movups 0x1470ec(%rip), %xmm0   # 0x54a25e
movups %xmm0, (%rax)
movups 0x1470f2(%rip), %xmm0   # 0x54a26e
movups %xmm0, 0x10(%rax)
movabsq $0x29657572742073, %rcx # imm = 0x29657572742073
movq %rcx, 0x1d(%rax)
jmp 0x40346a
movq 0x1a1656(%rip), %rdi    # 0x5a47f0
callq 0x412bcc
movq $0x0, 0x1a1646(%rip) # 0x5a47f0
callq 0x402e87
callq 0x40308e
cmpl %r15d, 0x1a1641(%rip)   # 0x5a47fc
je 0x4031ea
movl $0x64, %edi
callq 0x4a7d10
movq %rax, %r12
leaq 0x1470b2(%rip), %rdx    # 0x54a283
movl $0x64, %esi
movq %rax, %rdi
movl %r15d, %ecx
xorl %eax, %eax
callq 0x487370
movq %r12, 0x1a1646(%rip)    # 0x5a4830
leaq 0x38(%rsp), %r8
movq $0x0, (%r8)
leaq 0x1470de(%rip), %rax    # 0x54a2db
leaq 0x1470f1(%rip), %rsi    # 0x54a2f5
testb %r14b, %r14b
cmovneq %rax, %rsi
movq 0x1a15de(%rip), %rdi    # 0x5a47f0
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x40f24b
testl %eax, %eax
jne 0x4034bb
movq 0x1a15c6(%rip), %rdi    # 0x5a47f0
leaq 0x14705b(%rip), %rsi    # 0x54a28c
leaq 0x8(%rsp), %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
callq 0x40f725
testl %eax, %eax
jne 0x4034c5
movq 0x1a159e(%rip), %rdi    # 0x5a47f0
leaq 0x147060(%rip), %rsi    # 0x54a2b9
leaq 0x20(%rsp), %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
callq 0x40f725
testl %eax, %eax
jne 0x4034cc
movq 0x1a1576(%rip), %rdi    # 0x5a47f0
leaq 0x14704a(%rip), %rsi    # 0x54a2cb
leaq 0x18(%rsp), %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
callq 0x40f725
testl %eax, %eax
jne 0x4034d3
testl %r15d, %r15d
jle 0x403438
movslq %ebp, %rax
movq %rax, 0x30(%rsp)
leaq 0x40(%rsp), %r14
movq $0x0, 0x10(%rsp)
cmpl $0x2, %r13d
jl 0x4032f1
movq $0x5a4869, %rax         # imm = 0x5A4869
cmpb $0x0, (%rax)
je 0x4032f1
movq 0x20(%rsp), %rdi
callq 0x40b5c5
cmpl $0x65, %eax
jne 0x4034a6
movq 0x20(%rsp), %rdi
callq 0x40aa37
testl %eax, %eax
jne 0x40349f
movl %r15d, %ebx
testl %r13d, %r13d
jle 0x4033e8
xorl %r15d, %r15d
leaq 0x1a1509(%rip), %rdi    # 0x5a4810
movl %ebp, %esi
callq 0x404224
movq %rax, %r12
cmpl $0x0, 0x2c(%rsp)
je 0x40332c
leaq 0x1a1509(%rip), %rdi    # 0x5a4828
callq 0x40408c
xorl %edx, %edx
divl %ebx
movl %edx, %ecx
jmp 0x403335
movq 0x10(%rsp), %rax
leal (%rax,%r15), %ecx
movl $0x64, %esi
movq %r14, %rdi
leaq 0x146fca(%rip), %rdx    # 0x54a30e
xorl %eax, %eax
callq 0x487370
movq 0x8(%rsp), %rdi
movl $0x1, %esi
movq %r14, %rdx
movl $0x10, %ecx
xorl %r8d, %r8d
callq 0x40ca7c
testl %eax, %eax
jne 0x403498
movq 0x8(%rsp), %rdi
movl $0x2, %esi
movq %r12, %rdx
movl %ebp, %ecx
xorl %r8d, %r8d
callq 0x40ca7c
testl %eax, %eax
jne 0x403483
movq %r14, %rdi
callq 0x401100
addq 0x30(%rsp), %rax
addq %rax, 0x1a1468(%rip)    # 0x5a4808
movq 0x8(%rsp), %rdi
callq 0x40b5c5
cmpl $0x65, %eax
jne 0x403491
movq 0x8(%rsp), %rdi
callq 0x40ab79
testl %eax, %eax
jne 0x40347c
movq 0x8(%rsp), %rdi
callq 0x40aa37
testl %eax, %eax
jne 0x40348a
callq 0x402c78
incl %r15d
cmpl %r15d, %r13d
jne 0x403300
cmpl $0x2, %r13d
movl %ebx, %r15d
jl 0x403422
movq $0x5a4869, %rax         # imm = 0x5A4869
cmpb $0x0, (%rax)
je 0x403422
movq 0x18(%rsp), %rdi
callq 0x40b5c5
cmpl $0x65, %eax
jne 0x4034b4
movq 0x18(%rsp), %rdi
callq 0x40aa37
testl %eax, %eax
jne 0x4034ad
movq 0x10(%rsp), %rax
addl %r13d, %eax
movq %rax, 0x10(%rsp)
cmpl %r15d, %eax
jl 0x4032ba
movq 0x8(%rsp), %rdi
callq 0x40a8ca
testl %eax, %eax
jne 0x4034da
movq 0x20(%rsp), %rdi
callq 0x40a8ca
testl %eax, %eax
jne 0x4034e1
movq 0x18(%rsp), %rdi
callq 0x40a8ca
testl %eax, %eax
jne 0x4034e8
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %edi
callq 0x401c1b
movl %eax, %edi
callq 0x401bd5
movl %eax, %edi
callq 0x401c3e
movl %eax, %edi
callq 0x401bf8
movl %eax, %edi
callq 0x401bb2
movl %eax, %edi
callq 0x401b8f
movl %eax, %edi
callq 0x401b6c
movl %eax, %edi
callq 0x401c84
movl %eax, %edi
callq 0x401c61
leaq 0x38(%rsp), %rdi
callq 0x401ad4
movl %eax, %edi
callq 0x401b03
movl %eax, %edi
callq 0x401b26
movl %eax, %edi
callq 0x401b49
movl %eax, %edi
callq 0x401ca7
movl %eax, %edi
callq 0x401cca
movl %eax, %edi
callq 0x401ced