#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 sprintf(char *, char *, ...);
int main(void) {
  char buffer[12] = "hello";
  int ret = sprintf(buffer, "%s world", buffer);
  if (ret > 0)
    printf("sprintf wrote %d chars\n", ret);
  else
    printf("sprintf error\n");
  printf("Buffer contents: %s\n", buffer);
  sprintf(buffer, "world");
  ret = sprintf(buffer, "hello %s", buffer);
  if (ret > 0)
    printf("sprintf wrote %d chars\n", ret);
  else
    printf("sprintf error\n");
  printf("Buffer contents: %s\n", buffer);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xeca(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    0xec8(%rip),%eax
mov    %eax,-0x8(%rbp)
lea    -0x10(%rbp),%rdi
lea    -0x10(%rbp),%rdx
lea    0xeba(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    117c <main+0x5c>
mov    -0x14(%rbp),%esi
lea    0xea5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    118a <main+0x6a>
lea    0xeaa(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x10(%rbp),%rsi
lea    0xea7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x10(%rbp),%rdi
lea    0xe68(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
lea    -0x10(%rbp),%rdi
lea    -0x10(%rbp),%rdx
lea    0xe94(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11e7 <main+0xc7>
mov    -0x14(%rbp),%esi
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11f5 <main+0xd5>
lea    0xe3f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x10(%rbp),%rsi
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
movabs $0x6f6c6c6568,%rax
mov    %rax,(%rsp)
movl   $0x0,0x8(%rsp)
lea    0xeba(%rip),%rsi
mov    %rsp,%rdi
mov    %rdi,%rdx
xor    %eax,%eax
call   1050 <sprintf@plt>
test   %eax,%eax
jle    1175 <main+0x45>
lea    0xeab(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1181 <main+0x51>
lea    0xecf(%rip),%rdi
call   1030 <puts@plt>
lea    0xea5(%rip),%rdi
mov    %rsp,%rbx
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
movl   $0x6c726f77,(%rsp)
movw   $0x64,0x4(%rsp)
lea    0xe98(%rip),%rsi
mov    %rbx,%rdi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <sprintf@plt>
test   %eax,%eax
jle    11cd <main+0x9d>
lea    0xe53(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11d9 <main+0xa9>
lea    0xe77(%rip),%rdi
call   1030 <puts@plt>
lea    0xe4d(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@__const.main.buffer = [12 x i8] c"hello\00\00\00\00\00\00\00"
@.str = [9 x i8] c"%s world\00"
@.str.1 = [24 x i8] c"sprintf wrote %d chars\0A\00"
@.str.2 = [15 x i8] c"sprintf error\0A\00"
@.str.3 = [21 x i8] c"Buffer contents: %s\0A\00"
@.str.4 = [6 x i8] c"world\00"
@.str.5 = [9 x i8] c"hello %s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [12 x i8]
%3 = alloca i32
store i32 0, i32* %1
%4 = bitcast [12 x i8]* %2 to i8*
%5 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 0
%7 = call i32 (i8*, i8*, ...) @sprintf(i8* %5, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %6)
store i32 %7, i32* %3
%8 = load i32, i32* %3
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = load i32, i32* %3
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %11)
br label %15
13: ; preds = %0
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %15
15: ; preds = %13, %10
%16 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 0
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %16)
%18 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 0
%19 = call i32 (i8*, i8*, ...) @sprintf(i8* %18, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%20 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 0
%21 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 0
%22 = call i32 (i8*, i8*, ...) @sprintf(i8* %20, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* %21)
store i32 %22, i32* %3
%23 = load i32, i32* %3
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %15
%26 = load i32, i32* %3
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %26)
br label %30
28: ; preds = %15
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %28, %25
%31 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 0
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %31)
ret i32 0
}

LLVM IR (-O2):
@__const.main.buffer = [12 x i8] c"hello\00\00\00\00\00\00\00"
@.str = [9 x i8] c"%s world\00"
@.str.1 = [24 x i8] c"sprintf wrote %d chars\0A\00"
@.str.3 = [21 x i8] c"Buffer contents: %s\0A\00"
@.str.4 = [6 x i8] c"world\00"
@.str.5 = [9 x i8] c"hello %s\00"
@str.6 = [14 x i8] c"sprintf error\00"
define i32 @main() {
%1 = alloca [12 x i8]
%2 = getelementptr inbounds [12 x i8], [12 x i8]* %1, i64 0, i64 0
%3 = call i32 (i8*, i8*, ...) @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %2)
%4 = icmp sgt i32 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %3)
br label %9
7: ; preds = %0
%8 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.6, i64 0, i64 0))
br label %9
9: ; preds = %7, %5
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %2)
%11 = call i32 (i8*, i8*, ...) @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* %2)
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %11)
br label %17
15: ; preds = %9
%16 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.6, i64 0, i64 0))
br label %17
17: ; preds = %15, %13
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %2)
ret i32 0
}