#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*/ buf;

/* Variables and functions */
int /*<<< orphan*/ printf(char *, ...);
int snprintf(char *, int, char *, char *);
int main(int argc, char *argv[]) {
  char buf[4];
  printf("char buf[4] snprintf\n");
  int ret1 = snprintf(buf, sizeof(buf), "%s", "hello");
  int ret2 = snprintf(buf, sizeof(buf), "%s", "中国");
  printf("%s(%d) ret:%d %s(%d) ret:%d\n", "hello", sizeof("hello"), ret1,
         "中国", sizeof("中国"), ret2);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xec3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x14(%rbp),%rdi
mov    $0x4,%esi
lea    0xec2(%rip),%rdx
lea    0xebe(%rip),%rcx
call   1040 <snprintf@plt>
mov    %eax,-0x18(%rbp)
lea    -0x14(%rbp),%rdi
mov    $0x4,%esi
lea    0xea3(%rip),%rdx
lea    0xea5(%rip),%rcx
call   1040 <snprintf@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x1c(%rbp),%eax
lea    0xe97(%rip),%rdi
lea    0xe83(%rip),%rsi
mov    $0x6,%edx
lea    0xe7d(%rip),%r8
mov    $0x7,%r9d
mov    %eax,(%rsp)
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
lea    0xeea(%rip),%rdi
call   1030 <puts@plt>
lea    0xeb1(%rip),%r15
lea    0xead(%rip),%r14
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x4,%esi
mov    %r15,%rdx
mov    %r14,%rcx
call   1050 <snprintf@plt>
mov    %eax,%ebp
lea    0xe92(%rip),%r12
mov    %rbx,%rdi
mov    $0x4,%esi
mov    %r15,%rdx
mov    %r12,%rcx
call   1050 <snprintf@plt>
mov    %eax,(%rsp)
lea    0xe7c(%rip),%rdi
mov    $0x6,%edx
mov    $0x7,%r9d
mov    %r14,%rsi
mov    %ebp,%ecx
mov    %r12,%r8
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [22 x i8] c"char buf[4] snprintf\0A\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [6 x i8] c"hello\00"
@.str.3 = [7 x i8] c"\E4\B8\AD\E5\9B\BD\00"
@.str.4 = [29 x i8] c"%s(%d) ret:%d %s(%d) ret:%d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [4 x i8]
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%10 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%11 = call i32 @snprintf(i8* %10, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i32 %11, i32* %7
%12 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%13 = call i32 @snprintf(i8* %12, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
store i32 %13, i32* %8
%14 = load i32, i32* %7
%15 = load i32, i32* %8
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64 6, i32 %14, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i64 7, i32 %15)
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [6 x i8] c"hello\00"
@.str.3 = [7 x i8] c"\E4\B8\AD\E5\9B\BD\00"
@.str.4 = [29 x i8] c"%s(%d) ret:%d %s(%d) ret:%d\0A\00"
@str = [21 x i8] c"char buf[4] snprintf\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [4 x i8]
%4 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%6 = call i32 @snprintf(i8* %4, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%7 = call i32 @snprintf(i8* %4, i32 4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%8 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64 6, i32 %6, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i64 7, i32 %7)
ret i32 0
}