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

/* Variables and functions */
int EINVAL;
int EIO;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ init_monitor(int);
int /*<<< orphan*/ printf(char *);
int send_command(int, int, int /*<<< orphan*/ *, int /*<<< orphan*/,
                 int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
scalar_t__ wait_for_ack(int);
int command_write_unprotect(int fd) {
  int res;
  res = send_command(fd, 0x73, NULL, 0, NULL, 0);
  if (res < 0)
    return -EIO;
  if (wait_for_ack(fd) < 0) {
    fprintf(stderr, "Failed to get write-protect ACK\n");
    return -EINVAL;
  }
  printf("Flash write unprotected.\n");
  if (init_monitor(fd) < 0) {
    fprintf(stderr, "Cannot recover after WP reset\n");
    return -EIO;
  }
  return 0;
}


x86 assembly (clang, -O0):
<command_write_unprotect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    $0x73,%esi
xor    %eax,%eax
mov    %eax,%r8d
xor    %r9d,%r9d
mov    %r8,%rdx
mov    %r9d,%ecx
call   1060 <send_command@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    1196 <command_write_unprotect+0x46>
xor    %eax,%eax
mov    0x2e54(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    121d <command_write_unprotect+0xcd>
mov    -0x8(%rbp),%edi
call   1050 <wait_for_ack@plt>
cmp    $0x0,%rax
jge    11d0 <command_write_unprotect+0x80>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
lea    0xe48(%rip),%rsi
call   1040 <fprintf@plt>
xor    %eax,%eax
mov    0x2e0a(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    121d <command_write_unprotect+0xcd>
lea    0xe4a(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1070 <init_monitor@plt>
cmp    $0x0,%rax
jge    1216 <command_write_unprotect+0xc6>
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
lea    0xe3d(%rip),%rsi
call   1040 <fprintf@plt>
xor    %eax,%eax
mov    0x2dd4(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    121d <command_write_unprotect+0xcd>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<command_write_unprotect>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
xor    %ebx,%ebx
mov    $0x73,%esi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1060 <send_command@plt>
test   %eax,%eax
js     11b3 <command_write_unprotect+0x63>
mov    %ebp,%edi
call   1050 <wait_for_ack@plt>
test   %rax,%rax
js     11bc <command_write_unprotect+0x6c>
lea    0xebe(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1070 <init_monitor@plt>
xor    %ebx,%ebx
test   %rax,%rax
jns    11dc <command_write_unprotect+0x8c>
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
lea    0xe7c(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e2f(%rip),%rax
jmp    11d8 <command_write_unprotect+0x88>
mov    0x2e26(%rip),%rax
jmp    11da <command_write_unprotect+0x8a>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
lea    0xe34(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2df8(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@EIO = global i32 0
@stderr = global i32 0
@.str = [33 x i8] c"Failed to get write-protect ACK\0A\00"
@EINVAL = global i32 0
@.str.1 = [26 x i8] c"Flash write unprotected.\0A\00"
@.str.2 = [31 x i8] c"Cannot recover after WP reset\0A\00"
define i32 @command_write_unprotect(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = call i32 @send_command(i32 %5, i32 115, i32* null, i32 0, i32* null, i32 0)
store i32 %6, i32* %4
%7 = load i32, i32* %4
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = load i32, i32* @EIO
%11 = sub nsw i32 0, %10
store i32 %11, i32* %2
br label %32
12: ; preds = %1
%13 = load i32, i32* %3
%14 = call i64 @wait_for_ack(i32 %13)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%19 = load i32, i32* @EINVAL
%20 = sub nsw i32 0, %19
store i32 %20, i32* %2
br label %32
21: ; preds = %12
%22 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%23 = load i32, i32* %3
%24 = call i64 @init_monitor(i32 %23)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %21
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%29 = load i32, i32* @EIO
%30 = sub nsw i32 0, %29
store i32 %30, i32* %2
br label %32
31: ; preds = %21
store i32 0, i32* %2
br label %32
32: ; preds = %31, %26, %16, %9
%33 = load i32, i32* %2
ret i32 %33
}

LLVM IR (-O2):
@EIO = global i32 0
@stderr = global i32 0
@.str = [33 x i8] c"Failed to get write-protect ACK\0A\00"
@EINVAL = global i32 0
@.str.2 = [31 x i8] c"Cannot recover after WP reset\0A\00"
@str = [25 x i8] c"Flash write unprotected.\00"
define i32 @command_write_unprotect(i32 %0) {
%2 = tail call i32 @send_command(i32 %0, i32 115, i32* null, i32 0, i32* null, i32 0)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = load i32, i32* @EIO
%6 = sub nsw i32 0, %5
br label %24
7: ; preds = %1
%8 = tail call i64 @wait_for_ack(i32 %0)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %15
10: ; preds = %7
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EINVAL
%14 = sub nsw i32 0, %13
br label %24
15: ; preds = %7
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%17 = tail call i64 @init_monitor(i32 %0)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %15
%20 = load i32, i32* @stderr
%21 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%22 = load i32, i32* @EIO
%23 = sub nsw i32 0, %22
br label %24
24: ; preds = %15, %19, %10, %4
%25 = phi i32 [ %6, %4 ], [ %14, %10 ], [ %23, %19 ], [ 0, %15 ]
ret i32 %25
}