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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
char *form_filename(int, char *);
int /*<<< orphan*/ free(char *);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int save_buf(char *buf, int n, char *dir, int chunk) {
  FILE *f = NULL;
  int nwrite;
  char *path;
  int ret = 0;
  path = form_filename(chunk, dir);
  f = fopen(path, "wb");
  if (f == NULL) {
    perror("fopen");
    ret = -1;
    goto exit;
  }
  nwrite = fwrite(buf, sizeof(int), n, f);
  if (nwrite != n) {
    perror("fwrite");
    ret = -1;
    goto exit;
  }
exit:
  if (f) {
    fclose(f);
  }
  if (path) {
    free(path);
  }
  return ret;
}


x86 assembly (clang, -O0):
<save_buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1080 <form_filename@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xe60(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11cc <save_buf+0x6c>
lea    0xe48(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x3c(%rbp)
jmp    120d <save_buf+0xad>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x4,%esi
call   1070 <fwrite@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0xc(%rbp),%eax
je     1208 <save_buf+0xa8>
lea    0xe12(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x3c(%rbp)
jmp    120d <save_buf+0xad>
jmp    120d <save_buf+0xad>
cmpq   $0x0,-0x28(%rbp)
je     1221 <save_buf+0xc1>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
cmpq   $0x0,-0x38(%rbp)
je     1235 <save_buf+0xd5>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x3c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_buf>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%r15
mov    %ecx,%edi
mov    %rdx,%rsi
call   1080 <form_filename@plt>
mov    %rax,%r14
lea    0xe80(%rip),%rsi
mov    %rax,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     11cc <save_buf+0x6c>
mov    %rax,%rbx
mov    %r15,%rdi
mov    $0x4,%esi
mov    %ebp,%edx
mov    %rax,%rcx
call   1070 <fwrite@plt>
xor    %r15d,%r15d
cmp    %ebp,%eax
jne    11e5 <save_buf+0x85>
mov    %rbx,%rdi
call   1040 <fclose@plt>
test   %r14,%r14
je     11be <save_buf+0x5e>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe30(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%r15d
test   %r14,%r14
jne    11b6 <save_buf+0x56>
jmp    11be <save_buf+0x5e>
lea    0xe1d(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%r15d
jmp    11a9 <save_buf+0x49>

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

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@.str.1 = [6 x i8] c"fopen\00"
@.str.2 = [7 x i8] c"fwrite\00"
define i32 @save_buf(i8* nocapture %0, i32 %1, i8* %2, i32 %3) {
%5 = tail call i8* @form_filename(i32 %3, i8* %2)
%6 = tail call i32* @fopen(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %13, label %8
8: ; preds = %4
%9 = tail call i32 @fwrite(i8* %0, i32 4, i32 %1, i32* %6)
%10 = icmp eq i32 %9, %1
br i1 %10, label %15, label %11
11: ; preds = %8
%12 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %15
13: ; preds = %4
%14 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %18
15: ; preds = %8, %11
%16 = phi i32 [ 0, %8 ], [ -1, %11 ]
%17 = tail call i32 @fclose(i32* %6)
br label %18
18: ; preds = %13, %15
%19 = phi i32 [ %16, %15 ], [ -1, %13 ]
%20 = icmp eq i8* %5, null
br i1 %20, label %23, label %21
21: ; preds = %18
%22 = tail call i32 @free(i8* %5)
br label %23
23: ; preds = %21, %18
ret i32 %19
}