#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 EIO;
int O_CREAT;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char const *, int, int);
int /*<<< orphan*/ stderr;
scalar_t__ write(int, unsigned char const *, size_t);
int writeFile(const char *fileName, const unsigned char *bytes,
              unsigned numBytes) {
  int rtn;
  int fd;
  fd = open(fileName, O_RDWR | O_CREAT | O_TRUNC, 0600);
  if (fd <= 0) {
    return errno;
  }
  rtn = (int)write(fd, bytes, (size_t)numBytes);
  if (rtn != (int)numBytes) {
    if (rtn >= 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    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
mov    0x2e91(%rip),%rax
or     (%rax),%esi
mov    0x2e80(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jg     119a <writeFile+0x5a>
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11ff <writeFile+0xbf>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%eax
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     11ea <writeFile+0xaa>
cmpl   $0x0,-0x20(%rbp)
jl     11d9 <writeFile+0x99>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
lea    0xe2c(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
jmp    11f1 <writeFile+0xb1>
movl   $0x0,-0x20(%rbp)
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%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    0x2e8a(%rip),%rcx
mov    0x2ea3(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    %edx,%r15d
mov    %rsi,%r14
mov    0x2e8a(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%esi
mov    $0x180,%edx
call   1060 <open@plt>
test   %eax,%eax
jle    11b9 <writeFile+0x79>
mov    %eax,%ebx
mov    %r15d,%edx
mov    %eax,%edi
mov    %r14,%rsi
call   1030 <write@plt>
xor    %ebp,%ebp
cmp    %r15d,%eax
je     11b0 <writeFile+0x70>
test   %eax,%eax
js     11a7 <writeFile+0x67>
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
lea    0xe5e(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e1a(%rip),%rax
mov    (%rax),%ebp
mov    %ebx,%edi
call   1040 <close@plt>
jmp    11c2 <writeFile+0x82>
mov    0x2df8(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@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, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_CREAT
%13 = or i32 %11, %12
%14 = load i32, i32* @O_TRUNC
%15 = or i32 %13, %14
%16 = call i32 @open(i8* %10, i32 %15, i32 384)
store i32 %16, i32* %9
%17 = load i32, i32* %9
%18 = icmp sle i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %3
%20 = load i32, i32* @errno
store i32 %20, i32* %4
br label %44
21: ; preds = %3
%22 = load i32, i32* %9
%23 = load i8*, i8** %6
%24 = load i32, i32* %7
%25 = zext i32 %24 to i64
%26 = call i64 @write(i32 %22, i8* %23, i64 %25)
%27 = trunc i64 %26 to i32
store i32 %27, i32* %8
%28 = load i32, i32* %8
%29 = load i32, i32* %7
%30 = icmp ne i32 %28, %29
br i1 %30, label %31, label %39
31: ; preds = %21
%32 = load i32, i32* %8
%33 = icmp sge i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = load i32, i32* @stderr
%36 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %37
37: ; preds = %34, %31
%38 = load i32, i32* @EIO
store i32 %38, i32* %8
br label %40
39: ; preds = %21
store i32 0, i32* %8
br label %40
40: ; preds = %39, %37
%41 = load i32, i32* %9
%42 = call i32 @close(i32 %41)
%43 = load i32, i32* %8
store i32 %43, i32* %4
br label %44
44: ; preds = %40, %19
%45 = load i32, i32* %4
ret i32 %45
}

LLVM IR (-O2):
@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* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_CREAT
%6 = or i32 %5, %4
%7 = load i32, i32* @O_TRUNC
%8 = or i32 %6, %7
%9 = tail call i32 @open(i8* %0, i32 %8, i32 384)
%10 = icmp slt i32 %9, 1
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = load i32, i32* @errno
br label %28
13: ; preds = %3
%14 = zext i32 %2 to i64
%15 = tail call i64 @write(i32 %9, i8* %1, i64 %14)
%16 = trunc i64 %15 to i32
%17 = icmp eq i32 %16, %2
br i1 %17, label %25, label %18
18: ; preds = %13
%19 = icmp sgt i32 %16, -1
br i1 %19, label %20, label %23
20: ; preds = %18
%21 = load i32, i32* @stderr
%22 = tail call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %23
23: ; preds = %20, %18
%24 = load i32, i32* @EIO
br label %25
25: ; preds = %13, %23
%26 = phi i32 [ %24, %23 ], [ 0, %13 ]
%27 = tail call i32 @close(i32 %9)
br label %28
28: ; preds = %25, %11
%29 = phi i32 [ %12, %11 ], [ %26, %25 ]
ret i32 %29
}