int main(void) {
  char c[40];
  printf("Num bytes needed is %d\n", snprintf(&c[0], 40, "%d", 99));
  printf("Num bytes needed is %d\n", snprintf(&c[0], 2, "%d %d", 99, 54353));
  printf("Num bytes needed is %d\n", snprintf(&c[0], 40, "%d", 333934));
  printf("%s\n", &c[0]);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x30(%rbp),%rdi
mov    $0x28,%esi
lea    0xedc(%rip),%rdx
mov    $0x63,%ecx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    %eax,%esi
lea    0xeac(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x30(%rbp),%rdi
mov    $0x2,%esi
lea    0xead(%rip),%rdx
mov    $0x63,%ecx
mov    $0xd451,%r8d
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    %eax,%esi
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x30(%rbp),%rdi
mov    $0x28,%esi
lea    0xe7e(%rip),%rdx
mov    $0x5186e,%ecx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    %eax,%esi
lea    0xe4e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x30(%rbp),%rsi
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x30,%rsp
lea    0xedb(%rip),%r14
mov    %rsp,%r15
mov    $0x28,%esi
mov    %r15,%rdi
mov    %r14,%rdx
mov    $0x63,%ecx
xor    %eax,%eax
call   1050 <snprintf@plt>
lea    0xe9f(%rip),%rbx
mov    %rbx,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xea4(%rip),%rdx
mov    $0x2,%esi
mov    %r15,%rdi
mov    $0x63,%ecx
mov    $0xd451,%r8d
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    %rbx,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x28,%esi
mov    %r15,%rdi
mov    %r14,%rdx
mov    $0x5186e,%ecx
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    %rbx,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r15,%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [24 x i8] c"Num bytes needed is %d\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [6 x i8] c"%d %d\00"
@.str.3 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [40 x i8]
store i32 0, i32* %1
%3 = getelementptr inbounds [40 x i8], [40 x i8]* %2, i64 0, i64 0
%4 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %3, i64 40, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 99)
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = getelementptr inbounds [40 x i8], [40 x i8]* %2, i64 0, i64 0
%7 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %6, i64 2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 99, i32 54353)
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = getelementptr inbounds [40 x i8], [40 x i8]* %2, i64 0, i64 0
%10 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %9, i64 40, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 333934)
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [40 x i8], [40 x i8]* %2, i64 0, i64 0
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %12)
ret i32 0
}

LLVM IR (-O2):
@.str = [24 x i8] c"Num bytes needed is %d\0A\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [6 x i8] c"%d %d\00"
define i32 @main() {
%1 = alloca [40 x i8]
%2 = getelementptr inbounds [40 x i8], [40 x i8]* %1, i64 0, i64 0
%3 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %2, i64 40, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 99)
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %2, i64 2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 99, i32 54353)
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* %2, i64 40, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 333934)
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = call i32 @puts(i8* %2)
ret i32 0
}