#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 *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ stderr;
char *strchr(char *, char);
scalar_t__ strcmp(char *, char *);
char *findmtddevice(char *mtdname) {
  FILE *f;
  char buf[80];
  int found;
  static char device[80];
  char *p, *q;
  f = fopen("/proc/mtd", "r");
  if (!f) {
    perror("setmac: open /proc/mtd failed");
    return NULL;
  }
  found = 0;
  while (!found && fgets(buf, sizeof(buf), f)) {
    p = strchr(buf, ':');
    if (!p)
      continue;
    *p++ = '\0';
    p = strchr(p, '"');
    if (!p)
      continue;
    p++;
    q = strchr(p, '"');
    if (!q)
      continue;
    *q = '\0';
    if (strcmp(p, mtdname) == 0) {
      found = 1;
      break;
    }
  }
  fclose(f);
  if (found) {
    sprintf(device, "/dev/%s", buf);
    return device;
  } else {
    fprintf(stderr, "setmac: mtd device '%s' not found\n", mtdname);
    return NULL;
  }
}


x86 assembly (clang, -O0):
<findmtddevice>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe6a(%rip),%rdi
lea    0xe6d(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ca <findmtddevice+0x4a>
lea    0xe54(%rip),%rdi
call   1090 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    132c <findmtddevice+0x1ac>
movl   $0x0,-0x74(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x74(%rbp)
mov    %al,-0x89(%rbp)
jne    1202 <findmtddevice+0x82>
lea    -0x70(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x50,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x89(%rbp)
mov    -0x89(%rbp),%al
test   $0x1,%al
jne    1215 <findmtddevice+0x95>
jmp    12d1 <findmtddevice+0x151>
lea    -0x70(%rbp),%rdi
mov    $0x3a,%esi
call   1040 <strchr@plt>
mov    %rax,-0x80(%rbp)
cmpq   $0x0,-0x80(%rbp)
jne    1237 <findmtddevice+0xb7>
jmp    11d1 <findmtddevice+0x51>
mov    -0x80(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x80(%rbp)
movb   $0x0,(%rax)
mov    -0x80(%rbp),%rdi
mov    $0x22,%esi
call   1040 <strchr@plt>
mov    %rax,-0x80(%rbp)
cmpq   $0x0,-0x80(%rbp)
jne    126b <findmtddevice+0xeb>
jmp    11d1 <findmtddevice+0x51>
mov    -0x80(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x80(%rbp)
mov    -0x80(%rbp),%rdi
mov    $0x22,%esi
call   1040 <strchr@plt>
mov    %rax,-0x88(%rbp)
cmpq   $0x0,-0x88(%rbp)
jne    129f <findmtddevice+0x11f>
jmp    11d1 <findmtddevice+0x51>
mov    -0x88(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x80(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    12cc <findmtddevice+0x14c>
movl   $0x1,-0x74(%rbp)
jmp    12d1 <findmtddevice+0x151>
jmp    11d1 <findmtddevice+0x51>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
cmpl   $0x0,-0x74(%rbp)
je     130b <findmtddevice+0x18b>
lea    -0x70(%rbp),%rdx
lea    0x2d81(%rip),%rdi
lea    0xd34(%rip),%rsi
call   10a0 <sprintf@plt>
lea    0x2d6e(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    132c <findmtddevice+0x1ac>
mov    0x2cce(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd13(%rip),%rsi
call   1070 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<findmtddevice>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %rdi,%r14
lea    0xe6b(%rip),%rdi
lea    0xe6e(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1290 <findmtddevice+0x110>
mov    %rax,%r15
mov    %rsp,%rdi
mov    $0x50,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     125f <findmtddevice+0xdf>
mov    %rsp,%r12
jmp    11e5 <findmtddevice+0x65>
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    $0x50,%esi
mov    %r15,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     125f <findmtddevice+0xdf>
mov    %r12,%rdi
mov    $0x3a,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11d0 <findmtddevice+0x50>
mov    %rax,%rdi
add    $0x1,%rdi
movb   $0x0,(%rax)
mov    $0x22,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11d0 <findmtddevice+0x50>
mov    %rax,%rbx
add    $0x1,%rbx
mov    %rbx,%rdi
mov    $0x22,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11d0 <findmtddevice+0x50>
movb   $0x0,(%rax)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    11d0 <findmtddevice+0x50>
mov    %r15,%rdi
call   1030 <fclose@plt>
lea    0x2e25(%rip),%rbx
lea    0xdd8(%rip),%rsi
mov    %rsp,%rdx
mov    %rbx,%rdi
call   10a0 <sprintf@plt>
jmp    1281 <findmtddevice+0x101>
mov    %r15,%rdi
call   1030 <fclose@plt>
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
lea    0xdbb(%rip),%rsi
mov    %r14,%rdx
call   1070 <fprintf@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xd75(%rip),%rdi
call   1090 <perror@plt>
jmp    127f <findmtddevice+0xff>

LLVM IR (-O0):
@findmtddevice.device = internal global [80 x i8] zeroinitializer
@.str = [10 x i8] c"/proc/mtd\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [30 x i8] c"setmac: open /proc/mtd failed\00"
@.str.3 = [8 x i8] c"/dev/%s\00"
@stderr = global i32 0
@.str.4 = [35 x i8] c"setmac: mtd device '%s' not found\0A\00"
define i8* @findmtddevice(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca [80 x i8]
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %3
%9 = 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* %9, i32** %4
%10 = load i32*, i32** %4
%11 = icmp ne i32* %10, null
br i1 %11, label %14, label %12
12: ; preds = %1
%13 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %2
br label %67
14: ; preds = %1
store i32 0, i32* %6
br label %15
15: ; preds = %54, %46, %38, %30, %14
%16 = load i32, i32* %6
%17 = icmp ne i32 %16, 0
br i1 %17, label %23, label %18
18: ; preds = %15
%19 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%20 = load i32*, i32** %4
%21 = call i64 @fgets(i8* %19, i32 80, i32* %20)
%22 = icmp ne i64 %21, 0
br label %23
23: ; preds = %18, %15
%24 = phi i1 [ false, %15 ], [ %22, %18 ]
br i1 %24, label %25, label %55
25: ; preds = %23
%26 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%27 = call i8* @strchr(i8* %26, i8 signext 58)
store i8* %27, i8** %7
%28 = load i8*, i8** %7
%29 = icmp ne i8* %28, null
br i1 %29, label %31, label %30
30: ; preds = %25
br label %15
31: ; preds = %25
%32 = load i8*, i8** %7
%33 = getelementptr inbounds i8, i8* %32, i32 1
store i8* %33, i8** %7
store i8 0, i8* %32
%34 = load i8*, i8** %7
%35 = call i8* @strchr(i8* %34, i8 signext 34)
store i8* %35, i8** %7
%36 = load i8*, i8** %7
%37 = icmp ne i8* %36, null
br i1 %37, label %39, label %38
38: ; preds = %31
br label %15
39: ; preds = %31
%40 = load i8*, i8** %7
%41 = getelementptr inbounds i8, i8* %40, i32 1
store i8* %41, i8** %7
%42 = load i8*, i8** %7
%43 = call i8* @strchr(i8* %42, i8 signext 34)
store i8* %43, i8** %8
%44 = load i8*, i8** %8
%45 = icmp ne i8* %44, null
br i1 %45, label %47, label %46
46: ; preds = %39
br label %15
47: ; preds = %39
%48 = load i8*, i8** %8
store i8 0, i8* %48
%49 = load i8*, i8** %7
%50 = load i8*, i8** %3
%51 = call i64 @strcmp(i8* %49, i8* %50)
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %47
store i32 1, i32* %6
br label %55
54: ; preds = %47
br label %15
55: ; preds = %53, %23
%56 = load i32*, i32** %4
%57 = call i32 @fclose(i32* %56)
%58 = load i32, i32* %6
%59 = icmp ne i32 %58, 0
br i1 %59, label %60, label %63
60: ; preds = %55
%61 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%62 = call i32 @sprintf(i8* getelementptr inbounds ([80 x i8], [80 x i8]* @findmtddevice.device, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %61)
store i8* getelementptr inbounds ([80 x i8], [80 x i8]* @findmtddevice.device, i64 0, i64 0), i8** %2
br label %67
63: ; preds = %55
%64 = load i32, i32* @stderr
%65 = load i8*, i8** %3
%66 = call i32 @fprintf(i32 %64, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i8* %65)
store i8* null, i8** %2
br label %67
67: ; preds = %63, %60, %12
%68 = load i8*, i8** %2
ret i8* %68
}

LLVM IR (-O2):
@findmtddevice.device = internal global [80 x i8] zeroinitializer
@.str = [10 x i8] c"/proc/mtd\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [30 x i8] c"setmac: open /proc/mtd failed\00"
@.str.3 = [8 x i8] c"/dev/%s\00"
@stderr = global i32 0
@.str.4 = [35 x i8] c"setmac: mtd device '%s' not found\0A\00"
define i8* @findmtddevice(i8* %0) {
%2 = alloca [80 x i8]
%3 = getelementptr inbounds [80 x i8], [80 x i8]* %2, i64 0, i64 0
%4 = 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))
%5 = icmp eq i32* %4, null
br i1 %5, label %9, label %6
6: ; preds = %1
%7 = call i64 @fgets(i8* %3, i32 80, i32* %4)
%8 = icmp eq i64 %7, 0
br i1 %8, label %31, label %11
9: ; preds = %1
%10 = tail call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
br label %35
11: ; preds = %6, %14
%12 = call i8* @strchr(i8* %3, i8 signext 58)
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %17
14: ; preds = %11, %17, %21, %25
%15 = call i64 @fgets(i8* %3, i32 80, i32* %4)
%16 = icmp eq i64 %15, 0
br i1 %16, label %31, label %11
17: ; preds = %11
%18 = getelementptr inbounds i8, i8* %12, i64 1
store i8 0, i8* %12
%19 = call i8* @strchr(i8* %18, i8 signext 34)
%20 = icmp eq i8* %19, null
br i1 %20, label %14, label %21
21: ; preds = %17
%22 = getelementptr inbounds i8, i8* %19, i64 1
%23 = call i8* @strchr(i8* %22, i8 signext 34)
%24 = icmp eq i8* %23, null
br i1 %24, label %14, label %25
25: ; preds = %21
store i8 0, i8* %23
%26 = call i64 @strcmp(i8* %22, i8* %0)
%27 = icmp eq i64 %26, 0
br i1 %27, label %28, label %14
28: ; preds = %25
%29 = call i32 @fclose(i32* %4)
%30 = call i32 @sprintf(i8* dereferenceable(1) getelementptr inbounds ([80 x i8], [80 x i8]* @findmtddevice.device, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %3)
br label %35
31: ; preds = %14, %6
%32 = call i32 @fclose(i32* %4)
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i8* %0)
br label %35
35: ; preds = %31, %28, %9
%36 = phi i8* [ getelementptr inbounds ([80 x i8], [80 x i8]* @findmtddevice.device, i64 0, i64 0), %28 ], [ null, %31 ], [ null, %9 ]
ret i8* %36
}