#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 */
typedef int /*<<< orphan*/ buff;

/* Variables and functions */
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ sprintf(char *, char *, char *);
int main(void) {
  char *s = "Hello world";
  char buff[20];
  memset(buff, 0, sizeof(buff));
  if (sprintf(buff, "%s", s) < 0) {
    perror("sprintf");
    return -1;
  }
  printf("%s\n", buff);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%rip),%rax
mov    %rax,-0x10(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1040 <memset@plt>
lea    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe93(%rip),%rsi
call   1060 <sprintf@plt>
cmp    $0x0,%rax
jge    11a0 <main+0x60>
lea    0xe80(%rip),%rdi
call   1050 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b7 <main+0x77>
lea    -0x30(%rbp),%rsi
lea    0xe6c(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rsp,%rbx
xor    %ebp,%ebp
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x14,%edx
call   1040 <memset@plt>
lea    0xeab(%rip),%rsi
lea    0xe98(%rip),%rdx
mov    %rbx,%rdi
call   1060 <sprintf@plt>
test   %rax,%rax
js     118d <main+0x4d>
lea    0xe9b(%rip),%rdi
mov    %rsp,%rsi
call   1030 <printf@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe7b(%rip),%rdi
call   1050 <perror@plt>
mov    $0xffffffff,%ebp
jmp    1184 <main+0x44>

LLVM IR (-O0):
@.str = [12 x i8] c"Hello world\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [8 x i8] c"sprintf\00"
@.str.3 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca [20 x i8]
store i32 0, i32* %1
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8** %2
%4 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%5 = call i32 @memset(i8* %4, i32 0, i32 20)
%6 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%7 = load i8*, i8** %2
%8 = call i64 @sprintf(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %7)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %15
12: ; preds = %0
%13 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%14 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %13)
store i32 0, i32* %1
br label %15
15: ; preds = %12, %10
%16 = load i32, i32* %1
ret i32 %16
}

LLVM IR (-O2):
@.str = [12 x i8] c"Hello world\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [8 x i8] c"sprintf\00"
@.str.3 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca [20 x i8]
%2 = getelementptr inbounds [20 x i8], [20 x i8]* %1, i64 0, i64 0
%3 = call i32 @memset(i8* %2, i32 0, i32 20)
%4 = call i64 @sprintf(i8* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %10
8: ; preds = %0
%9 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %2)
br label %10
10: ; preds = %8, %6
%11 = phi i32 [ -1, %6 ], [ 0, %8 ]
ret i32 %11
}