#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int uint64_t;
typedef int uint32_t;
typedef int uint16_t;
typedef scalar_t__ int64_t;
typedef scalar_t__ int32_t;
typedef scalar_t__ int16_t;

/* Variables and functions */
void AddWithCarry(uint32_t op1, uint32_t op2, uint32_t carry_in,
                  uint32_t *result, uint32_t *carry_out, uint32_t *overflow) {
  if (sizeof(uint64_t) == 8) {
    uint64_t unsigned_sum = (uint64_t)op1 + (uint64_t)op2 + (uint64_t)carry_in;
    int64_t op1_64 = (int32_t)op1;
    int64_t op2_64 = (int32_t)op2;
    int64_t signed_sum = op1_64 + op2_64 + (int64_t)carry_in;
    uint32_t _result = unsigned_sum & 0xFFFFFFFFL;
    *overflow = (signed_sum == (int32_t)_result) ? 0 : 1;
    *carry_out = (unsigned_sum == (uint64_t)_result) ? 0 : 1;
    *result = _result;
  } else {
    uint16_t op1_low = op1 & 0xFFFF;
    uint16_t op1_high = (uint32_t)op1 >> 16;
    uint16_t op2_low = op2 & 0xFFFF;
    uint16_t op2_high = (op2) >> 16;
    int low_carry;
    uint32_t ulow_sum =
        (uint32_t)op1_low + (uint32_t)op2_low + (uint32_t)carry_in;
    uint32_t low_result = ulow_sum & 0xFFFF;
    low_carry = (ulow_sum == (uint32_t)low_result) ? 0 : 1;
    uint32_t uhigh_sum =
        (uint32_t)op1_high + (uint32_t)op2_high + (uint32_t)low_carry;
    int32_t op1_high_32 = (int16_t)op1_high;
    int32_t op2_high_32 = (int16_t)op2_high;
    int32_t shigh_sum = op1_high_32 + op2_high_32 + (int32_t)low_carry;
    uint16_t high_result = uhigh_sum & 0xFFFF;
    *overflow = (shigh_sum == (int16_t)high_result) ? 0 : 1;
    *carry_out = (uhigh_sum == (uint32_t)high_result) ? 0 : 1;
    *result = op1 + op2 + carry_in;
  }
}


x86 assembly (clang, -O0):
<AddWithCarry>:
push   %rbp
mov    %rsp,%rbp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    %r9,-0x28(%rbp)
mov    -0x4(%rbp),%eax
and    $0xffff,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x4(%rbp),%eax
sar    $0x10,%eax
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%eax
and    $0xffff,%eax
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%eax
sar    $0x10,%eax
mov    %eax,-0x38(%rbp)
mov    -0x2c(%rbp),%eax
add    -0x34(%rbp),%eax
add    -0xc(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%eax
and    $0xffff,%eax
mov    %eax,-0x44(%rbp)
mov    -0x40(%rbp),%edx
mov    -0x44(%rbp),%esi
mov    $0x1,%eax
xor    %ecx,%ecx
cmp    %esi,%edx
cmove  %ecx,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x30(%rbp),%eax
add    -0x38(%rbp),%eax
add    -0x3c(%rbp),%eax
mov    %eax,-0x48(%rbp)
movslq -0x30(%rbp),%rax
mov    %rax,-0x50(%rbp)
movslq -0x38(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x50(%rbp),%rax
add    -0x58(%rbp),%rax
movslq -0x3c(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x60(%rbp)
mov    -0x48(%rbp),%eax
and    $0xffff,%eax
mov    %eax,-0x64(%rbp)
mov    -0x60(%rbp),%rdx
movslq -0x64(%rbp),%rsi
mov    $0x1,%ecx
xor    %eax,%eax
cmp    %rsi,%rdx
cmove  %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x48(%rbp),%edx
mov    -0x64(%rbp),%esi
mov    $0x1,%ecx
xor    %eax,%eax
cmp    %esi,%edx
cmove  %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x4(%rbp),%ecx
add    -0x8(%rbp),%ecx
add    -0xc(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
pop    %rbp
ret

x86 assembly (clang, -O2):
<AddWithCarry>:
push   %rbx
movzwl %di,%r10d
mov    %edi,%r11d
sar    $0x10,%r11d
movzwl %si,%ebx
add    %r10d,%ebx
mov    %esi,%eax
sar    $0x10,%eax
add    %edx,%ebx
xor    %r10d,%r10d
cmp    $0x10000,%ebx
setae  %r10b
add    %r11d,%eax
movslq %eax,%rbx
add    %r10d,%eax
add    %rbx,%r10
movzwl %ax,%r11d
xor    %ebx,%ebx
cmp    %r11,%r10
setne  %bl
mov    %ebx,(%r9)
xor    %ebx,%ebx
cmp    $0x10000,%eax
setae  %bl
mov    %ebx,(%r8)
add    %edi,%esi
add    %edx,%esi
mov    %esi,(%rcx)
pop    %rbx
ret

LLVM IR (-O0):
define void @AddWithCarry(i32 %0, i32 %1, i32 %2, i32* %3, i32* %4, i32* %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
%20 = alloca i32
%21 = alloca i64
%22 = alloca i64
%23 = alloca i64
%24 = alloca i32
store i32 %0, i32* %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i32* %3, i32** %10
store i32* %4, i32** %11
store i32* %5, i32** %12
%25 = load i32, i32* %7
%26 = and i32 %25, 65535
store i32 %26, i32* %13
%27 = load i32, i32* %7
%28 = ashr i32 %27, 16
store i32 %28, i32* %14
%29 = load i32, i32* %8
%30 = and i32 %29, 65535
store i32 %30, i32* %15
%31 = load i32, i32* %8
%32 = ashr i32 %31, 16
store i32 %32, i32* %16
%33 = load i32, i32* %13
%34 = load i32, i32* %15
%35 = add nsw i32 %33, %34
%36 = load i32, i32* %9
%37 = add nsw i32 %35, %36
store i32 %37, i32* %18
%38 = load i32, i32* %18
%39 = and i32 %38, 65535
store i32 %39, i32* %19
%40 = load i32, i32* %18
%41 = load i32, i32* %19
%42 = icmp eq i32 %40, %41
%43 = zext i1 %42 to i64
%44 = select i1 %42, i32 0, i32 1
store i32 %44, i32* %17
%45 = load i32, i32* %14
%46 = load i32, i32* %16
%47 = add nsw i32 %45, %46
%48 = load i32, i32* %17
%49 = add nsw i32 %47, %48
store i32 %49, i32* %20
%50 = load i32, i32* %14
%51 = sext i32 %50 to i64
store i64 %51, i64* %21
%52 = load i32, i32* %16
%53 = sext i32 %52 to i64
store i64 %53, i64* %22
%54 = load i64, i64* %21
%55 = load i64, i64* %22
%56 = add nsw i64 %54, %55
%57 = load i32, i32* %17
%58 = sext i32 %57 to i64
%59 = add nsw i64 %56, %58
store i64 %59, i64* %23
%60 = load i32, i32* %20
%61 = and i32 %60, 65535
store i32 %61, i32* %24
%62 = load i64, i64* %23
%63 = load i32, i32* %24
%64 = sext i32 %63 to i64
%65 = icmp eq i64 %62, %64
%66 = zext i1 %65 to i64
%67 = select i1 %65, i32 0, i32 1
%68 = load i32*, i32** %12
store i32 %67, i32* %68
%69 = load i32, i32* %20
%70 = load i32, i32* %24
%71 = icmp eq i32 %69, %70
%72 = zext i1 %71 to i64
%73 = select i1 %71, i32 0, i32 1
%74 = load i32*, i32** %11
store i32 %73, i32* %74
%75 = load i32, i32* %7
%76 = load i32, i32* %8
%77 = add nsw i32 %75, %76
%78 = load i32, i32* %9
%79 = add nsw i32 %77, %78
%80 = load i32*, i32** %10
store i32 %79, i32* %80
ret void
}

LLVM IR (-O2):
define void @AddWithCarry(i32 %0, i32 %1, i32 %2, i32* nocapture writeonly %3, i32* nocapture writeonly %4, i32* nocapture writeonly %5) {
%7 = and i32 %0, 65535
%8 = ashr i32 %0, 16
%9 = and i32 %1, 65535
%10 = ashr i32 %1, 16
%11 = add nuw nsw i32 %9, %7
%12 = add nsw i32 %11, %2
%13 = icmp ugt i32 %12, 65535
%14 = zext i1 %13 to i32
%15 = add nsw i32 %10, %8
%16 = add nsw i32 %15, %14
%17 = sext i32 %15 to i64
%18 = zext i1 %13 to i64
%19 = add nsw i64 %18, %17
%20 = and i32 %16, 65535
%21 = zext i32 %20 to i64
%22 = icmp ne i64 %19, %21
%23 = zext i1 %22 to i32
store i32 %23, i32* %5
%24 = icmp ugt i32 %16, 65535
%25 = zext i1 %24 to i32
store i32 %25, i32* %4
%26 = add nsw i32 %1, %0
%27 = add nsw i32 %26, %2
store i32 %27, i32* %3
ret void
}