#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 PATH_MAX;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int open(char const *, 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 *mtd, int flags) {
  FILE *fp;
  char dev[PATH_MAX];
  int i;
  int ret;
  if ((fp = fopen("/proc/mtd", "r"))) {
    while (fgets(dev, sizeof(dev), fp)) {
      if (sscanf(dev, "mtd%d:", &i) && strstr(dev, mtd)) {
        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 open(mtd, flags);
}


x86 assembly (clang, -O0):
<mtd_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x50(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
lea    0xe4a(%rip),%rdi
lea    0xe4d(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     12a9 <mtd_open+0x139>
jmp    11d5 <mtd_open+0x65>
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rax
mov    %eax,%esi
mov    -0x20(%rbp),%rdx
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     12a0 <mtd_open+0x130>
mov    -0x48(%rbp),%rdi
lea    0xe0f(%rip),%rsi
lea    -0x34(%rbp),%rdx
call   1060 <sscanf@plt>
cmp    $0x0,%rax
je     129b <mtd_open+0x12b>
mov    -0x48(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <strstr@plt>
cmp    $0x0,%rax
je     129b <mtd_open+0x12b>
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rax
mov    %eax,%esi
mov    -0x34(%rbp),%ecx
lea    0xdd8(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x48(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1070 <open@plt>
mov    %eax,-0x38(%rbp)
cmp    $0x0,%eax
jge    1280 <mtd_open+0x110>
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rax
mov    %eax,%esi
mov    -0x34(%rbp),%ecx
lea    0xdb3(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x48(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1070 <open@plt>
mov    %eax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
jmp    12bf <mtd_open+0x14f>
jmp    11d5 <mtd_open+0x65>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1070 <open@plt>
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x3c(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mtd_open>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %rdi,-0x38(%rbp)
mov    0x2e61(%rip),%rax
mov    (%rax),%r15d
mov    %rsp,%rbx
lea    0xf(%r15),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe56(%rip),%rdi
lea    0xe59(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     128b <mtd_open+0x11b>
mov    %rax,%r12
mov    %r14d,-0x2c(%rbp)
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     127f <mtd_open+0x10f>
lea    0xe28(%rip),%r14
lea    -0x30(%rbp),%r13
jmp    1203 <mtd_open+0x93>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %r12,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     127f <mtd_open+0x10f>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r13,%rdx
call   1060 <sscanf@plt>
test   %rax,%rax
je     11f0 <mtd_open+0x80>
mov    %rbx,%rdi
mov    -0x38(%rbp),%rsi
call   1090 <strstr@plt>
test   %rax,%rax
je     11f0 <mtd_open+0x80>
mov    -0x30(%rbp),%ecx
lea    0xde2(%rip),%rdx
mov    %rbx,%rdi
mov    %r15d,%esi
call   1040 <snprintf@plt>
mov    %rbx,%rdi
mov    -0x2c(%rbp),%r13d
mov    %r13d,%esi
call   1070 <open@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    1275 <mtd_open+0x105>
mov    -0x30(%rbp),%ecx
lea    0xdc3(%rip),%rdx
mov    %rbx,%rdi
mov    %r15d,%esi
call   1040 <snprintf@plt>
mov    %rbx,%rdi
mov    %r13d,%esi
call   1070 <open@plt>
mov    %eax,%r14d
mov    %r12,%rdi
call   1030 <fclose@plt>
jmp    129a <mtd_open+0x12a>
mov    %r12,%rdi
call   1030 <fclose@plt>
mov    -0x2c(%rbp),%r14d
mov    -0x38(%rbp),%rdi
mov    %r14d,%esi
call   1070 <open@plt>
mov    %eax,%r14d
mov    %r14d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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