#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*/ uint8_t;

/* Variables and functions */
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ check(int, char *);
int /*<<< orphan*/ close(int);
int open(char *, int, int);
int /*<<< orphan*/ perror_exit(char *);
int write(int, int /*<<< orphan*/ *, int);
void save_file(char *name, uint8_t *data, int size) {
  int fd, rsize;
  check(NULL != name, "output file name is not specified");
  fd = open(name, O_WRONLY | O_TRUNC | O_CREAT | 0, 0644);
  if (fd < 0)
    perror_exit("open()");
  rsize = write(fd, data, size);
  if (rsize < 0)
    perror_exit("write()");
  check(rsize == size, "error writing the file");
  close(fd);
}


x86 assembly (clang, -O0):
<save_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
xor    %eax,%eax
cmp    -0x8(%rbp),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
lea    0xe88(%rip),%rsi
call   1070 <check@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
mov    0x2e5f(%rip),%rax
or     (%rax),%esi
mov    0x2e5e(%rip),%rax
or     (%rax),%esi
or     $0x0,%esi
mov    $0x1a4,%edx
call   1050 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11c2 <save_file+0x72>
lea    0xe65(%rip),%rdi
call   1060 <perror_exit@plt>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11ea <save_file+0x9a>
lea    0xe44(%rip),%rdi
call   1060 <perror_exit@plt>
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
lea    0xe32(%rip),%rsi
call   1070 <check@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
lea    0xe90(%rip),%rsi
call   1070 <check@plt>
mov    0x2e4c(%rip),%rax
mov    0x2e6d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e6a(%rip),%rax
or     (%rax),%esi
mov    %rbx,%rdi
mov    $0x1a4,%edx
call   1050 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11af <save_file+0x5f>
lea    0xe78(%rip),%rdi
call   1060 <perror_exit@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %r15d,%edx
call   1030 <write@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11ce <save_file+0x7e>
lea    0xe60(%rip),%rdi
call   1060 <perror_exit@plt>
xor    %edi,%edi
cmp    %r15d,%ebx
sete   %dil
lea    0xe53(%rip),%rsi
call   1070 <check@plt>
mov    %ebp,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <close@plt>

LLVM IR (-O0):
@.str = [34 x i8] c"output file name is not specified\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@.str.1 = [7 x i8] c"open()\00"
@.str.2 = [8 x i8] c"write()\00"
@.str.3 = [23 x i8] c"error writing the file\00"
define void @save_file(i8* %0, i32* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
store i32 %2, i32* %6
%9 = load i8*, i8** %4
%10 = icmp ne i8* null, %9
%11 = zext i1 %10 to i32
%12 = call i32 @check(i32 %11, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%13 = load i8*, i8** %4
%14 = load i32, i32* @O_WRONLY
%15 = load i32, i32* @O_TRUNC
%16 = or i32 %14, %15
%17 = load i32, i32* @O_CREAT
%18 = or i32 %16, %17
%19 = or i32 %18, 0
%20 = call i32 @open(i8* %13, i32 %19, i32 420)
store i32 %20, i32* %7
%21 = load i32, i32* %7
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %3
%24 = call i32 @perror_exit(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %3
%26 = load i32, i32* %7
%27 = load i32*, i32** %5
%28 = load i32, i32* %6
%29 = call i32 @write(i32 %26, i32* %27, i32 %28)
store i32 %29, i32* %8
%30 = load i32, i32* %8
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %25
%33 = call i32 @perror_exit(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %32, %25
%35 = load i32, i32* %8
%36 = load i32, i32* %6
%37 = icmp eq i32 %35, %36
%38 = zext i1 %37 to i32
%39 = call i32 @check(i32 %38, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%40 = load i32, i32* %7
%41 = call i32 @close(i32 %40)
ret void
}

LLVM IR (-O2):
@.str = [34 x i8] c"output file name is not specified\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@.str.1 = [7 x i8] c"open()\00"
@.str.2 = [8 x i8] c"write()\00"
@.str.3 = [23 x i8] c"error writing the file\00"
define void @save_file(i8* %0, i32* nocapture %1, i32 %2) {
%4 = icmp ne i8* %0, null
%5 = zext i1 %4 to i32
%6 = tail call i32 @check(i32 %5, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @O_WRONLY
%8 = load i32, i32* @O_TRUNC
%9 = or i32 %8, %7
%10 = load i32, i32* @O_CREAT
%11 = or i32 %9, %10
%12 = tail call i32 @open(i8* %0, i32 %11, i32 420)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = tail call i32 @perror_exit(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %3
%17 = tail call i32 @write(i32 %12, i32* %1, i32 %2)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = tail call i32 @perror_exit(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %21
21: ; preds = %19, %16
%22 = icmp eq i32 %17, %2
%23 = zext i1 %22 to i32
%24 = tail call i32 @check(i32 %23, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%25 = tail call i32 @close(i32 %12)
ret void
}