#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 pdf_doc {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ errno;
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int pdf_save_file(struct pdf_doc *, int /*<<< orphan*/ *);
int pdf_set_err(struct pdf_doc *, int /*<<< orphan*/, char *, char const *,
                int /*<<< orphan*/);
int /*<<< orphan*/ *stdout;
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int pdf_save(struct pdf_doc *pdf, const char *filename) {
  FILE *fp;
  int e;
  if (filename == NULL)
    fp = stdout;
  else if ((fp = fopen(filename, "wb")) == NULL)
    return pdf_set_err(pdf, -errno, "Unable to open '%s': %s", filename,
                       strerror(errno));
  e = pdf_save_file(pdf, fp);
  if (fp != stdout)
    if (fclose(fp) != 0 && e >= 0)
      return pdf_set_err(pdf, -errno, "Unable to close '%s': %s", filename,
                         strerror(errno));
  return e;
}


x86 assembly (clang, -O0):
<pdf_save>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    117e <pdf_save+0x2e>
mov    0x2e66(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    11ef <pdf_save+0x9f>
mov    -0x18(%rbp),%rdi
lea    0xe77(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11ea <pdf_save+0x9a>
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
xor    %eax,%eax
mov    0x2e33(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x40(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    -0x30(%rbp),%rcx
mov    %eax,%r8d
lea    0xe26(%rip),%rdx
call   1060 <pdf_set_err@plt>
mov    %eax,-0x4(%rbp)
jmp    1289 <pdf_save+0x139>
jmp    11ef <pdf_save+0x9f>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <pdf_save_file@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2dce(%rip),%rcx
cmp    (%rcx),%rax
je     1283 <pdf_save+0x133>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
cmp    $0x0,%rax
je     127e <pdf_save+0x12e>
cmpl   $0x0,-0x24(%rbp)
jl     127e <pdf_save+0x12e>
mov    -0x10(%rbp),%rax
mov    %rax,-0x58(%rbp)
xor    %eax,%eax
mov    0x2d9f(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x58(%rbp),%rdi
mov    -0x4c(%rbp),%esi
mov    -0x48(%rbp),%rcx
mov    %eax,%r8d
lea    0xdaa(%rip),%rdx
call   1060 <pdf_set_err@plt>
mov    %eax,-0x4(%rbp)
jmp    1289 <pdf_save+0x139>
jmp    1283 <pdf_save+0x133>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pdf_save>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%r14
test   %rsi,%rsi
je     1194 <pdf_save+0x44>
lea    0xe97(%rip),%rsi
mov    %r15,%rdi
call   1050 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    119e <pdf_save+0x4e>
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
mov    %edi,%ebx
neg    %ebx
call   1070 <strerror@plt>
lea    0xe71(%rip),%rdx
jmp    11e1 <pdf_save+0x91>
mov    0x2e3d(%rip),%rax
mov    (%rax),%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <pdf_save_file@plt>
mov    %eax,%ebp
mov    0x2e26(%rip),%rax
cmp    (%rax),%rbx
je     11fb <pdf_save+0xab>
mov    %rbx,%rdi
call   1030 <fclose@plt>
test   %rax,%rax
je     11fb <pdf_save+0xab>
test   %ebp,%ebp
js     11fb <pdf_save+0xab>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
mov    %edi,%ebx
neg    %ebx
call   1070 <strerror@plt>
lea    0xe3a(%rip),%rdx
mov    %r14,%rdi
mov    %ebx,%esi
mov    %r15,%rcx
mov    %eax,%r8d
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <pdf_set_err@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.pdf_doc = type { i32 }
@stdout = global i32* null
@.str = [3 x i8] c"wb\00"
@errno = global i32 0
@.str.1 = [24 x i8] c"Unable to open '%s': %s\00"
@.str.2 = [25 x i8] c"Unable to close '%s': %s\00"
define i32 @pdf_save(%struct.pdf_doc* %0, i8* %1) {
%3 = alloca i32
%4 = alloca %struct.pdf_doc*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
store %struct.pdf_doc* %0, %struct.pdf_doc** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %5
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = load i32*, i32** @stdout
store i32* %11, i32** %6
br label %25
12: ; preds = %2
%13 = load i8*, i8** %5
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %6
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %24
16: ; preds = %12
%17 = load %struct.pdf_doc*, %struct.pdf_doc** %4
%18 = load i32, i32* @errno
%19 = sub nsw i32 0, %18
%20 = load i8*, i8** %5
%21 = load i32, i32* @errno
%22 = call i32 @strerror(i32 %21)
%23 = call i32 @pdf_set_err(%struct.pdf_doc* %17, i32 %19, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %20, i32 %22)
store i32 %23, i32* %3
br label %50
24: ; preds = %12
br label %25
25: ; preds = %24, %10
%26 = load %struct.pdf_doc*, %struct.pdf_doc** %4
%27 = load i32*, i32** %6
%28 = call i32 @pdf_save_file(%struct.pdf_doc* %26, i32* %27)
store i32 %28, i32* %7
%29 = load i32*, i32** %6
%30 = load i32*, i32** @stdout
%31 = icmp ne i32* %29, %30
br i1 %31, label %32, label %48
32: ; preds = %25
%33 = load i32*, i32** %6
%34 = call i64 @fclose(i32* %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %47
36: ; preds = %32
%37 = load i32, i32* %7
%38 = icmp sge i32 %37, 0
br i1 %38, label %39, label %47
39: ; preds = %36
%40 = load %struct.pdf_doc*, %struct.pdf_doc** %4
%41 = load i32, i32* @errno
%42 = sub nsw i32 0, %41
%43 = load i8*, i8** %5
%44 = load i32, i32* @errno
%45 = call i32 @strerror(i32 %44)
%46 = call i32 @pdf_set_err(%struct.pdf_doc* %40, i32 %42, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %43, i32 %45)
store i32 %46, i32* %3
br label %50
47: ; preds = %36, %32
br label %48
48: ; preds = %47, %25
%49 = load i32, i32* %7
store i32 %49, i32* %3
br label %50
50: ; preds = %48, %39, %16
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
%struct.pdf_doc = type { i32 }
@stdout = global i32* null
@.str = [3 x i8] c"wb\00"
@errno = global i32 0
@.str.1 = [24 x i8] c"Unable to open '%s': %s\00"
@.str.2 = [25 x i8] c"Unable to close '%s': %s\00"
define i32 @pdf_save(%struct.pdf_doc* %0, i8* %1) {
%3 = icmp eq i8* %1, null
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = load i32*, i32** @stdout
br label %14
6: ; preds = %2
%7 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %14
9: ; preds = %6
%10 = load i32, i32* @errno
%11 = sub nsw i32 0, %10
%12 = tail call i32 @strerror(i32 %10)
%13 = tail call i32 @pdf_set_err(%struct.pdf_doc* %0, i32 %11, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %1, i32 %12)
br label %29
14: ; preds = %6, %4
%15 = phi i32* [ %5, %4 ], [ %7, %6 ]
%16 = tail call i32 @pdf_save_file(%struct.pdf_doc* %0, i32* %15)
%17 = load i32*, i32** @stdout
%18 = icmp eq i32* %15, %17
br i1 %18, label %29, label %19
19: ; preds = %14
%20 = tail call i64 @fclose(i32* %15)
%21 = icmp ne i64 %20, 0
%22 = icmp sgt i32 %16, -1
%23 = select i1 %21, i1 %22, i1 false
br i1 %23, label %24, label %29
24: ; preds = %19
%25 = load i32, i32* @errno
%26 = sub nsw i32 0, %25
%27 = tail call i32 @strerror(i32 %25)
%28 = tail call i32 @pdf_set_err(%struct.pdf_doc* %0, i32 %26, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %1, i32 %27)
br label %29
29: ; preds = %14, %19, %24, %9
%30 = phi i32 [ %28, %24 ], [ %13, %9 ], [ %16, %19 ], [ %16, %14 ]
ret i32 %30
}