#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 scalar_t__ uint16_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int /*<<< orphan*/ snprintf(char *, size_t, char *, char const *, unsigned int,
                            unsigned int, char const *);
int strlen(char const *);
FILE *open_file(const char *prefix, const char *type_ext, uint16_t subtype,
                int file_number) {
  size_t namelen = strlen(prefix) + 1 + 5 + 1 + 4 + 1 + strlen(type_ext) + 1;
  char *namebuf = malloc(namelen);
  if (!namebuf) {
    return 0;
  }
  snprintf(namebuf, namelen, "%s_%05u_%04x.%s", prefix,
           (unsigned int)file_number, (unsigned int)subtype, type_ext);
  FILE *outfile = fopen(namebuf, "wb");
  free(namebuf);
  return outfile;
}


x86 assembly (clang, -O0):
<open_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
add    $0x5,%eax
add    $0x1,%eax
add    $0x4,%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x44(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
cltq
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11c0 <open_file+0x70>
movq   $0x0,-0x8(%rbp)
jmp    1210 <open_file+0xc0>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x10(%rbp),%rcx
mov    -0x24(%rbp),%r8d
mov    -0x20(%rbp),%rax
mov    %eax,%r9d
mov    -0x18(%rbp),%rax
lea    0xe1e(%rip),%rdx
mov    %rax,(%rsp)
call   1050 <snprintf@plt>
mov    -0x38(%rbp),%rdi
lea    0xe1a(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r15d
mov    %rdx,%r14
mov    %rsi,%r13
mov    %rdi,%r12
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %r13,%rdi
call   1040 <strlen@plt>
add    %ebx,%eax
add    $0xd,%eax
movslq %eax,%rbp
mov    %rbp,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     11cc <open_file+0x7c>
mov    %rax,%rbx
mov    %r13,(%rsp)
lea    0xe67(%rip),%rdx
mov    %rax,%rdi
mov    %rbp,%rsi
mov    %r12,%rcx
mov    %r15d,%r8d
mov    %r14d,%r9d
call   1050 <snprintf@plt>
lea    0xe5c(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
mov    %rax,%rbp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %rbp,%rax
jmp    11ce <open_file+0x7e>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [16 x i8] c"%s_%05u_%04x.%s\00"
@.str.1 = [3 x i8] c"wb\00"
define i32* @open_file(i8* %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i32*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i32 %3, i32* %9
%13 = load i8*, i8** %6
%14 = call i32 @strlen(i8* %13)
%15 = add nsw i32 %14, 1
%16 = add nsw i32 %15, 5
%17 = add nsw i32 %16, 1
%18 = add nsw i32 %17, 4
%19 = add nsw i32 %18, 1
%20 = load i8*, i8** %7
%21 = call i32 @strlen(i8* %20)
%22 = add nsw i32 %19, %21
%23 = add nsw i32 %22, 1
%24 = sext i32 %23 to i64
store i64 %24, i64* %10
%25 = load i64, i64* %10
%26 = call i8* @malloc(i64 %25)
store i8* %26, i8** %11
%27 = load i8*, i8** %11
%28 = icmp ne i8* %27, null
br i1 %28, label %30, label %29
29: ; preds = %4
store i32* null, i32** %5
br label %44
30: ; preds = %4
%31 = load i8*, i8** %11
%32 = load i64, i64* %10
%33 = load i8*, i8** %6
%34 = load i32, i32* %9
%35 = load i64, i64* %8
%36 = trunc i64 %35 to i32
%37 = load i8*, i8** %7
%38 = call i32 @snprintf(i8* %31, i64 %32, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %33, i32 %34, i32 %36, i8* %37)
%39 = load i8*, i8** %11
%40 = call i32* @fopen(i8* %39, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %40, i32** %12
%41 = load i8*, i8** %11
%42 = call i32 @free(i8* %41)
%43 = load i32*, i32** %12
store i32* %43, i32** %5
br label %44
44: ; preds = %30, %29
%45 = load i32*, i32** %5
ret i32* %45
}

LLVM IR (-O2):
@.str = [16 x i8] c"%s_%05u_%04x.%s\00"
@.str.1 = [3 x i8] c"wb\00"
define noalias i32* @open_file(i8* %0, i8* %1, i64 %2, i32 %3) {
%5 = tail call i32 @strlen(i8* %0)
%6 = tail call i32 @strlen(i8* %1)
%7 = add i32 %5, 13
%8 = add i32 %7, %6
%9 = sext i32 %8 to i64
%10 = tail call i8* @malloc(i64 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %17, label %12
12: ; preds = %4
%13 = trunc i64 %2 to i32
%14 = tail call i32 @snprintf(i8* %10, i64 %9, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %3, i32 %13, i8* %1)
%15 = tail call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%16 = tail call i32 @free(i8* %10)
br label %17
17: ; preds = %4, %12
%18 = phi i32* [ %15, %12 ], [ null, %4 ]
ret i32* %18
}