// File: /workspace/github_cpp_makefile_star2_O2/A-deLuna[P]encora-cpp-test/main.cpp
// Function: testBitwiseAdd()
void testBitwiseAdd() {
  struct TestCase {
    int a;
    int b;
    int result;
    bool overflow;
  };
  // I know designated initializers are not part of the standard yet...
  // but I feel they make the code a lot better and they're supported by
  // clang and gcc.
  TestCase cases[] = {
      {.a = 0, .b = 0, .result = 0, .overflow = false},
      {.a = 1, .b = 1, .result = 2, .overflow = false},
      {.a = -1, .b = -1, .result = -2, .overflow = false},
      {.a = -1, .b = 1, .result = 0, .overflow = false},
      {.a = -10, .b = 5, .result = -5, .overflow = false},
      {.a = 10, .b = -5, .result = 5, .overflow = false},

      {.a = std::numeric_limits<int>::max(), .b = 1, .overflow = true},
      {.a = std::numeric_limits<int>::min(), .b = -1, .overflow = true},
  };

  int failedCases = 0;
  for (const TestCase& c : cases) {
    bool threw = false;
    int result = 0;
    try {
      result = add(c.a, c.b);
    } catch (const std::overflow_error& e) {
      threw = true;
    }
    if (c.overflow && !threw) {
      std::cout << "error on test case add(" << c.a << ", " << c.b << ");\n";
      std::cout << "expected exception and did not throw.\n";
      failedCases++;
      continue;
    }
    if (!c.overflow && threw) {
      std::cout << "error on test case add(" << c.a << ", " << c.b << ");\n";
      std::cout << "received exception but did not expect any.\n";
      failedCases++;
      continue;
    }

    if (result != c.result) {
      std::cout << "result of add(" << c.a << ", " << c.b << ") was " << result
                << "\n";
      std::cout << "but expected " << c.result << "\n";
      failedCases++;
      continue;
    }
  }
  if (failedCases == 0) {
    std::cout << "bitwise add: All cases succeeded\n";
  } else {
    std::cout << "bitwise add: failed a total of " << failedCases << " cases\n";
  }
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
xorl %r14d, %r14d
leaq 0x15f7(%rip), %rbx      # 0x3080
xorl %r15d, %r15d
cmpq $0x80, %r14
je 0x1c52
movl (%r14,%rbx), %ebp
movl 0x4(%r14,%rbx), %r13d
movl %ebp, %edi
movl %r13d, %esi
callq 0x12ec
movl %eax, %r12d
cmpb $0x1, 0xc(%r14,%rbx)
jne 0x1b40
movq 0x4516(%rip), %r12      # 0x5fd8
movq %r12, %rdi
leaq 0x1678(%rip), %rsi      # 0x3144
callq 0x10d0
movq %rax, %rdi
movl %ebp, %esi
callq 0x1150
movq %rax, %rdi
leaq 0x1677(%rip), %rsi      # 0x315c
callq 0x10d0
movq %rax, %rdi
movl %r13d, %esi
callq 0x1150
movq %rax, %rdi
leaq 0x1660(%rip), %rsi      # 0x315f
callq 0x10d0
movq %r12, %rdi
leaq 0x1655(%rip), %rsi      # 0x3163
callq 0x10d0
incl %r15d
jmp 0x1bf1
movq %rax, %rdi
cmpl $0x1, %edx
jne 0x1c9b
callq 0x1060
callq 0x1130
xorl %r12d, %r12d
cmpb $0x0, 0xc(%r14,%rbx)
je 0x1bfa
movq %rbx, %rax
movl %r15d, %ebx
movl 0x8(%r14,%rax), %r15d
cmpl %r15d, %r12d
jne 0x1b58
movl %ebx, %r15d
jmp 0x1bea
movq 0x4479(%rip), %rdi      # 0x5fd8
leaq 0x1650(%rip), %rsi      # 0x31b6
callq 0x10d0
movq %rax, %rdi
movl %ebp, %esi
callq 0x1150
movq %rax, %rdi
leaq 0x15dd(%rip), %rsi      # 0x315c
callq 0x10d0
movq %rax, %rdi
movl %r13d, %esi
callq 0x1150
movq %rax, %rdi
leaq 0x162c(%rip), %rsi      # 0x31c5
callq 0x10d0
movq %rax, %rdi
movl %r12d, %esi
callq 0x1150
movq %rax, %rdi
leaq 0x15d5(%rip), %r12      # 0x3188
movq %r12, %rsi
callq 0x10d0
movq 0x4416(%rip), %rdi      # 0x5fd8
leaq 0x1603(%rip), %rsi      # 0x31cc
callq 0x10d0
movq %rax, %rdi
movl %r15d, %esi
callq 0x1150
movq %rax, %rdi
movq %r12, %rsi
callq 0x10d0
movl %ebx, %r15d
incl %r15d
leaq 0x148f(%rip), %rbx      # 0x3080
addq $0x10, %r14
jmp 0x1a8c
movq 0x43d7(%rip), %r12      # 0x5fd8
movq %r12, %rdi
leaq 0x1539(%rip), %rsi      # 0x3144
callq 0x10d0
movq %rax, %rdi
movl %ebp, %esi
callq 0x1150
movq %rax, %rdi
leaq 0x1538(%rip), %rsi      # 0x315c
callq 0x10d0
movq %rax, %rdi
movl %r13d, %esi
callq 0x1150
movq %rax, %rdi
leaq 0x1521(%rip), %rsi      # 0x315f
callq 0x10d0
movq %r12, %rdi
leaq 0x153d(%rip), %rsi      # 0x318a
jmp 0x1b0e
movq 0x437f(%rip), %rdi      # 0x5fd8
testl %r15d, %r15d
je 0x1c81
leaq 0x1597(%rip), %rsi      # 0x31fc
callq 0x10d0
movq %rax, %rdi
movl %r15d, %esi
callq 0x1150
leaq 0x15a0(%rip), %rsi      # 0x321c
movq %rax, %rdi
jmp 0x1c88
leaq 0x1552(%rip), %rsi      # 0x31da
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x10d0
callq 0x1160