#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 /*<<< orphan*/ close(int);
int open(char const *, int, int const);
size_t write(int, char const *, size_t const);
bool fs_writecontents(const char *fn, const char *cnt, const size_t len,
                      const int mode) {
  int fh;
  size_t bw;
  fh = open(fn, O_CREAT | O_WRONLY | O_TRUNC, mode);
  if (fh == -1) {
    return false;
  }
  bw = write(fh, cnt, len);
  (void)close(fh);
  if (len != bw)
    return false;
  return true;
}


x86 assembly (clang, -O0):
<fs_writecontents>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2ea6(%rip),%rax
mov    (%rax),%esi
mov    0x2e6d(%rip),%rax
or     (%rax),%esi
mov    0x2e8c(%rip),%rax
or     (%rax),%esi
mov    -0x24(%rbp),%edx
call   1050 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    1187 <fs_writecontents+0x57>
movl   $0x0,-0x4(%rbp)
jmp    11c4 <fs_writecontents+0x94>
mov    -0x28(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%rax
cmp    -0x30(%rbp),%rax
je     11bd <fs_writecontents+0x8d>
movl   $0x0,-0x4(%rbp)
jmp    11c4 <fs_writecontents+0x94>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fs_writecontents>:
push   %rbp
push   %r14
push   %rbx
mov    0x2ebd(%rip),%rbp
mov    0x2e86(%rip),%rax
mov    (%rax),%eax
or     0x0(%rbp),%eax
mov    %rdx,%rbx
mov    %rsi,%r14
mov    0x2e9c(%rip),%rdx
or     (%rdx),%eax
mov    %eax,%esi
mov    %ecx,%edx
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     1187 <fs_writecontents+0x57>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
mov    %rax,%r14
mov    %ebp,%edi
call   1040 <close@plt>
xor    %eax,%eax
cmp    %rbx,%r14
sete   %al
jmp    1189 <fs_writecontents+0x59>
xor    %eax,%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
define i32 @fs_writecontents(i8* %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
%11 = alloca i64
store i8* %0, i8** %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i32 %3, i32* %9
%12 = load i8*, i8** %6
%13 = load i32, i32* @O_CREAT
%14 = load i32, i32* @O_WRONLY
%15 = or i32 %13, %14
%16 = load i32, i32* @O_TRUNC
%17 = or i32 %15, %16
%18 = load i32, i32* %9
%19 = call i32 @open(i8* %12, i32 %17, i32 %18)
store i32 %19, i32* %10
%20 = load i32, i32* %10
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %23
22: ; preds = %4
store i32 0, i32* %5
br label %35
23: ; preds = %4
%24 = load i32, i32* %10
%25 = load i8*, i8** %7
%26 = load i64, i64* %8
%27 = call i64 @write(i32 %24, i8* %25, i64 %26)
store i64 %27, i64* %11
%28 = load i32, i32* %10
%29 = call i32 @close(i32 %28)
%30 = load i64, i64* %8
%31 = load i64, i64* %11
%32 = icmp ne i64 %30, %31
br i1 %32, label %33, label %34
33: ; preds = %23
store i32 0, i32* %5
br label %35
34: ; preds = %23
store i32 1, i32* %5
br label %35
35: ; preds = %34, %33, %22
%36 = load i32, i32* %5
ret i32 %36
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
define i32 @fs_writecontents(i8* nocapture %0, i8* nocapture %1, i64 %2, i32 %3) {
%5 = load i32, i32* @O_CREAT
%6 = load i32, i32* @O_WRONLY
%7 = or i32 %6, %5
%8 = load i32, i32* @O_TRUNC
%9 = or i32 %7, %8
%10 = tail call i32 @open(i8* %0, i32 %9, i32 %3)
%11 = icmp eq i32 %10, -1
br i1 %11, label %17, label %12
12: ; preds = %4
%13 = tail call i64 @write(i32 %10, i8* %1, i64 %2)
%14 = tail call i32 @close(i32 %10)
%15 = icmp eq i64 %13, %2
%16 = zext i1 %15 to i32
br label %17
17: ; preds = %12, %4
%18 = phi i32 [ 0, %4 ], [ %16, %12 ]
ret i32 %18
}