#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 /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int flash_read(char *buf, int offset, int len) {
  int fh;
  int ok = 1;
  fh = open(("/dev/mtdblock0"), O_RDWR);
  if (fh == -1) {
    printf("open file error\n");
    return 0;
  }
  lseek(fh, offset, SEEK_SET);
  if (read(fh, buf, len) != len)
    ok = 0;
  close(fh);
  return ok;
}


x86 assembly (clang, -O0):
<flash_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
movl   $0x1,-0x20(%rbp)
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
lea    0xe87(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11a3 <flash_read+0x53>
lea    0xe7d(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    11e4 <flash_read+0x94>
mov    -0x1c(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    0x2e28(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1060 <read@plt>
cmp    -0x18(%rbp),%eax
je     11d6 <flash_read+0x86>
movl   $0x0,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<flash_read>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r12d
mov    %esi,%r15d
mov    %rdi,%r14
mov    0x2e80(%rip),%rax
mov    (%rax),%esi
lea    0xe8f(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11ae <flash_read+0x5e>
mov    %eax,%ebp
mov    0x2e54(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    %r15d,%esi
call   1040 <lseek@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %r12d,%edx
call   1060 <read@plt>
xor    %ebx,%ebx
cmp    %r12d,%eax
sete   %bl
mov    %ebp,%edi
call   1050 <close@plt>
jmp    11bc <flash_read+0x6c>
lea    0xe5a(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"/dev/mtdblock0\00"
@O_RDWR = global i32 0
@.str.1 = [17 x i8] c"open file error\0A\00"
@SEEK_SET = global i32 0
define i32 @flash_read(i8* %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 1, i32* %9
%10 = load i32, i32* @O_RDWR
%11 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %8
%12 = load i32, i32* %8
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %4
br label %32
16: ; preds = %3
%17 = load i32, i32* %8
%18 = load i32, i32* %6
%19 = load i32, i32* @SEEK_SET
%20 = call i32 @lseek(i32 %17, i32 %18, i32 %19)
%21 = load i32, i32* %8
%22 = load i8*, i8** %5
%23 = load i32, i32* %7
%24 = call i32 @read(i32 %21, i8* %22, i32 %23)
%25 = load i32, i32* %7
%26 = icmp ne i32 %24, %25
br i1 %26, label %27, label %28
27: ; preds = %16
store i32 0, i32* %9
br label %28
28: ; preds = %27, %16
%29 = load i32, i32* %8
%30 = call i32 @close(i32 %29)
%31 = load i32, i32* %9
store i32 %31, i32* %4
br label %32
32: ; preds = %28, %14
%33 = load i32, i32* %4
ret i32 %33
}

LLVM IR (-O2):
@.str = [15 x i8] c"/dev/mtdblock0\00"
@O_RDWR = global i32 0
@SEEK_SET = global i32 0
@str = [16 x i8] c"open file error\00"
define i32 @flash_read(i8* nocapture %0, i32 %1, i32 %2) {
%4 = load i32, i32* @O_RDWR
%5 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %3
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
br label %16
9: ; preds = %3
%10 = load i32, i32* @SEEK_SET
%11 = tail call i32 @lseek(i32 %5, i32 %1, i32 %10)
%12 = tail call i32 @read(i32 %5, i8* %0, i32 %2)
%13 = icmp eq i32 %12, %2
%14 = zext i1 %13 to i32
%15 = tail call i32 @close(i32 %5)
br label %16
16: ; preds = %9, %7
%17 = phi i32 [ 0, %7 ], [ %14, %9 ]
ret i32 %17
}