#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 /*<<< orphan*/ ssize_t;
typedef scalar_t__ off_t;

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
scalar_t__ calloc(int, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(unsigned char *);
scalar_t__ lseek(int, scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ write(int, unsigned char *, int /*<<< orphan*/);
int rk30_zero_out(int fd, off_t pos, ssize_t size) {
  if (lseek(fd, pos, SEEK_SET) != pos) {
    fprintf(stderr, "mtd: erase failure at 0x%08lx (%s)\n", pos,
            strerror(errno));
    return -1;
  }
  unsigned char *zb = (unsigned char *)calloc(1, size);
  if (zb == NULL) {
    fprintf(stderr, "mtd: erase failure, could not allocate memory\n");
    return -1;
  }
  if (write(fd, zb, size) != size) {
    fprintf(stderr, "mtd: erase failure at 0x%08lx (%s)\n", pos,
            strerror(errno));
    free(zb);
    return -1;
  }
  free(zb);
  return 0;
}


x86 assembly (clang, -O0):
<rk30_zero_out>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2e50(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
cmp    -0x10(%rbp),%rax
je     11d6 <rk30_zero_out+0x76>
mov    0x2e48(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    %eax,%ecx
lea    0xe3d(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128b <rk30_zero_out+0x12b>
mov    -0x14(%rbp),%esi
mov    $0x1,%edi
call   1060 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1215 <rk30_zero_out+0xb5>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
lea    0xe22(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128b <rk30_zero_out+0x12b>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1040 <write@plt>
cmp    -0x14(%rbp),%eax
je     127b <rk30_zero_out+0x11b>
mov    0x2dac(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %eax,%ecx
lea    0xda1(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    128b <rk30_zero_out+0x12b>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rk30_zero_out>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2e5a(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
cmp    %rbx,%rax
jne    11b8 <rk30_zero_out+0x58>
mov    $0x1,%edi
mov    %r15d,%esi
call   1060 <calloc@plt>
test   %rax,%rax
je     11e6 <rk30_zero_out+0x86>
mov    %rax,%r14
mov    %ebp,%edi
mov    %rax,%rsi
mov    %r15d,%edx
call   1040 <write@plt>
cmp    %r15d,%eax
jne    11ff <rk30_zero_out+0x9f>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    1238 <rk30_zero_out+0xd8>
mov    0x2e21(%rip),%rax
mov    (%rax),%ebp
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe2a(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1233 <rk30_zero_out+0xd3>
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
lea    0xe2e(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1233 <rk30_zero_out+0xd3>
mov    0x2dda(%rip),%rax
mov    (%rax),%ebp
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xde3(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %eax,%ecx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_SET = global i32 0
@stderr = global i32 0
@.str = [36 x i8] c"mtd: erase failure at 0x%08lx (%s)\0A\00"
@errno = global i32 0
@.str.1 = [47 x i8] c"mtd: erase failure, could not allocate memory\0A\00"
define i32 @rk30_zero_out(i32 %0, i64 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32
%8 = alloca i8*
store i32 %0, i32* %5
store i64 %1, i64* %6
store i32 %2, i32* %7
%9 = load i32, i32* %5
%10 = load i64, i64* %6
%11 = load i32, i32* @SEEK_SET
%12 = call i64 @lseek(i32 %9, i64 %10, i32 %11)
%13 = load i64, i64* %6
%14 = icmp ne i64 %12, %13
br i1 %14, label %15, label %21
15: ; preds = %3
%16 = load i32, i32* @stderr
%17 = load i64, i64* %6
%18 = load i32, i32* @errno
%19 = call i32 @strerror(i32 %18)
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i64 %17, i32 %19)
store i32 -1, i32* %4
br label %48
21: ; preds = %3
%22 = load i32, i32* %7
%23 = call i64 @calloc(i32 1, i32 %22)
%24 = inttoptr i64 %23 to i8*
store i8* %24, i8** %8
%25 = load i8*, i8** %8
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %30
27: ; preds = %21
%28 = load i32, i32* @stderr
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %48
30: ; preds = %21
%31 = load i32, i32* %5
%32 = load i8*, i8** %8
%33 = load i32, i32* %7
%34 = call i32 @write(i32 %31, i8* %32, i32 %33)
%35 = load i32, i32* %7
%36 = icmp ne i32 %34, %35
br i1 %36, label %37, label %45
37: ; preds = %30
%38 = load i32, i32* @stderr
%39 = load i64, i64* %6
%40 = load i32, i32* @errno
%41 = call i32 @strerror(i32 %40)
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i64 %39, i32 %41)
%43 = load i8*, i8** %8
%44 = call i32 @free(i8* %43)
store i32 -1, i32* %4
br label %48
45: ; preds = %30
%46 = load i8*, i8** %8
%47 = call i32 @free(i8* %46)
store i32 0, i32* %4
br label %48
48: ; preds = %45, %37, %27, %15
%49 = load i32, i32* %4
ret i32 %49
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@stderr = global i32 0
@.str = [36 x i8] c"mtd: erase failure at 0x%08lx (%s)\0A\00"
@errno = global i32 0
@.str.1 = [47 x i8] c"mtd: erase failure, could not allocate memory\0A\00"
define i32 @rk30_zero_out(i32 %0, i64 %1, i32 %2) {
%4 = load i32, i32* @SEEK_SET
%5 = tail call i64 @lseek(i32 %0, i64 %1, i32 %4)
%6 = icmp eq i64 %5, %1
br i1 %6, label %12, label %7
7: ; preds = %3
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = tail call i32 @strerror(i32 %9)
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i64 %1, i32 %10)
br label %30
12: ; preds = %3
%13 = tail call i64 @calloc(i32 1, i32 %2)
%14 = inttoptr i64 %13 to i8*
%15 = icmp eq i64 %13, 0
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = tail call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.1, i64 0, i64 0))
br label %30
19: ; preds = %12
%20 = tail call i32 @write(i32 %0, i8* %14, i32 %2)
%21 = icmp eq i32 %20, %2
br i1 %21, label %28, label %22
22: ; preds = %19
%23 = load i32, i32* @stderr
%24 = load i32, i32* @errno
%25 = tail call i32 @strerror(i32 %24)
%26 = tail call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i64 %1, i32 %25)
%27 = tail call i32 @free(i8* %14)
br label %30
28: ; preds = %19
%29 = tail call i32 @free(i8* %14)
br label %30
30: ; preds = %16, %22, %28, %7
%31 = phi i32 [ -1, %7 ], [ -1, %16 ], [ -1, %22 ], [ 0, %28 ]
ret i32 %31
}