#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 */
scalar_t__ malloc(int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *strrchr(char const *, char);
char *make_filename(const char *filename, int load, int start) {
  int a;
  char *buf, *ptr;
  if ((load < 0) && (start >= 0))
    load = 0;
  buf = (char *)malloc(strlen(filename) + 20);
  ptr = strrchr(filename, '.');
  if (ptr == NULL) {
    strcpy(buf, filename);
    if (load >= 0) {
      strcat(buf, "_0x");
      snprintf(buf + strlen(buf), 5, "%04x", load);
    }
    if (start >= 0) {
      strcat(buf, "_0x");
      snprintf(buf + strlen(buf), 5, "%04x", start);
    }
    strcat(buf, ".img");
  } else {
    a = ptr - filename;
    strncpy(buf, filename, a);
    buf[a] = '\0';
    if (load >= 0) {
      strcat(buf, "_0x");
      snprintf(buf + strlen(buf), 5, "%04x", load);
    }
    if (start >= 0) {
      strcat(buf, "_0x");
      snprintf(buf + strlen(buf), 5, "%04x", start);
    }
    strcat(buf, ptr);
  }
  return buf;
}


x86 assembly (clang, -O0):
<make_filename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    119d <make_filename+0x2d>
cmpl   $0x0,-0x10(%rbp)
jl     119d <make_filename+0x2d>
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%edi
add    $0x14,%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x2e,%esi
call   1070 <strrchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1283 <make_filename+0x113>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcpy@plt>
cmpl   $0x0,-0xc(%rbp)
jl     1226 <make_filename+0xb6>
mov    -0x20(%rbp),%rdi
lea    0xe0d(%rip),%rsi
call   1090 <strcat@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x30(%rbp),%rdi
cltq
add    %rax,%rdi
mov    -0xc(%rbp),%ecx
mov    $0x5,%esi
lea    0xde3(%rip),%rdx
call   1060 <snprintf@plt>
cmpl   $0x0,-0x10(%rbp)
jl     126e <make_filename+0xfe>
mov    -0x20(%rbp),%rdi
lea    0xdc5(%rip),%rsi
call   1090 <strcat@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x38(%rbp),%rdi
cltq
add    %rax,%rdi
mov    -0x10(%rbp),%ecx
mov    $0x5,%esi
lea    0xd9b(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x20(%rbp),%rdi
lea    0xd90(%rip),%rsi
call   1090 <strcat@plt>
jmp    134a <make_filename+0x1da>
mov    -0x28(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
cmpl   $0x0,-0xc(%rbp)
jl     12f5 <make_filename+0x185>
mov    -0x20(%rbp),%rdi
lea    0xd3e(%rip),%rsi
call   1090 <strcat@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x40(%rbp),%rdi
cltq
add    %rax,%rdi
mov    -0xc(%rbp),%ecx
mov    $0x5,%esi
lea    0xd14(%rip),%rdx
call   1060 <snprintf@plt>
cmpl   $0x0,-0x10(%rbp)
jl     133d <make_filename+0x1cd>
mov    -0x20(%rbp),%rdi
lea    0xcf6(%rip),%rsi
call   1090 <strcat@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x48(%rbp),%rdi
cltq
add    %rax,%rdi
mov    -0x10(%rbp),%ecx
mov    $0x5,%esi
lea    0xccc(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1090 <strcat@plt>
mov    -0x20(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<make_filename>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
xor    %r13d,%r13d
test   %edx,%edx
cmovs  %esi,%r13d
mov    %rdi,%rbx
test   %esi,%esi
cmovns %esi,%r13d
call   1050 <strlen@plt>
lea    0x14(%rax),%edi
call   1080 <malloc@plt>
mov    %rax,%r12
mov    %rbx,%rdi
mov    $0x2e,%esi
call   1070 <strrchr@plt>
test   %rax,%rax
je     124d <make_filename+0xdd>
mov    %rax,%r15
mov    %rax,%rbp
sub    %rbx,%rbp
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <strncpy@plt>
movslq %ebp,%rax
movb   $0x0,(%r12,%rax,1)
test   %r13d,%r13d
js     120a <make_filename+0x9a>
lea    0xe20(%rip),%rsi
mov    %r12,%rdi
call   1090 <strcat@plt>
mov    %r12,%rdi
call   1050 <strlen@plt>
movslq %eax,%rdi
add    %r12,%rdi
lea    0xe07(%rip),%rdx
mov    $0x5,%esi
mov    %r13d,%ecx
call   1060 <snprintf@plt>
test   %r14d,%r14d
js     1242 <make_filename+0xd2>
lea    0xdea(%rip),%rsi
mov    %r12,%rdi
call   1090 <strcat@plt>
mov    %r12,%rdi
call   1050 <strlen@plt>
cltq
mov    %r12,%rdi
add    %rax,%rdi
lea    0xdcf(%rip),%rdx
mov    $0x5,%esi
mov    %r14d,%ecx
call   1060 <snprintf@plt>
mov    %r12,%rdi
mov    %r15,%rsi
jmp    12d0 <make_filename+0x160>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
test   %r13d,%r13d
js     128e <make_filename+0x11e>
lea    0xd9c(%rip),%rsi
mov    %r12,%rdi
call   1090 <strcat@plt>
mov    %r12,%rdi
call   1050 <strlen@plt>
movslq %eax,%rdi
add    %r12,%rdi
lea    0xd83(%rip),%rdx
mov    $0x5,%esi
mov    %r13d,%ecx
call   1060 <snprintf@plt>
test   %r14d,%r14d
js     12c6 <make_filename+0x156>
lea    0xd66(%rip),%rsi
mov    %r12,%rdi
call   1090 <strcat@plt>
mov    %r12,%rdi
call   1050 <strlen@plt>
cltq
mov    %r12,%rdi
add    %rax,%rdi
lea    0xd4b(%rip),%rdx
mov    $0x5,%esi
mov    %r14d,%ecx
call   1060 <snprintf@plt>
lea    0xd3c(%rip),%rsi
mov    %r12,%rdi
call   1090 <strcat@plt>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"_0x\00"
@.str.1 = [5 x i8] c"%04x\00"
@.str.2 = [5 x i8] c".img\00"
define i8* @make_filename(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%10 = load i32, i32* %5
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %3
%13 = load i32, i32* %6
%14 = icmp sge i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %12
store i32 0, i32* %5
br label %16
16: ; preds = %15, %12, %3
%17 = load i8*, i8** %4
%18 = call i32 @strlen(i8* %17)
%19 = add nsw i32 %18, 20
%20 = call i64 @malloc(i32 %19)
%21 = inttoptr i64 %20 to i8*
store i8* %21, i8** %8
%22 = load i8*, i8** %4
%23 = call i8* @strrchr(i8* %22, i8 signext 46)
store i8* %23, i8** %9
%24 = load i8*, i8** %9
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %58
26: ; preds = %16
%27 = load i8*, i8** %8
%28 = load i8*, i8** %4
%29 = call i32 @strcpy(i8* %27, i8* %28)
%30 = load i32, i32* %5
%31 = icmp sge i32 %30, 0
br i1 %31, label %32, label %42
32: ; preds = %26
%33 = load i8*, i8** %8
%34 = call i32 @strcat(i8* %33, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%35 = load i8*, i8** %8
%36 = load i8*, i8** %8
%37 = call i32 @strlen(i8* %36)
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %35, i64 %38
%40 = load i32, i32* %5
%41 = call i32 @snprintf(i8* %39, i32 5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %40)
br label %42
42: ; preds = %32, %26
%43 = load i32, i32* %6
%44 = icmp sge i32 %43, 0
br i1 %44, label %45, label %55
45: ; preds = %42
%46 = load i8*, i8** %8
%47 = call i32 @strcat(i8* %46, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%48 = load i8*, i8** %8
%49 = load i8*, i8** %8
%50 = call i32 @strlen(i8* %49)
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %48, i64 %51
%53 = load i32, i32* %6
%54 = call i32 @snprintf(i8* %52, i32 5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %53)
br label %55
55: ; preds = %45, %42
%56 = load i8*, i8** %8
%57 = call i32 @strcat(i8* %56, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %102
58: ; preds = %16
%59 = load i8*, i8** %9
%60 = load i8*, i8** %4
%61 = ptrtoint i8* %59 to i64
%62 = ptrtoint i8* %60 to i64
%63 = sub i64 %61, %62
%64 = trunc i64 %63 to i32
store i32 %64, i32* %7
%65 = load i8*, i8** %8
%66 = load i8*, i8** %4
%67 = load i32, i32* %7
%68 = call i32 @strncpy(i8* %65, i8* %66, i32 %67)
%69 = load i8*, i8** %8
%70 = load i32, i32* %7
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds i8, i8* %69, i64 %71
store i8 0, i8* %72
%73 = load i32, i32* %5
%74 = icmp sge i32 %73, 0
br i1 %74, label %75, label %85
75: ; preds = %58
%76 = load i8*, i8** %8
%77 = call i32 @strcat(i8* %76, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%78 = load i8*, i8** %8
%79 = load i8*, i8** %8
%80 = call i32 @strlen(i8* %79)
%81 = sext i32 %80 to i64
%82 = getelementptr inbounds i8, i8* %78, i64 %81
%83 = load i32, i32* %5
%84 = call i32 @snprintf(i8* %82, i32 5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %83)
br label %85
85: ; preds = %75, %58
%86 = load i32, i32* %6
%87 = icmp sge i32 %86, 0
br i1 %87, label %88, label %98
88: ; preds = %85
%89 = load i8*, i8** %8
%90 = call i32 @strcat(i8* %89, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%91 = load i8*, i8** %8
%92 = load i8*, i8** %8
%93 = call i32 @strlen(i8* %92)
%94 = sext i32 %93 to i64
%95 = getelementptr inbounds i8, i8* %91, i64 %94
%96 = load i32, i32* %6
%97 = call i32 @snprintf(i8* %95, i32 5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %96)
br label %98
98: ; preds = %88, %85
%99 = load i8*, i8** %8
%100 = load i8*, i8** %9
%101 = call i32 @strcat(i8* %99, i8* %100)
br label %102
102: ; preds = %98, %55
%103 = load i8*, i8** %8
ret i8* %103
}

LLVM IR (-O2):
@.str = [4 x i8] c"_0x\00"
@.str.1 = [5 x i8] c"%04x\00"
@.str.2 = [5 x i8] c".img\00"
define i8* @make_filename(i8* %0, i32 %1, i32 %2) {
%4 = icmp slt i32 %1, 0
%5 = icmp sgt i32 %2, -1
%6 = and i1 %4, %5
%7 = select i1 %6, i32 0, i32 %1
%8 = tail call i32 @strlen(i8* %0)
%9 = add nsw i32 %8, 20
%10 = tail call i64 @malloc(i32 %9)
%11 = inttoptr i64 %10 to i8*
%12 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 46)
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %32
14: ; preds = %3
%15 = tail call i32 @strcpy(i8* %11, i8* %0)
%16 = icmp sgt i32 %7, -1
br i1 %16, label %17, label %23
17: ; preds = %14
%18 = tail call i32 @strcat(i8* %11, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%19 = tail call i32 @strlen(i8* %11)
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %11, i64 %20
%22 = tail call i32 @snprintf(i8* %21, i32 5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %7)
br label %23
23: ; preds = %17, %14
br i1 %5, label %24, label %30
24: ; preds = %23
%25 = tail call i32 @strcat(i8* %11, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%26 = tail call i32 @strlen(i8* %11)
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %11, i64 %27
%29 = tail call i32 @snprintf(i8* %28, i32 5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %2)
br label %30
30: ; preds = %24, %23
%31 = tail call i32 @strcat(i8* %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %57
32: ; preds = %3
%33 = ptrtoint i8* %12 to i64
%34 = ptrtoint i8* %0 to i64
%35 = sub i64 %33, %34
%36 = trunc i64 %35 to i32
%37 = tail call i32 @strncpy(i8* %11, i8* %0, i32 %36)
%38 = shl i64 %35, 32
%39 = ashr exact i64 %38, 32
%40 = getelementptr inbounds i8, i8* %11, i64 %39
store i8 0, i8* %40
%41 = icmp sgt i32 %7, -1
br i1 %41, label %42, label %48
42: ; preds = %32
%43 = tail call i32 @strcat(i8* %11, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%44 = tail call i32 @strlen(i8* %11)
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %11, i64 %45
%47 = tail call i32 @snprintf(i8* %46, i32 5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %7)
br label %48
48: ; preds = %42, %32
br i1 %5, label %49, label %55
49: ; preds = %48
%50 = tail call i32 @strcat(i8* %11, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%51 = tail call i32 @strlen(i8* %11)
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %11, i64 %52
%54 = tail call i32 @snprintf(i8* %53, i32 5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %2)
br label %55
55: ; preds = %49, %48
%56 = tail call i32 @strcat(i8* %11, i8* %12)
br label %57
57: ; preds = %55, %30
ret i8* %11
}