#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 /*<<< orphan*/ printf(char *);
int read_extcsd(int, int *);
int /*<<< orphan*/ stderr;
int write_extcsd_value(int, int, int);
int do_disable_512B_emulation(int nargs, char **argv) {
  __u8 ext_csd[512], native_sector_size, data_sector_size, wr_rel_param;
  int fd, ret;
  char *device;
  {
    if (nargs != 2) {
      fprintf(stderr, "Usage: mmc disable 512B emulation </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);
  }
  wr_rel_param = ext_csd[166];
  native_sector_size = ext_csd[63];
  data_sector_size = ext_csd[61];
  if (native_sector_size && !data_sector_size && (wr_rel_param & (1 << 2))) {
    ret = write_extcsd_value(fd, 62, 1);
    if (ret) {
      fprintf(stderr, "Could not write 0x%02x to EXT_CSD[%d] in %s\n", 1, 173,
              device);
      exit(1);
    }
    printf("MMC disable 512B emulation successful.  Now reset the device to "
           "switch to 4KB native sector mode.\n");
  } else if (native_sector_size && data_sector_size) {
    printf("MMC 512B emulation mode is already disabled; doing nothing.\n");
  } else {
    printf("MMC does not support disabling 512B emulation mode.\n");
  }
  return ret;
}


x86 assembly (clang, -O0):
<do_disable_512B_emulation>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x4(%rbp)
je     11ad <do_disable_512B_emulation+0x3d>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
lea    0xe64(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x830(%rbp)
mov    -0x830(%rbp),%rdi
mov    0x2e1e(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x820(%rbp)
cmpl   $0x0,-0x820(%rbp)
jge    11fa <do_disable_512B_emulation+0x8a>
lea    0xe4b(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x820(%rbp),%edi
lea    -0x810(%rbp),%rsi
call   1060 <read_extcsd@plt>
mov    %eax,-0x824(%rbp)
cmpl   $0x0,-0x824(%rbp)
je     1247 <do_disable_512B_emulation+0xd7>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
mov    -0x830(%rbp),%rdx
lea    0xe05(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x578(%rbp),%eax
mov    %eax,-0x81c(%rbp)
mov    -0x714(%rbp),%eax
mov    %eax,-0x814(%rbp)
mov    -0x71c(%rbp),%eax
mov    %eax,-0x818(%rbp)
cmpl   $0x0,-0x814(%rbp)
je     1302 <do_disable_512B_emulation+0x192>
cmpl   $0x0,-0x818(%rbp)
jne    1302 <do_disable_512B_emulation+0x192>
mov    -0x81c(%rbp),%eax
and    $0x4,%eax
cmp    $0x0,%eax
je     1302 <do_disable_512B_emulation+0x192>
mov    -0x820(%rbp),%edi
mov    $0x3e,%esi
mov    $0x1,%edx
call   1050 <write_extcsd_value@plt>
mov    %eax,-0x824(%rbp)
cmpl   $0x0,-0x824(%rbp)
je     12f1 <do_disable_512B_emulation+0x181>
mov    0x2d12(%rip),%rax
mov    (%rax),%edi
mov    -0x830(%rbp),%r8
lea    0xd85(%rip),%rsi
mov    $0x1,%edx
mov    $0xad,%ecx
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xd90(%rip),%rdi
call   1030 <printf@plt>
jmp    133e <do_disable_512B_emulation+0x1ce>
cmpl   $0x0,-0x814(%rbp)
je     132d <do_disable_512B_emulation+0x1bd>
cmpl   $0x0,-0x818(%rbp)
je     132d <do_disable_512B_emulation+0x1bd>
lea    0xdc8(%rip),%rdi
call   1030 <printf@plt>
jmp    1339 <do_disable_512B_emulation+0x1c9>
lea    0xdf4(%rip),%rdi
call   1030 <printf@plt>
jmp    133e <do_disable_512B_emulation+0x1ce>
mov    -0x824(%rbp),%eax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_disable_512B_emulation>:
push   %rbp
push   %rbx
sub    $0x808,%rsp
cmp    $0x2,%edi
jne    121c <do_disable_512B_emulation+0xac>
mov    0x8(%rsi),%rbx
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     123d <do_disable_512B_emulation+0xcd>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
call   1060 <read_extcsd@plt>
test   %eax,%eax
jne    1253 <do_disable_512B_emulation+0xe3>
mov    0xf4(%rsp),%ecx
mov    0xfc(%rsp),%eax
test   %eax,%eax
je     11d3 <do_disable_512B_emulation+0x63>
test   %ecx,%ecx
jne    11d3 <do_disable_512B_emulation+0x63>
testb  $0x4,0x298(%rsp)
04
jne    11ef <do_disable_512B_emulation+0x7f>
test   %ecx,%ecx
lea    0xee0(%rip),%rdi
lea    0xea5(%rip),%rcx
cmove  %rcx,%rdi
test   %eax,%eax
cmove  %rcx,%rdi
jmp    120b <do_disable_512B_emulation+0x9b>
mov    %ebp,%edi
mov    $0x3e,%esi
mov    $0x1,%edx
call   1050 <write_extcsd_value@plt>
test   %eax,%eax
jne    1277 <do_disable_512B_emulation+0x107>
lea    0xeed(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x808,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
lea    0xdd4(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xdf2(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
lea    0xdd8(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
lea    0xdd4(%rip),%rsi
mov    $0x1,%edx
mov    $0xad,%ecx
mov    %rbx,%r8
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [54 x i8] c"Usage: mmc disable 512B emulation </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 = [45 x i8] c"Could not write 0x%02x to EXT_CSD[%d] in %s\0A\00"
@.str.4 = [99 x i8] c"MMC disable 512B emulation successful. Now reset the device to switch to 4KB native sector mode.\0A\00"
@.str.5 = [61 x i8] c"MMC 512B emulation mode is already disabled; doing nothing.\0A\00"
@.str.6 = [53 x i8] c"MMC does not support disabling 512B emulation mode.\0A\00"
define i32 @do_disable_512B_emulation(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 i32
%10 = alloca i32
%11 = alloca i8*
store i32 %0, i32* %3
store i8** %1, i8*** %4
%12 = load i32, i32* %3
%13 = icmp ne i32 %12, 2
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i8**, i8*** %4
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
store i8* %21, i8** %11
%22 = load i8*, i8** %11
%23 = load i32, i32* @O_RDWR
%24 = call i32 @open(i8* %22, i32 %23)
store i32 %24, i32* %9
%25 = load i32, i32* %9
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %18
%28 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %18
%31 = load i32, i32* %9
%32 = getelementptr inbounds [512 x i32], [512 x i32]* %5, i64 0, i64 0
%33 = call i32 @read_extcsd(i32 %31, i32* %32)
store i32 %33, i32* %10
%34 = load i32, i32* %10
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %30
%37 = load i32, i32* @stderr
%38 = load i8*, i8** %11
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %38)
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %30
%42 = getelementptr inbounds [512 x i32], [512 x i32]* %5, i64 0, i64 166
%43 = load i32, i32* %42
store i32 %43, i32* %8
%44 = getelementptr inbounds [512 x i32], [512 x i32]* %5, i64 0, i64 63
%45 = load i32, i32* %44
store i32 %45, i32* %6
%46 = getelementptr inbounds [512 x i32], [512 x i32]* %5, i64 0, i64 61
%47 = load i32, i32* %46
store i32 %47, i32* %7
%48 = load i32, i32* %6
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %69
50: ; preds = %41
%51 = load i32, i32* %7
%52 = icmp ne i32 %51, 0
br i1 %52, label %69, label %53
53: ; preds = %50
%54 = load i32, i32* %8
%55 = and i32 %54, 4
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %69
57: ; preds = %53
%58 = load i32, i32* %9
%59 = call i32 @write_extcsd_value(i32 %58, i32 62, i32 1)
store i32 %59, i32* %10
%60 = load i32, i32* %10
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %67
62: ; preds = %57
%63 = load i32, i32* @stderr
%64 = load i8*, i8** %11
%65 = call i32 (i32, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0), i32 1, i32 173, i8* %64)
%66 = call i32 @exit(i32 1)
unreachable
67: ; preds = %57
%68 = call i32 @printf(i8* getelementptr inbounds ([99 x i8], [99 x i8]* @.str.4, i64 0, i64 0))
br label %80
69: ; preds = %53, %50, %41
%70 = load i32, i32* %6
%71 = icmp ne i32 %70, 0
br i1 %71, label %72, label %77
72: ; preds = %69
%73 = load i32, i32* %7
%74 = icmp ne i32 %73, 0
br i1 %74, label %75, label %77
75: ; preds = %72
%76 = call i32 @printf(i8* getelementptr inbounds ([61 x i8], [61 x i8]* @.str.5, i64 0, i64 0))
br label %79
77: ; preds = %72, %69
%78 = call i32 @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.6, i64 0, i64 0))
br label %79
79: ; preds = %77, %75
br label %80
80: ; preds = %79, %67
%81 = load i32, i32* %10
ret i32 %81
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [54 x i8] c"Usage: mmc disable 512B emulation </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 = [45 x i8] c"Could not write 0x%02x to EXT_CSD[%d] in %s\0A\00"
@str = [52 x i8] c"MMC does not support disabling 512B emulation mode.\00"
@str.7 = [60 x i8] c"MMC 512B emulation mode is already disabled; doing nothing.\00"
@str.8 = [98 x i8] c"MMC disable 512B emulation successful. Now reset the device to switch to 4KB native sector mode.\00"
define i32 @do_disable_512B_emulation(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 ([54 x i8], [54 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 63
%29 = load i32, i32* %28
%30 = getelementptr inbounds [512 x i32], [512 x i32]* %3, i64 0, i64 61
%31 = load i32, i32* %30
%32 = icmp eq i32 %29, 0
%33 = icmp ne i32 %31, 0
%34 = select i1 %32, i1 true, i1 %33
br i1 %34, label %47, label %35
35: ; preds = %27
%36 = getelementptr inbounds [512 x i32], [512 x i32]* %3, i64 0, i64 166
%37 = load i32, i32* %36
%38 = and i32 %37, 4
%39 = icmp eq i32 %38, 0
br i1 %39, label %47, label %40
40: ; preds = %35
%41 = call i32 @write_extcsd_value(i32 %14, i32 62, i32 1)
%42 = icmp eq i32 %41, 0
br i1 %42, label %51, label %43
43: ; preds = %40
%44 = load i32, i32* @stderr
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0), i32 1, i32 173, i8* %12)
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %35, %27
%48 = icmp ne i32 %29, 0
%49 = select i1 %48, i1 %33, i1 false
%50 = select i1 %49, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @str.7, i64 0, i64 0), i8* getelementptr inbounds ([52 x i8], [52 x i8]* @str, i64 0, i64 0)
br label %51
51: ; preds = %47, %40
%52 = phi i8* [ getelementptr inbounds ([98 x i8], [98 x i8]* @str.8, i64 0, i64 0), %40 ], [ %50, %47 ]
%53 = call i32 @puts(i8* dereferenceable(1) %52)
ret i32 0
}