int __attribute__((ms_abi)) foo(int n, ...) {
  __builtin_ms_va_list ap;
  int sum = 0;
  __builtin_ms_va_start(ap, n);
  while (n--) {
    sum += __builtin_va_arg(ap, int);
    printf("sum = %d\n", sum);
  }
  __builtin_ms_va_end(ap);
  return sum;
}


x86 assembly (clang, -O0):
<foo>:
push   %rbp
mov    %rsp,%rbp
push   %rsi
push   %rdi
sub    $0xc0,%rsp
movaps %xmm15,-0x20(%rbp)
movaps %xmm14,-0x30(%rbp)
movaps %xmm13,-0x40(%rbp)
movaps %xmm12,-0x50(%rbp)
movaps %xmm11,-0x60(%rbp)
movaps %xmm10,-0x70(%rbp)
movaps %xmm9,-0x80(%rbp)
movaps %xmm8,-0x90(%rbp)
ff
movaps %xmm7,-0xa0(%rbp)
movaps %xmm6,-0xb0(%rbp)
mov    %r9,0x28(%rbp)
mov    %r8,0x20(%rbp)
mov    %rdx,0x18(%rbp)
mov    %ecx,-0xb4(%rbp)
movl   $0x0,-0xc4(%rbp)
lea    -0xc0(%rbp),%rax
lea    0x18(%rbp),%rcx
mov    %rcx,(%rax)
mov    -0xb4(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0xb4(%rbp)
cmp    $0x0,%eax
je     11d6 <foo+0xc6>
mov    -0xc0(%rbp),%rax
mov    %rax,%rcx
add    $0x8,%rcx
mov    %rcx,-0xc0(%rbp)
mov    (%rax),%eax
add    -0xc4(%rbp),%eax
mov    %eax,-0xc4(%rbp)
mov    -0xc4(%rbp),%esi
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1180 <foo+0x70>
mov    -0xc4(%rbp),%eax
movaps -0xb0(%rbp),%xmm6
movaps -0xa0(%rbp),%xmm7
movaps -0x90(%rbp),%xmm8
ff
movaps -0x80(%rbp),%xmm9
movaps -0x70(%rbp),%xmm10
movaps -0x60(%rbp),%xmm11
movaps -0x50(%rbp),%xmm12
movaps -0x40(%rbp),%xmm13
movaps -0x30(%rbp),%xmm14
movaps -0x20(%rbp),%xmm15
add    $0xc0,%rsp
pop    %rdi
pop    %rsi
pop    %rbp
ret

x86 assembly (clang, -O2):
<foo>:
push   %r14
push   %rsi
push   %rdi
push   %rbp
push   %rbx
sub    $0xb0,%rsp
movaps %xmm15,0xa0(%rsp)
movaps %xmm14,0x90(%rsp)
movaps %xmm13,0x80(%rsp)
movaps %xmm12,0x70(%rsp)
movaps %xmm11,0x60(%rsp)
movaps %xmm10,0x50(%rsp)
movaps %xmm9,0x40(%rsp)
movaps %xmm8,0x30(%rsp)
movaps %xmm7,0x20(%rsp)
movaps %xmm6,0x10(%rsp)
mov    %rdx,0xe8(%rsp)
mov    %r8,0xf0(%rsp)
mov    %r9,0xf8(%rsp)
lea    0xe8(%rsp),%rax
mov    %rax,0x8(%rsp)
test   %ecx,%ecx
je     11c3 <foo+0xb3>
mov    %ecx,%ebx
lea    0xe6e(%rip),%r14
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x8(%rsp),%rax
lea    0x8(%rax),%rcx
mov    %rcx,0x8(%rsp)
add    (%rax),%ebp
mov    %r14,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1030 <printf@plt>
add    $0xffffffff,%ebx
jne    11a0 <foo+0x90>
jmp    11c5 <foo+0xb5>
xor    %ebp,%ebp
mov    %ebp,%eax
movaps 0x10(%rsp),%xmm6
movaps 0x20(%rsp),%xmm7
movaps 0x30(%rsp),%xmm8
movaps 0x40(%rsp),%xmm9
movaps 0x50(%rsp),%xmm10
movaps 0x60(%rsp),%xmm11
movaps 0x70(%rsp),%xmm12
movaps 0x80(%rsp),%xmm13
movaps 0x90(%rsp),%xmm14
movaps 0xa0(%rsp),%xmm15
add    $0xb0,%rsp
pop    %rbx
pop    %rbp
pop    %rdi
pop    %rsi
pop    %r14
ret

LLVM IR (-O0):
@.str = [10 x i8] c"sum = %d\0A\00"
define win64cc i32 @foo(i32 %0, ...) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %4
%5 = bitcast i8** %3 to i8*
br label %6
6: ; preds = %10, %1
%7 = load i32, i32* %2
%8 = add nsw i32 %7, -1
store i32 %8, i32* %2
%9 = icmp ne i32 %7, 0
br i1 %9, label %10, label %19
10: ; preds = %6
%11 = load i8*, i8** %3
%12 = getelementptr inbounds i8, i8* %11, i64 8
store i8* %12, i8** %3
%13 = bitcast i8* %11 to i32*
%14 = load i32, i32* %13
%15 = load i32, i32* %4
%16 = add nsw i32 %15, %14
store i32 %16, i32* %4
%17 = load i32, i32* %4
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %17)
br label %6
19: ; preds = %6
%20 = bitcast i8** %3 to i8*
%21 = load i32, i32* %4
ret i32 %21
}

LLVM IR (-O2):
@.str = [10 x i8] c"sum = %d\0A\00"
define win64cc i32 @foo(i32 %0, ...) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
%4 = icmp eq i32 %0, 0
br i1 %4, label %16, label %5
5: ; preds = %1, %5
%6 = phi i32 [ %13, %5 ], [ 0, %1 ]
%7 = phi i32 [ %8, %5 ], [ %0, %1 ]
%8 = add nsw i32 %7, -1
%9 = load i8*, i8** %2
%10 = getelementptr inbounds i8, i8* %9, i64 8
store i8* %10, i8** %2
%11 = bitcast i8* %9 to i32*
%12 = load i32, i32* %11
%13 = add nsw i32 %12, %6
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = icmp eq i32 %8, 0
br i1 %15, label %16, label %5
16: ; preds = %5, %1
%17 = phi i32 [ 0, %1 ], [ %13, %5 ]
ret i32 %17
}