#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;
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ enable_infen();
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fwrite(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ spi_transfer(int *, int);
int /*<<< orphan*/ stderr;
void cmd_read_ip(const char *filename) {
  FILE *f;
  uint8_t buf[512 + 3] = {0};
  enable_infen();
  printf("reading infopage...\n");
  buf[0] = 0x03;
  buf[1] = 0x00;
  buf[2] = 0x00;
  if (!spi_transfer(buf, sizeof(buf))) {
    fprintf(stderr, "something wrong reading InfoPage\n");
    exit(EXIT_FAILURE);
  }
  f = fopen(filename, "w");
  if (!f) {
    fprintf(stderr, "can't open %s to write\n", filename);
    exit(EXIT_FAILURE);
  }
  fwrite(buf + 3, sizeof(buf) - 3, 1, f);
  fclose(f);
}


x86 assembly (clang, -O0):
<cmd_read_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x820,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x820(%rbp),%rdi
xor    %esi,%esi
mov    $0x80c,%edx
call   1050 <memset@plt>
mov    $0x0,%al
call   1060 <enable_infen@plt>
lea    0xe40(%rip),%rdi
call   1040 <printf@plt>
movl   $0x3,-0x820(%rbp)
movl   $0x0,-0x81c(%rbp)
movl   $0x0,-0x818(%rbp)
lea    -0x820(%rbp),%rdi
mov    $0x80c,%esi
call   1080 <spi_transfer@plt>
cmp    $0x0,%eax
jne    1222 <cmd_read_ip+0x92>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
lea    0xe08(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%rdi
lea    0xe0a(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    126a <cmd_read_ip+0xda>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xde4(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    -0x820(%rbp),%rdi
add    $0xc,%rdi
mov    -0x10(%rbp),%rcx
mov    $0x809,%esi
mov    $0x1,%edx
call   10b0 <fwrite@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x820,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_read_ip>:
push   %r14
push   %rbx
sub    $0x818,%rsp
mov    %rdi,%r14
mov    %rsp,%rbx
mov    $0x80c,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
xor    %eax,%eax
call   1060 <enable_infen@plt>
lea    0xe7f(%rip),%rdi
call   1030 <puts@plt>
movq   $0x3,(%rsp)
movl   $0x0,0x8(%rsp)
mov    %rbx,%rdi
mov    $0x80c,%esi
call   1080 <spi_transfer@plt>
test   %eax,%eax
je     1224 <cmd_read_ip+0x94>
lea    0xe38(%rip),%rsi
mov    %r14,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     123d <cmd_read_ip+0xad>
mov    %rax,%rbx
lea    0xc(%rsp),%rdi
mov    $0x809,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10b0 <fwrite@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
add    $0x818,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
lea    0xdcc(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1257 <cmd_read_ip+0xc7>
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
lea    0xdd7(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [21 x i8] c"reading infopage...\0A\00"
@stderr = global i32 0
@.str.1 = [34 x i8] c"something wrong reading InfoPage\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [24 x i8] c"can't open %s to write\0A\00"
define void @cmd_read_ip(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca [515 x i32]
store i8* %0, i8** %2
%5 = bitcast [515 x i32]* %4 to i8*
%6 = call i32 (...) @enable_infen()
%7 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
%8 = getelementptr inbounds [515 x i32], [515 x i32]* %4, i64 0, i64 0
store i32 3, i32* %8
%9 = getelementptr inbounds [515 x i32], [515 x i32]* %4, i64 0, i64 1
store i32 0, i32* %9
%10 = getelementptr inbounds [515 x i32], [515 x i32]* %4, i64 0, i64 2
store i32 0, i32* %10
%11 = getelementptr inbounds [515 x i32], [515 x i32]* %4, i64 0, i64 0
%12 = call i32 @spi_transfer(i32* %11, i32 2060)
%13 = icmp ne i32 %12, 0
br i1 %13, label %19, label %14
14: ; preds = %1
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %1
%20 = load i8*, i8** %2
%21 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %21, i32** %3
%22 = load i32*, i32** %3
%23 = icmp ne i32* %22, null
br i1 %23, label %30, label %24
24: ; preds = %19
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %2
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %26)
%28 = load i32, i32* @EXIT_FAILURE
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %19
%31 = getelementptr inbounds [515 x i32], [515 x i32]* %4, i64 0, i64 0
%32 = getelementptr inbounds i32, i32* %31, i64 3
%33 = load i32*, i32** %3
%34 = call i32 @fwrite(i32* %32, i32 2057, i32 1, i32* %33)
%35 = load i32*, i32** %3
%36 = call i32 @fclose(i32* %35)
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str.1 = [34 x i8] c"something wrong reading InfoPage\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [24 x i8] c"can't open %s to write\0A\00"
@str = [20 x i8] c"reading infopage...\00"
define void @cmd_read_ip(i8* %0) {
%2 = alloca [515 x i32]
%3 = bitcast [515 x i32]* %2 to i8*
%4 = tail call i32 (...) @enable_infen()
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
%6 = getelementptr inbounds [515 x i32], [515 x i32]* %2, i64 0, i64 0
store i32 3, i32* %6
%7 = getelementptr inbounds [515 x i32], [515 x i32]* %2, i64 0, i64 1
store i32 0, i32* %7
%8 = getelementptr inbounds [515 x i32], [515 x i32]* %2, i64 0, i64 2
store i32 0, i32* %8
%9 = call i32 @spi_transfer(i32* %6, i32 2060)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = call i32 @exit(i32 %14)
unreachable
16: ; preds = %1
%17 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %24
19: ; preds = %16
%20 = load i32, i32* @stderr
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %16
%25 = getelementptr inbounds [515 x i32], [515 x i32]* %2, i64 0, i64 3
%26 = call i32 @fwrite(i32* %25, i32 2057, i32 1, i32* %17)
%27 = call i32 @fclose(i32* %17)
ret void
}