#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_IRUSR;
int S_IWUSR;
int close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ io_error(int /*<<< orphan*/, char *);
int open(char const *, int, int);
int write(int, char *, size_t);
int bsave(const char *name, char *adr, size_t len) {
  int fdis =
      open(name, O_CREAT | 0 | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR | 0);
  if (fdis == -1)
    return (-1);
  if (write(fdis, adr, len) == -1)
    io_error(errno, "write");
  return (close(fdis));
}


x86 assembly (clang, -O0):
<bsave>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e99(%rip),%rax
mov    (%rax),%esi
or     $0x0,%esi
mov    0x2e45(%rip),%rax
or     (%rax),%esi
mov    0x2e7c(%rip),%rax
or     (%rax),%esi
mov    0x2e43(%rip),%rax
mov    (%rax),%edx
mov    0x2e52(%rip),%rax
or     (%rax),%edx
or     $0x0,%edx
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11a9 <bsave+0x69>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e2 <bsave+0xa2>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    11d7 <bsave+0x97>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
lea    0xe2e(%rip),%rsi
call   1060 <io_error@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bsave>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%rbx
mov    0x2ea7(%rip),%rax
mov    0x2e58(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e8d(%rip),%rax
or     (%rax),%esi
mov    0x2e54(%rip),%rax
mov    0x2e65(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11b5 <bsave+0x75>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    11aa <bsave+0x6a>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
lea    0xe5b(%rip),%rsi
call   1060 <io_error@plt>
mov    %ebp,%edi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <close@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@errno = global i32 0
@.str = [6 x i8] c"write\00"
define i32 @bsave(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%9 = load i8*, i8** %5
%10 = load i32, i32* @O_CREAT
%11 = or i32 %10, 0
%12 = load i32, i32* @O_WRONLY
%13 = or i32 %11, %12
%14 = load i32, i32* @O_TRUNC
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IRUSR
%17 = load i32, i32* @S_IWUSR
%18 = or i32 %16, %17
%19 = or i32 %18, 0
%20 = call i32 @open(i8* %9, i32 %15, i32 %19)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %24
23: ; preds = %3
store i32 -1, i32* %4
br label %36
24: ; preds = %3
%25 = load i32, i32* %8
%26 = load i8*, i8** %6
%27 = load i64, i64* %7
%28 = call i32 @write(i32 %25, i8* %26, i64 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %33
30: ; preds = %24
%31 = load i32, i32* @errno
%32 = call i32 @io_error(i32 %31, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %33
33: ; preds = %30, %24
%34 = load i32, i32* %8
%35 = call i32 @close(i32 %34)
store i32 %35, i32* %4
br label %36
36: ; preds = %33, %23
%37 = load i32, i32* %4
ret i32 %37
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@errno = global i32 0
@.str = [6 x i8] c"write\00"
define i32 @bsave(i8* nocapture %0, i8* nocapture %1, i64 %2) {
%4 = load i32, i32* @O_CREAT
%5 = load i32, i32* @O_WRONLY
%6 = or i32 %5, %4
%7 = load i32, i32* @O_TRUNC
%8 = or i32 %6, %7
%9 = load i32, i32* @S_IRUSR
%10 = load i32, i32* @S_IWUSR
%11 = or i32 %10, %9
%12 = tail call i32 @open(i8* %0, i32 %8, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %22, label %14
14: ; preds = %3
%15 = tail call i32 @write(i32 %12, i8* %1, i64 %2)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i32, i32* @errno
%19 = tail call i32 @io_error(i32 %18, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %17, %14
%21 = tail call i32 @close(i32 %12)
br label %22
22: ; preds = %3, %20
%23 = phi i32 [ %21, %20 ], [ -1, %3 ]
ret i32 %23
}