#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 char uint8_t;
typedef int uint32_t;
typedef int uint16_t;
typedef int /*<<< orphan*/ filename;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
char *convert_key(char *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fatal(char *, char *, ...);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
char *make_carousel_root(char *, int, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *, int, char *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ verbose(char *, char *);
void save_file(char *paramDir, char *kind, uint16_t elementary_pid,
               uint32_t carousel_id, uint16_t module_id, char *key,
               uint32_t key_size, char *file, uint32_t file_size,
               uint8_t module_version) {
  char *root;
  char *ascii_key;
  char filename[4096];
  FILE *f;
  root = make_carousel_root(paramDir, elementary_pid, carousel_id);
  ascii_key = convert_key(key, key_size);
  snprintf(filename, sizeof(filename), "%s/%s-%u-%s", root, kind, module_id,
           ascii_key);
  if ((f = fopen(filename, "wb")) == NULL)
    fatal("Unable to create file '%s': %s", filename, strerror(errno));
  if (fwrite(file, 1, file_size, f) != file_size)
    fatal("Unable to write to file '%s'", filename);
  fclose(f);
  verbose("Created file '%s'\n", filename);
  snprintf(filename, sizeof(filename), "%s/%s-%u-%s.version", root, kind,
           module_id, ascii_key);
  if ((f = fopen(filename, "wb")) == NULL)
    fatal("Unable to create file '%s': %s", filename, strerror(errno));
  if (fwrite(&module_version, 1, 1, f) != 1)
    fatal("Unable to write to file '%s'", filename);
  fclose(f);
  return;
}


x86 assembly (clang, -O0):
<save_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1060,%rsp
mov    0x28(%rbp),%al
mov    0x20(%rbp),%eax
mov    0x18(%rbp),%rax
mov    0x10(%rbp),%eax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    %r9,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
call   1030 <make_carousel_root@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x10(%rbp),%esi
call   1040 <convert_key@plt>
mov    %rax,-0x38(%rbp)
lea    -0x1040(%rbp),%rdi
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%r8
mov    -0x1c(%rbp),%r9d
mov    -0x38(%rbp),%rax
mov    $0x1000,%esi
lea    0xdfc(%rip),%rdx
mov    %rax,(%rsp)
call   1060 <snprintf@plt>
lea    -0x1040(%rbp),%rdi
lea    0xdf1(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x1048(%rbp)
cmp    $0x0,%rax
jne    1264 <save_file+0xd4>
lea    -0x1040(%rbp),%rax
mov    %rax,-0x1050(%rbp)
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x1050(%rbp),%rsi
mov    %eax,%edx
lea    0xdb2(%rip),%rdi
mov    $0x0,%al
call   10b0 <fatal@plt>
mov    0x18(%rbp),%rdi
mov    0x20(%rbp),%edx
mov    -0x1048(%rbp),%rcx
mov    $0x1,%esi
call   1090 <fwrite@plt>
cmp    0x20(%rbp),%eax
je     129a <save_file+0x10a>
lea    -0x1040(%rbp),%rsi
lea    0xd9b(%rip),%rdi
mov    $0x0,%al
call   10b0 <fatal@plt>
mov    -0x1048(%rbp),%rdi
call   1050 <fclose@plt>
lea    -0x1040(%rbp),%rsi
lea    0xd97(%rip),%rdi
call   1080 <verbose@plt>
lea    -0x1040(%rbp),%rdi
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%r8
mov    -0x1c(%rbp),%r9d
mov    -0x38(%rbp),%rax
mov    $0x1000,%esi
lea    0xd82(%rip),%rdx
mov    %rax,(%rsp)
call   1060 <snprintf@plt>
lea    -0x1040(%rbp),%rdi
lea    0xd19(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x1048(%rbp)
cmp    $0x0,%rax
jne    133c <save_file+0x1ac>
lea    -0x1040(%rbp),%rax
mov    %rax,-0x1058(%rbp)
mov    0x2cc2(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x1058(%rbp),%rsi
mov    %eax,%edx
lea    0xcda(%rip),%rdi
mov    $0x0,%al
call   10b0 <fatal@plt>
mov    -0x1048(%rbp),%rcx
lea    0x28(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1090 <fwrite@plt>
cmp    $0x1,%eax
je     1371 <save_file+0x1e1>
lea    -0x1040(%rbp),%rsi
lea    0xcc4(%rip),%rdi
mov    $0x0,%al
call   10b0 <fatal@plt>
mov    -0x1048(%rbp),%rdi
call   1050 <fclose@plt>
add    $0x1060,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %r9,%rbx
mov    %r8d,%r14d
mov    %rsi,%r15
mov    0x1050(%rsp),%ebp
mov    %edx,%esi
mov    %ecx,%edx
call   1030 <make_carousel_root@plt>
mov    %rax,%r12
mov    %rbx,%rdi
mov    %ebp,%esi
call   1040 <convert_key@plt>
mov    %rax,%rbx
mov    %rax,(%rsp)
lea    0xe2b(%rip),%rdx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x1000,%esi
mov    %r12,%rcx
mov    %r15,%r8
mov    %r14d,%r9d
call   1060 <snprintf@plt>
lea    0xe15(%rip),%rsi
mov    %rbp,%rdi
call   1070 <fopen@plt>
mov    %rax,%r13
test   %rax,%rax
jne    122a <save_file+0x9a>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdf3(%rip),%rdi
lea    0x10(%rsp),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   10b0 <fatal@plt>
mov    0x1058(%rsp),%rdi
mov    $0x1,%esi
mov    0x1060(%rsp),%ebp
mov    %ebp,%edx
mov    %r13,%rcx
call   1090 <fwrite@plt>
cmp    %ebp,%eax
je     125f <save_file+0xcf>
lea    0xddb(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   10b0 <fatal@plt>
mov    %r13,%rdi
call   1050 <fclose@plt>
lea    0xddd(%rip),%rdi
lea    0x10(%rsp),%rbp
mov    %rbp,%rsi
call   1080 <verbose@plt>
mov    %rbx,(%rsp)
lea    0xdd8(%rip),%rdx
mov    %rbp,%rdi
mov    $0x1000,%esi
mov    %r12,%rcx
mov    %r15,%r8
mov    %r14d,%r9d
call   1060 <snprintf@plt>
lea    0xd69(%rip),%rsi
mov    %rbp,%rdi
call   1070 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    12d6 <save_file+0x146>
mov    0x2d26(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd47(%rip),%rdi
lea    0x10(%rsp),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   10b0 <fatal@plt>
lea    0x1068(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1090 <fwrite@plt>
cmp    $0x1,%eax
je     1308 <save_file+0x178>
lea    0xd32(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   10b0 <fatal@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"%s/%s-%u-%s\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [31 x i8] c"Unable to create file '%s': %s\00"
@errno = global i32 0
@.str.3 = [29 x i8] c"Unable to write to file '%s'\00"
@.str.4 = [19 x i8] c"Created file '%s'\0A\00"
@.str.5 = [20 x i8] c"%s/%s-%u-%s.version\00"
define void @save_file(i8* %0, i8* %1, i32 %2, i32 %3, i32 %4, i8* %5, i32 %6, i8* %7, i32 %8, i8 signext %9) {
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i8*
%17 = alloca i32
%18 = alloca i8*
%19 = alloca i32
%20 = alloca i8
%21 = alloca i8*
%22 = alloca i8*
%23 = alloca [4096 x i8]
%24 = alloca i32*
store i8* %0, i8** %11
store i8* %1, i8** %12
store i32 %2, i32* %13
store i32 %3, i32* %14
store i32 %4, i32* %15
store i8* %5, i8** %16
store i32 %6, i32* %17
store i8* %7, i8** %18
store i32 %8, i32* %19
store i8 %9, i8* %20
%25 = load i8*, i8** %11
%26 = load i32, i32* %13
%27 = load i32, i32* %14
%28 = call i8* @make_carousel_root(i8* %25, i32 %26, i32 %27)
store i8* %28, i8** %21
%29 = load i8*, i8** %16
%30 = load i32, i32* %17
%31 = call i8* @convert_key(i8* %29, i32 %30)
store i8* %31, i8** %22
%32 = getelementptr inbounds [4096 x i8], [4096 x i8]* %23, i64 0, i64 0
%33 = load i8*, i8** %21
%34 = load i8*, i8** %12
%35 = load i32, i32* %15
%36 = load i8*, i8** %22
%37 = call i32 @snprintf(i8* %32, i32 4096, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %33, i8* %34, i32 %35, i8* %36)
%38 = getelementptr inbounds [4096 x i8], [4096 x i8]* %23, i64 0, i64 0
%39 = call i32* @fopen(i8* %38, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %39, i32** %24
%40 = icmp eq i32* %39, null
br i1 %40, label %41, label %46
41: ; preds = %10
%42 = getelementptr inbounds [4096 x i8], [4096 x i8]* %23, i64 0, i64 0
%43 = load i32, i32* @errno
%44 = call i32 @strerror(i32 %43)
%45 = call i32 (i8*, i8*, ...) @fatal(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %42, i32 %44)
br label %46
46: ; preds = %41, %10
%47 = load i8*, i8** %18
%48 = load i32, i32* %19
%49 = load i32*, i32** %24
%50 = call i32 @fwrite(i8* %47, i32 1, i32 %48, i32* %49)
%51 = load i32, i32* %19
%52 = icmp ne i32 %50, %51
br i1 %52, label %53, label %56
53: ; preds = %46
%54 = getelementptr inbounds [4096 x i8], [4096 x i8]* %23, i64 0, i64 0
%55 = call i32 (i8*, i8*, ...) @fatal(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %54)
br label %56
56: ; preds = %53, %46
%57 = load i32*, i32** %24
%58 = call i32 @fclose(i32* %57)
%59 = getelementptr inbounds [4096 x i8], [4096 x i8]* %23, i64 0, i64 0
%60 = call i32 @verbose(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %59)
%61 = getelementptr inbounds [4096 x i8], [4096 x i8]* %23, i64 0, i64 0
%62 = load i8*, i8** %21
%63 = load i8*, i8** %12
%64 = load i32, i32* %15
%65 = load i8*, i8** %22
%66 = call i32 @snprintf(i8* %61, i32 4096, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i8* %62, i8* %63, i32 %64, i8* %65)
%67 = getelementptr inbounds [4096 x i8], [4096 x i8]* %23, i64 0, i64 0
%68 = call i32* @fopen(i8* %67, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %68, i32** %24
%69 = icmp eq i32* %68, null
br i1 %69, label %70, label %75
70: ; preds = %56
%71 = getelementptr inbounds [4096 x i8], [4096 x i8]* %23, i64 0, i64 0
%72 = load i32, i32* @errno
%73 = call i32 @strerror(i32 %72)
%74 = call i32 (i8*, i8*, ...) @fatal(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %71, i32 %73)
br label %75
75: ; preds = %70, %56
%76 = load i32*, i32** %24
%77 = call i32 @fwrite(i8* %20, i32 1, i32 1, i32* %76)
%78 = icmp ne i32 %77, 1
br i1 %78, label %79, label %82
79: ; preds = %75
%80 = getelementptr inbounds [4096 x i8], [4096 x i8]* %23, i64 0, i64 0
%81 = call i32 (i8*, i8*, ...) @fatal(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %80)
br label %82
82: ; preds = %79, %75
%83 = load i32*, i32** %24
%84 = call i32 @fclose(i32* %83)
ret void
}

LLVM IR (-O2):
@.str = [12 x i8] c"%s/%s-%u-%s\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [31 x i8] c"Unable to create file '%s': %s\00"
@errno = global i32 0
@.str.3 = [29 x i8] c"Unable to write to file '%s'\00"
@.str.4 = [19 x i8] c"Created file '%s'\0A\00"
@.str.5 = [20 x i8] c"%s/%s-%u-%s.version\00"
define void @save_file(i8* %0, i8* %1, i32 %2, i32 %3, i32 %4, i8* %5, i32 %6, i8* nocapture %7, i32 %8, i8 signext %9) {
%11 = alloca i8
%12 = alloca [4096 x i8]
store i8 %9, i8* %11
%13 = getelementptr inbounds [4096 x i8], [4096 x i8]* %12, i64 0, i64 0
%14 = tail call i8* @make_carousel_root(i8* %0, i32 %2, i32 %3)
%15 = tail call i8* @convert_key(i8* %5, i32 %6)
%16 = call i32 @snprintf(i8* %13, i32 4096, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %14, i8* %1, i32 %4, i8* %15)
%17 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %23
19: ; preds = %10
%20 = load i32, i32* @errno
%21 = call i32 @strerror(i32 %20)
%22 = call i32 (i8*, i8*, ...) @fatal(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %13, i32 %21)
br label %23
23: ; preds = %19, %10
%24 = call i32 @fwrite(i8* %7, i32 1, i32 %8, i32* %17)
%25 = icmp eq i32 %24, %8
br i1 %25, label %28, label %26
26: ; preds = %23
%27 = call i32 (i8*, i8*, ...) @fatal(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %13)
br label %28
28: ; preds = %26, %23
%29 = call i32 @fclose(i32* %17)
%30 = call i32 @verbose(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %13)
%31 = call i32 @snprintf(i8* %13, i32 4096, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i8* %14, i8* %1, i32 %4, i8* %15)
%32 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %38
34: ; preds = %28
%35 = load i32, i32* @errno
%36 = call i32 @strerror(i32 %35)
%37 = call i32 (i8*, i8*, ...) @fatal(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %13, i32 %36)
br label %38
38: ; preds = %34, %28
%39 = call i32 @fwrite(i8* %11, i32 1, i32 1, i32* %32)
%40 = icmp eq i32 %39, 1
br i1 %40, label %43, label %41
41: ; preds = %38
%42 = call i32 (i8*, i8*, ...) @fatal(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %13)
br label %43
43: ; preds = %41, %38
%44 = call i32 @fclose(i32* %32)
ret void
}