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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ free(void *);
scalar_t__ lseek(int, scalar_t__, int /*<<< orphan*/);
size_t read(int, void *, size_t);
void *save_malloc(size_t);
void *read_flash(int fd, off_t offset, size_t length) {
  void *ptr;
  if (lseek(fd, offset, SEEK_SET) != (off_t)(-1)) {
    ptr = save_malloc(length);
    if (read(fd, ptr, length) == length)
      return ptr;
    free(ptr);
  }
  return NULL;
}


x86 assembly (clang, -O0):
<read_flash>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    0x2e7f(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
cmp    $0xffffffffffffffff,%rax
je     11af <read_flash+0x6f>
mov    -0x20(%rbp),%rdi
call   1040 <save_malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1060 <read@plt>
cmp    -0x20(%rbp),%rax
jne    11a6 <read_flash+0x66>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11b7 <read_flash+0x77>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_flash>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %edi,%ebp
mov    0x2e90(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
cmp    $0xffffffffffffffff,%rax
je     1182 <read_flash+0x42>
mov    %r14,%rdi
call   1040 <save_malloc@plt>
mov    %rax,%rbx
mov    %ebp,%edi
mov    %rax,%rsi
mov    %r14,%rdx
call   1060 <read@plt>
cmp    %r14,%rax
je     1184 <read_flash+0x44>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
define i8* @read_flash(i32 %0, i64 %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
store i32 %0, i32* %5
store i64 %1, i64* %6
store i64 %2, i64* %7
%9 = load i32, i32* %5
%10 = load i64, i64* %6
%11 = load i32, i32* @SEEK_SET
%12 = call i64 @lseek(i32 %9, i64 %10, i32 %11)
%13 = icmp ne i64 %12, -1
br i1 %13, label %14, label %28
14: ; preds = %3
%15 = load i64, i64* %7
%16 = call i8* @save_malloc(i64 %15)
store i8* %16, i8** %8
%17 = load i32, i32* %5
%18 = load i8*, i8** %8
%19 = load i64, i64* %7
%20 = call i64 @read(i32 %17, i8* %18, i64 %19)
%21 = load i64, i64* %7
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %25
23: ; preds = %14
%24 = load i8*, i8** %8
store i8* %24, i8** %4
br label %29
25: ; preds = %14
%26 = load i8*, i8** %8
%27 = call i32 @free(i8* %26)
br label %28
28: ; preds = %25, %3
store i8* null, i8** %4
br label %29
29: ; preds = %28, %23
%30 = load i8*, i8** %4
ret i8* %30
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
define i8* @read_flash(i32 %0, i64 %1, i64 %2) {
%4 = load i32, i32* @SEEK_SET
%5 = tail call i64 @lseek(i32 %0, i64 %1, i32 %4)
%6 = icmp eq i64 %5, -1
br i1 %6, label %13, label %7
7: ; preds = %3
%8 = tail call i8* @save_malloc(i64 %2)
%9 = tail call i64 @read(i32 %0, i8* %8, i64 %2)
%10 = icmp eq i64 %9, %2
br i1 %10, label %13, label %11
11: ; preds = %7
%12 = tail call i32 @free(i8* %8)
br label %13
13: ; preds = %3, %11, %7
%14 = phi i8* [ %8, %7 ], [ null, %11 ], [ null, %3 ]
ret i8* %14
}