#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 */
struct timeval {
  int dummy;
};
struct stat {
  int st_blksize;
};
struct mtd_info_user {
  int dummy;
};
typedef int off_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
scalar_t__ get_flash_info(char *, struct mtd_info_user *, struct stat *);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
scalar_t__ malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ show_performance(struct timeval *, struct timeval *, int);
int write_to_file(char *, char *, int);
int read_flash(char *device, char *filename, off_t offset, int length) {
  struct mtd_info_user iu;
  struct stat fs;
  char *buffer = NULL;
  int status = -1;
  int fd = 0;
  int i = 0;
  int left_to_read = length;
  struct timeval start, end;
  if (get_flash_info(device, &iu, &fs))
    goto exit;
  if (!(buffer = (char *)malloc(length))) {
    printf("Can't allocate %d bytes buffer\n", length);
    goto exit;
  }
  if ((fd = open(device, O_RDONLY)) < 0) {
    printf("Can't open %s\n", device);
    goto exit;
  }
  printf("Read %d bytes from offset 0x%lx\n", length, offset);
  gettimeofday(&start, NULL);
  lseek(fd, offset, SEEK_SET);
  for (i = 0; left_to_read > 0; i += fs.st_blksize) {
    int bytes =
        left_to_read < fs.st_blksize ? left_to_read : (int)fs.st_blksize;
    int ret;
    ret = read(fd, &buffer[i], bytes);
    if (ret != bytes) {
      printf("read() returned %d instead of %d\n", ret, bytes);
      status = -1;
      goto exit;
    }
    left_to_read -= bytes;
  }
  gettimeofday(&end, NULL);
  show_performance(&start, &end, length);
  status = write_to_file(filename, buffer, length);
exit:
  if (fd > 0)
    close(fd);
  if (buffer)
    free(buffer);
  return status;
}


x86 assembly (clang, -O0):
<read_flash>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0xffffffff,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x20(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   10d0 <get_flash_info@plt>
cmp    $0x0,%rax
je     1209 <read_flash+0x59>
jmp    1361 <read_flash+0x1b1>
mov    -0x18(%rbp),%edi
call   10a0 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    1235 <read_flash+0x85>
mov    -0x18(%rbp),%esi
lea    0xdd7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1361 <read_flash+0x1b1>
mov    -0x8(%rbp),%rdi
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
call   10c0 <open@plt>
mov    %eax,-0x38(%rbp)
cmp    $0x0,%eax
jge    126a <read_flash+0xba>
mov    -0x8(%rbp),%rsi
lea    0xdc2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1361 <read_flash+0x1b1>
mov    -0x18(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    0xdb8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x48(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <gettimeofday@plt>
mov    -0x38(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    0x2d40(%rip),%rax
mov    (%rax),%edx
call   1060 <lseek@plt>
movl   $0x0,-0x3c(%rbp)
cmpl   $0x0,-0x40(%rbp)
jle    1331 <read_flash+0x181>
mov    -0x40(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    12c7 <read_flash+0x117>
mov    -0x40(%rbp),%eax
mov    %eax,-0x5c(%rbp)
jmp    12cd <read_flash+0x11d>
mov    -0x28(%rbp),%eax
mov    %eax,-0x5c(%rbp)
mov    -0x5c(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x38(%rbp),%edi
mov    -0x30(%rbp),%rsi
movslq -0x3c(%rbp),%rax
add    %rax,%rsi
mov    -0x54(%rbp),%edx
call   1090 <read@plt>
mov    %eax,-0x58(%rbp)
mov    -0x58(%rbp),%eax
cmp    -0x54(%rbp),%eax
je     1318 <read_flash+0x168>
mov    -0x58(%rbp),%esi
mov    -0x54(%rbp),%edx
lea    0xd4b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x34(%rbp)
jmp    1361 <read_flash+0x1b1>
mov    -0x54(%rbp),%ecx
mov    -0x40(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x40(%rbp)
mov    -0x28(%rbp),%eax
add    -0x3c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
jmp    12a6 <read_flash+0xf6>
lea    -0x50(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <gettimeofday@plt>
mov    -0x18(%rbp),%edx
lea    -0x48(%rbp),%rdi
lea    -0x50(%rbp),%rsi
call   10b0 <show_performance@plt>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1050 <write_to_file@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x38(%rbp)
jle    1373 <read_flash+0x1c3>
mov    -0x38(%rbp),%edi
call   1080 <close@plt>
cmpq   $0x0,-0x30(%rbp)
je     1387 <read_flash+0x1d7>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x34(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_flash>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %ecx,%r13d
mov    %edx,%r12d
mov    %rsi,%rbp
mov    %rdi,%r14
lea    0x30(%rsp),%rsi
lea    0x8(%rsp),%rdx
call   10d0 <get_flash_info@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
jne    132d <read_flash+0x17d>
mov    %r13d,%edi
call   10a0 <malloc@plt>
mov    %rax,0x18(%rsp)
test   %rax,%rax
je     12d1 <read_flash+0x121>
mov    %rbp,0x20(%rsp)
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   10c0 <open@plt>
test   %eax,%eax
js     12e4 <read_flash+0x134>
mov    %eax,%r15d
lea    0xe0a(%rip),%rdi
xor    %r14d,%r14d
mov    %r13d,%esi
mov    %r12d,%edx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   1070 <gettimeofday@plt>
mov    0x2d90(%rip),%rax
mov    (%rax),%edx
mov    %r15d,%edi
mov    %r12d,%esi
call   1060 <lseek@plt>
mov    %r13d,%r12d
test   %r13d,%r13d
mov    0x18(%rsp),%r13
jle    1296 <read_flash+0xe6>
mov    0x8(%rsp),%ebx
mov    %r12d,%ebp
nopl   0x0(%rax)
cmp    %ebx,%ebp
cmovl  %ebp,%ebx
movslq %r14d,%rsi
add    %r13,%rsi
mov    %r15d,%edi
mov    %ebx,%edx
call   1090 <read@plt>
cmp    %ebx,%eax
jne    1301 <read_flash+0x151>
mov    0x8(%rsp),%eax
add    %eax,%r14d
sub    %ebx,%ebp
mov    %eax,%ebx
jg     1270 <read_flash+0xc0>
lea    0x28(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <gettimeofday@plt>
lea    0x10(%rsp),%rdi
mov    %rbx,%rsi
mov    %r12d,%ebx
mov    %r12d,%edx
call   10b0 <show_performance@plt>
mov    0x20(%rsp),%rdi
mov    %r13,%rsi
mov    %r12d,%edx
call   1050 <write_to_file@plt>
mov    %eax,%ebx
test   %r15d,%r15d
jg     131d <read_flash+0x16d>
jmp    1325 <read_flash+0x175>
lea    0xd28(%rip),%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    132d <read_flash+0x17d>
lea    0xd35(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%ebx
mov    0x18(%rsp),%r13
jmp    1325 <read_flash+0x175>
lea    0xd48(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%ebx
test   %r15d,%r15d
jle    1325 <read_flash+0x175>
mov    %r15d,%edi
call   1080 <close@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.mtd_info_user = type { i32 }
%struct.stat = type { i32 }
%struct.timeval = type { i32 }
@.str = [32 x i8] c"Can't allocate %d bytes buffer\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [15 x i8] c"Can't open %s\0A\00"
@.str.2 = [33 x i8] c"Read %d bytes from offset 0x%lx\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [34 x i8] c"read() returned %d instead of %d\0A\00"
define i32 @read_flash(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.mtd_info_user
%10 = alloca %struct.stat
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca %struct.timeval
%17 = alloca %struct.timeval
%18 = alloca i32
%19 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
store i8* null, i8** %11
store i32 -1, i32* %12
store i32 0, i32* %13
store i32 0, i32* %14
%20 = load i32, i32* %8
store i32 %20, i32* %15
%21 = load i8*, i8** %5
%22 = call i64 @get_flash_info(i8* %21, %struct.mtd_info_user* %9, %struct.stat* %10)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %4
br label %96
25: ; preds = %4
%26 = load i32, i32* %8
%27 = call i64 @malloc(i32 %26)
%28 = inttoptr i64 %27 to i8*
store i8* %28, i8** %11
%29 = icmp ne i8* %28, null
br i1 %29, label %33, label %30
30: ; preds = %25
%31 = load i32, i32* %8
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %31)
br label %96
33: ; preds = %25
%34 = load i8*, i8** %5
%35 = load i32, i32* @O_RDONLY
%36 = call i32 @open(i8* %34, i32 %35)
store i32 %36, i32* %13
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %33
%39 = load i8*, i8** %5
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %39)
br label %96
41: ; preds = %33
%42 = load i32, i32* %8
%43 = load i32, i32* %7
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %42, i32 %43)
%45 = call i32 @gettimeofday(%struct.timeval* %16, i32* null)
%46 = load i32, i32* %13
%47 = load i32, i32* %7
%48 = load i32, i32* @SEEK_SET
%49 = call i32 @lseek(i32 %46, i32 %47, i32 %48)
store i32 0, i32* %14
br label %50
50: ; preds = %83, %41
%51 = load i32, i32* %15
%52 = icmp sgt i32 %51, 0
br i1 %52, label %53, label %88
53: ; preds = %50
%54 = load i32, i32* %15
%55 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%56 = load i32, i32* %55
%57 = icmp slt i32 %54, %56
br i1 %57, label %58, label %60
58: ; preds = %53
%59 = load i32, i32* %15
br label %63
60: ; preds = %53
%61 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%62 = load i32, i32* %61
br label %63
63: ; preds = %60, %58
%64 = phi i32 [ %59, %58 ], [ %62, %60 ]
store i32 %64, i32* %18
%65 = load i32, i32* %13
%66 = load i8*, i8** %11
%67 = load i32, i32* %14
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i8, i8* %66, i64 %68
%70 = load i32, i32* %18
%71 = call i32 @read(i32 %65, i8* %69, i32 %70)
store i32 %71, i32* %19
%72 = load i32, i32* %19
%73 = load i32, i32* %18
%74 = icmp ne i32 %72, %73
br i1 %74, label %75, label %79
75: ; preds = %63
%76 = load i32, i32* %19
%77 = load i32, i32* %18
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i32 %76, i32 %77)
store i32 -1, i32* %12
br label %96
79: ; preds = %63
%80 = load i32, i32* %18
%81 = load i32, i32* %15
%82 = sub nsw i32 %81, %80
store i32 %82, i32* %15
br label %83
83: ; preds = %79
%84 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%85 = load i32, i32* %84
%86 = load i32, i32* %14
%87 = add nsw i32 %86, %85
store i32 %87, i32* %14
br label %50
88: ; preds = %50
%89 = call i32 @gettimeofday(%struct.timeval* %17, i32* null)
%90 = load i32, i32* %8
%91 = call i32 @show_performance(%struct.timeval* %16, %struct.timeval* %17, i32 %90)
%92 = load i8*, i8** %6
%93 = load i8*, i8** %11
%94 = load i32, i32* %8
%95 = call i32 @write_to_file(i8* %92, i8* %93, i32 %94)
store i32 %95, i32* %12
br label %96
96: ; preds = %88, %75, %38, %30, %24
%97 = load i32, i32* %13
%98 = icmp sgt i32 %97, 0
br i1 %98, label %99, label %102
99: ; preds = %96
%100 = load i32, i32* %13
%101 = call i32 @close(i32 %100)
br label %102
102: ; preds = %99, %96
%103 = load i8*, i8** %11
%104 = icmp ne i8* %103, null
br i1 %104, label %105, label %108
105: ; preds = %102
%106 = load i8*, i8** %11
%107 = call i32 @free(i8* %106)
br label %108
108: ; preds = %105, %102
%109 = load i32, i32* %12
ret i32 %109
}

LLVM IR (-O2):
%struct.mtd_info_user = type { i32 }
%struct.stat = type { i32 }
%struct.timeval = type { i32 }
@.str = [32 x i8] c"Can't allocate %d bytes buffer\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [15 x i8] c"Can't open %s\0A\00"
@.str.2 = [33 x i8] c"Read %d bytes from offset 0x%lx\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [34 x i8] c"read() returned %d instead of %d\0A\00"
define i32 @read_flash(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca %struct.mtd_info_user
%6 = alloca %struct.stat
%7 = alloca %struct.timeval
%8 = alloca %struct.timeval
%9 = bitcast %struct.mtd_info_user* %5 to i8*
%10 = bitcast %struct.stat* %6 to i8*
%11 = bitcast %struct.timeval* %7 to i8*
%12 = bitcast %struct.timeval* %8 to i8*
%13 = call i64 @get_flash_info(i8* %0, %struct.mtd_info_user* %5, %struct.stat* %6)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %65
15: ; preds = %4
%16 = call i64 @malloc(i32 %3)
%17 = inttoptr i64 %16 to i8*
%18 = icmp eq i64 %16, 0
br i1 %18, label %19, label %21
19: ; preds = %15
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i32 %3)
br label %65
21: ; preds = %15
%22 = load i32, i32* @O_RDONLY
%23 = call i32 @open(i8* %0, i32 %22)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %62
27: ; preds = %21
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %3, i32 %2)
%29 = call i32 @gettimeofday(%struct.timeval* %7, i32* null)
%30 = load i32, i32* @SEEK_SET
%31 = call i32 @lseek(i32 %23, i32 %2, i32 %30)
%32 = icmp sgt i32 %3, 0
br i1 %32, label %33, label %53
33: ; preds = %27
%34 = getelementptr inbounds %struct.stat, %struct.stat* %6, i64 0, i32 0
%35 = load i32, i32* %34
br label %36
36: ; preds = %33, %48
%37 = phi i32 [ %35, %33 ], [ %50, %48 ]
%38 = phi i32 [ %3, %33 ], [ %49, %48 ]
%39 = phi i32 [ 0, %33 ], [ %51, %48 ]
%40 = icmp slt i32 %38, %37
%41 = select i1 %40, i32 %38, i32 %37
%42 = sext i32 %39 to i64
%43 = getelementptr inbounds i8, i8* %17, i64 %42
%44 = call i32 @read(i32 %23, i8* %43, i32 %41)
%45 = icmp eq i32 %44, %41
br i1 %45, label %48, label %46
46: ; preds = %36
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i32 %44, i32 %41)
br label %57
48: ; preds = %36
%49 = sub nsw i32 %38, %41
%50 = load i32, i32* %34
%51 = add nsw i32 %50, %39
%52 = icmp sgt i32 %49, 0
br i1 %52, label %36, label %53
53: ; preds = %48, %27
%54 = call i32 @gettimeofday(%struct.timeval* %8, i32* null)
%55 = call i32 @show_performance(%struct.timeval* %7, %struct.timeval* %8, i32 %3)
%56 = call i32 @write_to_file(i8* %1, i8* %17, i32 %3)
br label %57
57: ; preds = %46, %53
%58 = phi i32 [ -1, %46 ], [ %56, %53 ]
%59 = icmp sgt i32 %23, 0
br i1 %59, label %60, label %62
60: ; preds = %57
%61 = call i32 @close(i32 %23)
br label %62
62: ; preds = %57, %60, %25
%63 = phi i32 [ %58, %60 ], [ %58, %57 ], [ -1, %25 ]
%64 = call i32 @free(i8* %17)
br label %65
65: ; preds = %4, %19, %62
%66 = phi i32 [ %63, %62 ], [ -1, %19 ], [ -1, %4 ]
ret i32 %66
}