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

/* Variables and functions */
scalar_t__ snprintf(char *, int, char *, int, char);
int /*<<< orphan*/ strcpy(char *, char *);
int fmt1(int t, char s, char **buf, size_t *buflen) {
  char tmp[50];
  size_t len;
  len = (size_t)snprintf(tmp, sizeof(tmp), "%d%c", t, s);
  if ((int)len < 0 || len + 1 > *buflen)
    return (-1);
  strcpy(*buf, tmp);
  *buf += len;
  *buflen -= len;
  return (0);
}


x86 assembly (clang, -O0):
<fmt1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %sil,%al
mov    %edi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%ecx
mov    $0x32,%esi
lea    0xeb4(%rip),%rdx
movsbl -0x9(%rbp),%r8d
call   1040 <snprintf@plt>
mov    %rax,-0x68(%rbp)
mov    -0x68(%rbp),%rax
cmp    $0x0,%eax
jl     117c <fmt1+0x5c>
mov    -0x68(%rbp),%rax
add    $0x1,%rax
mov    -0x20(%rbp),%rcx
cmp    (%rcx),%rax
jbe    1188 <fmt1+0x68>
movl   $0xffffffff,-0x4(%rbp)
jmp    11be <fmt1+0x9e>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    -0x60(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x68(%rbp),%rcx
mov    -0x18(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
mov    -0x68(%rbp),%rdx
mov    -0x20(%rbp),%rax
mov    (%rax),%rcx
sub    %rdx,%rcx
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fmt1>:
push   %r15
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %rcx,%r15
mov    %rdx,%r14
mov    %esi,%r8d
mov    %edi,%ecx
lea    0xec5(%rip),%rdx
mov    %rsp,%rdi
mov    $0x32,%esi
call   1040 <snprintf@plt>
mov    %rax,%rbx
mov    $0xffffffff,%eax
test   $0x80000000,%ebx
jne    1170 <fmt1+0x50>
cmp    (%r15),%rbx
jae    1170 <fmt1+0x50>
mov    (%r14),%rdi
mov    %rsp,%rsi
call   1030 <strcpy@plt>
add    %rbx,(%r14)
sub    %rbx,(%r15)
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [5 x i8] c"%d%c\00"
define i32 @fmt1(i32 %0, i8 signext %1, i8** nocapture %2, i64* nocapture %3) {
%5 = alloca [50 x i8]
%6 = getelementptr inbounds [50 x i8], [50 x i8]* %5, i64 0, i64 0
%7 = call i64 @snprintf(i8* %6, i32 50, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %0, i8 signext %1)
%8 = and i64 %7, 2147483648
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %20
10: ; preds = %4
%11 = load i64, i64* %3
%12 = icmp ult i64 %7, %11
br i1 %12, label %13, label %20
13: ; preds = %10
%14 = load i8*, i8** %2
%15 = call i32 @strcpy(i8* %14, i8* %6)
%16 = load i8*, i8** %2
%17 = getelementptr inbounds i8, i8* %16, i64 %7
store i8* %17, i8** %2
%18 = load i64, i64* %3
%19 = sub i64 %18, %7
store i64 %19, i64* %3
br label %20
20: ; preds = %4, %10, %13
%21 = phi i32 [ 0, %13 ], [ -1, %10 ], [ -1, %4 ]
ret i32 %21
}