#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*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
scalar_t__ sscanf(char *, char *, int *, int *);
int stat(char *, struct stat *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, scalar_t__);
scalar_t__ strstr(char *, char *);
int board_find_mtd(char *path) {
  FILE *fp;
  int i, esz;
  char dev[PATH_MAX];
  struct stat s;
  int result = -1;
  if (fp = fopen("/proc/mtd", "r")) {
    while (fgets(dev, sizeof(dev), fp)) {
      if (strstr(dev, "board_data") && sscanf(dev, "mtd%d: %08x", &i, &esz)) {
        sprintf(dev, "/dev/mtdblock/%d", i);
        if (stat(dev, &s) > -1 && (s.st_mode & S_IFBLK)) {
          strncpy(path, dev, strlen(dev) + 1);
          result = 0;
          break;
        } else {
          sprintf(dev, "/dev/mtdblock%d", i);
          if (stat(dev, &s) > -1 && (s.st_mode & S_IFBLK)) {
            strncpy(path, dev, strlen(dev) + 1);
            result = 0;
            break;
          }
        }
      }
    }
    fclose(fp);
  }
  return result;
}


x86 assembly (clang, -O0):
<board_find_mtd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e4d(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x48(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
movl   $0xffffffff,-0x34(%rbp)
lea    0xe26(%rip),%rdi
lea    0xe29(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     1348 <board_find_mtd+0x1b8>
jmp    11f9 <board_find_mtd+0x69>
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rax
mov    %eax,%esi
mov    -0x10(%rbp),%rdx
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     133f <board_find_mtd+0x1af>
mov    -0x40(%rbp),%rdi
lea    0xdeb(%rip),%rsi
call   10b0 <strstr@plt>
cmp    $0x0,%rax
je     133a <board_find_mtd+0x1aa>
mov    -0x40(%rbp),%rdi
lea    0xddc(%rip),%rsi
lea    -0x14(%rbp),%rdx
lea    -0x18(%rbp),%rcx
call   1080 <sscanf@plt>
cmp    $0x0,%rax
je     133a <board_find_mtd+0x1aa>
mov    -0x40(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xdc3(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x40(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
jle    12c1 <board_find_mtd+0x131>
mov    -0x30(%rbp),%eax
mov    0x2d53(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12c1 <board_find_mtd+0x131>
mov    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    %rax,-0x50(%rbp)
call   1050 <strlen@plt>
mov    -0x50(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1030 <strncpy@plt>
movl   $0x0,-0x34(%rbp)
jmp    133f <board_find_mtd+0x1af>
mov    -0x40(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xd65(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x40(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
jle    1330 <board_find_mtd+0x1a0>
mov    -0x30(%rbp),%eax
mov    0x2ce4(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1330 <board_find_mtd+0x1a0>
mov    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    %rax,-0x58(%rbp)
call   1050 <strlen@plt>
mov    -0x58(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1030 <strncpy@plt>
movl   $0x0,-0x34(%rbp)
jmp    133f <board_find_mtd+0x1af>
jmp    1335 <board_find_mtd+0x1a5>
jmp    133a <board_find_mtd+0x1aa>
jmp    11f9 <board_find_mtd+0x69>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x34(%rbp),%eax
mov    -0x20(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<board_find_mtd>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%r13
mov    %rsp,%r14
mov    0x2e42(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%rbx
lea    0xf(%r12),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe36(%rip),%rdi
lea    0xe39(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     12d9 <board_find_mtd+0x149>
mov    %rax,%r15
mov    %rbx,%rdi
mov    %r12d,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     12e4 <board_find_mtd+0x154>
mov    %r13,-0x48(%rbp)
mov    %r14,-0x40(%rbp)
lea    0xe04(%rip),%r14
lea    -0x30(%rbp),%r13
jmp    1227 <board_find_mtd+0x97>
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %r12d,%esi
mov    %r15,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     12e0 <board_find_mtd+0x150>
mov    %rbx,%rdi
mov    %r14,%rsi
call   10b0 <strstr@plt>
test   %rax,%rax
je     1210 <board_find_mtd+0x80>
mov    %rbx,%rdi
lea    0xdd6(%rip),%rsi
lea    -0x34(%rbp),%rdx
lea    -0x4c(%rbp),%rcx
call   1080 <sscanf@plt>
test   %rax,%rax
je     1210 <board_find_mtd+0x80>
mov    -0x34(%rbp),%edx
mov    %rbx,%rdi
lea    0xdc3(%rip),%rsi
call   10a0 <sprintf@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
call   1070 <stat@plt>
test   %eax,%eax
js     1282 <board_find_mtd+0xf2>
mov    0x2d5d(%rip),%rax
mov    (%rax),%eax
test   %eax,-0x30(%rbp)
jne    12b9 <board_find_mtd+0x129>
mov    -0x34(%rbp),%edx
mov    %rbx,%rdi
lea    0xda5(%rip),%rsi
call   10a0 <sprintf@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
call   1070 <stat@plt>
test   %eax,%eax
js     1210 <board_find_mtd+0x80>
mov    0x2d2a(%rip),%rax
mov    (%rax),%eax
test   %eax,-0x30(%rbp)
je     1210 <board_find_mtd+0x80>
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdx
mov    -0x48(%rbp),%rdi
mov    %rbx,%rsi
call   1030 <strncpy@plt>
xor    %ebx,%ebx
mov    -0x40(%rbp),%r14
jmp    12e9 <board_find_mtd+0x159>
mov    $0xffffffff,%ebx
jmp    12f1 <board_find_mtd+0x161>
mov    -0x40(%rbp),%r14
mov    $0xffffffff,%ebx
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %r14,%rsp
mov    %ebx,%eax
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 = [11 x i8] c"board_data\00"
@.str.3 = [12 x i8] c"mtd%d: %08x\00"
@.str.4 = [17 x i8] c"/dev/mtdblock/%d\00"
@S_IFBLK = global i32 0
@.str.5 = [16 x i8] c"/dev/mtdblock%d\00"
define i32 @board_find_mtd(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca %struct.stat
%9 = alloca i32
store i8* %0, i8** %2
%10 = load i32, i32* @PATH_MAX
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %6
%13 = alloca i8, i64 %11
store i64 %11, i64* %7
store i32 -1, i32* %9
%14 = 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* %14, i32** %3
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %66
16: ; preds = %1
br label %17
17: ; preds = %62, %16
%18 = trunc i64 %11 to i32
%19 = load i32*, i32** %3
%20 = call i64 @fgets(i8* %13, i32 %18, i32* %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %63
22: ; preds = %17
%23 = call i64 @strstr(i8* %13, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %62
25: ; preds = %22
%26 = call i64 @sscanf(i8* %13, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32* %4, i32* %5)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %62
28: ; preds = %25
%29 = load i32, i32* %4
%30 = call i32 @sprintf(i8* %13, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %29)
%31 = call i32 @stat(i8* %13, %struct.stat* %8)
%32 = icmp sgt i32 %31, -1
br i1 %32, label %33, label %44
33: ; preds = %28
%34 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%35 = load i32, i32* %34
%36 = load i32, i32* @S_IFBLK
%37 = and i32 %35, %36
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %44
39: ; preds = %33
%40 = load i8*, i8** %2
%41 = call i64 @strlen(i8* %13)
%42 = add nsw i64 %41, 1
%43 = call i32 @strncpy(i8* %40, i8* %13, i64 %42)
store i32 0, i32* %9
br label %63
44: ; preds = %33, %28
%45 = load i32, i32* %4
%46 = call i32 @sprintf(i8* %13, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 %45)
%47 = call i32 @stat(i8* %13, %struct.stat* %8)
%48 = icmp sgt i32 %47, -1
br i1 %48, label %49, label %60
49: ; preds = %44
%50 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%51 = load i32, i32* %50
%52 = load i32, i32* @S_IFBLK
%53 = and i32 %51, %52
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %60
55: ; preds = %49
%56 = load i8*, i8** %2
%57 = call i64 @strlen(i8* %13)
%58 = add nsw i64 %57, 1
%59 = call i32 @strncpy(i8* %56, i8* %13, i64 %58)
store i32 0, i32* %9
br label %63
60: ; preds = %49, %44
br label %61
61: ; preds = %60
br label %62
62: ; preds = %61, %25, %22
br label %17
63: ; preds = %55, %39, %17
%64 = load i32*, i32** %3
%65 = call i32 @fclose(i32* %64)
br label %66
66: ; preds = %63, %1
%67 = load i32, i32* %9
%68 = load i8*, i8** %6
ret i32 %67
}

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 = [11 x i8] c"board_data\00"
@.str.3 = [12 x i8] c"mtd%d: %08x\00"
@.str.4 = [17 x i8] c"/dev/mtdblock/%d\00"
@S_IFBLK = global i32 0
@.str.5 = [16 x i8] c"/dev/mtdblock%d\00"
define i32 @board_find_mtd(i8* %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.stat
%5 = bitcast i32* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = load i32, i32* @PATH_MAX
%8 = zext i32 %7 to i64
%9 = tail call i8* @llvm.stacksave()
%10 = alloca i8, i64 %8
%11 = bitcast %struct.stat* %4 to i8*
%12 = 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))
%13 = icmp eq i32* %12, null
br i1 %13, label %55, label %14
14: ; preds = %1
%15 = call i64 @fgets(i8* %10, i32 %7, i32* %12)
%16 = icmp eq i64 %15, 0
br i1 %16, label %52, label %17
17: ; preds = %14
%18 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
br label %19
19: ; preds = %17, %45
%20 = call i64 @strstr(i8* %10, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%21 = icmp eq i64 %20, 0
br i1 %21, label %45, label %22
22: ; preds = %19
%23 = call i64 @sscanf(i8* %10, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32* %2, i32* %3)
%24 = icmp eq i64 %23, 0
br i1 %24, label %45, label %25
25: ; preds = %22
%26 = load i32, i32* %2
%27 = call i32 @sprintf(i8* %10, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0), i32 %26)
%28 = call i32 @stat(i8* %10, %struct.stat* %4)
%29 = icmp sgt i32 %28, -1
br i1 %29, label %30, label %35
30: ; preds = %25
%31 = load i32, i32* %18
%32 = load i32, i32* @S_IFBLK
%33 = and i32 %32, %31
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %48
35: ; preds = %30, %25
%36 = load i32, i32* %2
%37 = call i32 @sprintf(i8* %10, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 %36)
%38 = call i32 @stat(i8* %10, %struct.stat* %4)
%39 = icmp sgt i32 %38, -1
br i1 %39, label %40, label %45
40: ; preds = %35
%41 = load i32, i32* %18
%42 = load i32, i32* @S_IFBLK
%43 = and i32 %42, %41
%44 = icmp eq i32 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %40, %35, %22, %19
%46 = call i64 @fgets(i8* %10, i32 %7, i32* %12)
%47 = icmp eq i64 %46, 0
br i1 %47, label %52, label %19
48: ; preds = %40, %30
%49 = call i64 @strlen(i8* %10)
%50 = add nsw i64 %49, 1
%51 = call i32 @strncpy(i8* %0, i8* %10, i64 %50)
br label %52
52: ; preds = %45, %48, %14
%53 = phi i32 [ -1, %14 ], [ 0, %48 ], [ -1, %45 ]
%54 = call i32 @fclose(i32* %12)
br label %55
55: ; preds = %52, %1
%56 = phi i32 [ %53, %52 ], [ -1, %1 ]
ret i32 %56
}