#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 int ssize_t;
typedef scalar_t__ off_t;

/* Variables and functions */
int ENOMEM;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int errno;
int /*<<< orphan*/ free(void *);
int lseek(int, scalar_t__, int /*<<< orphan*/);
void *malloc(size_t);
int read(int, void *, size_t);
void *win_read_create(void *addr, int fd, off_t offset, size_t length) {
  ssize_t rc;
  void *buf = addr;
  if (NULL == buf && NULL == (buf = malloc(length))) {
    errno = ENOMEM;
    return ((void *)-1);
  }
  if (-1 == lseek(fd, offset, offset >= 0 ? SEEK_SET : SEEK_END)) {
    if (NULL == addr)
      free(buf);
    return ((void *)-1);
  }
  if ((rc = read(fd, buf, length)) == -1) {
    int saved_errno = errno;
    if (NULL == addr)
      free(buf);
    errno = saved_errno;
    return ((void *)-1);
  }
  return buf;
}


x86 assembly (clang, -O0):
<win_read_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
xor    %eax,%eax
cmp    -0x38(%rbp),%rax
jne    11a8 <win_read_create+0x68>
mov    -0x28(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,%rcx
mov    %rcx,-0x38(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    11a8 <win_read_create+0x68>
mov    0x2e4b(%rip),%rax
mov    (%rax),%ecx
mov    0x2e3a(%rip),%rax
mov    %ecx,(%rax)
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
jmp    1282 <win_read_create+0x142>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x20(%rbp)
jl     11d2 <win_read_create+0x92>
mov    0x2e00(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
jmp    11de <win_read_create+0x9e>
mov    0x2e0f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    -0x48(%rbp),%rsi
mov    -0x4c(%rbp),%edi
mov    -0x50(%rbp),%edx
call   1040 <lseek@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1221 <win_read_create+0xe1>
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jne    1211 <win_read_create+0xd1>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
jmp    1282 <win_read_create+0x142>
mov    -0x14(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1050 <read@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
jne    127a <win_read_create+0x13a>
mov    0x2d8c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jne    125e <win_read_create+0x11e>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x3c(%rbp),%ecx
mov    0x2d68(%rip),%rax
mov    %ecx,(%rax)
mov    $0xffffffffffffffff,%rax
mov    %rax,-0x8(%rbp)
jmp    1282 <win_read_create+0x142>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<win_read_create>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rcx,%r15
mov    %rdx,%rbp
mov    %esi,%r12d
mov    %rdi,%rbx
mov    %rdi,%r14
test   %rdi,%rdi
jne    116c <win_read_create+0x2c>
mov    %r15,%rdi
call   1060 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
je     11dd <win_read_create+0x9d>
test   %rbp,%rbp
js     117a <win_read_create+0x3a>
mov    0x2e50(%rip),%rax
jmp    1181 <win_read_create+0x41>
mov    0x2e67(%rip),%rax
mov    (%rax),%edx
mov    %r12d,%edi
mov    %rbp,%rsi
call   1040 <lseek@plt>
cmp    $0xffffffff,%eax
je     11ab <win_read_create+0x6b>
mov    %r12d,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
je     11c1 <win_read_create+0x81>
mov    %r14,%rbp
jmp    11f6 <win_read_create+0xb6>
mov    $0xffffffffffffffff,%rbp
test   %rbx,%rbx
jne    11f6 <win_read_create+0xb6>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    11f6 <win_read_create+0xb6>
mov    0x2e08(%rip),%r15
mov    (%r15),%ebp
test   %rbx,%rbx
jne    11d8 <win_read_create+0x98>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebp,(%r15)
jmp    11ef <win_read_create+0xaf>
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
mov    0x2de3(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffffffffffff,%rbp
mov    %rbp,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@ENOMEM = global i32 0
@errno = global i32 0
@SEEK_SET = global i32 0
@SEEK_END = global i32 0
define i8* @win_read_create(i8* %0, i32 %1, i64 %2, i64 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i64 %2, i64* %8
store i64 %3, i64* %9
%13 = load i8*, i8** %6
store i8* %13, i8** %11
%14 = load i8*, i8** %11
%15 = icmp eq i8* null, %14
br i1 %15, label %16, label %22
16: ; preds = %4
%17 = load i64, i64* %9
%18 = call i8* @malloc(i64 %17)
store i8* %18, i8** %11
%19 = icmp eq i8* null, %18
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = load i32, i32* @ENOMEM
store i32 %21, i32* @errno
store i8* inttoptr (i64 -1 to i8*), i8** %5
br label %59
22: ; preds = %16, %4
%23 = load i32, i32* %7
%24 = load i64, i64* %8
%25 = load i64, i64* %8
%26 = icmp sge i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = load i32, i32* @SEEK_SET
br label %31
29: ; preds = %22
%30 = load i32, i32* @SEEK_END
br label %31
31: ; preds = %29, %27
%32 = phi i32 [ %28, %27 ], [ %30, %29 ]
%33 = call i32 @lseek(i32 %23, i64 %24, i32 %32)
%34 = icmp eq i32 -1, %33
br i1 %34, label %35, label %42
35: ; preds = %31
%36 = load i8*, i8** %6
%37 = icmp eq i8* null, %36
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = load i8*, i8** %11
%40 = call i32 @free(i8* %39)
br label %41
41: ; preds = %38, %35
store i8* inttoptr (i64 -1 to i8*), i8** %5
br label %59
42: ; preds = %31
%43 = load i32, i32* %7
%44 = load i8*, i8** %11
%45 = load i64, i64* %9
%46 = call i32 @read(i32 %43, i8* %44, i64 %45)
store i32 %46, i32* %10
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %57
48: ; preds = %42
%49 = load i32, i32* @errno
store i32 %49, i32* %12
%50 = load i8*, i8** %6
%51 = icmp eq i8* null, %50
br i1 %51, label %52, label %55
52: ; preds = %48
%53 = load i8*, i8** %11
%54 = call i32 @free(i8* %53)
br label %55
55: ; preds = %52, %48
%56 = load i32, i32* %12
store i32 %56, i32* @errno
store i8* inttoptr (i64 -1 to i8*), i8** %5
br label %59
57: ; preds = %42
%58 = load i8*, i8** %11
store i8* %58, i8** %5
br label %59
59: ; preds = %57, %55, %41, %20
%60 = load i8*, i8** %5
ret i8* %60
}

LLVM IR (-O2):
@ENOMEM = global i32 0
@errno = global i32 0
@SEEK_SET = global i32 0
@SEEK_END = global i32 0
define i8* @win_read_create(i8* %0, i32 %1, i64 %2, i64 %3) {
%5 = icmp eq i8* %0, null
br i1 %5, label %6, label %11
6: ; preds = %4
%7 = tail call i8* @malloc(i64 %3)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = load i32, i32* @ENOMEM
store i32 %10, i32* @errno
br label %30
11: ; preds = %6, %4
%12 = phi i8* [ %7, %6 ], [ %0, %4 ]
%13 = icmp sgt i64 %2, -1
%14 = load i32, i32* @SEEK_SET
%15 = load i32, i32* @SEEK_END
%16 = select i1 %13, i32 %14, i32 %15
%17 = tail call i32 @lseek(i32 %1, i64 %2, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %11
br i1 %5, label %20, label %30
20: ; preds = %19
%21 = tail call i32 @free(i8* %12)
br label %30
22: ; preds = %11
%23 = tail call i32 @read(i32 %1, i8* %12, i64 %3)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %30
25: ; preds = %22
%26 = load i32, i32* @errno
br i1 %5, label %27, label %29
27: ; preds = %25
%28 = tail call i32 @free(i8* %12)
br label %29
29: ; preds = %27, %25
store i32 %26, i32* @errno
br label %30
30: ; preds = %22, %19, %20, %29, %9
%31 = phi i8* [ inttoptr (i64 -1 to i8*), %9 ], [ inttoptr (i64 -1 to i8*), %29 ], [ inttoptr (i64 -1 to i8*), %20 ], [ inttoptr (i64 -1 to i8*), %19 ], [ %12, %22 ]
ret i8* %31
}