#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 /*<<< orphan*/ __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 /*<<< orphan*/ print_writeprotect_status(int /*<<< orphan*/ *);
int read_extcsd(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int do_writeprotect_get(int nargs, char **argv) {
  __u8 ext_csd[512];
  int fd, ret;
  char *device;
  {
    if (nargs != 2) {
      fprintf(stderr, "Usage: mmc writeprotect get </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);
  }
  print_writeprotect_status(ext_csd);
  return ret;
}


x86 assembly (clang, -O0):
<do_writeprotect_get>:
push   %rbp
mov    %rsp,%rbp
sub    $0x820,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x4(%rbp)
je     119d <do_writeprotect_get+0x3d>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
lea    0xe74(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x820(%rbp)
mov    -0x820(%rbp),%rdi
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x814(%rbp)
cmpl   $0x0,-0x814(%rbp)
jge    11ea <do_writeprotect_get+0x8a>
lea    0xe55(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x814(%rbp),%edi
lea    -0x810(%rbp),%rsi
call   1040 <read_extcsd@plt>
mov    %eax,-0x818(%rbp)
cmpl   $0x0,-0x818(%rbp)
je     1237 <do_writeprotect_get+0xd7>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
mov    -0x820(%rbp),%rdx
lea    0xe0f(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    -0x810(%rbp),%rdi
call   1080 <print_writeprotect_status@plt>
mov    -0x818(%rbp),%eax
add    $0x820,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_writeprotect_get>:
push   %rbx
sub    $0x800,%rsp
cmp    $0x2,%edi
jne    11a7 <do_writeprotect_get+0x47>
mov    0x8(%rsi),%rbx
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1050 <open@plt>
test   %eax,%eax
js     11c8 <do_writeprotect_get+0x68>
mov    %rsp,%rsi
mov    %eax,%edi
call   1040 <read_extcsd@plt>
test   %eax,%eax
jne    11de <do_writeprotect_get+0x7e>
mov    %rsp,%rdi
call   1080 <print_writeprotect_status@plt>
xor    %eax,%eax
add    $0x800,%rsp
pop    %rbx
ret
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
lea    0xe49(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe61(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
lea    0xe47(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [48 x i8] c"Usage: mmc writeprotect get </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"
define i32 @do_writeprotect_get(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca [512 x i32]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i32 %0, i32* %3
store i8** %1, i8*** %4
%9 = load i32, i32* %3
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = load i8**, i8*** %4
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
store i8* %18, i8** %8
%19 = load i8*, i8** %8
%20 = load i32, i32* @O_RDWR
%21 = call i32 @open(i8* %19, i32 %20)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %15
%25 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %15
%28 = load i32, i32* %6
%29 = getelementptr inbounds [512 x i32], [512 x i32]* %5, i64 0, i64 0
%30 = call i32 @read_extcsd(i32 %28, i32* %29)
store i32 %30, i32* %7
%31 = load i32, i32* %7
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %27
%34 = load i32, i32* @stderr
%35 = load i8*, i8** %8
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %35)
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %27
%39 = getelementptr inbounds [512 x i32], [512 x i32]* %5, i64 0, i64 0
%40 = call i32 @print_writeprotect_status(i32* %39)
%41 = load i32, i32* %7
ret i32 %41
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [48 x i8] c"Usage: mmc writeprotect get </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"
define i32 @do_writeprotect_get(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 ([48 x i8], [48 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 = call i32 @print_writeprotect_status(i32* %20)
ret i32 0
}