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

/* Variables and functions */
int /*<<< orphan*/ free(int *);
int *malloc(int);
int /*<<< orphan*/ memcpy(int *, int *, int);
int /*<<< orphan*/ perror(char *);
int spi_write(int, int *, int);
int do_page_program(int fd, int addr, uint8_t *buf, int len) {
  uint8_t *wbuf = NULL;
  int ret;
  wbuf = malloc(len + 4);
  if (wbuf == NULL) {
    perror("malloc failed");
    return -1;
  }
  wbuf[0] = 0x02;
  wbuf[1] = (addr & 0xff0000) >> 16;
  wbuf[2] = (addr & 0xff00) >> 8;
  wbuf[3] = addr & 0xff;
  memcpy(wbuf + 4, buf, len);
  ret = spi_write(fd, wbuf, len + 4);
  free(wbuf);
  return ret;
}


x86 assembly (clang, -O0):
<do_page_program>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
add    $0x4,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    119f <do_page_program+0x4f>
lea    0xe72(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1217 <do_page_program+0xc7>
mov    -0x28(%rbp),%rax
movl   $0x2,(%rax)
mov    -0xc(%rbp),%ecx
and    $0xff0000,%ecx
sar    $0x10,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,0x4(%rax)
mov    -0xc(%rbp),%ecx
and    $0xff00,%ecx
sar    $0x8,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0xc(%rbp),%ecx
and    $0xff,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,0xc(%rax)
mov    -0x28(%rbp),%rdi
add    $0x10,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
add    $0x4,%edx
call   1060 <spi_write@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_page_program>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r13d
mov    %rdx,%r12
mov    %esi,%ebx
mov    %edi,%r14d
lea    0x4(%r13),%r15d
mov    %r15d,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11d2 <do_page_program+0x82>
mov    %rax,%rbp
movl   $0x2,(%rax)
mov    %ebx,%eax
shr    $0x10,%eax
movzbl %al,%eax
mov    %eax,0x4(%rbp)
movzbl %bh,%eax
mov    %eax,0x8(%rbp)
movzbl %bl,%eax
mov    %eax,0xc(%rbp)
mov    %rbp,%rdi
add    $0x10,%rdi
mov    %r12,%rsi
mov    %r13d,%edx
call   1040 <memcpy@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %r15d,%edx
call   1060 <spi_write@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe27(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%eax
jmp    11c3 <do_page_program+0x73>

LLVM IR (-O0):
@.str = [14 x i8] c"malloc failed\00"
define i32 @do_page_program(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 i32*
%11 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32* %2, i32** %8
store i32 %3, i32* %9
store i32* null, i32** %10
%12 = load i32, i32* %9
%13 = add nsw i32 %12, 4
%14 = call i32* @malloc(i32 %13)
store i32* %14, i32** %10
%15 = load i32*, i32** %10
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %19
17: ; preds = %4
%18 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %49
19: ; preds = %4
%20 = load i32*, i32** %10
%21 = getelementptr inbounds i32, i32* %20, i64 0
store i32 2, i32* %21
%22 = load i32, i32* %7
%23 = and i32 %22, 16711680
%24 = ashr i32 %23, 16
%25 = load i32*, i32** %10
%26 = getelementptr inbounds i32, i32* %25, i64 1
store i32 %24, i32* %26
%27 = load i32, i32* %7
%28 = and i32 %27, 65280
%29 = ashr i32 %28, 8
%30 = load i32*, i32** %10
%31 = getelementptr inbounds i32, i32* %30, i64 2
store i32 %29, i32* %31
%32 = load i32, i32* %7
%33 = and i32 %32, 255
%34 = load i32*, i32** %10
%35 = getelementptr inbounds i32, i32* %34, i64 3
store i32 %33, i32* %35
%36 = load i32*, i32** %10
%37 = getelementptr inbounds i32, i32* %36, i64 4
%38 = load i32*, i32** %8
%39 = load i32, i32* %9
%40 = call i32 @memcpy(i32* %37, i32* %38, i32 %39)
%41 = load i32, i32* %6
%42 = load i32*, i32** %10
%43 = load i32, i32* %9
%44 = add nsw i32 %43, 4
%45 = call i32 @spi_write(i32 %41, i32* %42, i32 %44)
store i32 %45, i32* %11
%46 = load i32*, i32** %10
%47 = call i32 @free(i32* %46)
%48 = load i32, i32* %11
store i32 %48, i32* %5
br label %49
49: ; preds = %19, %17
%50 = load i32, i32* %5
ret i32 %50
}

LLVM IR (-O2):
@.str = [14 x i8] c"malloc failed\00"
define i32 @do_page_program(i32 %0, i32 %1, i32* %2, i32 %3) {
%5 = add nsw i32 %3, 4
%6 = tail call i32* @malloc(i32 %5)
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %4
%9 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %23
10: ; preds = %4
store i32 2, i32* %6
%11 = lshr i32 %1, 16
%12 = and i32 %11, 255
%13 = getelementptr inbounds i32, i32* %6, i64 1
store i32 %12, i32* %13
%14 = lshr i32 %1, 8
%15 = and i32 %14, 255
%16 = getelementptr inbounds i32, i32* %6, i64 2
store i32 %15, i32* %16
%17 = and i32 %1, 255
%18 = getelementptr inbounds i32, i32* %6, i64 3
store i32 %17, i32* %18
%19 = getelementptr inbounds i32, i32* %6, i64 4
%20 = tail call i32 @memcpy(i32* %19, i32* %2, i32 %3)
%21 = tail call i32 @spi_write(i32 %0, i32* %6, i32 %5)
%22 = tail call i32 @free(i32* %6)
br label %23
23: ; preds = %10, %8
%24 = phi i32 [ -1, %8 ], [ %21, %10 ]
ret i32 %24
}