#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*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int errno;
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char const *, int, int);
int /*<<< orphan*/ printf(char *);
int 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 = lseek(fd, 0, SEEK_SET);
  if (rtn < 0) {
    return errno;
  }
  rtn = write(fd, bytes, (size_t)numBytes);
  if (rtn != (int)numBytes) {
    if (rtn >= 0) {
      printf("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    0x2e6a(%rip),%rax
mov    (%rax),%esi
mov    0x2e81(%rip),%rax
or     (%rax),%esi
mov    0x2e70(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jg     11aa <writeFile+0x5a>
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1237 <writeFile+0xe7>
mov    -0x24(%rbp),%edi
mov    0x2e04(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11db <writeFile+0x8b>
mov    0x2def(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1237 <writeFile+0xe7>
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     1222 <writeFile+0xd2>
cmpl   $0x0,-0x20(%rbp)
jl     1211 <writeFile+0xc1>
lea    0xdf4(%rip),%rdi
call   1040 <printf@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
jmp    1229 <writeFile+0xd9>
movl   $0x0,-0x20(%rbp)
mov    -0x24(%rbp),%edi
call   1060 <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    0x2e7a(%rip),%rcx
mov    0x2e93(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    %edx,%r14d
mov    %rsi,%r15
mov    0x2e7a(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%esi
mov    $0x180,%edx
call   1070 <open@plt>
test   %eax,%eax
jle    11d6 <writeFile+0x86>
mov    %eax,%ebx
mov    0x2e27(%rip),%rax
mov    (%rax),%edx
xor    %ebp,%ebp
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
test   %eax,%eax
js     11d6 <writeFile+0x86>
mov    %r14d,%edx
mov    %ebx,%edi
mov    %r15,%rsi
call   1040 <write@plt>
cmp    %r14d,%eax
je     11cd <writeFile+0x7d>
test   %eax,%eax
js     11c4 <writeFile+0x74>
lea    0xe41(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dfd(%rip),%rax
mov    (%rax),%ebp
mov    %ebx,%edi
call   1060 <close@plt>
jmp    11df <writeFile+0x8f>
mov    0x2de3(%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
@SEEK_SET = 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 %50
21: ; preds = %3
%22 = load i32, i32* %9
%23 = load i32, i32* @SEEK_SET
%24 = call i32 @lseek(i32 %22, i32 0, i32 %23)
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = load i32, i32* @errno
store i32 %28, i32* %4
br label %50
29: ; preds = %21
%30 = load i32, i32* %9
%31 = load i8*, i8** %6
%32 = load i32, i32* %7
%33 = zext i32 %32 to i64
%34 = call i32 @write(i32 %30, i8* %31, i64 %33)
store i32 %34, i32* %8
%35 = load i32, i32* %8
%36 = load i32, i32* %7
%37 = icmp ne i32 %35, %36
br i1 %37, label %38, label %45
38: ; preds = %29
%39 = load i32, i32* %8
%40 = icmp sge i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %43
43: ; preds = %41, %38
%44 = load i32, i32* @EIO
store i32 %44, i32* %8
br label %46
45: ; preds = %29
store i32 0, i32* %8
br label %46
46: ; preds = %45, %43
%47 = load i32, i32* %9
%48 = call i32 @close(i32 %47)
%49 = load i32, i32* %8
store i32 %49, i32* %4
br label %50
50: ; preds = %46, %27, %19
%51 = load i32, i32* %4
ret i32 %51
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@errno = global i32 0
@SEEK_SET = global i32 0
@EIO = global i32 0
@str = [23 x i8] c"writeFile: short write\00"
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 %32
13: ; preds = %3
%14 = load i32, i32* @SEEK_SET
%15 = tail call i32 @lseek(i32 %9, i32 0, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = load i32, i32* @errno
br label %32
19: ; preds = %13
%20 = zext i32 %2 to i64
%21 = tail call i32 @write(i32 %9, i8* %1, i64 %20)
%22 = icmp eq i32 %21, %2
br i1 %22, label %29, label %23
23: ; preds = %19
%24 = icmp sgt i32 %21, -1
br i1 %24, label %25, label %27
25: ; preds = %23
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
br label %27
27: ; preds = %25, %23
%28 = load i32, i32* @EIO
br label %29
29: ; preds = %19, %27
%30 = phi i32 [ %28, %27 ], [ 0, %19 ]
%31 = tail call i32 @close(i32 %9)
br label %32
32: ; preds = %29, %17, %11
%33 = phi i32 [ %12, %11 ], [ %18, %17 ], [ %30, %29 ]
ret i32 %33
}