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

/* Variables and functions */
int bp_serial_readto(int, int *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ fwrite(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(int *, int, int);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ usleep(int);
int /*<<< orphan*/ write(int, int *, int);
bool sx1276_dump(int fd) {
  printf("DUMP REGS\n");
  int n;
  uint8_t regval[0x71];
  memset(regval, 0x5a, sizeof(regval));
  bool ok = true;
  for (uint8_t r = 0; r <= 0x70; r++) {
    uint8_t cmd[6] = {0x04, 0, 1, 0, 1, r};
    write(fd, cmd, 6);
    usleep(60 * 0x71);
    n = bp_serial_readto(fd, cmd, 2);
    if (n != 2 || cmd[0] != 0x1) {
      ok = false;
      fprintf(stderr, "Dump fault at %.02x\n", r);
      break;
    }
    regval[r] = cmd[1];
  }
  if (ok) {
    FILE *f = popen("od -Ax -tx1z -v -w8", "w");
    if (f) {
      fwrite(regval, 0x71, 1, f);
      pclose(f);
    }
  }
  return ok;
}


x86 assembly (clang, -O0):
<sx1276_dump>:
push   %rbp
mov    %rsp,%rbp
sub    $0x200,%rsp
mov    %edi,-0x4(%rbp)
lea    0xe5b(%rip),%rdi
call   1050 <printf@plt>
lea    -0x1d0(%rbp),%rdi
mov    $0x5a,%esi
mov    $0x1c4,%edx
call   1070 <memset@plt>
movl   $0x1,-0x1d4(%rbp)
movl   $0x0,-0x1d8(%rbp)
cmpl   $0x70,-0x1d8(%rbp)
jg     12bd <sx1276_dump+0x12d>
movl   $0x4,-0x1f0(%rbp)
movl   $0x0,-0x1ec(%rbp)
movl   $0x1,-0x1e8(%rbp)
movl   $0x0,-0x1e4(%rbp)
movl   $0x1,-0x1e0(%rbp)
mov    -0x1d8(%rbp),%eax
mov    %eax,-0x1dc(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x1f0(%rbp),%rsi
mov    $0x6,%edx
call   1040 <write@plt>
mov    $0x1a7c,%edi
call   10b0 <usleep@plt>
mov    -0x4(%rbp),%edi
lea    -0x1f0(%rbp),%rsi
mov    $0x2,%edx
call   1030 <bp_serial_readto@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    126b <sx1276_dump+0xdb>
cmpl   $0x1,-0x1f0(%rbp)
je     1295 <sx1276_dump+0x105>
movl   $0x0,-0x1d4(%rbp)
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
mov    -0x1d8(%rbp),%edx
lea    0xd80(%rip),%rsi
call   1080 <fprintf@plt>
jmp    12bd <sx1276_dump+0x12d>
mov    -0x1ec(%rbp),%ecx
movslq -0x1d8(%rbp),%rax
mov    %ecx,-0x1d0(%rbp,%rax,4)
mov    -0x1d8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1d8(%rbp)
jmp    11d4 <sx1276_dump+0x44>
cmpl   $0x0,-0x1d4(%rbp)
je     1320 <sx1276_dump+0x190>
lea    0xd4f(%rip),%rdi
lea    0xd5c(%rip),%rsi
call   1090 <popen@plt>
mov    %rax,-0x1f8(%rbp)
cmpq   $0x0,-0x1f8(%rbp)
je     131b <sx1276_dump+0x18b>
lea    -0x1d0(%rbp),%rdi
mov    -0x1f8(%rbp),%rcx
mov    $0x71,%esi
mov    $0x1,%edx
call   10a0 <fwrite@plt>
mov    -0x1f8(%rbp),%rdi
call   1060 <pclose@plt>
jmp    1320 <sx1276_dump+0x190>
mov    -0x1d4(%rbp),%eax
add    $0x200,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sx1276_dump>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1f0,%rsp
mov    %edi,%ebp
lea    0xe97(%rip),%rdi
call   1040 <puts@plt>
lea    0x20(%rsp),%rdi
mov    $0x5a,%esi
mov    $0x1c4,%edx
call   1070 <memset@plt>
xor    %ebx,%ebx
mov    %rsp,%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movaps 0xe29(%rip),%xmm0
movaps %xmm0,(%rsp)
movl   $0x1,0x10(%rsp)
mov    %ebx,0x14(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x6,%edx
call   1050 <write@plt>
mov    $0x1a7c,%edi
call   10b0 <usleep@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x2,%edx
call   1030 <bp_serial_readto@plt>
cmp    $0x2,%eax
jne    126d <sx1276_dump+0xdd>
cmpl   $0x1,(%rsp)
jne    126d <sx1276_dump+0xdd>
mov    0x4(%rsp),%eax
mov    %eax,0x20(%rsp,%rbx,4)
add    $0x1,%rbx
cmp    $0x71,%rbx
jne    11d0 <sx1276_dump+0x40>
lea    0xdf2(%rip),%rdi
lea    0xdff(%rip),%rsi
call   1090 <popen@plt>
test   %rax,%rax
je     1288 <sx1276_dump+0xf8>
mov    %rax,%rbp
lea    0x20(%rsp),%rdi
mov    $0x1,%ebx
mov    $0x71,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10a0 <fwrite@plt>
mov    %rbp,%rdi
call   1060 <pclose@plt>
jmp    128d <sx1276_dump+0xfd>
mov    0x2d6c(%rip),%rax
mov    (%rax),%edi
lea    0xd93(%rip),%rsi
mov    %ebx,%edx
call   1080 <fprintf@plt>
xor    %ebx,%ebx
jmp    128d <sx1276_dump+0xfd>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x1f0,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"DUMP REGS\0A\00"
@stderr = global i32 0
@.str.1 = [21 x i8] c"Dump fault at %.02x\0A\00"
@.str.2 = [20 x i8] c"od -Ax -tx1z -v -w8\00"
@.str.3 = [2 x i8] c"w\00"
define i32 @sx1276_dump(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [113 x i32]
%5 = alloca i32
%6 = alloca i32
%7 = alloca [6 x i32]
%8 = alloca i32*
store i32 %0, i32* %2
%9 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%10 = getelementptr inbounds [113 x i32], [113 x i32]* %4, i64 0, i64 0
%11 = call i32 @memset(i32* %10, i32 90, i32 452)
store i32 1, i32* %5
store i32 0, i32* %6
br label %12
12: ; preds = %46, %1
%13 = load i32, i32* %6
%14 = icmp sle i32 %13, 112
br i1 %14, label %15, label %49
15: ; preds = %12
%16 = getelementptr inbounds [6 x i32], [6 x i32]* %7, i64 0, i64 0
store i32 4, i32* %16
%17 = getelementptr inbounds i32, i32* %16, i64 1
store i32 0, i32* %17
%18 = getelementptr inbounds i32, i32* %17, i64 1
store i32 1, i32* %18
%19 = getelementptr inbounds i32, i32* %18, i64 1
store i32 0, i32* %19
%20 = getelementptr inbounds i32, i32* %19, i64 1
store i32 1, i32* %20
%21 = getelementptr inbounds i32, i32* %20, i64 1
%22 = load i32, i32* %6
store i32 %22, i32* %21
%23 = load i32, i32* %2
%24 = getelementptr inbounds [6 x i32], [6 x i32]* %7, i64 0, i64 0
%25 = call i32 @write(i32 %23, i32* %24, i32 6)
%26 = call i32 @usleep(i32 6780)
%27 = load i32, i32* %2
%28 = getelementptr inbounds [6 x i32], [6 x i32]* %7, i64 0, i64 0
%29 = call i32 @bp_serial_readto(i32 %27, i32* %28, i32 2)
store i32 %29, i32* %3
%30 = load i32, i32* %3
%31 = icmp ne i32 %30, 2
br i1 %31, label %36, label %32
32: ; preds = %15
%33 = getelementptr inbounds [6 x i32], [6 x i32]* %7, i64 0, i64 0
%34 = load i32, i32* %33
%35 = icmp ne i32 %34, 1
br i1 %35, label %36, label %40
36: ; preds = %32, %15
store i32 0, i32* %5
%37 = load i32, i32* @stderr
%38 = load i32, i32* %6
%39 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %38)
br label %49
40: ; preds = %32
%41 = getelementptr inbounds [6 x i32], [6 x i32]* %7, i64 0, i64 1
%42 = load i32, i32* %41
%43 = load i32, i32* %6
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds [113 x i32], [113 x i32]* %4, i64 0, i64 %44
store i32 %42, i32* %45
br label %46
46: ; preds = %40
%47 = load i32, i32* %6
%48 = add nsw i32 %47, 1
store i32 %48, i32* %6
br label %12
49: ; preds = %36, %12
%50 = load i32, i32* %5
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %63
52: ; preds = %49
%53 = call i32* @popen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %53, i32** %8
%54 = load i32*, i32** %8
%55 = icmp ne i32* %54, null
br i1 %55, label %56, label %62
56: ; preds = %52
%57 = getelementptr inbounds [113 x i32], [113 x i32]* %4, i64 0, i64 0
%58 = load i32*, i32** %8
%59 = call i32 @fwrite(i32* %57, i32 113, i32 1, i32* %58)
%60 = load i32*, i32** %8
%61 = call i32 @pclose(i32* %60)
br label %62
62: ; preds = %56, %52
br label %63
63: ; preds = %62, %49
%64 = load i32, i32* %5
ret i32 %64
}

LLVM IR (-O2):
@stderr = global i32 0
@.str.1 = [21 x i8] c"Dump fault at %.02x\0A\00"
@.str.2 = [20 x i8] c"od -Ax -tx1z -v -w8\00"
@.str.3 = [2 x i8] c"w\00"
@str = [10 x i8] c"DUMP REGS\00"
define i32 @sx1276_dump(i32 %0) {
%2 = alloca [113 x i32]
%3 = alloca [6 x i32]
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
%5 = bitcast [113 x i32]* %2 to i8*
%6 = getelementptr inbounds [113 x i32], [113 x i32]* %2, i64 0, i64 0
%7 = call i32 @memset(i32* %6, i32 90, i32 452)
%8 = bitcast [6 x i32]* %3 to i8*
%9 = getelementptr inbounds [6 x i32], [6 x i32]* %3, i64 0, i64 0
%10 = getelementptr inbounds [6 x i32], [6 x i32]* %3, i64 0, i64 1
%11 = getelementptr inbounds [6 x i32], [6 x i32]* %3, i64 0, i64 4
%12 = getelementptr inbounds [6 x i32], [6 x i32]* %3, i64 0, i64 5
%13 = bitcast [6 x i32]* %3 to <4 x i32>*
br label %14
14: ; preds = %1, %28
%15 = phi i64 [ 0, %1 ], [ %31, %28 ]
store <4 x i32> <i32 4, i32 0, i32 1, i32 0>, <4 x i32>* %13
store i32 1, i32* %11
%16 = trunc i64 %15 to i32
store i32 %16, i32* %12
%17 = call i32 @write(i32 %0, i32* %9, i32 6)
%18 = call i32 @usleep(i32 6780)
%19 = call i32 @bp_serial_readto(i32 %0, i32* %9, i32 2)
%20 = icmp ne i32 %19, 2
%21 = load i32, i32* %9
%22 = icmp ne i32 %21, 1
%23 = select i1 %20, i1 true, i1 %22
br i1 %23, label %24, label %28
24: ; preds = %14
%25 = trunc i64 %15 to i32
%26 = load i32, i32* @stderr
%27 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %25)
br label %39
28: ; preds = %14
%29 = load i32, i32* %10
%30 = getelementptr inbounds [113 x i32], [113 x i32]* %2, i64 0, i64 %15
store i32 %29, i32* %30
%31 = add nuw nsw i64 %15, 1
%32 = icmp eq i64 %31, 113
br i1 %32, label %33, label %14
33: ; preds = %28
%34 = call i32* @popen(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%35 = icmp eq i32* %34, null
br i1 %35, label %39, label %36
36: ; preds = %33
%37 = call i32 @fwrite(i32* %6, i32 113, i32 1, i32* %34)
%38 = call i32 @pclose(i32* %34)
br label %39
39: ; preds = %24, %33, %36
%40 = phi i32 [ 0, %24 ], [ 1, %33 ], [ 1, %36 ]
ret i32 %40
}