#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*/ set_partitioning_setting_completed(int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(char *, char *);
int strtol(char *, int /*<<< orphan*/ *, int);
int write_extcsd_value(int, int, int);
int do_write_reliability_set(int nargs, char **argv) {
  __u8 value;
  __u8 ext_csd[512];
  int fd, ret;
  int dry_run = 1;
  int partition;
  char *device;
  {
    if (nargs != 4) {
      fprintf(stderr, "Usage: mmc write_reliability set <-y|-n> "
                      "<partition> </path/to/mmcblkX>\n");
      exit(1);
    }
  };
  if (!strcmp("-y", argv[1]))
    dry_run = 0;
  partition = strtol(argv[2], NULL, 10);
  device = argv[3];
  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[155]) {
    printf(" Device is already partitioned\n");
    exit(1);
  }
  if (!(ext_csd[166] & (1 << 0))) {
    printf("Cannot set write reliability parameters, WR_REL_SET is "
           "read-only\n");
    exit(1);
  }
  value = ext_csd[167] | (1 << partition);
  ret = write_extcsd_value(fd, 167, value);
  if (ret) {
    fprintf(stderr, "Could not write 0x%02x to EXT_CSD[%d] in %s\n", value, 167,
            device);
    exit(1);
  }
  printf("Done setting EXT_CSD_WR_REL_SET to 0x%02x on %s\n", value, device);
  if (!set_partitioning_setting_completed(dry_run, device, fd))
    exit(1);
  return 0;
}


x86 assembly (clang, -O0):
<do_write_reliability_set>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x82c(%rbp)
cmpl   $0x4,-0x4(%rbp)
je     11e7 <do_write_reliability_set+0x47>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
lea    0xe2a(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe53(%rip),%rdi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
jne    120e <do_write_reliability_set+0x6e>
movl   $0x0,-0x82c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1070 <strtol@plt>
mov    %eax,-0x830(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x838(%rbp)
mov    -0x838(%rbp),%rdi
mov    0x2da1(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x824(%rbp)
cmpl   $0x0,-0x824(%rbp)
jge    1277 <do_write_reliability_set+0xd7>
lea    0xde4(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x824(%rbp),%edi
lea    -0x820(%rbp),%rsi
call   1090 <read_extcsd@plt>
mov    %eax,-0x828(%rbp)
cmpl   $0x0,-0x828(%rbp)
je     12c4 <do_write_reliability_set+0x124>
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
mov    -0x838(%rbp),%rdx
lea    0xd9e(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
cmpl   $0x0,-0x5b4(%rbp)
je     12e9 <do_write_reliability_set+0x149>
lea    0xd99(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x588(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
jne    1313 <do_write_reliability_set+0x173>
lea    0xd8f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x584(%rbp),%eax
mov    -0x830(%rbp),%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    %edx,%ecx
or     %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x824(%rbp),%edi
mov    -0x14(%rbp),%edx
mov    $0xa7,%esi
call   1080 <write_extcsd_value@plt>
mov    %eax,-0x828(%rbp)
cmpl   $0x0,-0x828(%rbp)
je     1383 <do_write_reliability_set+0x1e3>
mov    0x2c7e(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
mov    -0x838(%rbp),%r8
lea    0xd66(%rip),%rsi
mov    $0xa7,%ecx
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x14(%rbp),%esi
mov    -0x838(%rbp),%rdx
lea    0xd6c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x82c(%rbp),%edi
mov    -0x838(%rbp),%rsi
mov    -0x824(%rbp),%edx
call   1030 <set_partitioning_setting_completed@plt>
cmp    $0x0,%eax
jne    13c6 <do_write_reliability_set+0x226>
mov    $0x1,%edi
call   10c0 <exit@plt>
xor    %eax,%eax
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_write_reliability_set>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x808,%rsp
cmp    $0x4,%edi
jne    12a6 <do_write_reliability_set+0xf6>
mov    %rsi,%rbx
mov    0x8(%rsi),%rsi
lea    0xe71(%rip),%rdi
call   1060 <strcmp@plt>
xor    %r12d,%r12d
test   %eax,%eax
setne  %r13b
mov    0x10(%rbx),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    %eax,%r15d
mov    0x18(%rbx),%r14
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   10b0 <open@plt>
test   %eax,%eax
js     12c7 <do_write_reliability_set+0x117>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
call   10a0 <read_extcsd@plt>
test   %eax,%eax
jne    12dd <do_write_reliability_set+0x12d>
cmpl   $0x0,0x26c(%rsp)
jne    1301 <do_write_reliability_set+0x151>
testb  $0x1,0x298(%rsp)
01
je     130a <do_write_reliability_set+0x15a>
mov    0x29c(%rsp),%ebp
bts    %r15d,%ebp
mov    %ebx,%edi
mov    $0xa7,%esi
mov    %ebp,%edx
call   1090 <write_extcsd_value@plt>
test   %eax,%eax
jne    1320 <do_write_reliability_set+0x170>
mov    %r13b,%r12b
lea    0xe2d(%rip),%rdi
mov    %ebp,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r12d,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1030 <set_partitioning_setting_completed@plt>
test   %eax,%eax
je     1341 <do_write_reliability_set+0x191>
xor    %eax,%eax
add    $0x808,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d2b(%rip),%rax
mov    (%rax),%edi
lea    0xd4a(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xd7e(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x2cf4(%rip),%rax
mov    (%rax),%edi
lea    0xd64(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xe08(%rip),%rdi
jmp    1311 <do_write_reliability_set+0x161>
lea    0xdbe(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x2cb1(%rip),%rax
mov    (%rax),%edi
lea    0xd41(%rip),%rsi
mov    %ebp,%edx
mov    $0xa7,%ecx
mov    %r14,%r8
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [73 x i8] c"Usage: mmc write_reliability set <-y|-n> <partition> </path/to/mmcblkX>\0A\00"
@.str.1 = [3 x i8] c"-y\00"
@O_RDWR = global i32 0
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [32 x i8] c"Could not read EXT_CSD from %s\0A\00"
@.str.4 = [32 x i8] c" Device is already partitioned\0A\00"
@.str.5 = [66 x i8] c"Cannot set write reliability parameters, WR_REL_SET is read-only\0A\00"
@.str.6 = [45 x i8] c"Could not write 0x%02x to EXT_CSD[%d] in %s\0A\00"
@.str.7 = [49 x i8] c"Done setting EXT_CSD_WR_REL_SET to 0x%02x on %s\0A\00"
define i32 @do_write_reliability_set(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca [512 x 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
store i32 1, i32* %9
%12 = load i32, i32* %3
%13 = icmp ne i32 %12, 4
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 ([73 x i8], [73 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
%22 = call i32 @strcmp(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%23 = icmp ne i32 %22, 0
br i1 %23, label %25, label %24
24: ; preds = %18
store i32 0, i32* %9
br label %25
25: ; preds = %24, %18
%26 = load i8**, i8*** %4
%27 = getelementptr inbounds i8*, i8** %26, i64 2
%28 = load i8*, i8** %27
%29 = call i32 @strtol(i8* %28, i32* null, i32 10)
store i32 %29, i32* %10
%30 = load i8**, i8*** %4
%31 = getelementptr inbounds i8*, i8** %30, i64 3
%32 = load i8*, i8** %31
store i8* %32, i8** %11
%33 = load i8*, i8** %11
%34 = load i32, i32* @O_RDWR
%35 = call i32 @open(i8* %33, i32 %34)
store i32 %35, i32* %7
%36 = load i32, i32* %7
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %25
%39 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %25
%42 = load i32, i32* %7
%43 = getelementptr inbounds [512 x i32], [512 x i32]* %6, i64 0, i64 0
%44 = call i32 @read_extcsd(i32 %42, i32* %43)
store i32 %44, i32* %8
%45 = load i32, i32* %8
%46 = icmp ne i32 %45, 0
br i1 %46, label %47, label %52
47: ; preds = %41
%48 = load i32, i32* @stderr
%49 = load i8*, i8** %11
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %49)
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %41
%53 = getelementptr inbounds [512 x i32], [512 x i32]* %6, i64 0, i64 155
%54 = load i32, i32* %53
%55 = icmp ne i32 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %52
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
%58 = call i32 @exit(i32 1)
unreachable
59: ; preds = %52
%60 = getelementptr inbounds [512 x i32], [512 x i32]* %6, i64 0, i64 166
%61 = load i32, i32* %60
%62 = and i32 %61, 1
%63 = icmp ne i32 %62, 0
br i1 %63, label %67, label %64
64: ; preds = %59
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.5, i64 0, i64 0))
%66 = call i32 @exit(i32 1)
unreachable
67: ; preds = %59
%68 = getelementptr inbounds [512 x i32], [512 x i32]* %6, i64 0, i64 167
%69 = load i32, i32* %68
%70 = load i32, i32* %10
%71 = shl i32 1, %70
%72 = or i32 %69, %71
store i32 %72, i32* %5
%73 = load i32, i32* %7
%74 = load i32, i32* %5
%75 = call i32 @write_extcsd_value(i32 %73, i32 167, i32 %74)
store i32 %75, i32* %8
%76 = load i32, i32* %8
%77 = icmp ne i32 %76, 0
br i1 %77, label %78, label %84
78: ; preds = %67
%79 = load i32, i32* @stderr
%80 = load i32, i32* %5
%81 = load i8*, i8** %11
%82 = call i32 (i32, i8*, ...) @fprintf(i32 %79, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0), i32 %80, i32 167, i8* %81)
%83 = call i32 @exit(i32 1)
unreachable
84: ; preds = %67
%85 = load i32, i32* %5
%86 = load i8*, i8** %11
%87 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.7, i64 0, i64 0), i32 %85, i8* %86)
%88 = load i32, i32* %9
%89 = load i8*, i8** %11
%90 = load i32, i32* %7
%91 = call i32 @set_partitioning_setting_completed(i32 %88, i8* %89, i32 %90)
%92 = icmp ne i32 %91, 0
br i1 %92, label %95, label %93
93: ; preds = %84
%94 = call i32 @exit(i32 1)
unreachable
95: ; preds = %84
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [73 x i8] c"Usage: mmc write_reliability set <-y|-n> <partition> </path/to/mmcblkX>\0A\00"
@.str.1 = [3 x i8] c"-y\00"
@O_RDWR = global i32 0
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [32 x i8] c"Could not read EXT_CSD from %s\0A\00"
@.str.6 = [45 x i8] c"Could not write 0x%02x to EXT_CSD[%d] in %s\0A\00"
@.str.7 = [49 x i8] c"Done setting EXT_CSD_WR_REL_SET to 0x%02x on %s\0A\00"
@str = [65 x i8] c"Cannot set write reliability parameters, WR_REL_SET is read-only\00"
@str.8 = [31 x i8] c" Device is already partitioned\00"
define i32 @do_write_reliability_set(i32 %0, i8** nocapture %1) {
%3 = alloca [512 x i32]
%4 = bitcast [512 x i32]* %3 to i8*
%5 = icmp eq i32 %0, 4
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 ([73 x i8], [73 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 = tail call i32 @strcmp(i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* dereferenceable(1) %12)
%14 = icmp ne i32 %13, 0
%15 = zext i1 %14 to i32
%16 = getelementptr inbounds i8*, i8** %1, i64 2
%17 = load i8*, i8** %16
%18 = tail call i32 @strtol(i8* nocapture %17, i32* null, i32 10)
%19 = getelementptr inbounds i8*, i8** %1, i64 3
%20 = load i8*, i8** %19
%21 = load i32, i32* @O_RDWR
%22 = tail call i32 @open(i8* %20, i32 %21)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %10
%25 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%26 = tail call i32 @exit(i32 1)
unreachable
27: ; preds = %10
%28 = getelementptr inbounds [512 x i32], [512 x i32]* %3, i64 0, i64 0
%29 = call i32 @read_extcsd(i32 %22, i32* %28)
%30 = icmp eq i32 %29, 0
br i1 %30, label %35, label %31
31: ; preds = %27
%32 = load i32, i32* @stderr
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %20)
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %27
%36 = getelementptr inbounds [512 x i32], [512 x i32]* %3, i64 0, i64 155
%37 = load i32, i32* %36
%38 = icmp eq i32 %37, 0
br i1 %38, label %42, label %39
39: ; preds = %35
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.8, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %35
%43 = getelementptr inbounds [512 x i32], [512 x i32]* %3, i64 0, i64 166
%44 = load i32, i32* %43
%45 = and i32 %44, 1
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %42
%48 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([65 x i8], [65 x i8]* @str, i64 0, i64 0))
%49 = call i32 @exit(i32 1)
unreachable
50: ; preds = %42
%51 = getelementptr inbounds [512 x i32], [512 x i32]* %3, i64 0, i64 167
%52 = load i32, i32* %51
%53 = shl nuw i32 1, %18
%54 = or i32 %52, %53
%55 = call i32 @write_extcsd_value(i32 %22, i32 167, i32 %54)
%56 = icmp eq i32 %55, 0
br i1 %56, label %61, label %57
57: ; preds = %50
%58 = load i32, i32* @stderr
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0), i32 %54, i32 167, i8* %20)
%60 = call i32 @exit(i32 1)
unreachable
61: ; preds = %50
%62 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.7, i64 0, i64 0), i32 %54, i8* %20)
%63 = call i32 @set_partitioning_setting_completed(i32 %15, i8* %20, i32 %22)
%64 = icmp eq i32 %63, 0
br i1 %64, label %65, label %67
65: ; preds = %61
%66 = call i32 @exit(i32 1)
unreachable
67: ; preds = %61
ret i32 0
}