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

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
scalar_t__ sscanf(char *, char *, int *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
scalar_t__ strstr(char *, char const *);
int32_t mtd_open(const char *name, int32_t 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);
  }
  fprintf(stderr, "Could not open mtd device, %s\n", strerror(errno));
  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    0xe57(%rip),%rdi
lea    0xe5a(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1292 <mtd_open+0x102>
jmp    11c8 <mtd_open+0x38>
lea    -0x70(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x50,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1289 <mtd_open+0xf9>
lea    -0x70(%rbp),%rdi
lea    0xe1d(%rip),%rsi
lea    -0x74(%rbp),%rdx
call   1070 <sscanf@plt>
cmp    $0x0,%rax
je     1284 <mtd_open+0xf4>
lea    -0x70(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   10b0 <strstr@plt>
cmp    $0x0,%rax
je     1284 <mtd_open+0xf4>
lea    -0x70(%rbp),%rdi
mov    -0x74(%rbp),%ecx
mov    $0x50,%esi
lea    0xde7(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x70(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1080 <open@plt>
mov    %eax,-0x78(%rbp)
cmp    $0x0,%eax
jge    1270 <mtd_open+0xe0>
lea    -0x70(%rbp),%rdi
mov    -0x74(%rbp),%ecx
mov    $0x50,%esi
lea    0xdc2(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x70(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1080 <open@plt>
mov    %eax,-0x78(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x78(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12c5 <mtd_open+0x135>
jmp    11c8 <mtd_open+0x38>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    0x2d47(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x7c(%rbp)
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x7c(%rbp),%edi
mov    %rax,%rdx
lea    0xd71(%rip),%rsi
call   1060 <fprintf@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,%r14d
mov    %rdi,%r15
lea    0xe55(%rip),%rdi
lea    0xe58(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     12a6 <mtd_open+0x116>
mov    %rax,%rbx
lea    0x10(%rsp),%rdi
mov    $0x50,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     129e <mtd_open+0x10e>
lea    0xe27(%rip),%r12
lea    0x10(%rsp),%rbp
lea    0xc(%rsp),%r13
jmp    1219 <mtd_open+0x89>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    $0x50,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     129e <mtd_open+0x10e>
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1070 <sscanf@plt>
test   %rax,%rax
je     1200 <mtd_open+0x70>
mov    %rbp,%rdi
mov    %r15,%rsi
call   10b0 <strstr@plt>
test   %rax,%rax
je     1200 <mtd_open+0x70>
mov    0xc(%rsp),%ecx
lea    0xdcc(%rip),%rdx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x50,%esi
call   1040 <snprintf@plt>
mov    %rbp,%rdi
mov    %r14d,%esi
call   1080 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1294 <mtd_open+0x104>
mov    0xc(%rsp),%ecx
lea    0xda9(%rip),%rdx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x50,%esi
call   1040 <snprintf@plt>
mov    %rbp,%rdi
mov    %r14d,%esi
call   1080 <open@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    12d3 <mtd_open+0x143>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x2d33(%rip),%rax
mov    (%rax),%ebx
mov    0x2d22(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd66(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%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"
@stderr = global i32 0
@.str.5 = [31 x i8] c"Could not open mtd device, %s\0A\00"
@errno = global i32 0
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 %55
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
%51 = load i32, i32* @stderr
%52 = load i32, i32* @errno
%53 = call i8* @strerror(i32 %52)
%54 = call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i8* %53)
store i32 -1, i32* %3
br label %55
55: ; preds = %50, %42
%56 = load i32, i32* %3
ret i32 %56
}

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"
@stderr = global i32 0
@.str.5 = [31 x i8] c"Could not open mtd device, %s\0A\00"
@errno = global i32 0
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 %35, 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 %33, label %12
12: ; preds = %9, %30
%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 %30, label %15
15: ; preds = %12
%16 = call i64 @strstr(i8* %5, i8* %0)
%17 = icmp eq i64 %16, 0
br i1 %17, label %30, 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 %27
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 %27
27: ; preds = %23, %18
%28 = phi i32 [ %26, %23 ], [ %21, %18 ]
%29 = call i32 @fclose(i32* %7)
br label %40
30: ; preds = %15, %12
%31 = call i64 @fgets(i8* %5, i32 80, i32* %7)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %12
33: ; preds = %30, %9
%34 = call i32 @fclose(i32* %7)
br label %35
35: ; preds = %33, %2
%36 = load i32, i32* @stderr
%37 = load i32, i32* @errno
%38 = call i8* @strerror(i32 %37)
%39 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i8* %38)
br label %40
40: ; preds = %35, %27
%41 = phi i32 [ %28, %27 ], [ -1, %35 ]
ret i32 %41
}