#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_hwreset(int value, int nargs, char **argv) {
  __u8 ext_csd[512];
  int fd, ret;
  char *device;
  {
    if (nargs != 2) {
      fprintf(stderr, "Usage: mmc hwreset 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[162] & (0x03)) == (0x01)) {
    fprintf(stderr, "H/W Reset is already permanently enabled on %s\n", device);
    exit(1);
  }
  if ((ext_csd[162] & (0x03)) == (0x02)) {
    fprintf(stderr, "H/W Reset is already permanently disabled on %s\n",
            device);
    exit(1);
  }
  ret = write_extcsd_value(fd, 162, value);
  if (ret) {
    fprintf(stderr, "Could not write 0x%02x to EXT_CSD[%d] in %s\n", value, 162,
            device);
    exit(1);
  }
  return ret;
}


x86 assembly (clang, -O0):
<do_hwreset>:
push   %rbp
mov    %rsp,%rbp
sub    $0x820,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11a0 <do_hwreset+0x40>
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
lea    0xe71(%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,-0x820(%rbp)
mov    -0x820(%rbp),%rdi
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x814(%rbp)
cmpl   $0x0,-0x814(%rbp)
jge    11ed <do_hwreset+0x8d>
lea    0xe50(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x814(%rbp),%edi
lea    -0x810(%rbp),%rsi
call   1050 <read_extcsd@plt>
mov    %eax,-0x818(%rbp)
cmpl   $0x0,-0x818(%rbp)
je     123a <do_hwreset+0xda>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
mov    -0x820(%rbp),%rdx
lea    0xe0a(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x588(%rbp),%eax
and    $0x3,%eax
cmp    $0x1,%eax
jne    1274 <do_hwreset+0x114>
mov    0x2d85(%rip),%rax
mov    (%rax),%edi
mov    -0x820(%rbp),%rdx
lea    0xdf0(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x588(%rbp),%eax
and    $0x3,%eax
cmp    $0x2,%eax
jne    12ae <do_hwreset+0x14e>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
mov    -0x820(%rbp),%rdx
lea    0xde6(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x814(%rbp),%edi
mov    -0x4(%rbp),%edx
mov    $0xa2,%esi
call   1040 <write_extcsd_value@plt>
mov    %eax,-0x818(%rbp)
cmpl   $0x0,-0x818(%rbp)
je     1304 <do_hwreset+0x1a4>
mov    0x2cfd(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
mov    -0x820(%rbp),%r8
lea    0xdc6(%rip),%rsi
mov    $0xa2,%ecx
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x818(%rbp),%eax
add    $0x820,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_hwreset>:
push   %rbp
push   %r14
push   %rbx
sub    $0x800,%rsp
cmp    $0x2,%esi
jne    11d3 <do_hwreset+0x73>
mov    %edi,%ebp
mov    0x8(%rdx),%r14
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11f4 <do_hwreset+0x94>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
call   1050 <read_extcsd@plt>
test   %eax,%eax
jne    120a <do_hwreset+0xaa>
mov    0x288(%rsp),%eax
and    $0x3,%eax
cmp    $0x2,%eax
je     121c <do_hwreset+0xbc>
cmp    $0x1,%eax
je     122e <do_hwreset+0xce>
mov    %ebx,%edi
mov    $0xa2,%esi
mov    %ebp,%edx
call   1040 <write_extcsd_value@plt>
test   %eax,%eax
jne    1252 <do_hwreset+0xf2>
xor    %eax,%eax
add    $0x800,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2dfe(%rip),%rax
mov    (%rax),%edi
lea    0xe1d(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe33(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
lea    0xe19(%rip),%rsi
jmp    123e <do_hwreset+0xde>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
lea    0xe57(%rip),%rsi
jmp    123e <do_hwreset+0xde>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
lea    0xe15(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
lea    0xe52(%rip),%rsi
mov    %ebp,%edx
mov    $0xa2,%ecx
mov    %r14,%r8
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [46 x i8] c"Usage: mmc hwreset 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 = [48 x i8] c"H/W Reset is already permanently enabled on %s\0A\00"
@.str.4 = [49 x i8] c"H/W Reset is already permanently disabled on %s\0A\00"
@.str.5 = [45 x i8] c"Could not write 0x%02x to EXT_CSD[%d] in %s\0A\00"
define i32 @do_hwreset(i32 %0, i32 %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca [512 x i32]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
store i32 %0, i32* %4
store i32 %1, i32* %5
store i8** %2, i8*** %6
%11 = load i32, i32* %5
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %17
13: ; preds = %3
%14 = load i32, i32* @stderr
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %3
%18 = load i8**, i8*** %6
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
store i8* %20, i8** %10
%21 = load i8*, i8** %10
%22 = load i32, i32* @O_RDWR
%23 = call i32 @open(i8* %21, i32 %22)
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %17
%27 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %17
%30 = load i32, i32* %8
%31 = getelementptr inbounds [512 x i32], [512 x i32]* %7, i64 0, i64 0
%32 = call i32 @read_extcsd(i32 %30, i32* %31)
store i32 %32, i32* %9
%33 = load i32, i32* %9
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %29
%36 = load i32, i32* @stderr
%37 = load i8*, i8** %10
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %37)
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %29
%41 = getelementptr inbounds [512 x i32], [512 x i32]* %7, i64 0, i64 162
%42 = load i32, i32* %41
%43 = and i32 %42, 3
%44 = icmp eq i32 %43, 1
br i1 %44, label %45, label %50
45: ; preds = %40
%46 = load i32, i32* @stderr
%47 = load i8*, i8** %10
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i8* %47)
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %40
%51 = getelementptr inbounds [512 x i32], [512 x i32]* %7, i64 0, i64 162
%52 = load i32, i32* %51
%53 = and i32 %52, 3
%54 = icmp eq i32 %53, 2
br i1 %54, label %55, label %60
55: ; preds = %50
%56 = load i32, i32* @stderr
%57 = load i8*, i8** %10
%58 = call i32 (i32, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.4, i64 0, i64 0), i8* %57)
%59 = call i32 @exit(i32 1)
unreachable
60: ; preds = %50
%61 = load i32, i32* %8
%62 = load i32, i32* %4
%63 = call i32 @write_extcsd_value(i32 %61, i32 162, i32 %62)
store i32 %63, i32* %9
%64 = load i32, i32* %9
%65 = icmp ne i32 %64, 0
br i1 %65, label %66, label %72
66: ; preds = %60
%67 = load i32, i32* @stderr
%68 = load i32, i32* %4
%69 = load i8*, i8** %10
%70 = call i32 (i32, i8*, ...) @fprintf(i32 %67, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.5, i64 0, i64 0), i32 %68, i32 162, i8* %69)
%71 = call i32 @exit(i32 1)
unreachable
72: ; preds = %60
%73 = load i32, i32* %9
ret i32 %73
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [46 x i8] c"Usage: mmc hwreset 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 = [48 x i8] c"H/W Reset is already permanently enabled on %s\0A\00"
@.str.4 = [49 x i8] c"H/W Reset is already permanently disabled on %s\0A\00"
@.str.5 = [45 x i8] c"Could not write 0x%02x to EXT_CSD[%d] in %s\0A\00"
define i32 @do_hwreset(i32 %0, i32 %1, i8** nocapture %2) {
%4 = alloca [512 x i32]
%5 = bitcast [512 x i32]* %4 to i8*
%6 = icmp eq i32 %1, 2
br i1 %6, label %11, label %7
7: ; preds = %3
%8 = load i32, i32* @stderr
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %3
%12 = getelementptr inbounds i8*, i8** %2, i64 1
%13 = load i8*, i8** %12
%14 = load i32, i32* @O_RDWR
%15 = tail call i32 @open(i8* %13, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %11
%18 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %11
%21 = getelementptr inbounds [512 x i32], [512 x i32]* %4, i64 0, i64 0
%22 = call i32 @read_extcsd(i32 %15, i32* %21)
%23 = icmp eq i32 %22, 0
br i1 %23, label %28, label %24
24: ; preds = %20
%25 = load i32, i32* @stderr
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %13)
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %20
%29 = getelementptr inbounds [512 x i32], [512 x i32]* %4, i64 0, i64 162
%30 = load i32, i32* %29
%31 = and i32 %30, 3
switch i32 %31, label %40 [
i32 1, label %32
i32 2, label %36
]
32: ; preds = %28
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.3, i64 0, i64 0), i8* %13)
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %28
%37 = load i32, i32* @stderr
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.4, i64 0, i64 0), i8* %13)
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %28
%41 = call i32 @write_extcsd_value(i32 %15, i32 162, i32 %0)
%42 = icmp eq i32 %41, 0
br i1 %42, label %47, 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.5, i64 0, i64 0), i32 %0, i32 162, i8* %13)
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %40
ret i32 0
}