#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 snprintf(char *, size_t, char *, char const *const);
int snprint(char **buf, size_t *limit, const char *const str) {
  int rc;
  rc = snprintf(*buf, *limit, "%s", str);
  if (rc <= (int)*limit)
    *buf += rc, *limit -= rc;
  else
    *limit = 0;
  return rc;
}


x86 assembly (clang, -O0):
<snprint>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x18(%rbp),%rcx
lea    0xec3(%rip),%rdx
call   1030 <snprintf@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    -0x10(%rbp),%rcx
mov    (%rcx),%rcx
cmp    %ecx,%eax
jg     1180 <snprint+0x70>
mov    -0x1c(%rbp),%edx
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
movslq %edx,%rdx
add    %rdx,%rcx
mov    %rcx,(%rax)
movslq -0x1c(%rbp),%rdx
mov    -0x10(%rbp),%rax
mov    (%rax),%rcx
sub    %rdx,%rcx
mov    %rcx,(%rax)
jmp    118b <snprint+0x7b>
mov    -0x10(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<snprint>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rcx
mov    %rsi,%rbx
mov    %rdi,%r14
mov    (%rdi),%rdi
mov    (%rsi),%rsi
lea    0xed6(%rip),%rdx
call   1030 <snprintf@plt>
mov    (%rbx),%rcx
cmp    %ecx,%eax
jle    113a <snprint+0x2a>
xor    %ecx,%ecx
jmp    1143 <snprint+0x33>
movslq %eax,%rdx
add    %rdx,(%r14)
sub    %rdx,%rcx
mov    %rcx,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
define i32 @snprint(i8** %0, i64* %1, i8* %2) {
%4 = alloca i8**
%5 = alloca i64*
%6 = alloca i8*
%7 = alloca i32
store i8** %0, i8*** %4
store i64* %1, i64** %5
store i8* %2, i8** %6
%8 = load i8**, i8*** %4
%9 = load i8*, i8** %8
%10 = load i64*, i64** %5
%11 = load i64, i64* %10
%12 = load i8*, i8** %6
%13 = call i32 @snprintf(i8* %9, i64 %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %12)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = load i64*, i64** %5
%16 = load i64, i64* %15
%17 = trunc i64 %16 to i32
%18 = icmp sle i32 %14, %17
br i1 %18, label %19, label %30
19: ; preds = %3
%20 = load i32, i32* %7
%21 = load i8**, i8*** %4
%22 = load i8*, i8** %21
%23 = sext i32 %20 to i64
%24 = getelementptr inbounds i8, i8* %22, i64 %23
store i8* %24, i8** %21
%25 = load i32, i32* %7
%26 = sext i32 %25 to i64
%27 = load i64*, i64** %5
%28 = load i64, i64* %27
%29 = sub i64 %28, %26
store i64 %29, i64* %27
br label %32
30: ; preds = %3
%31 = load i64*, i64** %5
store i64 0, i64* %31
br label %32
32: ; preds = %30, %19
%33 = load i32, i32* %7
ret i32 %33
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
define i32 @snprint(i8** nocapture %0, i64* nocapture %1, i8* %2) {
%4 = load i8*, i8** %0
%5 = load i64, i64* %1
%6 = tail call i32 @snprintf(i8* %4, i64 %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %2)
%7 = load i64, i64* %1
%8 = trunc i64 %7 to i32
%9 = icmp sgt i32 %6, %8
br i1 %9, label %15, label %10
10: ; preds = %3
%11 = load i8*, i8** %0
%12 = sext i32 %6 to i64
%13 = getelementptr inbounds i8, i8* %11, i64 %12
store i8* %13, i8** %0
%14 = sub i64 %7, %12
br label %15
15: ; preds = %3, %10
%16 = phi i64 [ %14, %10 ], [ 0, %3 ]
store i64 %16, i64* %1
ret i32 %6
}