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

/* Variables and functions */
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, int, size_t);
int write(int, char *, size_t);
int savefile(char *name, char *buf, size_t size) {
  int retval = 0;
  int fd;
  fd = open(name, O_WRONLY | O_CREAT | O_TRUNC,
            S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  if (fd < 0) {
    perror("Can not open file for writing");
    return (0x20 + 0x00);
  }
  retval = write(fd, buf, size);
  if (retval < 0) {
    perror("Can not write file");
    retval = (0x20 + 0x00);
  }
  if (retval != size) {
    char s[256];
    sprintf(s, "Writen %d bytes while %zu requested", retval, size);
    perror(s);
    retval = (0x20 + 0x00);
  } else {
    retval = 0;
  }
  close(fd);
  return retval;
}


x86 assembly (clang, -O0):
<savefile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
mov    0x2e76(%rip),%rax
or     (%rax),%esi
mov    0x2e65(%rip),%rax
or     (%rax),%esi
mov    0x2e24(%rip),%rax
mov    (%rax),%edx
mov    0x2e3b(%rip),%rax
or     (%rax),%edx
mov    0x2e22(%rip),%rax
or     (%rax),%edx
mov    0x2e11(%rip),%rax
or     (%rax),%edx
call   1050 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11db <savefile+0x8b>
lea    0xe36(%rip),%rdi
call   1060 <perror@plt>
movl   $0x20,-0x4(%rbp)
jmp    1260 <savefile+0x110>
mov    -0x28(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    120b <savefile+0xbb>
lea    0xe1f(%rip),%rdi
call   1060 <perror@plt>
movl   $0x20,-0x24(%rbp)
movslq -0x24(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     124b <savefile+0xfb>
lea    -0x130(%rbp),%rdi
mov    -0x24(%rbp),%edx
mov    -0x20(%rbp),%rcx
lea    0xe03(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0x130(%rbp),%rdi
call   1060 <perror@plt>
movl   $0x20,-0x24(%rbp)
jmp    1252 <savefile+0x102>
movl   $0x0,-0x24(%rbp)
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<savefile>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    0x2e46(%rip),%rcx
mov    0x2e8f(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    0x2e7c(%rip),%rcx
or     (%rcx),%eax
mov    0x2e3b(%rip),%rbp
mov    0x2e54(%rip),%rcx
mov    (%rcx),%ecx
or     0x0(%rbp),%ecx
mov    0x2e38(%rip),%rbp
or     0x0(%rbp),%ecx
mov    %rdx,%r14
mov    %rsi,%rbp
mov    0x2e20(%rip),%rdx
or     (%rdx),%ecx
mov    %eax,%esi
mov    %ecx,%edx
call   1050 <open@plt>
test   %eax,%eax
js     11e0 <savefile+0x90>
mov    %eax,%ebx
mov    %eax,%edi
mov    %rbp,%rsi
mov    %r14,%rdx
call   1030 <write@plt>
test   %eax,%eax
js     11f3 <savefile+0xa3>
mov    %eax,%ecx
xor    %ebp,%ebp
cmp    %r14,%rcx
jne    1206 <savefile+0xb6>
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe19(%rip),%rdi
call   1060 <perror@plt>
mov    $0x20,%ebp
jmp    11d2 <savefile+0x82>
lea    0xe24(%rip),%rdi
call   1060 <perror@plt>
mov    $0x20,%eax
jmp    11c2 <savefile+0x72>
lea    0xe24(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %eax,%edx
mov    %r14,%rcx
call   1070 <sprintf@plt>
mov    %rbp,%rdi
call   1060 <perror@plt>
mov    $0x20,%ebp
jmp    11cb <savefile+0x7b>

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str = [30 x i8] c"Can not open file for writing\00"
@.str.1 = [19 x i8] c"Can not write file\00"
@.str.2 = [36 x i8] c"Writen %d bytes while %zu requested\00"
define i32 @savefile(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca [256 x i8]
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i32 0, i32* %8
%11 = load i8*, i8** %5
%12 = load i32, i32* @O_WRONLY
%13 = load i32, i32* @O_CREAT
%14 = or i32 %12, %13
%15 = load i32, i32* @O_TRUNC
%16 = or i32 %14, %15
%17 = load i32, i32* @S_IRUSR
%18 = load i32, i32* @S_IWUSR
%19 = or i32 %17, %18
%20 = load i32, i32* @S_IRGRP
%21 = or i32 %19, %20
%22 = load i32, i32* @S_IROTH
%23 = or i32 %21, %22
%24 = call i32 @open(i8* %11, i32 %16, i32 %23)
store i32 %24, i32* %9
%25 = load i32, i32* %9
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %3
%28 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
store i32 32, i32* %4
br label %55
29: ; preds = %3
%30 = load i32, i32* %9
%31 = load i8*, i8** %6
%32 = load i64, i64* %7
%33 = call i32 @write(i32 %30, i8* %31, i64 %32)
store i32 %33, i32* %8
%34 = load i32, i32* %8
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %29
%37 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
store i32 32, i32* %8
br label %38
38: ; preds = %36, %29
%39 = load i32, i32* %8
%40 = sext i32 %39 to i64
%41 = load i64, i64* %7
%42 = icmp ne i64 %40, %41
br i1 %42, label %43, label %50
43: ; preds = %38
%44 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%45 = load i32, i32* %8
%46 = load i64, i64* %7
%47 = call i32 @sprintf(i8* %44, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %45, i64 %46)
%48 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%49 = call i32 @perror(i8* %48)
store i32 32, i32* %8
br label %51
50: ; preds = %38
store i32 0, i32* %8
br label %51
51: ; preds = %50, %43
%52 = load i32, i32* %9
%53 = call i32 @close(i32 %52)
%54 = load i32, i32* %8
store i32 %54, i32* %4
br label %55
55: ; preds = %51, %27
%56 = load i32, i32* %4
ret i32 %56
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str = [30 x i8] c"Can not open file for writing\00"
@.str.1 = [19 x i8] c"Can not write file\00"
@.str.2 = [36 x i8] c"Writen %d bytes while %zu requested\00"
define i32 @savefile(i8* nocapture %0, i8* nocapture %1, i64 %2) {
%4 = alloca [256 x i8]
%5 = load i32, i32* @O_WRONLY
%6 = load i32, i32* @O_CREAT
%7 = or i32 %6, %5
%8 = load i32, i32* @O_TRUNC
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IRUSR
%11 = load i32, i32* @S_IWUSR
%12 = or i32 %11, %10
%13 = load i32, i32* @S_IRGRP
%14 = or i32 %12, %13
%15 = load i32, i32* @S_IROTH
%16 = or i32 %14, %15
%17 = tail call i32 @open(i8* %0, i32 %9, i32 %16)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %3
%20 = tail call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %37
21: ; preds = %3
%22 = tail call i32 @write(i32 %17, i8* %1, i64 %2)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %21
%27 = phi i32 [ 32, %24 ], [ %22, %21 ]
%28 = zext i32 %27 to i64
%29 = icmp eq i64 %28, %2
br i1 %29, label %34, label %30
30: ; preds = %26
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%32 = call i32 @sprintf(i8* %31, i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i32 %27, i64 %2)
%33 = call i32 @perror(i8* %31)
br label %34
34: ; preds = %26, %30
%35 = phi i32 [ 32, %30 ], [ 0, %26 ]
%36 = tail call i32 @close(i32 %17)
br label %37
37: ; preds = %34, %19
%38 = phi i32 [ 32, %19 ], [ %35, %34 ]
ret i32 %38
}