#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 */
struct stat {
  int st_mode;
};
typedef int /*<<< orphan*/ dev;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int PATH_MAX;
int S_IFBLK;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ sprintf(char *, char *, int);
int sscanf(char *, char *, int *, int *);
int stat(char *, struct stat *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, scalar_t__);
int /*<<< orphan*/ strstr(char *, char const *);
char *rbcfg_find_mtd(const char *name, int *erase_size) {
  FILE *f;
  int mtd_num;
  char dev[PATH_MAX];
  char *ret = NULL;
  struct stat s;
  int err;
  f = fopen("/proc/mtd", "r");
  if (!f)
    return NULL;
  while (1) {
    char *p;
    p = fgets(dev, sizeof(dev), f);
    if (!p)
      break;
    if (!strstr(dev, name))
      continue;
    err = sscanf(dev, "mtd%d: %08x", &mtd_num, erase_size);
    if (err != 2)
      break;
    sprintf(dev, "/dev/mtdblock%d", mtd_num);
    err = stat(dev, &s);
    if (err < 0)
      break;
    if ((s.st_mode & S_IFBLK) == 0)
      break;
    ret = malloc(strlen(dev) + 1);
    if (ret == NULL)
      break;
    strncpy(ret, dev, strlen(dev) + 1);
    break;
  }
  fclose(f);
  return ret;
}


x86 assembly (clang, -O0):
<rbcfg_find_mtd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x68(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
lea    0xe11(%rip),%rdi
lea    0xe14(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    121e <rbcfg_find_mtd+0x7e>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x50(%rbp)
jmp    1346 <rbcfg_find_mtd+0x1a6>
jmp    1223 <rbcfg_find_mtd+0x83>
jmp    1228 <rbcfg_find_mtd+0x88>
mov    -0x60(%rbp),%rdi
mov    -0x68(%rbp),%rax
mov    %eax,%esi
mov    -0x20(%rbp),%rdx
call   1060 <fgets@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jne    124f <rbcfg_find_mtd+0xaf>
jmp    132e <rbcfg_find_mtd+0x18e>
mov    -0x60(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10c0 <strstr@plt>
cmp    $0x0,%eax
jne    126a <rbcfg_find_mtd+0xca>
jmp    1223 <rbcfg_find_mtd+0x83>
mov    -0x60(%rbp),%rdi
mov    -0x18(%rbp),%rcx
lea    0xd93(%rip),%rsi
lea    -0x24(%rbp),%rdx
call   1090 <sscanf@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x2,-0x4c(%rbp)
je     1294 <rbcfg_find_mtd+0xf4>
jmp    132e <rbcfg_find_mtd+0x18e>
mov    -0x60(%rbp),%rdi
mov    -0x24(%rbp),%edx
lea    0xd76(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x60(%rbp),%rdi
lea    -0x48(%rbp),%rsi
call   1070 <stat@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jge    12c6 <rbcfg_find_mtd+0x126>
jmp    132e <rbcfg_find_mtd+0x18e>
mov    -0x48(%rbp),%eax
mov    0x2d08(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    12e0 <rbcfg_find_mtd+0x140>
jmp    132e <rbcfg_find_mtd+0x18e>
mov    -0x60(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1309 <rbcfg_find_mtd+0x169>
jmp    132e <rbcfg_find_mtd+0x18e>
mov    -0x60(%rbp),%rdi
mov    -0x40(%rbp),%rax
mov    %rax,-0x70(%rbp)
call   1050 <strlen@plt>
mov    -0x70(%rbp),%rdi
mov    -0x60(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1030 <strncpy@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x50(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rbcfg_find_mtd>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r15
mov    %rdi,%r13
mov    %rsp,-0x30(%rbp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%rbx
lea    0xf(%r12),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe22(%rip),%rdi
lea    0xe25(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12a1 <rbcfg_find_mtd+0x101>
mov    %rax,%r14
mov    %r15,-0x48(%rbp)
xor    %r15d,%r15d
nopl   (%rax)
mov    %rbx,%rdi
mov    %r12d,%esi
mov    %r14,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     12ad <rbcfg_find_mtd+0x10d>
mov    %rbx,%rdi
mov    %r13,%rsi
call   10c0 <strstr@plt>
test   %eax,%eax
je     1200 <rbcfg_find_mtd+0x60>
lea    0xddf(%rip),%rsi
lea    -0x3c(%rbp),%rdx
mov    %rbx,%rdi
mov    -0x48(%rbp),%rcx
call   1090 <sscanf@plt>
cmp    $0x2,%eax
jne    12aa <rbcfg_find_mtd+0x10a>
mov    -0x3c(%rbp),%edx
lea    0xdcc(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <sprintf@plt>
lea    -0x38(%rbp),%rsi
mov    %rbx,%rdi
call   1070 <stat@plt>
test   %eax,%eax
js     12aa <rbcfg_find_mtd+0x10a>
mov    0x2d6d(%rip),%rax
mov    (%rax),%eax
test   %eax,-0x38(%rbp)
je     12aa <rbcfg_find_mtd+0x10a>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %rax,%r12
add    $0x1,%r12
mov    %r12,%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     12aa <rbcfg_find_mtd+0x10a>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1030 <strncpy@plt>
jmp    12ad <rbcfg_find_mtd+0x10d>
xor    %r15d,%r15d
mov    -0x30(%rbp),%rbx
jmp    12b9 <rbcfg_find_mtd+0x119>
xor    %r15d,%r15d
mov    -0x30(%rbp),%rbx
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rsp
mov    %r15,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [10 x i8] c"/proc/mtd\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [12 x i8] c"mtd%d: %08x\00"
@.str.3 = [16 x i8] c"/dev/mtdblock%d\00"
@S_IFBLK = global i32 0
define i8* @rbcfg_find_mtd(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca %struct.stat
%12 = alloca i32
%13 = alloca i32
%14 = alloca i8*
store i8* %0, i8** %4
store i32* %1, i32** %5
%15 = load i32, i32* @PATH_MAX
%16 = zext i32 %15 to i64
%17 = call i8* @llvm.stacksave()
store i8* %17, i8** %8
%18 = alloca i8, i64 %16
store i64 %16, i64* %9
store i8* null, i8** %10
%19 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %19, i32** %6
%20 = load i32*, i32** %6
%21 = icmp ne i32* %20, null
br i1 %21, label %23, label %22
22: ; preds = %2
store i8* null, i8** %3
store i32 1, i32* %13
br label %73
23: ; preds = %2
br label %24
24: ; preds = %36, %23
br label %25
25: ; preds = %24
%26 = trunc i64 %16 to i32
%27 = load i32*, i32** %6
%28 = call i8* @fgets(i8* %18, i32 %26, i32* %27)
store i8* %28, i8** %14
%29 = load i8*, i8** %14
%30 = icmp ne i8* %29, null
br i1 %30, label %32, label %31
31: ; preds = %25
br label %69
32: ; preds = %25
%33 = load i8*, i8** %4
%34 = call i32 @strstr(i8* %18, i8* %33)
%35 = icmp ne i32 %34, 0
br i1 %35, label %37, label %36
36: ; preds = %32
br label %24
37: ; preds = %32
%38 = load i32*, i32** %5
%39 = call i32 @sscanf(i8* %18, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32* %7, i32* %38)
store i32 %39, i32* %12
%40 = load i32, i32* %12
%41 = icmp ne i32 %40, 2
br i1 %41, label %42, label %43
42: ; preds = %37
br label %69
43: ; preds = %37
%44 = load i32, i32* %7
%45 = call i32 @sprintf(i8* %18, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %44)
%46 = call i32 @stat(i8* %18, %struct.stat* %11)
store i32 %46, i32* %12
%47 = load i32, i32* %12
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %50
49: ; preds = %43
br label %69
50: ; preds = %43
%51 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%52 = load i32, i32* %51
%53 = load i32, i32* @S_IFBLK
%54 = and i32 %52, %53
%55 = icmp eq i32 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %50
br label %69
57: ; preds = %50
%58 = call i64 @strlen(i8* %18)
%59 = add nsw i64 %58, 1
%60 = call i8* @malloc(i64 %59)
store i8* %60, i8** %10
%61 = load i8*, i8** %10
%62 = icmp eq i8* %61, null
br i1 %62, label %63, label %64
63: ; preds = %57
br label %69
64: ; preds = %57
%65 = load i8*, i8** %10
%66 = call i64 @strlen(i8* %18)
%67 = add nsw i64 %66, 1
%68 = call i32 @strncpy(i8* %65, i8* %18, i64 %67)
br label %69
69: ; preds = %64, %63, %56, %49, %42, %31
%70 = load i32*, i32** %6
%71 = call i32 @fclose(i32* %70)
%72 = load i8*, i8** %10
store i8* %72, i8** %3
store i32 1, i32* %13
br label %73
73: ; preds = %69, %22
%74 = load i8*, i8** %8
%75 = load i8*, i8** %3
ret i8* %75
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@.str = [10 x i8] c"/proc/mtd\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [12 x i8] c"mtd%d: %08x\00"
@.str.3 = [16 x i8] c"/dev/mtdblock%d\00"
@S_IFBLK = global i32 0
define i8* @rbcfg_find_mtd(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca %struct.stat
%5 = bitcast i32* %3 to i8*
%6 = load i32, i32* @PATH_MAX
%7 = zext i32 %6 to i64
%8 = tail call i8* @llvm.stacksave()
%9 = alloca i8, i64 %7
%10 = bitcast %struct.stat* %4 to i8*
%11 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %43, label %13
13: ; preds = %2, %16
%14 = call i8* @fgets(i8* %9, i32 %6, i32* %11)
%15 = icmp eq i8* %14, null
br i1 %15, label %40, label %16
16: ; preds = %13
%17 = call i32 @strstr(i8* %9, i8* %0)
%18 = icmp eq i32 %17, 0
br i1 %18, label %13, label %19
19: ; preds = %16
%20 = call i32 @sscanf(i8* %9, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32* %3, i32* %1)
%21 = icmp eq i32 %20, 2
br i1 %21, label %22, label %40
22: ; preds = %19
%23 = load i32, i32* %3
%24 = call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %23)
%25 = call i32 @stat(i8* %9, %struct.stat* %4)
%26 = icmp slt i32 %25, 0
br i1 %26, label %40, label %27
27: ; preds = %22
%28 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%29 = load i32, i32* %28
%30 = load i32, i32* @S_IFBLK
%31 = and i32 %30, %29
%32 = icmp eq i32 %31, 0
br i1 %32, label %40, label %33
33: ; preds = %27
%34 = call i64 @strlen(i8* %9)
%35 = add nsw i64 %34, 1
%36 = call i8* @malloc(i64 %35)
%37 = icmp eq i8* %36, null
br i1 %37, label %40, label %38
38: ; preds = %33
%39 = call i32 @strncpy(i8* %36, i8* %9, i64 %35)
br label %40
40: ; preds = %13, %38, %19, %22, %27, %33
%41 = phi i8* [ null, %33 ], [ null, %27 ], [ null, %22 ], [ null, %19 ], [ %36, %38 ], [ null, %13 ]
%42 = call i32 @fclose(i32* %11)
br label %43
43: ; preds = %2, %40
%44 = phi i8* [ %41, %40 ], [ null, %2 ]
ret i8* %44
}