#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*/ dev;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int open(char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
scalar_t__ sscanf(char *, char *, int *);
scalar_t__ strstr(char *, char const *);
int mtd_open(const char *name, int flags) {
  FILE *fp;
  char dev[80];
  int i, ret;
  if ((fp = fopen("/proc/mtd", "r"))) {
    while (fgets(dev, sizeof(dev), fp)) {
      if (sscanf(dev, "mtd%d:", &i) && strstr(dev, name)) {
        snprintf(dev, sizeof(dev), "/dev/mtd%d", i);
        if ((ret = open(dev, flags)) < 0) {
          snprintf(dev, sizeof(dev), "/dev/mtd/%d", i);
          ret = open(dev, flags);
        }
        fclose(fp);
        return ret;
      }
    }
    fclose(fp);
  }
  return -1;
}


x86 assembly (clang, -O0):
<mtd_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    0xe77(%rip),%rdi
lea    0xe7a(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1272 <mtd_open+0x102>
jmp    11a8 <mtd_open+0x38>
lea    -0x70(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x50,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1269 <mtd_open+0xf9>
lea    -0x70(%rbp),%rdi
lea    0xe3d(%rip),%rsi
lea    -0x74(%rbp),%rdx
call   1060 <sscanf@plt>
cmp    $0x0,%rax
je     1264 <mtd_open+0xf4>
lea    -0x70(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <strstr@plt>
cmp    $0x0,%rax
je     1264 <mtd_open+0xf4>
lea    -0x70(%rbp),%rdi
mov    -0x74(%rbp),%ecx
mov    $0x50,%esi
lea    0xe07(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x70(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1070 <open@plt>
mov    %eax,-0x78(%rbp)
cmp    $0x0,%eax
jge    1250 <mtd_open+0xe0>
lea    -0x70(%rbp),%rdi
mov    -0x74(%rbp),%ecx
mov    $0x50,%esi
lea    0xde2(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x70(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1070 <open@plt>
mov    %eax,-0x78(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x78(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1279 <mtd_open+0x109>
jmp    11a8 <mtd_open+0x38>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mtd_open>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    %esi,%ebp
mov    %rdi,%r13
lea    0xe76(%rip),%rdi
lea    0xe79(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     127e <mtd_open+0x10e>
mov    %rax,%rbx
lea    0x10(%rsp),%rdi
mov    $0x50,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
je     1274 <mtd_open+0x104>
mov    %ebp,0xc(%rsp)
lea    0xe3e(%rip),%r12
lea    0x10(%rsp),%rbp
lea    0x8(%rsp),%r14
jmp    11f5 <mtd_open+0x85>
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    $0x50,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1274 <mtd_open+0x104>
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %r14,%rdx
call   1060 <sscanf@plt>
test   %rax,%rax
je     11e0 <mtd_open+0x70>
mov    %rbp,%rdi
mov    %r13,%rsi
call   1090 <strstr@plt>
test   %rax,%rax
je     11e0 <mtd_open+0x70>
mov    0x8(%rsp),%ecx
lea    0xdf0(%rip),%rdx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x50,%esi
call   1040 <snprintf@plt>
mov    %rbp,%rdi
mov    0xc(%rsp),%ebp
mov    %ebp,%esi
call   1070 <open@plt>
mov    %eax,%r15d
test   %eax,%eax
jns    1274 <mtd_open+0x104>
mov    0x8(%rsp),%ecx
lea    0xdc9(%rip),%rdx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x50,%esi
call   1040 <snprintf@plt>
mov    %r14,%rdi
mov    %ebp,%esi
call   1070 <open@plt>
mov    %eax,%r15d
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    1284 <mtd_open+0x114>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"/proc/mtd\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [7 x i8] c"mtd%d:\00"
@.str.3 = [11 x i8] c"/dev/mtd%d\00"
@.str.4 = [12 x i8] c"/dev/mtd/%d\00"
define i32 @mtd_open(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca [80 x i8]
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%10 = 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* %10, i32** %6
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %50
12: ; preds = %2
br label %13
13: ; preds = %46, %12
%14 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%15 = load i32*, i32** %6
%16 = call i64 @fgets(i8* %14, i32 80, i32* %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %47
18: ; preds = %13
%19 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%20 = call i64 @sscanf(i8* %19, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32* %8)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %46
22: ; preds = %18
%23 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%24 = load i8*, i8** %4
%25 = call i64 @strstr(i8* %23, i8* %24)
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %46
27: ; preds = %22
%28 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%29 = load i32, i32* %8
%30 = call i32 @snprintf(i8* %28, i32 80, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %29)
%31 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%32 = load i32, i32* %5
%33 = call i32 @open(i8* %31, i32 %32)
store i32 %33, i32* %9
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %42
35: ; preds = %27
%36 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%37 = load i32, i32* %8
%38 = call i32 @snprintf(i8* %36, i32 80, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 %37)
%39 = getelementptr inbounds [80 x i8], [80 x i8]* %7, i64 0, i64 0
%40 = load i32, i32* %5
%41 = call i32 @open(i8* %39, i32 %40)
store i32 %41, i32* %9
br label %42
42: ; preds = %35, %27
%43 = load i32*, i32** %6
%44 = call i32 @fclose(i32* %43)
%45 = load i32, i32* %9
store i32 %45, i32* %3
br label %51
46: ; preds = %22, %18
br label %13
47: ; preds = %13
%48 = load i32*, i32** %6
%49 = call i32 @fclose(i32* %48)
br label %50
50: ; preds = %47, %2
store i32 -1, i32* %3
br label %51
51: ; preds = %50, %42
%52 = load i32, i32* %3
ret i32 %52
}

LLVM IR (-O2):
@.str = [10 x i8] c"/proc/mtd\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [7 x i8] c"mtd%d:\00"
@.str.3 = [11 x i8] c"/dev/mtd%d\00"
@.str.4 = [12 x i8] c"/dev/mtd/%d\00"
define i32 @mtd_open(i8* %0, i32 %1) {
%3 = alloca [80 x i8]
%4 = alloca i32
%5 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%6 = bitcast i32* %4 to i8*
%7 = 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))
%8 = icmp eq i32* %7, null
br i1 %8, label %33, label %9
9: ; preds = %2
%10 = call i64 @fgets(i8* %5, i32 80, i32* %7)
%11 = icmp eq i64 %10, 0
br i1 %11, label %30, label %12
12: ; preds = %9, %27
%13 = call i64 @sscanf(i8* %5, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%14 = icmp eq i64 %13, 0
br i1 %14, label %27, label %15
15: ; preds = %12
%16 = call i64 @strstr(i8* %5, i8* %0)
%17 = icmp eq i64 %16, 0
br i1 %17, label %27, label %18
18: ; preds = %15
%19 = load i32, i32* %4
%20 = call i32 @snprintf(i8* %5, i32 80, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %19)
%21 = call i32 @open(i8* %5, i32 %1)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %30
23: ; preds = %18
%24 = load i32, i32* %4
%25 = call i32 @snprintf(i8* %5, i32 80, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i32 %24)
%26 = call i32 @open(i8* %5, i32 %1)
br label %30
27: ; preds = %15, %12
%28 = call i64 @fgets(i8* %5, i32 80, i32* %7)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %12
30: ; preds = %27, %9, %18, %23
%31 = phi i32 [ %26, %23 ], [ %21, %18 ], [ -1, %9 ], [ -1, %27 ]
%32 = call i32 @fclose(i32* %7)
br label %33
33: ; preds = %30, %2
%34 = phi i32 [ -1, %2 ], [ %31, %30 ]
ret i32 %34
}