#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, int);
int strlen(char *);
void format_to_string() {
  char stringBuffer[128];
  int formattedLength =
      snprintf(stringBuffer, sizeof stringBuffer,
               "Formatted %s with values x=%d y=%d", "string", 10, 20);
  printf("-------------------------------\n");
  printf("Formatted string length = %d\n", formattedLength);
  printf("Result: \"%s\"\n", stringBuffer);
  char buffer[28];
  int len = snprintf(buffer, sizeof buffer,
                     "Formatted %s with values x=%d y=%d", "string", 10, 20);
  int actualLength = (len >= sizeof buffer) ? strlen(buffer) : len;
  printf("Formatted string length = %d\n", len);
  printf("Actual    string length = %d\n", actualLength);
  printf("Result: \"%s\"\n", buffer);
}


x86 assembly (clang, -O0):
<format_to_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
lea    -0x80(%rbp),%rdi
mov    $0x80,%esi
lea    0xeb5(%rip),%rdx
lea    0xed1(%rip),%rcx
mov    $0xa,%r8d
mov    $0x14,%r9d
call   1050 <snprintf@plt>
mov    %eax,-0x84(%rbp)
lea    0xeba(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x84(%rbp),%esi
lea    0xec7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x80(%rbp),%rsi
lea    0xed3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0xa0(%rbp),%rdi
mov    $0x1c,%esi
lea    0xe50(%rip),%rdx
lea    0xe6c(%rip),%rcx
mov    $0xa,%r8d
mov    $0x14,%r9d
call   1050 <snprintf@plt>
mov    %eax,-0xa4(%rbp)
movslq -0xa4(%rbp),%rax
cmp    $0x1c,%rax
jb     11f6 <format_to_string+0xc6>
lea    -0xa0(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0xac(%rbp)
jmp    1202 <format_to_string+0xd2>
mov    -0xa4(%rbp),%eax
mov    %eax,-0xac(%rbp)
mov    -0xac(%rbp),%eax
mov    %eax,-0xa8(%rbp)
mov    -0xa4(%rbp),%esi
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xa8(%rbp),%esi
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0xa0(%rbp),%rsi
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<format_to_string>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0xa8,%rsp
lea    0xeac(%rip),%r15
lea    0xec8(%rip),%r14
lea    0x20(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x80,%esi
mov    %r15,%rdx
mov    %r14,%rcx
mov    $0xa,%r8d
mov    $0x14,%r9d
call   1060 <snprintf@plt>
mov    %eax,%ebx
lea    0xeec(%rip),%rdi
call   1030 <puts@plt>
lea    0xe96(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xea4(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rsp,%rdi
mov    $0x1c,%esi
mov    %r15,%rdx
mov    %r14,%rcx
mov    $0xa,%r8d
mov    $0x14,%r9d
call   1060 <snprintf@plt>
mov    %eax,%ebx
mov    %eax,%ebp
cmp    $0x1c,%eax
jb     11e0 <format_to_string+0xa0>
mov    %rsp,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
lea    0xe43(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe5f(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe41(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0xa8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [35 x i8] c"Formatted %s with values x=%d y=%d\00"
@.str.1 = [7 x i8] c"string\00"
@.str.2 = [33 x i8] c"-------------------------------\0A\00"
@.str.3 = [30 x i8] c"Formatted string length = %d\0A\00"
@.str.4 = [14 x i8] c"Result: \22%s\22\0A\00"
@.str.5 = [30 x i8] c"Actual string length = %d\0A\00"
define void @format_to_string() {
%1 = alloca [128 x i8]
%2 = alloca i32
%3 = alloca [28 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%7 = call i32 @snprintf(i8* %6, i32 128, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 10, i32 20)
store i32 %7, i32* %2
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
%9 = load i32, i32* %2
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i8* %11)
%13 = getelementptr inbounds [28 x i8], [28 x i8]* %3, i64 0, i64 0
%14 = call i32 @snprintf(i8* %13, i32 28, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 10, i32 20)
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = sext i32 %15 to i64
%17 = icmp uge i64 %16, 28
br i1 %17, label %18, label %21
18: ; preds = %0
%19 = getelementptr inbounds [28 x i8], [28 x i8]* %3, i64 0, i64 0
%20 = call i32 @strlen(i8* %19)
br label %23
21: ; preds = %0
%22 = load i32, i32* %4
br label %23
23: ; preds = %21, %18
%24 = phi i32 [ %20, %18 ], [ %22, %21 ]
store i32 %24, i32* %5
%25 = load i32, i32* %4
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %25)
%27 = load i32, i32* %5
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i32 %27)
%29 = getelementptr inbounds [28 x i8], [28 x i8]* %3, i64 0, i64 0
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i8* %29)
ret void
}

LLVM IR (-O2):
@.str = [35 x i8] c"Formatted %s with values x=%d y=%d\00"
@.str.1 = [7 x i8] c"string\00"
@.str.3 = [30 x i8] c"Formatted string length = %d\0A\00"
@.str.4 = [14 x i8] c"Result: \22%s\22\0A\00"
@.str.5 = [30 x i8] c"Actual string length = %d\0A\00"
@str = [32 x i8] c"-------------------------------\00"
define void @format_to_string() {
%1 = alloca [128 x i8]
%2 = alloca [28 x i8]
%3 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%4 = call i32 @snprintf(i8* %3, i32 128, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 10, i32 20)
%5 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
%6 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %4)
%7 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i8* %3)
%8 = getelementptr inbounds [28 x i8], [28 x i8]* %2, i64 0, i64 0
%9 = call i32 @snprintf(i8* %8, i32 28, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 10, i32 20)
%10 = icmp ugt i32 %9, 27
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 @strlen(i8* %8)
br label %13
13: ; preds = %0, %11
%14 = phi i32 [ %12, %11 ], [ %9, %0 ]
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i32 %9)
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i32 %14)
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i8* %8)
ret void
}