#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*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int snprintf(char *, int, char const *, ...);
char *get_file_name(const char *fmt, long n, int has_fmt_spec) {
  static char filename[4096];
  int bytes;
  bytes = has_fmt_spec ? snprintf(filename, 4095, fmt, n)
                       : snprintf(filename, 4095, "%s%ld", fmt, n);
  if (bytes >= 4095) {
    printf("file name too long\n");
    exit(EXIT_FAILURE);
  }
  return filename;
}


x86 assembly (clang, -O0):
<get_file_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1170 <get_file_name+0x40>
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0x2ef4(%rip),%rdi
mov    $0xfff,%esi
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    %eax,-0x1c(%rbp)
jmp    1195 <get_file_name+0x65>
mov    -0x8(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    0x2ed1(%rip),%rdi
mov    $0xfff,%esi
lea    0xe75(%rip),%rdx
mov    $0x0,%al
call   1040 <snprintf@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x18(%rbp)
cmpl   $0xfff,-0x18(%rbp)
jl     11c2 <get_file_name+0x92>
lea    0xe57(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>
lea    0x2e87(%rip),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_file_name>:
push   %rax
mov    %rsi,%r8
mov    %rdi,%rcx
lea    0x2f12(%rip),%rdi
test   %edx,%edx
je     1164 <get_file_name+0x34>
mov    $0xfff,%esi
mov    %rcx,%rdx
mov    %r8,%rcx
xor    %eax,%eax
call   1040 <snprintf@plt>
cmp    $0xfff,%eax
jge    117e <get_file_name+0x4e>
lea    0x2eee(%rip),%rax
pop    %rcx
ret
lea    0xe95(%rip),%rdx
mov    $0xfff,%esi
xor    %eax,%eax
call   1040 <snprintf@plt>
cmp    $0xfff,%eax
jl     115b <get_file_name+0x2b>
lea    0xe81(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@get_file_name.filename = internal global [4096 x i8] zeroinitializer
@.str = [6 x i8] c"%s%ld\00"
@.str.1 = [20 x i8] c"file name too long\0A\00"
@EXIT_FAILURE = global i32 0
define i8* @get_file_name(i8* %0, i64 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
store i32 %2, i32* %6
%8 = load i32, i32* %6
%9 = icmp ne i32 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %3
%11 = load i8*, i8** %4
%12 = load i64, i64* %5
%13 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @get_file_name.filename, i64 0, i64 0), i32 4095, i8* %11, i64 %12)
br label %18
14: ; preds = %3
%15 = load i8*, i8** %4
%16 = load i64, i64* %5
%17 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @get_file_name.filename, i64 0, i64 0), i32 4095, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %15, i64 %16)
br label %18
18: ; preds = %14, %10
%19 = phi i32 [ %13, %10 ], [ %17, %14 ]
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp sge i32 %20, 4095
br i1 %21, label %22, label %26
22: ; preds = %18
%23 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @exit(i32 %24)
unreachable
26: ; preds = %18
ret i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @get_file_name.filename, i64 0, i64 0)
}

LLVM IR (-O2):
@get_file_name.filename = internal global [4096 x i8] zeroinitializer
@.str = [6 x i8] c"%s%ld\00"
@EXIT_FAILURE = global i32 0
@str = [19 x i8] c"file name too long\00"
define i8* @get_file_name(i8* %0, i64 %1, i32 %2) {
%4 = icmp eq i32 %2, 0
br i1 %4, label %7, label %5
5: ; preds = %3
%6 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @get_file_name.filename, i64 0, i64 0), i32 4095, i8* %0, i64 %1)
br label %9
7: ; preds = %3
%8 = tail call i32 (i8*, i32, i8*, ...) @snprintf(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @get_file_name.filename, i64 0, i64 0), i32 4095, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i64 %1)
br label %9
9: ; preds = %7, %5
%10 = phi i32 [ %6, %5 ], [ %8, %7 ]
%11 = icmp sgt i32 %10, 4094
br i1 %11, label %12, label %16
12: ; preds = %9
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %9
ret i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @get_file_name.filename, i64 0, i64 0)
}