#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*/ fprintf(int /*<<< orphan*/, char *, size_t);
size_t snprintf(char *, size_t, char *, unsigned int, char *);
int /*<<< orphan*/ stderr;
int construct_out_name(char *buffer, size_t buffer_size, char *name,
                       unsigned int track) {
  if (snprintf(buffer, buffer_size, "%03d%s", track, name) >= buffer_size - 1) {
    fprintf(stderr, "Filename too large for buffer (max %zd)\n", buffer_size);
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<construct_out_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%r8
lea    0xeb3(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jb     1188 <construct_out_name+0x68>
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe90(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    118f <construct_out_name+0x6f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<construct_out_name>:
push   %rbx
mov    %rdx,%r8
mov    %rsi,%rbx
lea    0xed2(%rip),%rdx
call   1030 <snprintf@plt>
mov    %rax,%rcx
lea    -0x1(%rbx),%rdx
xor    %eax,%eax
cmp    %rdx,%rcx
jb     115e <construct_out_name+0x3e>
mov    0x2e98(%rip),%rax
mov    (%rax),%edi
lea    0xeb6(%rip),%rsi
mov    %rbx,%rdx
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [7 x i8] c"%03d%s\00"
@stderr = global i32 0
@.str.1 = [41 x i8] c"Filename too large for buffer (max %zd)\0A\00"
define i32 @construct_out_name(i8* %0, i64 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %6
store i64 %1, i64* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%10 = load i8*, i8** %6
%11 = load i64, i64* %7
%12 = load i32, i32* %9
%13 = load i8*, i8** %8
%14 = call i64 @snprintf(i8* %10, i64 %11, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %12, i8* %13)
%15 = load i64, i64* %7
%16 = sub i64 %15, 1
%17 = icmp uge i64 %14, %16
br i1 %17, label %18, label %22
18: ; preds = %4
%19 = load i32, i32* @stderr
%20 = load i64, i64* %7
%21 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i64 %20)
store i32 -1, i32* %5
br label %23
22: ; preds = %4
store i32 0, i32* %5
br label %23
23: ; preds = %22, %18
%24 = load i32, i32* %5
ret i32 %24
}

LLVM IR (-O2):
@.str = [7 x i8] c"%03d%s\00"
@stderr = global i32 0
@.str.1 = [41 x i8] c"Filename too large for buffer (max %zd)\0A\00"
define i32 @construct_out_name(i8* %0, i64 %1, i8* %2, i32 %3) {
%5 = tail call i64 @snprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %3, i8* %2)
%6 = add i64 %1, -1
%7 = icmp ult i64 %5, %6
br i1 %7, label %11, label %8
8: ; preds = %4
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i64 %1)
br label %11
11: ; preds = %4, %8
%12 = phi i32 [ -1, %8 ], [ 0, %4 ]
ret i32 %12
}