// File: /workspace/github_c_makefile_star2_O3/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 0x4031c4
movq $0x5a4868, %rax         # imm = 0x5A4868
cmpb $0x1, (%rax)
jne 0x4031a3
movl $0x64, %edi
callq 0x4a7390
movq %rax, 0x1a16b5(%rip)    # 0x5a4830
movups 0x1470dc(%rip), %xmm0   # 0x54a25e
movups %xmm0, (%rax)
movups 0x1470e2(%rip), %xmm0   # 0x54a26e
movups %xmm0, 0x10(%rax)
movabsq $0x29657572742073, %rcx # imm = 0x29657572742073
movq %rcx, 0x1d(%rax)
jmp 0x40348c
movq 0x1a1646(%rip), %rdi    # 0x5a47f0
callq 0x412b4b
movq $0x0, 0x1a1636(%rip) # 0x5a47f0
callq 0x402e97
callq 0x40309e
cmpl %r15d, 0x1a1631(%rip)   # 0x5a47fc
je 0x4031fa
movl $0x64, %edi
callq 0x4a7390
movq %rax, %r12
leaq 0x1470a2(%rip), %rdx    # 0x54a283
movl $0x64, %esi
movq %rax, %rdi
movl %r15d, %ecx
xorl %eax, %eax
callq 0x4869f0
movq %r12, 0x1a1636(%rip)    # 0x5a4830
leaq 0x38(%rsp), %r8
movq $0x0, (%r8)
leaq 0x1470ce(%rip), %rax    # 0x54a2db
leaq 0x1470e1(%rip), %rsi    # 0x54a2f5
testb %r14b, %r14b
cmovneq %rax, %rsi
movq 0x1a15ce(%rip), %rdi    # 0x5a47f0
xorl %edx, %edx
xorl %ecx, %ecx
callq 0x40f472
testl %eax, %eax
jne 0x4034dd
movq 0x1a15b6(%rip), %rdi    # 0x5a47f0
leaq 0x14704b(%rip), %rsi    # 0x54a28c
leaq 0x8(%rsp), %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
callq 0x40f8b1
testl %eax, %eax
jne 0x4034e7
movq 0x1a158e(%rip), %rdi    # 0x5a47f0
leaq 0x147050(%rip), %rsi    # 0x54a2b9
leaq 0x20(%rsp), %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
callq 0x40f8b1
testl %eax, %eax
jne 0x4034ee
movq 0x1a1566(%rip), %rdi    # 0x5a47f0
leaq 0x14703a(%rip), %rsi    # 0x54a2cb
leaq 0x18(%rsp), %rcx
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
xorl %r8d, %r8d
callq 0x40f8b1
testl %eax, %eax
jne 0x4034f5
testl %r15d, %r15d
jle 0x40345a
movslq %ebp, %rax
movq %rax, 0x30(%rsp)
movq $0x5a4869, %rax         # imm = 0x5A4869
movb (%rax), %al
leaq 0x40(%rsp), %r14
movq $0x0, 0x10(%rsp)
cmpl $0x2, %r13d
jl 0x403303
testb $0x1, %al
je 0x403303
movq 0x20(%rsp), %rdi
callq 0x40b77e
cmpl $0x65, %eax
jne 0x4034c1
movq 0x20(%rsp), %rdi
callq 0x40abee
testl %eax, %eax
je 0x40330c
jmp 0x4034c8
testl %r13d, %r13d
jle 0x40343b
movl %r15d, %ebx
xorl %r15d, %r15d
leaq 0x1a14f7(%rip), %rdi    # 0x5a4810
movl %ebp, %esi
callq 0x404210
movq %rax, %r12
cmpl $0x0, 0x2c(%rsp)
je 0x40333e
leaq 0x1a14f7(%rip), %rdi    # 0x5a4828
callq 0x404078
xorl %edx, %edx
divl %ebx
movl %edx, %ecx
jmp 0x403347
movq 0x10(%rsp), %rax
leal (%rax,%r15), %ecx
movl $0x64, %esi
movq %r14, %rdi
leaq 0x146fb8(%rip), %rdx    # 0x54a30e
xorl %eax, %eax
callq 0x4869f0
movq 0x8(%rsp), %rdi
movl $0x1, %esi
movq %r14, %rdx
movl $0x10, %ecx
xorl %r8d, %r8d
callq 0x40cc86
testl %eax, %eax
jne 0x4034b3
movq 0x8(%rsp), %rdi
movl $0x2, %esi
movq %r12, %rdx
movl %ebp, %ecx
xorl %r8d, %r8d
callq 0x40cc86
testl %eax, %eax
jne 0x40349e
movq %r14, %rdi
callq 0x401100
addq 0x30(%rsp), %rax
addq %rax, 0x1a1456(%rip)    # 0x5a4808
movq 0x8(%rsp), %rdi
callq 0x40b77e
cmpl $0x65, %eax
jne 0x4034a5
movq 0x8(%rsp), %rdi
callq 0x40ad30
testl %eax, %eax
jne 0x4034ba
movq 0x8(%rsp), %rdi
callq 0x40abee
testl %eax, %eax
jne 0x4034ac
callq 0x402c88
incl %r15d
cmpl %r15d, %r13d
jne 0x403312
cmpl $0x2, %r13d
setl %cl
movq $0x5a4869, %rax         # imm = 0x5A4869
movb (%rax), %al
movl %eax, %edx
xorb $0x1, %dl
orb %cl, %dl
movl %ebx, %r15d
jne 0x403444
movq 0x18(%rsp), %rdi
callq 0x40b77e
cmpl $0x65, %eax
jne 0x4034cf
movq 0x18(%rsp), %rdi
callq 0x40abee
testl %eax, %eax
jne 0x4034d6
movq $0x5a4869, %rax         # imm = 0x5A4869
movb (%rax), %al
movq 0x10(%rsp), %rcx
addl %r13d, %ecx
movq %rcx, 0x10(%rsp)
cmpl %r15d, %ecx
jl 0x4032d3
movq 0x8(%rsp), %rdi
callq 0x40aa81
testl %eax, %eax
jne 0x4034fc
movq 0x20(%rsp), %rdi
callq 0x40aa81
testl %eax, %eax
jne 0x403503
movq 0x18(%rsp), %rdi
callq 0x40aa81
testl %eax, %eax
jne 0x40350a
addq $0xa8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %eax, %edi
callq 0x401bef
movl %eax, %edi
callq 0x401c12
movl %eax, %edi
callq 0x401c58
movl %eax, %edi
callq 0x401bcc
movl %eax, %edi
callq 0x401c35
movl %eax, %edi
callq 0x401b86
movl %eax, %edi
callq 0x401ba9
movl %eax, %edi
callq 0x401c7b
movl %eax, %edi
callq 0x401c9e
leaq 0x38(%rsp), %rdi
callq 0x401aee
movl %eax, %edi
callq 0x401b1d
movl %eax, %edi
callq 0x401b40
movl %eax, %edi
callq 0x401b63
movl %eax, %edi
callq 0x401cc1
movl %eax, %edi
callq 0x401ce4
movl %eax, %edi
callq 0x401d07