int test_callfunc_va(char *dest, int n_args, ...) {
  char *p = dest;
  int i;
  __builtin_va_list va;
  __builtin_va_start(va, n_args);
  for (i = 0; i < n_args; i++) {
    p += sprintf(p, "%d ", __builtin_va_arg(va, int));
  }
  __builtin_va_end(va);
  return n_args;
}


x86 assembly (clang, -O0):
<test_callfunc_va>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
test   %al,%al
je     114f <test_callfunc_va+0x3f>
movaps %xmm0,-0xc0(%rbp)
movaps %xmm1,-0xb0(%rbp)
movaps %xmm2,-0xa0(%rbp)
movaps %xmm3,-0x90(%rbp)
movaps %xmm4,-0x80(%rbp)
movaps %xmm5,-0x70(%rbp)
movaps %xmm6,-0x60(%rbp)
movaps %xmm7,-0x50(%rbp)
mov    %r9,-0xc8(%rbp)
mov    %r8,-0xd0(%rbp)
mov    %rcx,-0xd8(%rbp)
mov    %rdx,-0xe0(%rbp)
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
lea    -0x40(%rbp),%rax
lea    -0xf0(%rbp),%rcx
mov    %rcx,0x10(%rax)
lea    0x10(%rbp),%rcx
mov    %rcx,0x8(%rax)
movl   $0x30,0x4(%rax)
movl   $0x10,(%rax)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1257 <test_callfunc_va+0x147>
mov    -0x18(%rbp),%rax
mov    %rax,-0x108(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x100(%rbp)
mov    -0x40(%rbp),%eax
mov    %eax,-0xf4(%rbp)
cmp    $0x28,%eax
ja     11fe <test_callfunc_va+0xee>
mov    -0x100(%rbp),%rcx
mov    -0xf4(%rbp),%edx
movslq %edx,%rax
add    0x10(%rcx),%rax
add    $0x8,%edx
mov    %edx,(%rcx)
mov    %rax,-0x110(%rbp)
jmp    121b <test_callfunc_va+0x10b>
mov    -0x100(%rbp),%rcx
mov    0x8(%rcx),%rdx
mov    %rdx,%rax
add    $0x8,%rdx
mov    %rdx,0x8(%rcx)
mov    %rax,-0x110(%rbp)
mov    -0x108(%rbp),%rdi
mov    -0x110(%rbp),%rax
mov    (%rax),%edx
lea    0xdce(%rip),%rsi
mov    $0x0,%al
call   1030 <sprintf@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11a5 <test_callfunc_va+0x95>
mov    -0xc(%rbp),%eax
add    $0x110,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_callfunc_va>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0xd8,%rsp
mov    %esi,%r14d
mov    %rdx,0x30(%rsp)
mov    %rcx,0x38(%rsp)
mov    %r8,0x40(%rsp)
mov    %r9,0x48(%rsp)
test   %al,%al
je     116f <test_callfunc_va+0x5f>
movaps %xmm0,0x50(%rsp)
movaps %xmm1,0x60(%rsp)
movaps %xmm2,0x70(%rsp)
movaps %xmm3,0x80(%rsp)
movaps %xmm4,0x90(%rsp)
movaps %xmm5,0xa0(%rsp)
movaps %xmm6,0xb0(%rsp)
movaps %xmm7,0xc0(%rsp)
lea    0x20(%rsp),%rax
mov    %rax,0x10(%rsp)
lea    0x100(%rsp),%rax
mov    %rax,0x8(%rsp)
movabs $0x3000000010,%rax
mov    %rax,(%rsp)
test   %esi,%esi
jle    11f0 <test_callfunc_va+0xe0>
mov    %rdi,%rbx
lea    0xe5e(%rip),%r15
mov    %r14d,%ebp
jmp    11d7 <test_callfunc_va+0xc7>
nopw   0x0(%rax,%rax,1)
mov    0x8(%rsp),%rax
lea    0x8(%rax),%rcx
mov    %rcx,0x8(%rsp)
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1030 <sprintf@plt>
cltq
add    %rax,%rbx
add    $0xffffffff,%ebp
je     11f0 <test_callfunc_va+0xe0>
mov    (%rsp),%ecx
cmp    $0x28,%rcx
ja     11b0 <test_callfunc_va+0xa0>
mov    %rcx,%rax
add    0x10(%rsp),%rax
add    $0x8,%ecx
mov    %ecx,(%rsp)
jmp    11be <test_callfunc_va+0xae>
mov    %r14d,%eax
add    $0xd8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.__va_list_tag = type { i32, i32, i8*, i8* }
@.str = [4 x i8] c"%d \00"
define i32 @test_callfunc_va(i8* %0, i32 %1, ...) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [1 x %struct.__va_list_tag]
store i8* %0, i8** %3
store i32 %1, i32* %4
%8 = load i8*, i8** %3
store i8* %8, i8** %5
%9 = getelementptr inbounds [1 x %struct.__va_list_tag], [1 x %struct.__va_list_tag]* %7, i64 0, i64 0
%10 = bitcast %struct.__va_list_tag* %9 to i8*
store i32 0, i32* %6
br label %11
11: ; preds = %39, %2
%12 = load i32, i32* %6
%13 = load i32, i32* %4
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %42
15: ; preds = %11
%16 = load i8*, i8** %5
%17 = getelementptr inbounds [1 x %struct.__va_list_tag], [1 x %struct.__va_list_tag]* %7, i64 0, i64 0
%18 = getelementptr inbounds %struct.__va_list_tag, %struct.__va_list_tag* %17, i32 0, i32 0
%19 = load i32, i32* %18
%20 = icmp ule i32 %19, 40
br i1 %20, label %21, label %27
21: ; preds = %15
%22 = getelementptr inbounds %struct.__va_list_tag, %struct.__va_list_tag* %17, i32 0, i32 3
%23 = load i8*, i8** %22
%24 = getelementptr i8, i8* %23, i32 %19
%25 = bitcast i8* %24 to i32*
%26 = add i32 %19, 8
store i32 %26, i32* %18
br label %32
27: ; preds = %15
%28 = getelementptr inbounds %struct.__va_list_tag, %struct.__va_list_tag* %17, i32 0, i32 2
%29 = load i8*, i8** %28
%30 = bitcast i8* %29 to i32*
%31 = getelementptr i8, i8* %29, i32 8
store i8* %31, i8** %28
br label %32
32: ; preds = %27, %21
%33 = phi i32* [ %25, %21 ], [ %30, %27 ]
%34 = load i32, i32* %33
%35 = call i32 (i8*, i8*, ...) @sprintf(i8* %16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %34)
%36 = load i8*, i8** %5
%37 = sext i32 %35 to i64
%38 = getelementptr inbounds i8, i8* %36, i64 %37
store i8* %38, i8** %5
br label %39
39: ; preds = %32
%40 = load i32, i32* %6
%41 = add nsw i32 %40, 1
store i32 %41, i32* %6
br label %11
42: ; preds = %11
%43 = getelementptr inbounds [1 x %struct.__va_list_tag], [1 x %struct.__va_list_tag]* %7, i64 0, i64 0
%44 = bitcast %struct.__va_list_tag* %43 to i8*
%45 = load i32, i32* %4
ret i32 %45
}

LLVM IR (-O2):
%struct.__va_list_tag = type { i32, i32, i8*, i8* }
@.str = [4 x i8] c"%d \00"
define i32 @test_callfunc_va(i8* nocapture writeonly %0, i32 returned %1, ...) {
%3 = alloca [1 x %struct.__va_list_tag]
%4 = bitcast [1 x %struct.__va_list_tag]* %3 to i8*
%5 = icmp sgt i32 %1, 0
br i1 %5, label %6, label %32
6: ; preds = %2
%7 = getelementptr inbounds [1 x %struct.__va_list_tag], [1 x %struct.__va_list_tag]* %3, i64 0, i64 0, i32 0
%8 = getelementptr inbounds [1 x %struct.__va_list_tag], [1 x %struct.__va_list_tag]* %3, i64 0, i64 0, i32 2
%9 = getelementptr inbounds [1 x %struct.__va_list_tag], [1 x %struct.__va_list_tag]* %3, i64 0, i64 0, i32 3
br label %10
10: ; preds = %6, %23
%11 = phi i32 [ 0, %6 ], [ %30, %23 ]
%12 = phi i8* [ %0, %6 ], [ %29, %23 ]
%13 = load i32, i32* %7
%14 = icmp ult i32 %13, 41
br i1 %14, label %15, label %20
15: ; preds = %10
%16 = load i8*, i8** %9
%17 = zext i32 %13 to i64
%18 = getelementptr i8, i8* %16, i64 %17
%19 = add nuw nsw i32 %13, 8
store i32 %19, i32* %7
br label %23
20: ; preds = %10
%21 = load i8*, i8** %8
%22 = getelementptr i8, i8* %21, i64 8
store i8* %22, i8** %8
br label %23
23: ; preds = %20, %15
%24 = phi i8* [ %18, %15 ], [ %21, %20 ]
%25 = bitcast i8* %24 to i32*
%26 = load i32, i32* %25
%27 = call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %12, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %26)
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %12, i64 %28
%30 = add nuw nsw i32 %11, 1
%31 = icmp eq i32 %30, %1
br i1 %31, label %32, label %10
32: ; preds = %23, %2
ret i32 %1
}