#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 */
struct stat {
  int /*<<< orphan*/ st_mode;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ S_ISDIR(int /*<<< orphan*/);
char *calloc(int, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char const *, ...);
scalar_t__ stat(char const *, struct stat *);
int /*<<< orphan*/ stderr;
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *strrchr(char const *, char);
FILE *util_create_file(const char *fname, const char *desc,
                       const char *extension, int is_stream) {
  int fileno;
  char *outname;
  const char *c, *r;
  FILE *out;
  struct stat st;
  if ((c = strrchr(fname, '/')) && strlen(c + 1))
    ++c;
  else
    c = fname;
  if ((stat(c, &st) == 0) && S_ISDIR(st.st_mode))
    c = "mp3nema";
  fileno = 0;
  outname = NULL;
  do {
    free(outname);
    outname = calloc(1, strlen(c) + 64);
    if (is_stream || !(r = strrchr(c, '.')))
      r = c + strlen(c);
    strncpy(outname, c, r - c);
    if (fileno > 0)
      sprintf(outname, "%s-%s-%d.%s", outname, desc, fileno, extension);
    else
      sprintf(outname, "%s-%s.%s", outname, desc, extension);
    ++fileno;
  } while ((out = fopen(outname, "r")) && !fclose(out));
  if (!(out = fopen(outname, "w"))) {
    {
      fprintf(stderr,
              "["
              "mp3nema"
              "]"
              "Error: "
              "Could not create output file '%s'\n",
              outname);
    };
    return NULL;
  }
  free(outname);
  return out;
}


x86 assembly (clang, -O0):
<util_create_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1080 <strrchr@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     120a <util_create_file+0x5a>
mov    -0x38(%rbp),%rdi
add    $0x1,%rdi
call   1060 <strlen@plt>
cmp    $0x0,%eax
je     120a <util_create_file+0x5a>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    1212 <util_create_file+0x62>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
lea    -0x50(%rbp),%rsi
call   10b0 <stat@plt>
cmp    $0x0,%rax
jne    1246 <util_create_file+0x96>
mov    -0x50(%rbp),%edi
call   1070 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1246 <util_create_file+0x96>
lea    0xdbe(%rip),%rax
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x38(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%esi
add    $0x40,%esi
mov    $0x1,%edi
call   1090 <calloc@plt>
mov    %rax,-0x30(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    12a0 <util_create_file+0xf0>
mov    -0x38(%rbp),%rdi
mov    $0x2e,%esi
call   1080 <strrchr@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    12c1 <util_create_file+0x111>
mov    -0x38(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x38(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x58(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x40(%rbp),%rax
mov    -0x38(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1040 <strncpy@plt>
cmpl   $0x0,-0x28(%rbp)
jle    130c <util_create_file+0x15c>
mov    -0x30(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x28(%rbp),%r8d
mov    -0x20(%rbp),%r9
lea    0xd08(%rip),%rsi
mov    $0x0,%al
call   10d0 <sprintf@plt>
jmp    132a <util_create_file+0x17a>
mov    -0x30(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
lea    0xcf1(%rip),%rsi
mov    $0x0,%al
call   10d0 <sprintf@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xcdf(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,%rcx
mov    %rcx,-0x48(%rbp)
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x59(%rbp)
je     136d <util_create_file+0x1bd>
mov    -0x48(%rbp),%rdi
call   1050 <fclose@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x59(%rbp)
mov    -0x59(%rbp),%al
test   $0x1,%al
jne    1255 <util_create_file+0xa5>
mov    -0x30(%rbp),%rdi
lea    0xc9c(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x48(%rbp)
cmp    $0x0,%rax
jne    13bc <util_create_file+0x20c>
mov    0x2c43(%rip),%rax
mov    (%rax),%edi
mov    -0x30(%rbp),%rdx
lea    0xc77(%rip),%rsi
call   10a0 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    13cd <util_create_file+0x21d>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x48(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_create_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%rbx
mov    $0x2f,%esi
call   1080 <strrchr@plt>
test   %rax,%rax
je     11eb <util_create_file+0x3b>
mov    %rax,%rbp
add    $0x1,%rbp
mov    %rbp,%rdi
call   1060 <strlen@plt>
test   %eax,%eax
cmovne %rbp,%rbx
mov    %rsp,%rsi
mov    %rbx,%rdi
call   10b0 <stat@plt>
test   %rax,%rax
jne    1211 <util_create_file+0x61>
mov    (%rsp),%edi
call   1070 <S_ISDIR@plt>
test   %rax,%rax
lea    0xdf3(%rip),%rax
cmovne %rax,%rbx
xor    %r13d,%r13d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
lea    0x40(%rax),%esi
mov    $0x1,%edi
call   1090 <calloc@plt>
mov    %rax,%rbp
test   %r14d,%r14d
je     1260 <util_create_file+0xb0>
mov    %rbx,%rdi
call   1060 <strlen@plt>
movslq %eax,%rdx
add    %rbx,%rdx
jmp    1275 <util_create_file+0xc5>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
mov    $0x2e,%esi
call   1080 <strrchr@plt>
mov    %rax,%rdx
test   %rax,%rax
je     1245 <util_create_file+0x95>
sub    %ebx,%edx
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1040 <strncpy@plt>
mov    %rbp,%rdi
test   %r13d,%r13d
je     12b0 <util_create_file+0x100>
lea    0xd77(%rip),%rsi
mov    %rbp,%rdx
mov    %r12,%rcx
mov    %r13d,%r8d
mov    %r15,%r9
xor    %eax,%eax
call   10d0 <sprintf@plt>
jmp    12c7 <util_create_file+0x117>
cs nopw 0x0(%rax,%rax,1)
lea    0xd5d(%rip),%rsi
mov    %rbp,%rdx
mov    %r12,%rcx
mov    %r15,%r8
xor    %eax,%eax
call   10d0 <sprintf@plt>
mov    %rbp,%rdi
lea    0xd4c(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
je     12ef <util_create_file+0x13f>
add    $0x1,%r13d
mov    %rax,%rdi
call   1050 <fclose@plt>
test   %eax,%eax
je     1220 <util_create_file+0x70>
lea    0xd29(%rip),%rsi
mov    %rbp,%rdi
call   10c0 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1310 <util_create_file+0x160>
mov    %rbp,%rdi
call   1030 <free@plt>
jmp    1328 <util_create_file+0x178>
mov    0x2cc9(%rip),%rax
mov    (%rax),%edi
lea    0xd01(%rip),%rsi
mov    %rbp,%rdx
call   10a0 <fprintf@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [8 x i8] c"mp3nema\00"
@.str.1 = [12 x i8] c"%s-%s-%d.%s\00"
@.str.2 = [9 x i8] c"%s-%s.%s\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.5 = [51 x i8] c"[mp3nema]Error: Could not create output file '%s'\0A\00"
define i32* @util_create_file(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i32*
%15 = alloca %struct.stat
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%16 = load i8*, i8** %6
%17 = call i8* @strrchr(i8* %16, i8 signext 47)
store i8* %17, i8** %12
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %27
19: ; preds = %4
%20 = load i8*, i8** %12
%21 = getelementptr inbounds i8, i8* %20, i64 1
%22 = call i32 @strlen(i8* %21)
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = load i8*, i8** %12
%26 = getelementptr inbounds i8, i8* %25, i32 1
store i8* %26, i8** %12
br label %29
27: ; preds = %19, %4
%28 = load i8*, i8** %6
store i8* %28, i8** %12
br label %29
29: ; preds = %27, %24
%30 = load i8*, i8** %12
%31 = call i64 @stat(i8* %30, %struct.stat* %15)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %39
33: ; preds = %29
%34 = getelementptr inbounds %struct.stat, %struct.stat* %15, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i64 @S_ISDIR(i32 %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %33
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %12
br label %39
39: ; preds = %38, %33, %29
store i32 0, i32* %10
store i8* null, i8** %11
br label %40
40: ; preds = %96, %39
%41 = load i8*, i8** %11
%42 = call i32 @free(i8* %41)
%43 = load i8*, i8** %12
%44 = call i32 @strlen(i8* %43)
%45 = add nsw i32 %44, 64
%46 = call i8* @calloc(i32 1, i32 %45)
store i8* %46, i8** %11
%47 = load i32, i32* %9
%48 = icmp ne i32 %47, 0
br i1 %48, label %53, label %49
49: ; preds = %40
%50 = load i8*, i8** %12
%51 = call i8* @strrchr(i8* %50, i8 signext 46)
store i8* %51, i8** %13
%52 = icmp ne i8* %51, null
br i1 %52, label %59, label %53
53: ; preds = %49, %40
%54 = load i8*, i8** %12
%55 = load i8*, i8** %12
%56 = call i32 @strlen(i8* %55)
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %54, i64 %57
store i8* %58, i8** %13
br label %59
59: ; preds = %53, %49
%60 = load i8*, i8** %11
%61 = load i8*, i8** %12
%62 = load i8*, i8** %13
%63 = load i8*, i8** %12
%64 = ptrtoint i8* %62 to i64
%65 = ptrtoint i8* %63 to i64
%66 = sub i64 %64, %65
%67 = trunc i64 %66 to i32
%68 = call i32 @strncpy(i8* %60, i8* %61, i32 %67)
%69 = load i32, i32* %10
%70 = icmp sgt i32 %69, 0
br i1 %70, label %71, label %78
71: ; preds = %59
%72 = load i8*, i8** %11
%73 = load i8*, i8** %11
%74 = load i8*, i8** %7
%75 = load i32, i32* %10
%76 = load i8*, i8** %8
%77 = call i32 (i8*, i8*, i8*, i8*, ...) @sprintf(i8* %72, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %73, i8* %74, i32 %75, i8* %76)
br label %84
78: ; preds = %59
%79 = load i8*, i8** %11
%80 = load i8*, i8** %11
%81 = load i8*, i8** %7
%82 = load i8*, i8** %8
%83 = call i32 (i8*, i8*, i8*, i8*, ...) @sprintf(i8* %79, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %80, i8* %81, i8* %82)
br label %84
84: ; preds = %78, %71
%85 = load i32, i32* %10
%86 = add nsw i32 %85, 1
store i32 %86, i32* %10
br label %87
87: ; preds = %84
%88 = load i8*, i8** %11
%89 = call i32* @fopen(i8* %88, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %89, i32** %14
%90 = icmp ne i32* %89, null
br i1 %90, label %91, label %96
91: ; preds = %87
%92 = load i32*, i32** %14
%93 = call i32 @fclose(i32* %92)
%94 = icmp ne i32 %93, 0
%95 = xor i1 %94, true
br label %96
96: ; preds = %91, %87
%97 = phi i1 [ false, %87 ], [ %95, %91 ]
br i1 %97, label %40, label %98
98: ; preds = %96
%99 = load i8*, i8** %11
%100 = call i32* @fopen(i8* %99, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %100, i32** %14
%101 = icmp ne i32* %100, null
br i1 %101, label %106, label %102
102: ; preds = %98
%103 = load i32, i32* @stderr
%104 = load i8*, i8** %11
%105 = call i32 @fprintf(i32 %103, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.5, i64 0, i64 0), i8* %104)
store i32* null, i32** %5
br label %110
106: ; preds = %98
%107 = load i8*, i8** %11
%108 = call i32 @free(i8* %107)
%109 = load i32*, i32** %14
store i32* %109, i32** %5
br label %110
110: ; preds = %106, %102
%111 = load i32*, i32** %5
ret i32* %111
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [8 x i8] c"mp3nema\00"
@.str.1 = [12 x i8] c"%s-%s-%d.%s\00"
@.str.2 = [9 x i8] c"%s-%s.%s\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.5 = [51 x i8] c"[mp3nema]Error: Could not create output file '%s'\0A\00"
define noalias i32* @util_create_file(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca %struct.stat
%6 = bitcast %struct.stat* %5 to i8*
%7 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%8 = icmp eq i8* %7, null
br i1 %8, label %14, label %9
9: ; preds = %4
%10 = getelementptr inbounds i8, i8* %7, i64 1
%11 = tail call i32 @strlen(i8* %10)
%12 = icmp eq i32 %11, 0
%13 = select i1 %12, i8* %0, i8* %10
br label %14
14: ; preds = %9, %4
%15 = phi i8* [ %0, %4 ], [ %13, %9 ]
%16 = call i64 @stat(i8* %15, %struct.stat* %5)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %14
%19 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%20 = load i32, i32* %19
%21 = call i64 @S_ISDIR(i32 %20)
%22 = icmp eq i64 %21, 0
%23 = select i1 %22, i8* %15, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0)
br label %24
24: ; preds = %18, %14
%25 = phi i8* [ %15, %14 ], [ %23, %18 ]
%26 = icmp eq i32 %3, 0
%27 = ptrtoint i8* %25 to i64
br label %28
28: ; preds = %56, %24
%29 = phi i32 [ 0, %24 ], [ %57, %56 ]
%30 = phi i8* [ null, %24 ], [ %34, %56 ]
%31 = call i32 @free(i8* %30)
%32 = call i32 @strlen(i8* %25)
%33 = add nsw i32 %32, 64
%34 = call i8* @calloc(i32 1, i32 %33)
br i1 %26, label %35, label %38
35: ; preds = %28
%36 = call i8* @strrchr(i8* dereferenceable(1) %25, i8 signext 46)
%37 = icmp eq i8* %36, null
br i1 %37, label %38, label %42
38: ; preds = %35, %28
%39 = call i32 @strlen(i8* %25)
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %25, i64 %40
br label %42
42: ; preds = %38, %35
%43 = phi i8* [ %41, %38 ], [ %36, %35 ]
%44 = ptrtoint i8* %43 to i64
%45 = sub i64 %44, %27
%46 = trunc i64 %45 to i32
%47 = call i32 @strncpy(i8* %34, i8* %25, i32 %46)
%48 = icmp eq i32 %29, 0
br i1 %48, label %51, label %49
49: ; preds = %42
%50 = call i32 (i8*, i8*, i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %34, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %34, i8* %1, i32 %29, i8* %2)
br label %53
51: ; preds = %42
%52 = call i32 (i8*, i8*, i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %34, i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %34, i8* %1, i8* %2)
br label %53
53: ; preds = %51, %49
%54 = call i32* @fopen(i8* %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%55 = icmp eq i32* %54, null
br i1 %55, label %60, label %56
56: ; preds = %53
%57 = add nuw nsw i32 %29, 1
%58 = call i32 @fclose(i32* %54)
%59 = icmp eq i32 %58, 0
br i1 %59, label %28, label %60
60: ; preds = %53, %56
%61 = call i32* @fopen(i8* %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%62 = icmp eq i32* %61, null
br i1 %62, label %63, label %66
63: ; preds = %60
%64 = load i32, i32* @stderr
%65 = call i32 @fprintf(i32 %64, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.5, i64 0, i64 0), i8* %34)
br label %68
66: ; preds = %60
%67 = call i32 @free(i8* %34)
br label %68
68: ; preds = %66, %63
ret i32* %61
}