#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ printf(char *, ...);
int snprintf(char *, int, char *, char *);
int sprintf(char *, char *, char *);
int strlen(char *);
int main(int argc, char *argv[]) {
  int ret = 0;
  char pad[128] = {0};
  char buf_small[16] = {0};
  char buf_huge[128] = {0};
  if (argc < 2) {
    printf("Usage: fprintf string\n");
    return 1;
  }
  ret = sprintf(buf_small, "the string is %s\n", argv[1]);
  printf("sprintf small buf result: [%d] %s, len = %u, pad = %s", ret,
         buf_small, strlen(buf_small), pad);
  ret = sprintf(buf_huge, "the string is %s\n", argv[1]);
  printf("sprintf huge buf result: [%d] %s, len = %u\n", ret, buf_huge,
         strlen(buf_huge));
  ret = snprintf(buf_small, 10, "%s\n", argv[1]);
  printf("sprintf small buf result: [%d] %s, len = %u, pad = %s", ret,
         buf_small, strlen(buf_small), pad);
  ret = snprintf(buf_huge, 100, "%s\n", argv[1]);
  printf("sprintf huge buf result: [%d] %s, len = %u\n", ret, buf_huge,
         strlen(buf_huge));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x170,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
lea    -0xa0(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1060 <memset@plt>
lea    -0xb0(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
lea    -0x130(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1060 <memset@plt>
cmpl   $0x2,-0x8(%rbp)
jge    11cd <main+0x7d>
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1364 <main+0x214>
lea    -0xb0(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe34(%rip),%rsi
call   1070 <sprintf@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x16c(%rbp)
lea    -0xb0(%rbp),%rax
mov    %rax,-0x168(%rbp)
lea    -0xb0(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x16c(%rbp),%esi
mov    -0x168(%rbp),%rdx
mov    %eax,%ecx
lea    -0xa0(%rbp),%r8
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x130(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xdcf(%rip),%rsi
call   1070 <sprintf@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x15c(%rbp)
lea    -0x130(%rbp),%rax
mov    %rax,-0x158(%rbp)
lea    -0x130(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x15c(%rbp),%esi
mov    -0x158(%rbp),%rdx
mov    %eax,%ecx
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0xb0(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
mov    $0xa,%esi
lea    0xd7a(%rip),%rdx
call   1050 <snprintf@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x14c(%rbp)
lea    -0xb0(%rbp),%rax
mov    %rax,-0x148(%rbp)
lea    -0xb0(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x14c(%rbp),%esi
mov    -0x148(%rbp),%rdx
mov    %eax,%ecx
lea    -0xa0(%rbp),%r8
lea    0xd36(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x130(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rcx
mov    $0x64,%esi
lea    0xd10(%rip),%rdx
call   1050 <snprintf@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x13c(%rbp)
lea    -0x130(%rbp),%rax
mov    %rax,-0x138(%rbp)
lea    -0x130(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x13c(%rbp),%esi
mov    -0x138(%rbp),%rdx
mov    %eax,%ecx
lea    0xd09(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x170,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
movaps %xmm0,0x100(%rsp)
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
cmp    $0x1,%edi
jg     11ef <main+0x9f>
lea    0xe95(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%r14d
jmp    12de <main+0x18e>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdx
lea    0xe03(%rip),%r12
mov    %rsp,%r15
mov    %r15,%rdi
mov    %r12,%rsi
call   1070 <sprintf@plt>
mov    %eax,%ebp
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    0xdf6(%rip),%rdi
xor    %r14d,%r14d
lea    0x10(%rsp),%r8
mov    %ebp,%esi
mov    %r15,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rbx),%rdx
lea    0x90(%rsp),%rbp
mov    %rbp,%rdi
mov    %r12,%rsi
call   1070 <sprintf@plt>
mov    %eax,%r13d
mov    %rbp,%rdi
call   1040 <strlen@plt>
lea    0xded(%rip),%rdi
mov    %r13d,%esi
mov    %rbp,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rbx),%rcx
lea    0xd99(%rip),%r13
mov    %r15,%rdi
mov    $0xa,%esi
mov    %r13,%rdx
call   1060 <snprintf@plt>
mov    %eax,%r12d
mov    %r15,%rdi
call   1040 <strlen@plt>
lea    0xd7b(%rip),%rdi
mov    %r12d,%esi
mov    %r15,%rdx
mov    %eax,%ecx
lea    0x10(%rsp),%r8
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x8(%rbx),%rcx
mov    %rbp,%rdi
mov    $0x64,%esi
mov    %r13,%rdx
call   1060 <snprintf@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1040 <strlen@plt>
lea    0xd78(%rip),%rdi
mov    %ebx,%esi
mov    %rbp,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14d,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [23 x i8] c"Usage: fprintf string\0A\00"
@.str.1 = [18 x i8] c"the string is %s\0A\00"
@.str.2 = [54 x i8] c"sprintf small buf result: [%d] %s, len = %u, pad = %s\00"
@.str.3 = [44 x i8] c"sprintf huge buf result: [%d] %s, len = %u\0A\00"
@.str.4 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [128 x i8]
%8 = alloca [16 x i8]
%9 = alloca [128 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
%10 = bitcast [128 x i8]* %7 to i8*
%11 = bitcast [16 x i8]* %8 to i8*
%12 = bitcast [128 x i8]* %9 to i8*
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 2
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %60
17: ; preds = %2
%18 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i32 @sprintf(i8* %18, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %21)
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%25 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%26 = call i32 @strlen(i8* %25)
%27 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.2, i64 0, i64 0), i32 %23, i8* %24, i32 %26, i8* %27)
%29 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = call i32 @sprintf(i8* %29, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %32)
store i32 %33, i32* %6
%34 = load i32, i32* %6
%35 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%36 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%37 = call i32 @strlen(i8* %36)
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i32 %34, i8* %35, i32 %37)
%39 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 1
%42 = load i8*, i8** %41
%43 = call i32 @snprintf(i8* %39, i32 10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %42)
store i32 %43, i32* %6
%44 = load i32, i32* %6
%45 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%46 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%47 = call i32 @strlen(i8* %46)
%48 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.2, i64 0, i64 0), i32 %44, i8* %45, i32 %47, i8* %48)
%50 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%51 = load i8**, i8*** %5
%52 = getelementptr inbounds i8*, i8** %51, i64 1
%53 = load i8*, i8** %52
%54 = call i32 @snprintf(i8* %50, i32 100, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %53)
store i32 %54, i32* %6
%55 = load i32, i32* %6
%56 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%57 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%58 = call i32 @strlen(i8* %57)
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i32 %55, i8* %56, i32 %58)
store i32 0, i32* %3
br label %60
60: ; preds = %17, %15
%61 = load i32, i32* %3
ret i32 %61
}

LLVM IR (-O2):
@.str.1 = [18 x i8] c"the string is %s\0A\00"
@.str.2 = [54 x i8] c"sprintf small buf result: [%d] %s, len = %u, pad = %s\00"
@.str.3 = [44 x i8] c"sprintf huge buf result: [%d] %s, len = %u\0A\00"
@.str.4 = [4 x i8] c"%s\0A\00"
@str = [22 x i8] c"Usage: fprintf string\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [128 x i8]
%4 = alloca [16 x i8]
%5 = alloca [128 x i8]
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%9 = icmp slt i32 %0, 2
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
br label %30
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %14)
%16 = call i32 @strlen(i8* %7)
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @.str.2, i64 0, i64 0), i32 %15, i8* %7, i32 %16, i8* %6)
%18 = load i8*, i8** %13
%19 = call i32 @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%20 = call i32 @strlen(i8* %8)
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i32 %19, i8* %8, i32 %20)
%22 = load i8*, i8** %13
%23 = call i32 @snprintf(i8* %7, i32 10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %22)
%24 = call i32 @strlen(i8* %7)
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @.str.2, i64 0, i64 0), i32 %23, i8* %7, i32 %24, i8* %6)
%26 = load i8*, i8** %13
%27 = call i32 @snprintf(i8* %8, i32 100, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %26)
%28 = call i32 @strlen(i8* %8)
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i32 %27, i8* %8, i32 %28)
br label %30
30: ; preds = %12, %10
%31 = phi i32 [ 1, %10 ], [ 0, %12 ]
ret i32 %31
}