#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 /*<<< orphan*/ uint8_t;
struct bladerf_image {
  int dummy;
};

/* Variables and functions */
int file_read_buffer(char const *, int /*<<< orphan*/ **, size_t *);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int unpack_image(struct bladerf_image *, int /*<<< orphan*/ *, size_t);
int verify_checksum(int /*<<< orphan*/ *, size_t);
int bladerf_image_read(struct bladerf_image *img, const char *file) {
  int rv = -1;
  uint8_t *buf = NULL;
  size_t buf_len;
  rv = file_read_buffer(file, &buf, &buf_len);
  if (rv < 0) {
    goto bladerf_image_read_out;
  }
  rv = verify_checksum(buf, buf_len);
  if (rv < 0) {
    goto bladerf_image_read_out;
  }
  rv = unpack_image(img, buf, buf_len);
bladerf_image_read_out:
  if (rv != 0) {
    free(buf);
  }
  return rv;
}


x86 assembly (clang, -O0):
<bladerf_image_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x20(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1050 <file_read_buffer@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1182 <bladerf_image_read+0x42>
jmp    11b5 <bladerf_image_read+0x75>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1060 <verify_checksum@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11a1 <bladerf_image_read+0x61>
jmp    11b5 <bladerf_image_read+0x75>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <unpack_image@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11c8 <bladerf_image_read+0x88>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bladerf_image_read>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rax
mov    %rdi,%r14
movq   $0x0,0x8(%rsp)
lea    0x8(%rsp),%rsi
lea    0x10(%rsp),%rdx
mov    %rax,%rdi
call   1050 <file_read_buffer@plt>
test   %eax,%eax
js     119b <bladerf_image_read+0x5b>
mov    0x8(%rsp),%rdi
mov    0x10(%rsp),%rsi
call   1060 <verify_checksum@plt>
test   %eax,%eax
js     119b <bladerf_image_read+0x5b>
mov    0x8(%rsp),%rsi
mov    0x10(%rsp),%rdx
mov    %r14,%rdi
call   1040 <unpack_image@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    119d <bladerf_image_read+0x5d>
xor    %ebx,%ebx
jmp    11a7 <bladerf_image_read+0x67>
mov    %eax,%ebx
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.bladerf_image = type { i32 }
define i32 @bladerf_image_read(%struct.bladerf_image* %0, i8* %1) {
%3 = alloca %struct.bladerf_image*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i64
store %struct.bladerf_image* %0, %struct.bladerf_image** %3
store i8* %1, i8** %4
store i32 -1, i32* %5
store i32* null, i32** %6
%8 = load i8*, i8** %4
%9 = call i32 @file_read_buffer(i8* %8, i32** %6, i64* %7)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %2
br label %25
13: ; preds = %2
%14 = load i32*, i32** %6
%15 = load i64, i64* %7
%16 = call i32 @verify_checksum(i32* %14, i64 %15)
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %13
br label %25
20: ; preds = %13
%21 = load %struct.bladerf_image*, %struct.bladerf_image** %3
%22 = load i32*, i32** %6
%23 = load i64, i64* %7
%24 = call i32 @unpack_image(%struct.bladerf_image* %21, i32* %22, i64 %23)
store i32 %24, i32* %5
br label %25
25: ; preds = %20, %19, %12
%26 = load i32, i32* %5
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = load i32*, i32** %6
%30 = call i32 @free(i32* %29)
br label %31
31: ; preds = %28, %25
%32 = load i32, i32* %5
ret i32 %32
}

LLVM IR (-O2):
%struct.bladerf_image = type { i32 }
define i32 @bladerf_image_read(%struct.bladerf_image* %0, i8* %1) {
%3 = alloca i32*
%4 = alloca i64
%5 = bitcast i32** %3 to i8*
store i32* null, i32** %3
%6 = bitcast i64* %4 to i8*
%7 = call i32 @file_read_buffer(i8* %1, i32** %3, i64* %4)
%8 = icmp slt i32 %7, 0
br i1 %8, label %19, label %9
9: ; preds = %2
%10 = load i32*, i32** %3
%11 = load i64, i64* %4
%12 = call i32 @verify_checksum(i32* %10, i64 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %19, label %14
14: ; preds = %9
%15 = load i32*, i32** %3
%16 = load i64, i64* %4
%17 = call i32 @unpack_image(%struct.bladerf_image* %0, i32* %15, i64 %16)
%18 = icmp eq i32 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %9, %2, %14
%20 = phi i32 [ %17, %14 ], [ %12, %9 ], [ %7, %2 ]
%21 = load i32*, i32** %3
%22 = call i32 @free(i32* %21)
br label %23
23: ; preds = %19, %14
%24 = phi i32 [ %20, %19 ], [ 0, %14 ]
ret i32 %24
}