int main(void) {
  long d = 20, s = 2;
  long xadd_r_r_res;
  __asm__ __volatile__("xadd %1, %0"
                       : "=r"(d), "=r"(s)
                       : "0"(d), "1"(s)
                       : "flags");
  xadd_r_r_res = s + d;
  assert(xadd_r_r_res == 42);
  if (xadd_r_r_res == 42)
    printf("success\n");
  else
    printf("failure\n");
  return xadd_r_r_res;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x14,-0x10(%rbp)
movq   $0x2,-0x18(%rbp)
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%rax
xadd   %rax,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x2a,-0x20(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
mov    $0x0,%al
call   1040 <assert@plt>
cmpq   $0x2a,-0x20(%rbp)
jne    1191 <main+0x71>
lea    0xe7b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    119f <main+0x7f>
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    $0x14,%eax
mov    $0x2,%ebx
xadd   %rbx,%rax
add    %rax,%rbx
xor    %edi,%edi
cmp    $0x2a,%rbx
sete   %dil
lea    0xec2(%rip),%rax
lea    0xeb3(%rip),%r14
cmove  %rax,%r14
xor    %eax,%eax
call   1040 <assert@plt>
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [9 x i8] c"success\0A\00"
@.str.1 = [9 x i8] c"failure\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
store i32 0, i32* %1
store i64 20, i64* %2
store i64 2, i64* %3
%5 = load i64, i64* %2
%6 = load i64, i64* %3
%7 = call { i64, i64 } asm sideeffect "xadd $1, $0", "=r,=r,0,1,~{flags},~{dirflag},~{fpsr},~{flags}"(i64 %5, i64 %6)
%8 = extractvalue { i64, i64 } %7, 0
%9 = extractvalue { i64, i64 } %7, 1
store i64 %8, i64* %2
store i64 %9, i64* %3
%10 = load i64, i64* %3
%11 = load i64, i64* %2
%12 = add nsw i64 %10, %11
store i64 %12, i64* %4
%13 = load i64, i64* %4
%14 = icmp eq i64 %13, 42
%15 = zext i1 %14 to i32
%16 = call i32 (i32, ...) bitcast (i32 (...)* @assert to i32 (i32, ...)*)(i32 %15)
%17 = load i64, i64* %4
%18 = icmp eq i64 %17, 42
br i1 %18, label %19, label %21
19: ; preds = %0
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %23
21: ; preds = %0
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %21, %19
%24 = load i64, i64* %4
%25 = trunc i64 %24 to i32
ret i32 %25
}

LLVM IR (-O2):
@str = [8 x i8] c"failure\00"
@str.2 = [8 x i8] c"success\00"
define i32 @main() {
%1 = tail call { i64, i64 } asm sideeffect "xadd $1, $0", "=r,=r,0,1,~{flags},~{dirflag},~{fpsr},~{flags}"(i64 20, i64 2)
%2 = extractvalue { i64, i64 } %1, 0
%3 = extractvalue { i64, i64 } %1, 1
%4 = add nsw i64 %3, %2
%5 = icmp eq i64 %4, 42
%6 = zext i1 %5 to i32
%7 = tail call i32 (i32, ...) bitcast (i32 (...)* @assert to i32 (i32, ...)*)(i32 %6)
%8 = select i1 %5, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @str.2, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0)
%9 = tail call i32 @puts(i8* dereferenceable(1) %8)
%10 = trunc i64 %4 to i32
ret i32 %10
}