#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ memset(unsigned char *, int, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int write(int, unsigned char *, int);
int nvram_reset() {
  int fd = -1;
  unsigned char buf[512];
  memset(buf, 0xff, 512);
  fd = open("/dev/mmcblk0", O_RDWR);
  if (fd < 0) {
    printf("open %s failed.\n", "/dev/mmcblk0");
    return -1;
  }
  lseek(fd, 0x40000, SEEK_SET);
  if (write(fd, buf, 512) != 512) {
    printf("write %s failed.\n", "/dev/mmcblk0");
    close(fd);
    return -1;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<nvram_reset>:
push   %rbp
mov    %rsp,%rbp
sub    $0x210,%rsp
movl   $0xffffffff,-0x8(%rbp)
lea    -0x210(%rbp),%rdi
mov    $0xff,%esi
mov    $0x200,%edx
call   1060 <memset@plt>
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
lea    0xe68(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11c9 <nvram_reset+0x69>
lea    0xe5c(%rip),%rdi
lea    0xe48(%rip),%rsi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1234 <nvram_reset+0xd4>
mov    -0x8(%rbp),%edi
mov    0x2e05(%rip),%rax
mov    (%rax),%edx
mov    $0x40000,%esi
call   1050 <lseek@plt>
mov    -0x8(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    $0x200,%edx
call   1030 <write@plt>
cmp    $0x200,%eax
je     1225 <nvram_reset+0xc5>
lea    0xe19(%rip),%rdi
lea    0xdf4(%rip),%rsi
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1234 <nvram_reset+0xd4>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x210,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nvram_reset>:
push   %rbx
sub    $0x200,%rsp
mov    %rsp,%rdi
mov    $0xff,%esi
mov    $0x200,%edx
call   1060 <memset@plt>
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
lea    0xe76(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11cb <nvram_reset+0x6b>
mov    %eax,%ebx
mov    0x2e3c(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    $0x40000,%esi
call   1050 <lseek@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x200,%edx
call   1030 <write@plt>
cmp    $0x200,%eax
jne    11e0 <nvram_reset+0x80>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
jmp    11ff <nvram_reset+0x9f>
lea    0xe3b(%rip),%rdi
lea    0xe27(%rip),%rsi
call   1040 <printf@plt>
jmp    11fa <nvram_reset+0x9a>
lea    0xe37(%rip),%rdi
lea    0xe12(%rip),%rsi
call   1040 <printf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0xffffffff,%eax
add    $0x200,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/mmcblk0\00"
@O_RDWR = global i32 0
@.str.1 = [17 x i8] c"open %s failed.\0A\00"
@SEEK_SET = global i32 0
@.str.2 = [18 x i8] c"write %s failed.\0A\00"
define i32 @nvram_reset() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [512 x i8]
store i32 -1, i32* %2
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%5 = call i32 @memset(i8* %4, i32 255, i32 512)
%6 = load i32, i32* @O_RDWR
%7 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
br label %27
12: ; preds = %0
%13 = load i32, i32* %2
%14 = load i32, i32* @SEEK_SET
%15 = call i32 @lseek(i32 %13, i32 262144, i32 %14)
%16 = load i32, i32* %2
%17 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%18 = call i32 @write(i32 %16, i8* %17, i32 512)
%19 = icmp ne i32 %18, 512
br i1 %19, label %20, label %24
20: ; preds = %12
%21 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%22 = load i32, i32* %2
%23 = call i32 @close(i32 %22)
store i32 -1, i32* %1
br label %27
24: ; preds = %12
%25 = load i32, i32* %2
%26 = call i32 @close(i32 %25)
store i32 0, i32* %1
br label %27
27: ; preds = %24, %20, %10
%28 = load i32, i32* %1
ret i32 %28
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/mmcblk0\00"
@O_RDWR = global i32 0
@.str.1 = [17 x i8] c"open %s failed.\0A\00"
@SEEK_SET = global i32 0
@.str.2 = [18 x i8] c"write %s failed.\0A\00"
define i32 @nvram_reset() {
%1 = alloca [512 x i8]
%2 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0
%3 = call i32 @memset(i8* %2, i32 255, i32 512)
%4 = load i32, i32* @O_RDWR
%5 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %19
9: ; preds = %0
%10 = load i32, i32* @SEEK_SET
%11 = call i32 @lseek(i32 %5, i32 262144, i32 %10)
%12 = call i32 @write(i32 %5, i8* %2, i32 512)
%13 = icmp eq i32 %12, 512
br i1 %13, label %17, label %14
14: ; preds = %9
%15 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @close(i32 %5)
br label %19
17: ; preds = %9
%18 = call i32 @close(i32 %5)
br label %19
19: ; preds = %17, %14, %7
%20 = phi i32 [ -1, %7 ], [ -1, %14 ], [ 0, %17 ]
ret i32 %20
}