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

/* Variables and functions */
int asprintf(char **, char *, char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ is_pnor_part(char *);
char *strchr(char *, char);
int strlen(char *);
char *get_dev_mtd(void) {
  FILE *f;
  char *ret = NULL, *pos = NULL;
  char line[255];
  f = fopen("/proc/mtd", "r");
  if (!f)
    return NULL;
  while (!pos && fgets(line, sizeof(line), f) != NULL) {
    if (line[strlen(line) - 1] != '\n')
      break;
    if (is_pnor_part(line)) {
      pos = strchr(line, ':');
      if (!pos)
        break;
    }
  }
  fclose(f);
  if (pos) {
    *pos = '\0';
    if (asprintf(&ret, "/dev/%s", line) == -1)
      ret = NULL;
  }
  return ret;
}


x86 assembly (clang, -O0):
<get_dev_mtd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
lea    0xe6e(%rip),%rdi
lea    0xe71(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ba <get_dev_mtd+0x4a>
movq   $0x0,-0x8(%rbp)
jmp    12c3 <get_dev_mtd+0x153>
jmp    11bf <get_dev_mtd+0x4f>
xor    %eax,%eax
cmpq   $0x0,-0x20(%rbp)
mov    %al,-0x121(%rbp)
jne    11f4 <get_dev_mtd+0x84>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0xff,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x121(%rbp)
mov    -0x121(%rbp),%al
test   $0x1,%al
jne    1207 <get_dev_mtd+0x97>
jmp    1273 <get_dev_mtd+0x103>
lea    -0x120(%rbp),%rdi
call   1040 <strlen@plt>
sub    $0x1,%eax
cltq
movsbl -0x120(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
je     122e <get_dev_mtd+0xbe>
jmp    1273 <get_dev_mtd+0x103>
lea    -0x120(%rbp),%rdi
call   1090 <is_pnor_part@plt>
cmp    $0x0,%rax
je     126e <get_dev_mtd+0xfe>
lea    -0x120(%rbp),%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1269 <get_dev_mtd+0xf9>
jmp    1273 <get_dev_mtd+0x103>
jmp    126e <get_dev_mtd+0xfe>
jmp    11bf <get_dev_mtd+0x4f>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
cmpq   $0x0,-0x20(%rbp)
je     12bb <get_dev_mtd+0x14b>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x120(%rbp),%rdx
lea    -0x18(%rbp),%rdi
lea    0xd6c(%rip),%rsi
call   1070 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    12b6 <get_dev_mtd+0x146>
movq   $0x0,-0x18(%rbp)
jmp    12bb <get_dev_mtd+0x14b>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_dev_mtd>:
push   %r14
push   %rbx
sub    $0x118,%rsp
movq   $0x0,0x8(%rsp)
lea    0xe76(%rip),%rdi
lea    0xe79(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     122f <get_dev_mtd+0xbf>
mov    %rax,%r14
lea    0x10(%rsp),%rbx
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0xff,%esi
mov    %r14,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     1227 <get_dev_mtd+0xb7>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    1227 <get_dev_mtd+0xb7>
mov    %rbx,%rdi
call   1090 <is_pnor_part@plt>
test   %rax,%rax
je     11b0 <get_dev_mtd+0x40>
lea    0x10(%rsp),%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     1227 <get_dev_mtd+0xb7>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <fclose@plt>
movb   $0x0,(%rbx)
lea    0xe00(%rip),%rsi
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rdx
call   1070 <asprintf@plt>
cmp    $0xffffffff,%eax
je     122f <get_dev_mtd+0xbf>
mov    0x8(%rsp),%rax
jmp    1231 <get_dev_mtd+0xc1>
mov    %r14,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [10 x i8] c"/proc/mtd\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [8 x i8] c"/dev/%s\00"
define i8* @get_dev_mtd() {
%1 = alloca i8*
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [255 x i8]
store i8* null, i8** %3
store i8* null, i8** %4
%6 = 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* %6, i32** %2
%7 = load i32*, i32** %2
%8 = icmp ne i32* %7, null
br i1 %8, label %10, label %9
9: ; preds = %0
store i8* null, i8** %1
br label %57
10: ; preds = %0
br label %11
11: ; preds = %42, %10
%12 = load i8*, i8** %4
%13 = icmp ne i8* %12, null
br i1 %13, label %19, label %14
14: ; preds = %11
%15 = getelementptr inbounds [255 x i8], [255 x i8]* %5, i64 0, i64 0
%16 = load i32*, i32** %2
%17 = call i32* @fgets(i8* %15, i32 255, i32* %16)
%18 = icmp ne i32* %17, null
br label %19
19: ; preds = %14, %11
%20 = phi i1 [ false, %11 ], [ %18, %14 ]
br i1 %20, label %21, label %43
21: ; preds = %19
%22 = getelementptr inbounds [255 x i8], [255 x i8]* %5, i64 0, i64 0
%23 = call i32 @strlen(i8* %22)
%24 = sub nsw i32 %23, 1
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [255 x i8], [255 x i8]* %5, i64 0, i64 %25
%27 = load i8, i8* %26
%28 = sext i8 %27 to i32
%29 = icmp ne i32 %28, 10
br i1 %29, label %30, label %31
30: ; preds = %21
br label %43
31: ; preds = %21
%32 = getelementptr inbounds [255 x i8], [255 x i8]* %5, i64 0, i64 0
%33 = call i64 @is_pnor_part(i8* %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %42
35: ; preds = %31
%36 = getelementptr inbounds [255 x i8], [255 x i8]* %5, i64 0, i64 0
%37 = call i8* @strchr(i8* %36, i8 signext 58)
store i8* %37, i8** %4
%38 = load i8*, i8** %4
%39 = icmp ne i8* %38, null
br i1 %39, label %41, label %40
40: ; preds = %35
br label %43
41: ; preds = %35
br label %42
42: ; preds = %41, %31
br label %11
43: ; preds = %40, %30, %19
%44 = load i32*, i32** %2
%45 = call i32 @fclose(i32* %44)
%46 = load i8*, i8** %4
%47 = icmp ne i8* %46, null
br i1 %47, label %48, label %55
48: ; preds = %43
%49 = load i8*, i8** %4
store i8 0, i8* %49
%50 = getelementptr inbounds [255 x i8], [255 x i8]* %5, i64 0, i64 0
%51 = call i32 @asprintf(i8** %3, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %50)
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %54
53: ; preds = %48
store i8* null, i8** %3
br label %54
54: ; preds = %53, %48
br label %55
55: ; preds = %54, %43
%56 = load i8*, i8** %3
store i8* %56, i8** %1
br label %57
57: ; preds = %55, %9
%58 = load i8*, i8** %1
ret i8* %58
}

LLVM IR (-O2):
@.str = [10 x i8] c"/proc/mtd\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [8 x i8] c"/dev/%s\00"
define i8* @get_dev_mtd() {
%1 = alloca i8*
%2 = alloca [255 x i8]
%3 = bitcast i8** %1 to i8*
store i8* null, i8** %1
%4 = getelementptr inbounds [255 x i8], [255 x i8]* %2, i64 0, i64 0
%5 = 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))
%6 = icmp eq i32* %5, null
br i1 %6, label %31, label %7
7: ; preds = %0, %17
%8 = call i32* @fgets(i8* %4, i32 255, i32* %5)
%9 = icmp eq i32* %8, null
br i1 %9, label %23, label %10
10: ; preds = %7
%11 = call i32 @strlen(i8* %4)
%12 = add nsw i32 %11, -1
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds [255 x i8], [255 x i8]* %2, i64 0, i64 %13
%15 = load i8, i8* %14
%16 = icmp eq i8 %15, 10
br i1 %16, label %17, label %23
17: ; preds = %10
%18 = call i64 @is_pnor_part(i8* %4)
%19 = icmp eq i64 %18, 0
br i1 %19, label %7, label %20
20: ; preds = %17
%21 = call i8* @strchr(i8* %4, i8 signext 58)
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %25
23: ; preds = %10, %7, %20
%24 = call i32 @fclose(i32* %5)
br label %31
25: ; preds = %20
%26 = call i32 @fclose(i32* %5)
store i8 0, i8* %21
%27 = call i32 @asprintf(i8** %1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %4)
%28 = icmp eq i32 %27, -1
%29 = load i8*, i8** %1
%30 = select i1 %28, i8* null, i8* %29
br label %31
31: ; preds = %25, %23, %0
%32 = phi i8* [ null, %0 ], [ null, %23 ], [ %30, %25 ]
ret i8* %32
}