#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 O_RDWR;
int O_SYNC;
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 *, char *, int);
scalar_t__ sscanf(char *, char *, int *);
scalar_t__ strstr(char *, char const *);
int mtd_open(const char *mtd, int block) {
  FILE *fp;
  char dev[PATH_MAX];
  int i;
  int ret;
  int flags = O_RDWR | O_SYNC;
  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%s/%d", (block ? "block" : "  "),
                 i);
        if ((ret = open(dev, flags)) < 0) {
          snprintf(dev, sizeof(dev), "/dev/mtd%s%d", (block ? "block" : ""), 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)
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    0x2e19(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x3c(%rbp)
lea    0xe35(%rip),%rdi
lea    0xe38(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     12f0 <mtd_open+0x180>
jmp    11ea <mtd_open+0x7a>
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rax
mov    %eax,%esi
mov    -0x20(%rbp),%rdx
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     12e7 <mtd_open+0x177>
mov    -0x48(%rbp),%rdi
lea    0xdfa(%rip),%rsi
lea    -0x34(%rbp),%rdx
call   1060 <sscanf@plt>
cmp    $0x0,%rax
je     12e2 <mtd_open+0x172>
mov    -0x48(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <strstr@plt>
cmp    $0x0,%rax
je     12e2 <mtd_open+0x172>
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rax
mov    %eax,%esi
mov    -0x14(%rbp),%edx
lea    0xdd7(%rip),%rcx
lea    0xdca(%rip),%rax
cmp    $0x0,%edx
cmovne %rax,%rcx
mov    -0x34(%rbp),%r8d
lea    0xdaa(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x48(%rbp),%rdi
mov    -0x3c(%rbp),%esi
call   1070 <open@plt>
mov    %eax,-0x38(%rbp)
cmp    $0x0,%eax
jge    12c7 <mtd_open+0x157>
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rax
mov    %eax,%esi
mov    -0x14(%rbp),%edx
lea    0xd8f(%rip),%rcx
lea    0xd80(%rip),%rax
cmp    $0x0,%edx
cmovne %rax,%rcx
mov    -0x34(%rbp),%r8d
lea    0xd77(%rip),%rdx
call   1040 <snprintf@plt>
mov    -0x48(%rbp),%rdi
mov    -0x3c(%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,-0x40(%rbp)
jmp    1306 <mtd_open+0x196>
jmp    11ea <mtd_open+0x7a>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x10(%rbp),%rdi
mov    -0x3c(%rbp),%esi
call   1070 <open@plt>
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x40(%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,%r13d
mov    %rdi,-0x40(%rbp)
mov    0x2e61(%rip),%rax
mov    (%rax),%r15d
mov    %rsp,%rbx
lea    0xf(%r15),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x2e36(%rip),%rax
mov    0x2e27(%rip),%rcx
mov    (%rcx),%r14d
or     (%rax),%r14d
lea    0xe42(%rip),%rdi
lea    0xe45(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     12d5 <mtd_open+0x165>
mov    %rax,%r12
mov    %r13d,-0x30(%rbp)
mov    %r14d,-0x2c(%rbp)
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     12c9 <mtd_open+0x159>
lea    0xe10(%rip),%r14
lea    -0x34(%rbp),%r13
jmp    1227 <mtd_open+0xb7>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %r12,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     12c9 <mtd_open+0x159>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r13,%rdx
call   1060 <sscanf@plt>
test   %rax,%rax
je     1210 <mtd_open+0xa0>
mov    %rbx,%rdi
mov    -0x40(%rbp),%rsi
call   1090 <strstr@plt>
test   %rax,%rax
je     1210 <mtd_open+0xa0>
cmpl   $0x0,-0x30(%rbp)
lea    0xdd1(%rip),%rcx
lea    0xdc4(%rip),%r13
cmovne %r13,%rcx
mov    -0x34(%rbp),%r8d
lea    0xda7(%rip),%rdx
mov    %rbx,%rdi
mov    %r15d,%esi
call   1040 <snprintf@plt>
mov    %rbx,%rdi
mov    -0x2c(%rbp),%esi
call   1070 <open@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    12bf <mtd_open+0x14f>
cmpl   $0x0,-0x30(%rbp)
lea    0xd95(%rip),%rax
cmove  %rax,%r13
mov    -0x34(%rbp),%r8d
lea    0xd87(%rip),%rdx
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %r13,%rcx
call   1040 <snprintf@plt>
mov    %rbx,%rdi
mov    -0x2c(%rbp),%esi
call   1070 <open@plt>
mov    %eax,%r14d
mov    %r12,%rdi
call   1030 <fclose@plt>
jmp    12e4 <mtd_open+0x174>
mov    %r12,%rdi
call   1030 <fclose@plt>
mov    -0x2c(%rbp),%r14d
mov    -0x40(%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
@O_RDWR = global i32 0
@O_SYNC = 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 = [14 x i8] c"/dev/mtd%s/%d\00"
@.str.4 = [6 x i8] c"block\00"
@.str.5 = [3 x i8] c" \00"
@.str.6 = [13 x i8] c"/dev/mtd%s%d\00"
@.str.7 = [1 x i8] zeroinitializer
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
%12 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%13 = load i32, i32* @PATH_MAX
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %7
%16 = alloca i8, i64 %14
store i64 %14, i64* %8
%17 = load i32, i32* @O_RDWR
%18 = load i32, i32* @O_SYNC
%19 = or i32 %17, %18
store i32 %19, i32* %11
%20 = 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* %20, i32** %6
%21 = icmp ne i32* %20, null
br i1 %21, label %22, label %64
22: ; preds = %2
br label %23
23: ; preds = %60, %22
%24 = trunc i64 %14 to i32
%25 = load i32*, i32** %6
%26 = call i64 @fgets(i8* %16, i32 %24, i32* %25)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %61
28: ; preds = %23
%29 = call i64 @sscanf(i8* %16, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32* %9)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %60
31: ; preds = %28
%32 = load i8*, i8** %4
%33 = call i64 @strstr(i8* %16, i8* %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %60
35: ; preds = %31
%36 = trunc i64 %14 to i32
%37 = load i32, i32* %5
%38 = icmp ne i32 %37, 0
%39 = zext i1 %38 to i64
%40 = select i1 %38, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0)
%41 = load i32, i32* %9
%42 = call i32 @snprintf(i8* %16, i32 %36, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* %40, i32 %41)
%43 = load i32, i32* %11
%44 = call i32 @open(i8* %16, i32 %43)
store i32 %44, i32* %10
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %56
46: ; preds = %35
%47 = trunc i64 %14 to i32
%48 = load i32, i32* %5
%49 = icmp ne i32 %48, 0
%50 = zext i1 %49 to i64
%51 = select i1 %49, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.7, i64 0, i64 0)
%52 = load i32, i32* %9
%53 = call i32 @snprintf(i8* %16, i32 %47, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0), i8* %51, i32 %52)
%54 = load i32, i32* %11
%55 = call i32 @open(i8* %16, i32 %54)
store i32 %55, i32* %10
br label %56
56: ; preds = %46, %35
%57 = load i32*, i32** %6
%58 = call i32 @fclose(i32* %57)
%59 = load i32, i32* %10
store i32 %59, i32* %3
store i32 1, i32* %12
br label %68
60: ; preds = %31, %28
br label %23
61: ; preds = %23
%62 = load i32*, i32** %6
%63 = call i32 @fclose(i32* %62)
br label %64
64: ; preds = %61, %2
%65 = load i8*, i8** %4
%66 = load i32, i32* %11
%67 = call i32 @open(i8* %65, i32 %66)
store i32 %67, i32* %3
store i32 1, i32* %12
br label %68
68: ; preds = %64, %56
%69 = load i8*, i8** %7
%70 = load i32, i32* %3
ret i32 %70
}

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