#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 size_t ssize_t;

/* Variables and functions */
int EIO;
int O_CREAT;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ close(int);
int errno;
int ferror(int /*<<< orphan*/);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fwrite(unsigned char const *, int, size_t,
                          int /*<<< orphan*/);
int open(char const *, int, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
size_t write(int, unsigned char const *, size_t);
int writeFile(const char *fileName, const unsigned char *bytes,
              size_t numBytes) {
  int rtn;
  int fd;
  ssize_t wrc;
  if (!fileName) {
    fwrite(bytes, 1, numBytes, stdout);
    fflush(stdout);
    return ferror(stdout);
  }
  fd = open(fileName, O_RDWR | O_CREAT | O_TRUNC, 0600);
  if (fd <= 0) {
    return errno;
  }
  wrc = write(fd, bytes, (size_t)numBytes);
  if (wrc != numBytes) {
    if (wrc >= 0) {
      fprintf(stderr, "writeFile: short write\n");
    }
    rtn = EIO;
  } else {
    rtn = 0;
  }
  close(fd);
  return rtn;
}


x86 assembly (clang, -O0):
<writeFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ce <writeFile+0x5e>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    0x2e12(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   1090 <fwrite@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    0x2df1(%rip),%rax
mov    (%rax),%edi
call   1030 <ferror@plt>
mov    %eax,-0x4(%rbp)
jmp    127d <writeFile+0x10d>
mov    -0x10(%rbp),%rdi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
mov    0x2e16(%rip),%rax
or     (%rax),%esi
mov    0x2e05(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   1080 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jg     1215 <writeFile+0xa5>
mov    0x2dad(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    127d <writeFile+0x10d>
mov    -0x28(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <write@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     1268 <writeFile+0xf8>
cmpq   $0x0,-0x30(%rbp)
jb     1257 <writeFile+0xe7>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
lea    0xdae(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2d6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    126f <writeFile+0xff>
movl   $0x0,-0x24(%rbp)
mov    -0x28(%rbp),%edi
call   1050 <close@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeFile>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
test   %rdi,%rdi
je     11e8 <writeFile+0x78>
mov    0x2e4f(%rip),%rax
mov    0x2e68(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e55(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   1080 <open@plt>
test   %eax,%eax
jle    1219 <writeFile+0xa9>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1040 <write@plt>
xor    %ebx,%ebx
cmp    %r15,%rax
je     11df <writeFile+0x6f>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
lea    0xe2f(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2deb(%rip),%rax
mov    (%rax),%ebx
mov    %ebp,%edi
call   1050 <close@plt>
jmp    1222 <writeFile+0xb2>
mov    0x2dc1(%rip),%rbx
mov    (%rbx),%ecx
mov    %r14,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
call   1090 <fwrite@plt>
mov    (%rbx),%edi
call   1070 <fflush@plt>
mov    (%rbx),%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <ferror@plt>
mov    0x2d98(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdout = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@errno = global i32 0
@stderr = global i32 0
@.str = [24 x i8] c"writeFile: short write\0A\00"
@EIO = global i32 0
define i32 @writeFile(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 i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i8*, i8** %5
%12 = icmp ne i8* %11, null
br i1 %12, label %22, label %13
13: ; preds = %3
%14 = load i8*, i8** %6
%15 = load i64, i64* %7
%16 = load i32, i32* @stdout
%17 = call i32 @fwrite(i8* %14, i32 1, i64 %15, i32 %16)
%18 = load i32, i32* @stdout
%19 = call i32 @fflush(i32 %18)
%20 = load i32, i32* @stdout
%21 = call i32 @ferror(i32 %20)
store i32 %21, i32* %4
br label %55
22: ; preds = %3
%23 = load i8*, i8** %5
%24 = load i32, i32* @O_RDWR
%25 = load i32, i32* @O_CREAT
%26 = or i32 %24, %25
%27 = load i32, i32* @O_TRUNC
%28 = or i32 %26, %27
%29 = call i32 @open(i8* %23, i32 %28, i32 384)
store i32 %29, i32* %9
%30 = load i32, i32* %9
%31 = icmp sle i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %22
%33 = load i32, i32* @errno
store i32 %33, i32* %4
br label %55
34: ; preds = %22
%35 = load i32, i32* %9
%36 = load i8*, i8** %6
%37 = load i64, i64* %7
%38 = call i64 @write(i32 %35, i8* %36, i64 %37)
store i64 %38, i64* %10
%39 = load i64, i64* %10
%40 = load i64, i64* %7
%41 = icmp ne i64 %39, %40
br i1 %41, label %42, label %50
42: ; preds = %34
%43 = load i64, i64* %10
%44 = icmp uge i64 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = load i32, i32* @stderr
%47 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %48
48: ; preds = %45, %42
%49 = load i32, i32* @EIO
store i32 %49, i32* %8
br label %51
50: ; preds = %34
store i32 0, i32* %8
br label %51
51: ; preds = %50, %48
%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, %32, %13
%56 = load i32, i32* %4
ret i32 %56
}

LLVM IR (-O2):
@stdout = global i32 0
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@errno = global i32 0
@stderr = global i32 0
@.str = [24 x i8] c"writeFile: short write\0A\00"
@EIO = global i32 0
define i32 @writeFile(i8* %0, i8* %1, i64 %2) {
%4 = icmp eq i8* %0, null
br i1 %4, label %5, label %12
5: ; preds = %3
%6 = load i32, i32* @stdout
%7 = tail call i32 @fwrite(i8* %1, i32 1, i64 %2, i32 %6)
%8 = load i32, i32* @stdout
%9 = tail call i32 @fflush(i32 %8)
%10 = load i32, i32* @stdout
%11 = tail call i32 @ferror(i32 %10)
br label %32
12: ; preds = %3
%13 = load i32, i32* @O_RDWR
%14 = load i32, i32* @O_CREAT
%15 = or i32 %14, %13
%16 = load i32, i32* @O_TRUNC
%17 = or i32 %15, %16
%18 = tail call i32 @open(i8* %0, i32 %17, i32 384)
%19 = icmp slt i32 %18, 1
br i1 %19, label %20, label %22
20: ; preds = %12
%21 = load i32, i32* @errno
br label %32
22: ; preds = %12
%23 = tail call i64 @write(i32 %18, i8* %1, i64 %2)
%24 = icmp eq i64 %23, %2
br i1 %24, label %29, label %25
25: ; preds = %22
%26 = load i32, i32* @stderr
%27 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%28 = load i32, i32* @EIO
br label %29
29: ; preds = %22, %25
%30 = phi i32 [ %28, %25 ], [ 0, %22 ]
%31 = tail call i32 @close(i32 %18)
br label %32
32: ; preds = %29, %20, %5
%33 = phi i32 [ %21, %20 ], [ %30, %29 ], [ %11, %5 ]
ret i32 %33
}