#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_writeprotect_set(int nargs, char **argv) {
  __u8 ext_csd[512], value;
  int fd, ret;
  char *device;
  {
    if (nargs != 2) {
      fprintf(stderr, "Usage: mmc writeprotect set </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);
  }
  value = ext_csd[173] | (0x01);
  ret = write_extcsd_value(fd, 173, value);
  if (ret) {
    fprintf(stderr,
            "Could not write 0x%02x to "
            "EXT_CSD[%d] in %s\n",
            value, 173, device);
    exit(1);
  }
  return ret;
}


x86 assembly (clang, -O0):
<do_writeprotect_set>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x4(%rbp)
je     119d <do_writeprotect_set+0x3d>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
lea    0xe74(%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    0x2e2e(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x818(%rbp)
cmpl   $0x0,-0x818(%rbp)
jge    11ea <do_writeprotect_set+0x8a>
lea    0xe55(%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     1237 <do_writeprotect_set+0xd7>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
mov    -0x828(%rbp),%rdx
lea    0xe0f(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x55c(%rbp),%eax
or     $0x1,%eax
mov    %eax,-0x814(%rbp)
mov    -0x818(%rbp),%edi
mov    -0x814(%rbp),%edx
mov    $0xad,%esi
call   1040 <write_extcsd_value@plt>
mov    %eax,-0x81c(%rbp)
cmpl   $0x0,-0x81c(%rbp)
je     12a2 <do_writeprotect_set+0x142>
mov    0x2d62(%rip),%rax
mov    (%rax),%edi
mov    -0x814(%rbp),%edx
mov    -0x828(%rbp),%r8
lea    0xdc9(%rip),%rsi
mov    $0xad,%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_writeprotect_set>:
push   %rbp
push   %r14
push   %rbx
sub    $0x800,%rsp
cmp    $0x2,%edi
jne    11c3 <do_writeprotect_set+0x63>
mov    0x8(%rsi),%r14
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1060 <open@plt>
test   %eax,%eax
js     11e4 <do_writeprotect_set+0x84>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
call   1050 <read_extcsd@plt>
test   %eax,%eax
jne    11fa <do_writeprotect_set+0x9a>
mov    0x2b4(%rsp),%ebp
or     $0x1,%ebp
mov    %ebx,%edi
mov    $0xad,%esi
mov    %ebp,%edx
call   1040 <write_extcsd_value@plt>
test   %eax,%eax
jne    121e <do_writeprotect_set+0xbe>
xor    %eax,%eax
add    $0x800,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
lea    0xe2d(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xe45(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2dd7(%rip),%rax
mov    (%rax),%edi
lea    0xe2b(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
lea    0xe27(%rip),%rsi
mov    %ebp,%edx
mov    $0xad,%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 = [48 x i8] c"Usage: mmc writeprotect set </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"
define i32 @do_writeprotect_set(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
%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 ([48 x i8], [48 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 173
%41 = load i32, i32* %40
%42 = or i32 %41, 1
store i32 %42, i32* %6
%43 = load i32, i32* %7
%44 = load i32, i32* %6
%45 = call i32 @write_extcsd_value(i32 %43, i32 173, i32 %44)
store i32 %45, i32* %8
%46 = load i32, i32* %8
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %54
48: ; preds = %39
%49 = load i32, i32* @stderr
%50 = load i32, i32* %6
%51 = load i8*, i8** %9
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0), i32 %50, i32 173, i8* %51)
%53 = call i32 @exit(i32 1)
unreachable
54: ; preds = %39
%55 = load i32, i32* %8
ret i32 %55
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [48 x i8] c"Usage: mmc writeprotect set </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"
define i32 @do_writeprotect_set(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 = getelementptr inbounds [512 x i32], [512 x i32]* %3, i64 0, i64 173
%29 = load i32, i32* %28
%30 = or i32 %29, 1
%31 = call i32 @write_extcsd_value(i32 %14, i32 173, i32 %30)
%32 = icmp eq i32 %31, 0
br i1 %32, label %37, label %33
33: ; preds = %27
%34 = load i32, i32* @stderr
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0), i32 %30, i32 173, i8* %12)
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %27
ret i32 0
}