unsigned long bn_add_words(unsigned long *rp, unsigned long *ap,
                           unsigned long *bp, int n) {
  unsigned long ret, i;
  if (n <= 0)
    return 0;
  asm("	subq	%2,%2		\n"
      ".align 16			\n"
      "1:	movq	(%4,%2,8),%0	\n"
      "	adcq	(%5,%2,8),%0	\n"
      "	movq	%0,(%3,%2,8)	\n"
      "	leaq	1(%2),%2	\n"
      "	loop	1b		\n"
      "	sbbq	%0,%0		\n"
      : "+a"(ret), "+c"(n), "+r"(i)
      : "r"(rp), "r"(ap), "r"(bp)
      : "cc");
  return ret & 1;
}


x86 assembly (clang, -O0):
<bn_add_words>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jg     112a <bn_add_words+0x2a>
movq   $0x0,-0x8(%rbp)
jmp    1187 <bn_add_words+0x87>
mov    -0x30(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    -0x38(%rbp),%rdx
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%r8
sub    %rdx,%rdx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    (%rdi,%rdx,8),%rax
adc    (%r8,%rdx,8),%rax
mov    %rax,(%rsi,%rdx,8)
lea    0x1(%rdx),%rdx
loop   1150 <bn_add_words+0x50>
sbb    %rax,%rax
mov    %rdx,-0x40(%rbp)
mov    %rax,%rdx
mov    -0x40(%rbp),%rax
mov    %rdx,-0x30(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rax
and    $0x1,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
pop    %rbp
ret

x86 assembly (clang, -O2):
<bn_add_words>:
test   %ecx,%ecx
jle    1129 <bn_add_words+0x29>
sub    %rdx,%rdx
nopw   0x0(%rax,%rax,1)
mov    (%rsi,%rdx,8),%rax
adc    (%rdx,%rdx,8),%rax
mov    %rax,(%rdi,%rdx,8)
lea    0x1(%rdx),%rdx
loop   1110 <bn_add_words+0x10>
sbb    %rax,%rax
and    $0x1,%eax
ret
xor    %eax,%eax
ret

LLVM IR (-O0):
define i64 @bn_add_words(i64* %0, i64* %1, i64* %2, i32 %3) {
%5 = alloca i64
%6 = alloca i64*
%7 = alloca i64*
%8 = alloca i64*
%9 = alloca i32
%10 = alloca i64
%11 = alloca i64
store i64* %0, i64** %6
store i64* %1, i64** %7
store i64* %2, i64** %8
store i32 %3, i32* %9
%12 = load i32, i32* %9
%13 = icmp sle i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %4
store i64 0, i64* %5
br label %28
15: ; preds = %4
%16 = load i64, i64* %10
%17 = load i32, i32* %9
%18 = load i64, i64* %11
%19 = load i64*, i64** %6
%20 = load i64*, i64** %7
%21 = load i64*, i64** %8
%22 = call { i64, i32, i64 } asm "\09subq\09$2,$2\09\09\0A.align 16\09\09\09\0A1:\09movq\09($4,$2,8),$0\09\0A\09adcq\09($5,$2,8),$0\09\0A\09movq\09$0,($3,$2,8)\09\0A\09leaq\091($2),$2\09\0A\09loop\091b\09\09\0A\09sbbq\09$0,$0\09\09\0A", "={ax},={cx},=r,r,r,r,0,1,2,~{cc},~{dirflag},~{fpsr},~{flags}"(i64* %19, i64* %20, i64* %21, i64 %16, i32 %17, i64 %18)
%23 = extractvalue { i64, i32, i64 } %22, 0
%24 = extractvalue { i64, i32, i64 } %22, 1
%25 = extractvalue { i64, i32, i64 } %22, 2
store i64 %23, i64* %10
store i32 %24, i32* %9
store i64 %25, i64* %11
%26 = load i64, i64* %10
%27 = and i64 %26, 1
store i64 %27, i64* %5
br label %28
28: ; preds = %15, %14
%29 = load i64, i64* %5
ret i64 %29
}

LLVM IR (-O2):
define i64 @bn_add_words(i64* %0, i64* %1, i64* %2, i32 %3) {
%5 = icmp slt i32 %3, 1
br i1 %5, label %10, label %6
6: ; preds = %4
%7 = tail call { i64, i32, i64 } asm "\09subq\09$2,$2\09\09\0A.align 16\09\09\09\0A1:\09movq\09($4,$2,8),$0\09\0A\09adcq\09($5,$2,8),$0\09\0A\09movq\09$0,($3,$2,8)\09\0A\09leaq\091($2),$2\09\0A\09loop\091b\09\09\0A\09sbbq\09$0,$0\09\09\0A", "={ax},={cx},=r,r,r,r,0,1,2,~{cc},~{dirflag},~{fpsr},~{flags}"(i64* %0, i64* %1, i64* %2, i64 undef, i32 %3, i64 undef)
%8 = extractvalue { i64, i32, i64 } %7, 0
%9 = and i64 %8, 1
br label %10
10: ; preds = %4, %6
%11 = phi i64 [ %9, %6 ], [ 0, %4 ]
ret i64 %11
}