#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;
scalar_t__ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char const *, int, int);
int /*<<< orphan*/ printf(char *);
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)lseek(fd, 0, SEEK_SET);
  if (rtn < 0) {
    return errno;
  }
  rtn = (int)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)
jge    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
js     11d9 <writeFile+0x89>
mov    %eax,%ebx
mov    0x2e27(%rip),%rax
mov    (%rax),%edx
xor    %ebp,%ebp
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
test   $0x80000000,%eax
jne    11d9 <writeFile+0x89>
mov    %r14d,%edx
mov    %ebx,%edi
mov    %r15,%rsi
call   1040 <write@plt>
cmp    %r14d,%eax
je     11d0 <writeFile+0x80>
test   %eax,%eax
js     11c7 <writeFile+0x77>
lea    0xe3e(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%ebp
mov    %ebx,%edi
call   1060 <close@plt>
jmp    11e2 <writeFile+0x92>
mov    0x2de0(%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 slt 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 %52
21: ; preds = %3
%22 = load i32, i32* %9
%23 = load i32, i32* @SEEK_SET
%24 = call i64 @lseek(i32 %22, i32 0, i32 %23)
%25 = trunc i64 %24 to i32
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %21
%29 = load i32, i32* @errno
store i32 %29, i32* %4
br label %52
30: ; preds = %21
%31 = load i32, i32* %9
%32 = load i8*, i8** %6
%33 = load i32, i32* %7
%34 = zext i32 %33 to i64
%35 = call i64 @write(i32 %31, i8* %32, i64 %34)
%36 = trunc i64 %35 to i32
store i32 %36, i32* %8
%37 = load i32, i32* %8
%38 = load i32, i32* %7
%39 = icmp ne i32 %37, %38
br i1 %39, label %40, label %47
40: ; preds = %30
%41 = load i32, i32* %8
%42 = icmp sge i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %40
%44 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %45
45: ; preds = %43, %40
%46 = load i32, i32* @EIO
store i32 %46, i32* %8
br label %48
47: ; preds = %30
store i32 0, i32* %8
br label %48
48: ; preds = %47, %45
%49 = load i32, i32* %9
%50 = call i32 @close(i32 %49)
%51 = load i32, i32* %8
store i32 %51, i32* %4
br label %52
52: ; preds = %48, %28, %19
%53 = load i32, i32* %4
ret i32 %53
}

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, 0
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = load i32, i32* @errno
br label %34
13: ; preds = %3
%14 = load i32, i32* @SEEK_SET
%15 = tail call i64 @lseek(i32 %9, i32 0, i32 %14)
%16 = and i64 %15, 2147483648
%17 = icmp eq i64 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %13
%19 = load i32, i32* @errno
br label %34
20: ; preds = %13
%21 = zext i32 %2 to i64
%22 = tail call i64 @write(i32 %9, i8* %1, i64 %21)
%23 = trunc i64 %22 to i32
%24 = icmp eq i32 %23, %2
br i1 %24, label %31, label %25
25: ; preds = %20
%26 = icmp sgt i32 %23, -1
br i1 %26, label %27, label %29
27: ; preds = %25
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
br label %29
29: ; preds = %27, %25
%30 = load i32, i32* @EIO
br label %31
31: ; preds = %20, %29
%32 = phi i32 [ %30, %29 ], [ 0, %20 ]
%33 = tail call i32 @close(i32 %9)
br label %34
34: ; preds = %31, %18, %11
%35 = phi i32 [ %12, %11 ], [ %19, %18 ], [ %32, %31 ]
ret i32 %35
}