void test_lock(void) {
  signed char sc[2];
  unsigned char uc[2];
  signed short ss[2];
  unsigned short us[2];
  signed int si[2];
  unsigned int ui[2];
  signed long sl[2];
  unsigned long ul[2];
  signed long long sll[2];
  unsigned long long ull[2];
  sc[1] = __sync_lock_test_and_set(&sc[1], -1);
  uc[1] = __sync_lock_test_and_set(&uc[1], -1);
  ss[1] = __sync_lock_test_and_set(&ss[1], -1);
  us[1] = __sync_lock_test_and_set(&us[1], -1);
  si[1] = __sync_lock_test_and_set(&si[1], -1);
  ui[1] = __sync_lock_test_and_set(&ui[1], -1);
  sl[1] = __sync_lock_test_and_set(&sl[1], -1);
  ul[1] = __sync_lock_test_and_set(&ul[1], -1);
  sll[1] = __sync_lock_test_and_set(&sll[1], -1);
  ull[1] = __sync_lock_test_and_set(&ull[1], -1);
}


x86 assembly (clang, -O0):
<test_lock>:
push   %rbp
mov    %rsp,%rbp
mov    $0xff,%al
mov    %al,%cl
xchg   %cl,-0x1(%rbp)
mov    %cl,-0x1(%rbp)
xchg   %al,-0x3(%rbp)
mov    %al,-0x3(%rbp)
mov    $0xffff,%ax
mov    %ax,%cx
xchg   %cx,-0x6(%rbp)
mov    %cx,-0x6(%rbp)
xchg   %ax,-0xa(%rbp)
mov    %ax,-0xa(%rbp)
mov    $0xffffffff,%eax
mov    %eax,%ecx
xchg   %ecx,-0x10(%rbp)
mov    %ecx,-0x10(%rbp)
xchg   %eax,-0x18(%rbp)
mov    %eax,-0x18(%rbp)
mov    $0xffffffffffffffff,%rax
mov    %rax,%rcx
xchg   %rcx,-0x28(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %rax,%rcx
xchg   %rcx,-0x38(%rbp)
mov    %rcx,-0x38(%rbp)
mov    %rax,%rcx
xchg   %rcx,-0x48(%rbp)
mov    %rcx,-0x48(%rbp)
xchg   %rax,-0x58(%rbp)
mov    %rax,-0x58(%rbp)
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_lock>:
mov    $0xff,%al
mov    $0xff,%cl
xchg   %cl,-0x61(%rsp)
mov    %cl,-0x61(%rsp)
xchg   %al,-0x63(%rsp)
mov    %al,-0x63(%rsp)
mov    $0xffff,%ax
mov    $0xffff,%cx
xchg   %cx,-0x5a(%rsp)
mov    %cx,-0x5a(%rsp)
xchg   %ax,-0x5e(%rsp)
mov    %ax,-0x5e(%rsp)
mov    $0xffffffff,%eax
mov    $0xffffffff,%ecx
xchg   %ecx,-0x4c(%rsp)
mov    %ecx,-0x4c(%rsp)
xchg   %eax,-0x54(%rsp)
mov    %eax,-0x54(%rsp)
mov    $0xffffffffffffffff,%rax
xchg   %rax,-0x20(%rsp)
mov    $0xffffffffffffffff,%rcx
mov    %rax,-0x20(%rsp)
mov    $0xffffffffffffffff,%rax
xchg   %rax,-0x30(%rsp)
mov    %rax,-0x30(%rsp)
mov    $0xffffffffffffffff,%rax
xchg   %rax,-0x40(%rsp)
mov    %rax,-0x40(%rsp)
xchg   %rcx,-0x10(%rsp)
ret

LLVM IR (-O0):
define void @test_lock() {
%1 = alloca [2 x i8]
%2 = alloca [2 x i8]
%3 = alloca [2 x i16]
%4 = alloca [2 x i16]
%5 = alloca [2 x i32]
%6 = alloca [2 x i32]
%7 = alloca [2 x i64]
%8 = alloca [2 x i64]
%9 = alloca [2 x i64]
%10 = alloca [2 x i64]
%11 = getelementptr inbounds [2 x i8], [2 x i8]* %1, i64 0, i64 1
%12 = atomicrmw xchg i8* %11, i8 -1 seq_cst
%13 = getelementptr inbounds [2 x i8], [2 x i8]* %1, i64 0, i64 1
store i8 %12, i8* %13
%14 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 1
%15 = atomicrmw xchg i8* %14, i8 -1 seq_cst
%16 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 1
store i8 %15, i8* %16
%17 = getelementptr inbounds [2 x i16], [2 x i16]* %3, i64 0, i64 1
%18 = atomicrmw xchg i16* %17, i16 -1 seq_cst
%19 = getelementptr inbounds [2 x i16], [2 x i16]* %3, i64 0, i64 1
store i16 %18, i16* %19
%20 = getelementptr inbounds [2 x i16], [2 x i16]* %4, i64 0, i64 1
%21 = atomicrmw xchg i16* %20, i16 -1 seq_cst
%22 = getelementptr inbounds [2 x i16], [2 x i16]* %4, i64 0, i64 1
store i16 %21, i16* %22
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%24 = atomicrmw xchg i32* %23, i32 -1 seq_cst
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
store i32 %24, i32* %25
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%27 = atomicrmw xchg i32* %26, i32 -1 seq_cst
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
store i32 %27, i32* %28
%29 = getelementptr inbounds [2 x i64], [2 x i64]* %7, i64 0, i64 1
%30 = atomicrmw xchg i64* %29, i64 -1 seq_cst
%31 = getelementptr inbounds [2 x i64], [2 x i64]* %7, i64 0, i64 1
store i64 %30, i64* %31
%32 = getelementptr inbounds [2 x i64], [2 x i64]* %8, i64 0, i64 1
%33 = atomicrmw xchg i64* %32, i64 -1 seq_cst
%34 = getelementptr inbounds [2 x i64], [2 x i64]* %8, i64 0, i64 1
store i64 %33, i64* %34
%35 = getelementptr inbounds [2 x i64], [2 x i64]* %9, i64 0, i64 1
%36 = atomicrmw xchg i64* %35, i64 -1 seq_cst
%37 = getelementptr inbounds [2 x i64], [2 x i64]* %9, i64 0, i64 1
store i64 %36, i64* %37
%38 = getelementptr inbounds [2 x i64], [2 x i64]* %10, i64 0, i64 1
%39 = atomicrmw xchg i64* %38, i64 -1 seq_cst
%40 = getelementptr inbounds [2 x i64], [2 x i64]* %10, i64 0, i64 1
store i64 %39, i64* %40
ret void
}

LLVM IR (-O2):
define void @test_lock() {
%1 = alloca [2 x i8]
%2 = alloca [2 x i8]
%3 = alloca [2 x i16]
%4 = alloca [2 x i16]
%5 = alloca [2 x i32]
%6 = alloca [2 x i32]
%7 = alloca [2 x i64]
%8 = alloca [2 x i64]
%9 = alloca [2 x i64]
%10 = alloca [2 x i64]
%11 = getelementptr inbounds [2 x i8], [2 x i8]* %1, i64 0, i64 0
%12 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 0
%13 = bitcast [2 x i16]* %3 to i8*
%14 = bitcast [2 x i16]* %4 to i8*
%15 = bitcast [2 x i32]* %5 to i8*
%16 = bitcast [2 x i32]* %6 to i8*
%17 = bitcast [2 x i64]* %7 to i8*
%18 = bitcast [2 x i64]* %8 to i8*
%19 = bitcast [2 x i64]* %9 to i8*
%20 = bitcast [2 x i64]* %10 to i8*
%21 = getelementptr inbounds [2 x i8], [2 x i8]* %1, i64 0, i64 1
%22 = atomicrmw xchg i8* %21, i8 -1 seq_cst
store i8 %22, i8* %21
%23 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 1
%24 = atomicrmw xchg i8* %23, i8 -1 seq_cst
store i8 %24, i8* %23
%25 = getelementptr inbounds [2 x i16], [2 x i16]* %3, i64 0, i64 1
%26 = atomicrmw xchg i16* %25, i16 -1 seq_cst
store i16 %26, i16* %25
%27 = getelementptr inbounds [2 x i16], [2 x i16]* %4, i64 0, i64 1
%28 = atomicrmw xchg i16* %27, i16 -1 seq_cst
store i16 %28, i16* %27
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%30 = atomicrmw xchg i32* %29, i32 -1 seq_cst
store i32 %30, i32* %29
%31 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%32 = atomicrmw xchg i32* %31, i32 -1 seq_cst
store i32 %32, i32* %31
%33 = getelementptr inbounds [2 x i64], [2 x i64]* %7, i64 0, i64 1
%34 = atomicrmw xchg i64* %33, i64 -1 seq_cst
store i64 %34, i64* %33
%35 = getelementptr inbounds [2 x i64], [2 x i64]* %8, i64 0, i64 1
%36 = atomicrmw xchg i64* %35, i64 -1 seq_cst
store i64 %36, i64* %35
%37 = getelementptr inbounds [2 x i64], [2 x i64]* %9, i64 0, i64 1
%38 = atomicrmw xchg i64* %37, i64 -1 seq_cst
store i64 %38, i64* %37
%39 = getelementptr inbounds [2 x i64], [2 x i64]* %10, i64 0, i64 1
%40 = atomicrmw xchg i64* %39, i64 -1 seq_cst
ret void
}