#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_RDONLY;
char *basename(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
scalar_t__ read(int, char *, int);
scalar_t__ snprintf(char *, int, char *, char *);
int sscanf(char *, char *, unsigned int *);
int /*<<< orphan*/ stderr;
unsigned int sd_erase_size(char *filename) {
  char *devname;
  char sys_fn[256];
  int fp;
  char tmp[64];
  unsigned int size;
  devname = basename(filename);
  if (snprintf(sys_fn, 256, "/sys/class/block/%s/device/preferred_erase_size",
               devname) < 0)
    goto def_bs;
  fp = open(sys_fn, O_RDONLY);
  if (fp < 0)
    goto def_bs;
  memset(tmp, 0, 64);
  if (read(fp, tmp, 64) <= 0)
    goto def_bs_close;
  if (sscanf(tmp, "%u", &size) != 1)
    goto def_bs_close;
  close(fp);
  fprintf(stderr, "\x1b[36mUsing erase size of %u bytes\x1b[0m\n", size);
  return size;
def_bs_close:
  close(fp);
def_bs:
  fprintf(stderr,
          "\x1b[1;33mNOTE: Could not determine erase block size of %s - "
          "assuming %u bytes\x1b[0m\n",
          devname, 8 * 1024 * 1024);
  return 8 * 1024 * 1024;
}


x86 assembly (clang, -O0):
<sd_erase_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x180,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <basename@plt>
mov    %rax,-0x18(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x100,%esi
lea    0xe4d(%rip),%rdx
call   1030 <snprintf@plt>
cmp    $0x0,%rax
jge    11c7 <sd_erase_size+0x47>
jmp    1296 <sd_erase_size+0x116>
lea    -0x120(%rbp),%rdi
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0x0,-0x124(%rbp)
jge    11f4 <sd_erase_size+0x74>
jmp    1296 <sd_erase_size+0x116>
lea    -0x170(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1040 <memset@plt>
mov    -0x124(%rbp),%edi
lea    -0x170(%rbp),%rsi
mov    $0x40,%edx
call   1060 <read@plt>
cmp    $0x0,%rax
jg     122d <sd_erase_size+0xad>
jmp    128b <sd_erase_size+0x10b>
lea    -0x170(%rbp),%rdi
lea    0xdf5(%rip),%rsi
lea    -0x174(%rbp),%rdx
call   1090 <sscanf@plt>
cmp    $0x1,%eax
je     1255 <sd_erase_size+0xd5>
jmp    128b <sd_erase_size+0x10b>
mov    -0x124(%rbp),%edi
call   1050 <close@plt>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
mov    -0x174(%rbp),%edx
lea    0xdbd(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x174(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12bd <sd_erase_size+0x13d>
mov    -0x124(%rbp),%edi
call   1050 <close@plt>
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xdb0(%rip),%rsi
mov    $0x800000,%ecx
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x800000,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x180,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sd_erase_size>:
push   %rbp
push   %r14
push   %rbx
sub    $0x150,%rsp
call   1080 <basename@plt>
mov    %rax,%rbx
lea    0xe66(%rip),%rdx
lea    0x50(%rsp),%rdi
mov    $0x100,%esi
mov    %rax,%rcx
call   1030 <snprintf@plt>
test   %rax,%rax
js     1240 <sd_erase_size+0xc0>
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
lea    0x50(%rsp),%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     1240 <sd_erase_size+0xc0>
mov    %eax,%ebp
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1040 <memset@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x40,%edx
call   1060 <read@plt>
test   %rax,%rax
jle    1239 <sd_erase_size+0xb9>
lea    0xe33(%rip),%rsi
lea    0x10(%rsp),%rdi
lea    0xc(%rsp),%rdx
call   1090 <sscanf@plt>
cmp    $0x1,%eax
jne    1239 <sd_erase_size+0xb9>
mov    %ebp,%edi
call   1050 <close@plt>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
mov    0xc(%rsp),%edx
lea    0xe07(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0xc(%rsp),%ebp
jmp    1264 <sd_erase_size+0xe4>
mov    %ebp,%edi
call   1050 <close@plt>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
lea    0xe0a(%rip),%rsi
mov    $0x800000,%ebp
mov    %rbx,%rdx
mov    $0x800000,%ecx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %ebp,%eax
add    $0x150,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [48 x i8] c"/sys/class/block/%s/device/preferred_erase_size\00"
@O_RDONLY = global i32 0
@.str.1 = [3 x i8] c"%u\00"
@stderr = global i32 0
@.str.2 = [39 x i8] c"\1B[36mUsing erase size of %u bytes\1B[0m\0A\00"
@.str.3 = [81 x i8] c"\1B[1;33mNOTE: Could not determine erase block size of %s - assuming %u bytes\1B[0m\0A\00"
define i32 @sd_erase_size(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [256 x i8]
%6 = alloca i32
%7 = alloca [64 x i8]
%8 = alloca i32
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = call i8* @basename(i8* %9)
store i8* %10, i8** %4
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%12 = load i8*, i8** %4
%13 = call i64 @snprintf(i8* %11, i32 256, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %1
br label %46
16: ; preds = %1
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%18 = load i32, i32* @O_RDONLY
%19 = call i32 @open(i8* %17, i32 %18)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %16
br label %46
23: ; preds = %16
%24 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%25 = call i32 @memset(i8* %24, i32 0, i32 64)
%26 = load i32, i32* %6
%27 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%28 = call i64 @read(i32 %26, i8* %27, i32 64)
%29 = icmp sle i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %23
br label %43
31: ; preds = %23
%32 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%33 = call i32 @sscanf(i8* %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %8)
%34 = icmp ne i32 %33, 1
br i1 %34, label %35, label %36
35: ; preds = %31
br label %43
36: ; preds = %31
%37 = load i32, i32* %6
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* @stderr
%40 = load i32, i32* %8
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %40)
%42 = load i32, i32* %8
store i32 %42, i32* %2
br label %50
43: ; preds = %35, %30
%44 = load i32, i32* %6
%45 = call i32 @close(i32 %44)
br label %46
46: ; preds = %43, %22, %15
%47 = load i32, i32* @stderr
%48 = load i8*, i8** %4
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([81 x i8], [81 x i8]* @.str.3, i64 0, i64 0), i8* %48, i32 8388608)
store i32 8388608, i32* %2
br label %50
50: ; preds = %46, %36
%51 = load i32, i32* %2
ret i32 %51
}

LLVM IR (-O2):
@.str = [48 x i8] c"/sys/class/block/%s/device/preferred_erase_size\00"
@O_RDONLY = global i32 0
@.str.1 = [3 x i8] c"%u\00"
@stderr = global i32 0
@.str.2 = [39 x i8] c"\1B[36mUsing erase size of %u bytes\1B[0m\0A\00"
@.str.3 = [81 x i8] c"\1B[1;33mNOTE: Could not determine erase block size of %s - assuming %u bytes\1B[0m\0A\00"
define i32 @sd_erase_size(i8* %0) {
%2 = alloca [256 x i8]
%3 = alloca [64 x i8]
%4 = alloca i32
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%7 = bitcast i32* %4 to i8*
%8 = tail call i8* @basename(i8* %0)
%9 = call i64 @snprintf(i8* %5, i32 256, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = icmp slt i64 %9, 0
br i1 %10, label %30, label %11
11: ; preds = %1
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* %5, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %30, label %15
15: ; preds = %11
%16 = call i32 @memset(i8* %6, i32 0, i32 64)
%17 = call i64 @read(i32 %13, i8* %6, i32 64)
%18 = icmp slt i64 %17, 1
br i1 %18, label %28, label %19
19: ; preds = %15
%20 = call i32 @sscanf(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %4)
%21 = icmp eq i32 %20, 1
br i1 %21, label %22, label %28
22: ; preds = %19
%23 = call i32 @close(i32 %13)
%24 = load i32, i32* @stderr
%25 = load i32, i32* %4
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0), i32 %25)
%27 = load i32, i32* %4
br label %33
28: ; preds = %19, %15
%29 = call i32 @close(i32 %13)
br label %30
30: ; preds = %11, %1, %28
%31 = load i32, i32* @stderr
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([81 x i8], [81 x i8]* @.str.3, i64 0, i64 0), i8* %8, i32 8388608)
br label %33
33: ; preds = %30, %22
%34 = phi i32 [ 8388608, %30 ], [ %27, %22 ]
ret i32 %34
}