#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 uint8_t;
struct can_frame {
  int can_id;
  int can_dlc;
  int *data;
};
typedef int canid_t;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
scalar_t__ can_receive_frame(int, struct can_frame *);
scalar_t__ can_send_frame(int, struct can_frame *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ memcpy(int *, int *, int);
int /*<<< orphan*/ stderr;
int can_send_read_flash(int fd, canid_t addr, uint8_t len, uint8_t *data) {
  struct can_frame msg;
  assert(len <= 8);
  msg.can_id = addr | 1 << 31;
  msg.can_dlc = 8;
  msg.data[0] = 0x02;
  msg.data[1] = len;
  if (can_send_frame(fd, &msg) < 0) {
    do {
      fprintf(stderr, "Sending read request failed"
                      "\n");
    } while (0);
    return -1;
  }
  if (can_receive_frame(fd, &msg) < 0) {
    do {
      fprintf(stderr, "Receiving read answer failed"
                      "\n");
    } while (0);
    return -1;
  }
  memcpy(data, msg.data, len);
  return 0;
}


x86 assembly (clang, -O0):
<can_send_read_flash>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
cmpl   $0x8,-0x10(%rbp)
setle  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0xc(%rbp),%eax
or     $0x80000000,%eax
mov    %eax,-0x28(%rbp)
movl   $0x8,-0x24(%rbp)
mov    -0x20(%rbp),%rax
movl   $0x2,(%rax)
mov    -0x10(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1030 <can_send_frame@plt>
cmp    $0x0,%rax
jge    11d8 <can_send_read_flash+0x88>
jmp    11b7 <can_send_read_flash+0x67>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
lea    0xe39(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122b <can_send_read_flash+0xdb>
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
call   1040 <can_receive_frame@plt>
cmp    $0x0,%rax
jge    1214 <can_send_read_flash+0xc4>
jmp    11f3 <can_send_read_flash+0xa3>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
lea    0xe1a(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    122b <can_send_read_flash+0xdb>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1070 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<can_send_read_flash>:
push   %rax
xor    %edi,%edi
cmp    $0x9,%edx
setl   %dil
call   1030 <assert@plt>

LLVM IR (-O0):
%struct.can_frame = type { i32, i32, i32* }
@stderr = global i32 0
@.str = [29 x i8] c"Sending read request failed\0A\00"
@.str.1 = [30 x i8] c"Receiving read answer failed\0A\00"
define i32 @can_send_read_flash(i32 %0, i32 %1, i32 %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca %struct.can_frame
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32* %3, i32** %9
%11 = load i32, i32* %8
%12 = icmp sle i32 %11, 8
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = load i32, i32* %7
%16 = or i32 %15, -2147483648
%17 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %10, i32 0, i32 0
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %10, i32 0, i32 1
store i32 8, i32* %18
%19 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %10, i32 0, i32 2
%20 = load i32*, i32** %19
%21 = getelementptr inbounds i32, i32* %20, i64 0
store i32 2, i32* %21
%22 = load i32, i32* %8
%23 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %10, i32 0, i32 2
%24 = load i32*, i32** %23
%25 = getelementptr inbounds i32, i32* %24, i64 1
store i32 %22, i32* %25
%26 = load i32, i32* %6
%27 = call i64 @can_send_frame(i32 %26, %struct.can_frame* %10)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %4
br label %30
30: ; preds = %29
%31 = load i32, i32* @stderr
%32 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %33
33: ; preds = %30
store i32 -1, i32* %5
br label %49
34: ; preds = %4
%35 = load i32, i32* %6
%36 = call i64 @can_receive_frame(i32 %35, %struct.can_frame* %10)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %43
38: ; preds = %34
br label %39
39: ; preds = %38
%40 = load i32, i32* @stderr
%41 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %42
42: ; preds = %39
store i32 -1, i32* %5
br label %49
43: ; preds = %34
%44 = load i32*, i32** %9
%45 = getelementptr inbounds %struct.can_frame, %struct.can_frame* %10, i32 0, i32 2
%46 = load i32*, i32** %45
%47 = load i32, i32* %8
%48 = call i32 @memcpy(i32* %44, i32* %46, i32 %47)
store i32 0, i32* %5
br label %49
49: ; preds = %43, %42, %33
%50 = load i32, i32* %5
ret i32 %50
}

LLVM IR (-O2):
@stderr = global i32 0
define i32 @can_send_read_flash(i32 %0, i32 %1, i32 %2, i32* %3) {
%5 = icmp slt i32 %2, 9
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
unreachable
}