#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*/ cmd;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
scalar_t__ check_rdismb();
int /*<<< orphan*/ enable_wen();
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ spi_transfer(int *, int);
int /*<<< orphan*/ stderr;
void cmd_lock() {
  uint8_t cmd[1];
  if (check_rdismb()) {
    fprintf(stderr, "flash memory already protected\n");
    return;
  }
  enable_wen();
  printf("Trying to lock memory...\n");
  cmd[0] = 0x85;
  spi_transfer(cmd, sizeof(cmd));
  if (!check_rdismb()) {
    fprintf(stderr, "failed to lock memory\n");
    exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<cmd_lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    $0x0,%al
call   1070 <check_rdismb@plt>
cmp    $0x0,%rax
je     1193 <cmd_lock+0x33>
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
lea    0xe77(%rip),%rsi
call   1040 <fprintf@plt>
jmp    11ef <cmd_lock+0x8f>
mov    $0x0,%al
call   1080 <enable_wen@plt>
lea    0xe7f(%rip),%rdi
call   1030 <printf@plt>
movl   $0x85,-0x4(%rbp)
lea    -0x4(%rbp),%rdi
mov    $0x4,%esi
call   1050 <spi_transfer@plt>
mov    $0x0,%al
call   1070 <check_rdismb@plt>
cmp    $0x0,%rax
jne    11ef <cmd_lock+0x8f>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
lea    0xe5e(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_lock>:
push   %rax
xor    %eax,%eax
call   1070 <check_rdismb@plt>
test   %rax,%rax
je     1183 <cmd_lock+0x23>
mov    0x2e6c(%rip),%rax
mov    (%rax),%edi
lea    0xe83(%rip),%rsi
pop    %rax
jmp    1040 <fprintf@plt>
xor    %eax,%eax
call   1080 <enable_wen@plt>
lea    0xea6(%rip),%rdi
call   1030 <puts@plt>
movl   $0x85,0x4(%rsp)
lea    0x4(%rsp),%rdi
mov    $0x4,%esi
call   1050 <spi_transfer@plt>
xor    %eax,%eax
call   1070 <check_rdismb@plt>
test   %rax,%rax
je     11bb <cmd_lock+0x5b>
pop    %rax
ret
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
lea    0xe55(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [32 x i8] c"flash memory already protected\0A\00"
@.str.1 = [26 x i8] c"Trying to lock memory...\0A\00"
@.str.2 = [23 x i8] c"failed to lock memory\0A\00"
@EXIT_FAILURE = global i32 0
define void @cmd_lock() {
%1 = alloca [1 x i32]
%2 = call i64 (...) @check_rdismb()
%3 = icmp ne i64 %2, 0
br i1 %3, label %4, label %7
4: ; preds = %0
%5 = load i32, i32* @stderr
%6 = call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %20
7: ; preds = %0
%8 = call i32 (...) @enable_wen()
%9 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%10 = getelementptr inbounds [1 x i32], [1 x i32]* %1, i64 0, i64 0
store i32 133, i32* %10
%11 = getelementptr inbounds [1 x i32], [1 x i32]* %1, i64 0, i64 0
%12 = call i32 @spi_transfer(i32* %11, i32 4)
%13 = call i64 (...) @check_rdismb()
%14 = icmp ne i64 %13, 0
br i1 %14, label %20, label %15
15: ; preds = %7
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %4, %7
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [32 x i8] c"flash memory already protected\0A\00"
@.str.2 = [23 x i8] c"failed to lock memory\0A\00"
@EXIT_FAILURE = global i32 0
@str = [25 x i8] c"Trying to lock memory...\00"
define void @cmd_lock() {
%1 = alloca [1 x i32]
%2 = bitcast [1 x i32]* %1 to i8*
%3 = tail call i64 (...) @check_rdismb()
%4 = icmp eq i64 %3, 0
br i1 %4, label %8, label %5
5: ; preds = %0
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %20
8: ; preds = %0
%9 = tail call i32 (...) @enable_wen()
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%11 = getelementptr inbounds [1 x i32], [1 x i32]* %1, i64 0, i64 0
store i32 133, i32* %11
%12 = call i32 @spi_transfer(i32* %11, i32 4)
%13 = call i64 (...) @check_rdismb()
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %8
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %8, %5
ret void
}