#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*/ buf;
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 /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ sscanf(char *, char *, char *, char *, char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(char *, char *);
unsigned int strtol(char *, int /*<<< orphan*/ *, int);
unsigned int getMTDPartSize(char *part) {
  char buf[128], name[32], size[32], dev[32], erase[32];
  unsigned int result = 0;
  FILE *fp = fopen("/proc/mtd", "r");
  if (!fp) {
    fprintf(stderr, "mtd support not enable?");
    return 0;
  }
  while (fgets(buf, sizeof(buf), fp)) {
    sscanf(buf, "%s %s %s %s", dev, size, erase, name);
    if (!strcmp(name, part)) {
      result = strtol(size, NULL, 16);
      break;
    }
  }
  fclose(fp);
  return result;
}


x86 assembly (clang, -O0):
<getMTDPartSize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x114(%rbp)
lea    0xe70(%rip),%rdi
lea    0xe73(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x120(%rbp)
cmpq   $0x0,-0x120(%rbp)
jne    11d2 <getMTDPartSize+0x62>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
lea    0xe4b(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    127b <getMTDPartSize+0x10b>
jmp    11d7 <getMTDPartSize+0x67>
lea    -0x90(%rbp),%rdi
mov    -0x120(%rbp),%rdx
mov    $0x80,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     1266 <getMTDPartSize+0xf6>
lea    -0x90(%rbp),%rdi
lea    -0xf0(%rbp),%rdx
lea    -0xd0(%rbp),%rcx
lea    -0x110(%rbp),%r8
lea    -0xb0(%rbp),%r9
lea    0xe01(%rip),%rsi
call   1080 <sscanf@plt>
lea    -0xb0(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
jne    1261 <getMTDPartSize+0xf1>
lea    -0xd0(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1070 <strtol@plt>
mov    %eax,-0x114(%rbp)
jmp    1266 <getMTDPartSize+0xf6>
jmp    11d7 <getMTDPartSize+0x67>
mov    -0x120(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x114(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getMTDPartSize>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    %rdi,%r14
lea    0xe75(%rip),%rdi
lea    0xe78(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1214 <getMTDPartSize+0xa4>
mov    %rax,%r15
lea    0x80(%rsp),%rbx
mov    %rsp,%rbp
lea    0x20(%rsp),%r12
lea    0x60(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    $0x80,%esi
mov    %r15,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     122d <getMTDPartSize+0xbd>
mov    %rbx,%rdi
lea    0xe45(%rip),%rsi
lea    0x40(%rsp),%rdx
mov    %rbp,%rcx
mov    %r12,%r8
mov    %r13,%r9
call   1080 <sscanf@plt>
mov    %r13,%rdi
mov    %r14,%rsi
call   1050 <strcmp@plt>
test   %eax,%eax
jne    11c0 <getMTDPartSize+0x50>
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1070 <strtol@plt>
mov    %eax,%ebx
jmp    122f <getMTDPartSize+0xbf>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
lea    0xde8(%rip),%rsi
call   1060 <fprintf@plt>
xor    %ebx,%ebx
jmp    1237 <getMTDPartSize+0xc7>
xor    %ebx,%ebx
mov    %r15,%rdi
call   1030 <fclose@plt>
mov    %ebx,%eax
add    $0x108,%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"
@stderr = global i32 0
@.str.2 = [24 x i8] c"mtd support not enable?\00"
@.str.3 = [12 x i8] c"%s %s %s %s\00"
define i32 @getMTDPartSize(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [128 x i8]
%5 = alloca [32 x i8]
%6 = alloca [32 x i8]
%7 = alloca [32 x i8]
%8 = alloca [32 x i8]
%9 = alloca i32
%10 = alloca i32*
store i8* %0, i8** %3
store i32 0, i32* %9
%11 = 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* %11, i32** %10
%12 = load i32*, i32** %10
%13 = icmp ne i32* %12, null
br i1 %13, label %17, label %14
14: ; preds = %1
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %2
br label %42
17: ; preds = %1
br label %18
18: ; preds = %37, %17
%19 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%20 = load i32*, i32** %10
%21 = call i64 @fgets(i8* %19, i32 128, i32* %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %38
23: ; preds = %18
%24 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%25 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%26 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%27 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%28 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%29 = call i32 @sscanf(i8* %24, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %25, i8* %26, i8* %27, i8* %28)
%30 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%31 = load i8*, i8** %3
%32 = call i32 @strcmp(i8* %30, i8* %31)
%33 = icmp ne i32 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %23
%35 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%36 = call i32 @strtol(i8* %35, i32* null, i32 16)
store i32 %36, i32* %9
br label %38
37: ; preds = %23
br label %18
38: ; preds = %34, %18
%39 = load i32*, i32** %10
%40 = call i32 @fclose(i32* %39)
%41 = load i32, i32* %9
store i32 %41, i32* %2
br label %42
42: ; preds = %38, %14
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
@.str = [10 x i8] c"/proc/mtd\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [24 x i8] c"mtd support not enable?\00"
@.str.3 = [12 x i8] c"%s %s %s %s\00"
define i32 @getMTDPartSize(i8* nocapture %0) {
%2 = alloca [128 x i8]
%3 = alloca [32 x i8]
%4 = alloca [32 x i8]
%5 = alloca [32 x i8]
%6 = alloca [32 x i8]
%7 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%8 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%9 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%10 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%11 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%12 = 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))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
br label %29
17: ; preds = %1, %20
%18 = call i64 @fgets(i8* %7, i32 128, i32* %12)
%19 = icmp eq i64 %18, 0
br i1 %19, label %26, label %20
20: ; preds = %17
%21 = call i32 @sscanf(i8* %7, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i8* %10, i8* %9, i8* %11, i8* %8)
%22 = call i32 @strcmp(i8* %8, i8* dereferenceable(1) %0)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %17
24: ; preds = %20
%25 = call i32 @strtol(i8* nocapture %9, i32* null, i32 16)
br label %26
26: ; preds = %17, %24
%27 = phi i32 [ %25, %24 ], [ 0, %17 ]
%28 = call i32 @fclose(i32* %12)
br label %29
29: ; preds = %26, %14
%30 = phi i32 [ %27, %26 ], [ 0, %14 ]
ret i32 %30
}