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

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read_extcsd(int, int *);
int /*<<< orphan*/ stderr;
int write_extcsd_value(int, int, int);
int do_write_bkops_en(int nargs, char **argv) {
  __u8 ext_csd[512], value = 0;
  int fd, ret;
  char *device;
  {
    if (nargs != 2) {
      fprintf(stderr, "Usage: mmc bkops enable </path/to/mmcblkX>\n");
      exit(1);
    }
  };
  device = argv[1];
  fd = open(device, O_RDWR);
  if (fd < 0) {
    perror("open");
    exit(1);
  }
  ret = read_extcsd(fd, ext_csd);
  if (ret) {
    fprintf(stderr, "Could not read EXT_CSD from %s\n", device);
    exit(1);
  }
  if (!(ext_csd[502] & 0x1)) {
    fprintf(stderr, "%s doesn't support BKOPS\n", device);
    exit(1);
  }
  ret = write_extcsd_value(fd, 163, (1 << 0));
  if (ret) {
    fprintf(stderr, "Could not write 0x%02x to EXT_CSD[%d] in %s\n", value, 163,
            device);
    exit(1);
  }
  return ret;
}


x86 assembly (clang, -O0):
<do_write_bkops_en>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x814(%rbp)
cmpl   $0x2,-0x4(%rbp)
je     11a7 <do_write_bkops_en+0x47>
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
lea    0xe6a(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x828(%rbp)
mov    -0x828(%rbp),%rdi
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x818(%rbp)
cmpl   $0x0,-0x818(%rbp)
jge    11f4 <do_write_bkops_en+0x94>
lea    0xe47(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x818(%rbp),%edi
lea    -0x810(%rbp),%rsi
call   1050 <read_extcsd@plt>
mov    %eax,-0x81c(%rbp)
cmpl   $0x0,-0x81c(%rbp)
je     1241 <do_write_bkops_en+0xe1>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
mov    -0x828(%rbp),%rdx
lea    0xe01(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x38(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
jne    1278 <do_write_bkops_en+0x118>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
mov    -0x828(%rbp),%rdx
lea    0xdea(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x818(%rbp),%edi
mov    $0xa3,%esi
mov    $0x1,%edx
call   1040 <write_extcsd_value@plt>
mov    %eax,-0x81c(%rbp)
cmpl   $0x0,-0x81c(%rbp)
je     12d3 <do_write_bkops_en+0x173>
mov    0x2d31(%rip),%rax
mov    (%rax),%edi
mov    -0x814(%rbp),%edx
mov    -0x828(%rbp),%r8
lea    0xdae(%rip),%rsi
mov    $0xa3,%ecx
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x81c(%rbp),%eax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_write_bkops_en>:
push   %rbp
push   %rbx
sub    $0x808,%rsp
cmp    $0x2,%edi
jne    11c2 <do_write_bkops_en+0x62>
mov    0x8(%rsi),%rbx
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11e3 <do_write_bkops_en+0x83>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
call   1050 <read_extcsd@plt>
test   %eax,%eax
jne    11f9 <do_write_bkops_en+0x99>
testb  $0x1,0x7d8(%rsp)
01
je     120b <do_write_bkops_en+0xab>
mov    %ebp,%edi
mov    $0xa3,%esi
mov    $0x1,%edx
call   1040 <write_extcsd_value@plt>
test   %eax,%eax
jne    122f <do_write_bkops_en+0xcf>
xor    %eax,%eax
add    $0x808,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
lea    0xe2e(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe42(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
lea    0xe28(%rip),%rsi
jmp    121b <do_write_bkops_en+0xbb>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
lea    0xe36(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
lea    0xe2c(%rip),%rsi
xor    %edx,%edx
mov    $0xa3,%ecx
mov    %rbx,%r8
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [44 x i8] c"Usage: mmc bkops enable </path/to/mmcblkX>\0A\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [32 x i8] c"Could not read EXT_CSD from %s\0A\00"
@.str.3 = [26 x i8] c"%s doesn't support BKOPS\0A\00"
@.str.4 = [45 x i8] c"Could not write 0x%02x to EXT_CSD[%d] in %s\0A\00"
define i32 @do_write_bkops_en(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca [512 x i32]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i32 %0, i32* %3
store i8** %1, i8*** %4
store i32 0, i32* %6
%10 = load i32, i32* %3
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = load i8**, i8*** %4
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
store i8* %19, i8** %9
%20 = load i8*, i8** %9
%21 = load i32, i32* @O_RDWR
%22 = call i32 @open(i8* %20, i32 %21)
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %16
%26 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %16
%29 = load i32, i32* %7
%30 = getelementptr inbounds [512 x i32], [512 x i32]* %5, i64 0, i64 0
%31 = call i32 @read_extcsd(i32 %29, i32* %30)
store i32 %31, i32* %8
%32 = load i32, i32* %8
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %28
%35 = load i32, i32* @stderr
%36 = load i8*, i8** %9
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %36)
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %28
%40 = getelementptr inbounds [512 x i32], [512 x i32]* %5, i64 0, i64 502
%41 = load i32, i32* %40
%42 = and i32 %41, 1
%43 = icmp ne i32 %42, 0
br i1 %43, label %49, label %44
44: ; preds = %39
%45 = load i32, i32* @stderr
%46 = load i8*, i8** %9
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %46)
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %39
%50 = load i32, i32* %7
%51 = call i32 @write_extcsd_value(i32 %50, i32 163, i32 1)
store i32 %51, i32* %8
%52 = load i32, i32* %8
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %60
54: ; preds = %49
%55 = load i32, i32* @stderr
%56 = load i32, i32* %6
%57 = load i8*, i8** %9
%58 = call i32 (i32, i8*, ...) @fprintf(i32 %55, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0), i32 %56, i32 163, i8* %57)
%59 = call i32 @exit(i32 1)
unreachable
60: ; preds = %49
%61 = load i32, i32* %8
ret i32 %61
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [44 x i8] c"Usage: mmc bkops enable </path/to/mmcblkX>\0A\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [32 x i8] c"Could not read EXT_CSD from %s\0A\00"
@.str.3 = [26 x i8] c"%s doesn't support BKOPS\0A\00"
@.str.4 = [45 x i8] c"Could not write 0x%02x to EXT_CSD[%d] in %s\0A\00"
define i32 @do_write_bkops_en(i32 %0, i8** nocapture %1) {
%3 = alloca [512 x i32]
%4 = bitcast [512 x i32]* %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_RDWR
%14 = tail call i32 @open(i8* %12, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %10
%17 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%18 = tail call i32 @exit(i32 1)
unreachable
19: ; preds = %10
%20 = getelementptr inbounds [512 x i32], [512 x i32]* %3, i64 0, i64 0
%21 = call i32 @read_extcsd(i32 %14, i32* %20)
%22 = icmp eq i32 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %19
%24 = load i32, i32* @stderr
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %12)
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %19
%28 = getelementptr inbounds [512 x i32], [512 x i32]* %3, i64 0, i64 502
%29 = load i32, i32* %28
%30 = and i32 %29, 1
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %27
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %12)
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %27
%37 = call i32 @write_extcsd_value(i32 %14, i32 163, i32 1)
%38 = icmp eq i32 %37, 0
br i1 %38, label %43, label %39
39: ; preds = %36
%40 = load i32, i32* @stderr
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0), i32 0, i32 163, i8* %12)
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %36
ret i32 0
}