#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ free(char *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int sscanf(char *, char *, int *, char **);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char const *);
int /*<<< orphan*/ strncmp(char *, char const *, int /*<<< orphan*/);
int find_major_no(const char *name) {
  const char *chardev_str = "Character devices:";
  FILE *fp;
  char *line = NULL;
  size_t len = 0;
  int major = -1;
  int in_char = 0;
  int tmp_major;
  char *tmp_name;
  fp = fopen("/proc/devices", "r");
  if (!fp) {
    perror("find_major_no: Unable to open /proc/devices");
    return -1;
  }
  while (getline(&line, &len, fp) != -1) {
    if (!strncmp(line, chardev_str, strlen(chardev_str)))
      in_char = 1;
    else if (in_char) {
      if (sscanf(line, "%i %ms", &tmp_major, &tmp_name) < 2) {
        in_char = 0;
        continue;
      }
      if (!strncmp(tmp_name, name, strlen(name))) {
        major = tmp_major;
        free(tmp_name);
        break;
      }
      free(tmp_name);
    }
  }
  if (major < 0)
    fprintf(stderr, "Unable to find %s in /proc/devices\n", name);
  free(line);
  fclose(fp);
  return major;
}


x86 assembly (clang, -O0):
<find_major_no>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe5d(%rip),%rax
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0xffffffff,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
lea    0xe67(%rip),%rdi
lea    0xe40(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ff <find_major_no+0x6f>
lea    0xe27(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1325 <find_major_no+0x195>
jmp    1204 <find_major_no+0x74>
mov    -0x20(%rbp),%rdx
lea    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   10b0 <getline@plt>
cmp    $0xffffffff,%eax
je     12ea <find_major_no+0x15a>
mov    -0x28(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    %eax,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%eax
jne    125b <find_major_no+0xcb>
movl   $0x1,-0x38(%rbp)
jmp    12e5 <find_major_no+0x155>
cmpl   $0x0,-0x38(%rbp)
je     12e0 <find_major_no+0x150>
mov    -0x28(%rbp),%rdi
lea    0xdd1(%rip),%rsi
lea    -0x3c(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   1080 <sscanf@plt>
cmp    $0x2,%eax
jge    1292 <find_major_no+0x102>
movl   $0x0,-0x38(%rbp)
jmp    1204 <find_major_no+0x74>
mov    -0x48(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x68(%rbp),%rdi
mov    -0x60(%rbp),%rsi
mov    %eax,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%eax
jne    12d7 <find_major_no+0x147>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
jmp    12ea <find_major_no+0x15a>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
jmp    12e5 <find_major_no+0x155>
jmp    1204 <find_major_no+0x74>
cmpl   $0x0,-0x34(%rbp)
jge    130d <find_major_no+0x17d>
mov    0x2ce5(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd40(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_major_no>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,0x18(%rsp)
movq   $0x0,(%rsp)
movq   $0x0,0x20(%rsp)
lea    0xe78(%rip),%rdi
lea    0xe51(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     12e4 <find_major_no+0x154>
mov    %rax,%r14
mov    %rsp,%rdi
lea    0x20(%rsp),%rsi
mov    %rax,%rdx
call   10b0 <getline@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     12a8 <find_major_no+0x118>
xor    %ebx,%ebx
lea    0xe06(%rip),%r12
lea    0x20(%rsp),%r13
jmp    1225 <find_major_no+0x95>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rsp,%rdi
mov    %r13,%rsi
mov    %r14,%rdx
call   10b0 <getline@plt>
mov    %ebp,%ebx
cmp    $0xffffffff,%eax
je     12a3 <find_major_no+0x113>
mov    (%rsp),%r15
mov    %r12,%rdi
call   1060 <strlen@plt>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %eax,%edx
call   1040 <strncmp@plt>
mov    $0x1,%ebp
test   %eax,%eax
je     1210 <find_major_no+0x80>
xor    %ebp,%ebp
test   %ebx,%ebx
je     1210 <find_major_no+0x80>
mov    (%rsp),%rdi
lea    0xde9(%rip),%rsi
lea    0x14(%rsp),%rdx
lea    0x8(%rsp),%rcx
call   1080 <sscanf@plt>
cmp    $0x2,%eax
jl     1210 <find_major_no+0x80>
mov    0x8(%rsp),%r15
mov    0x18(%rsp),%rbx
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <strncmp@plt>
test   %eax,%eax
je     12f7 <find_major_no+0x167>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    $0x1,%ebp
jmp    1210 <find_major_no+0x80>
mov    $0xffffffff,%ebx
mov    0x2d31(%rip),%rax
mov    (%rax),%edi
lea    0xd90(%rip),%rsi
mov    0x18(%rsp),%rdx
call   1070 <fprintf@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd2a(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%ebx
jmp    12d3 <find_major_no+0x143>
mov    0x14(%rsp),%ebx
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
test   %ebx,%ebx
jns    12c2 <find_major_no+0x132>
jmp    12a8 <find_major_no+0x118>

LLVM IR (-O0):
@.str = [19 x i8] c"Character devices:\00"
@.str.1 = [14 x i8] c"/proc/devices\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [44 x i8] c"find_major_no: Unable to open /proc/devices\00"
@.str.4 = [7 x i8] c"%i %ms\00"
@stderr = global i32 0
@.str.5 = [36 x i8] c"Unable to find %s in /proc/devices\0A\00"
define i32 @find_major_no(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
store i8* %0, i8** %3
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8** %4
store i8* null, i8** %6
store i64 0, i64* %7
store i32 -1, i32* %8
store i32 0, i32* %9
%12 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %12, i32** %5
%13 = load i32*, i32** %5
%14 = icmp ne i32* %13, null
br i1 %14, label %17, label %15
15: ; preds = %1
%16 = call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %2
br label %67
17: ; preds = %1
br label %18
18: ; preds = %53, %37, %17
%19 = load i32*, i32** %5
%20 = call i32 @getline(i8** %6, i64* %7, i32* %19)
%21 = icmp ne i32 %20, -1
br i1 %21, label %22, label %54
22: ; preds = %18
%23 = load i8*, i8** %6
%24 = load i8*, i8** %4
%25 = load i8*, i8** %4
%26 = call i32 @strlen(i8* %25)
%27 = call i32 @strncmp(i8* %23, i8* %24, i32 %26)
%28 = icmp ne i32 %27, 0
br i1 %28, label %30, label %29
29: ; preds = %22
store i32 1, i32* %9
br label %53
30: ; preds = %22
%31 = load i32, i32* %9
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %52
33: ; preds = %30
%34 = load i8*, i8** %6
%35 = call i32 @sscanf(i8* %34, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32* %10, i8** %11)
%36 = icmp slt i32 %35, 2
br i1 %36, label %37, label %38
37: ; preds = %33
store i32 0, i32* %9
br label %18
38: ; preds = %33
%39 = load i8*, i8** %11
%40 = load i8*, i8** %3
%41 = load i8*, i8** %3
%42 = call i32 @strlen(i8* %41)
%43 = call i32 @strncmp(i8* %39, i8* %40, i32 %42)
%44 = icmp ne i32 %43, 0
br i1 %44, label %49, label %45
45: ; preds = %38
%46 = load i32, i32* %10
store i32 %46, i32* %8
%47 = load i8*, i8** %11
%48 = call i32 @free(i8* %47)
br label %54
49: ; preds = %38
%50 = load i8*, i8** %11
%51 = call i32 @free(i8* %50)
br label %52
52: ; preds = %49, %30
br label %53
53: ; preds = %52, %29
br label %18
54: ; preds = %45, %18
%55 = load i32, i32* %8
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %61
57: ; preds = %54
%58 = load i32, i32* @stderr
%59 = load i8*, i8** %3
%60 = call i32 @fprintf(i32 %58, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i8* %59)
br label %61
61: ; preds = %57, %54
%62 = load i8*, i8** %6
%63 = call i32 @free(i8* %62)
%64 = load i32*, i32** %5
%65 = call i32 @fclose(i32* %64)
%66 = load i32, i32* %8
store i32 %66, i32* %2
br label %67
67: ; preds = %61, %15
%68 = load i32, i32* %2
ret i32 %68
}

LLVM IR (-O2):
@.str = [19 x i8] c"Character devices:\00"
@.str.1 = [14 x i8] c"/proc/devices\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [44 x i8] c"find_major_no: Unable to open /proc/devices\00"
@.str.4 = [7 x i8] c"%i %ms\00"
@stderr = global i32 0
@.str.5 = [36 x i8] c"Unable to find %s in /proc/devices\0A\00"
define i32 @find_major_no(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i32
%5 = alloca i8*
%6 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%7 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%8 = bitcast i32* %4 to i8*
%9 = bitcast i8** %5 to i8*
%10 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %15, label %12
12: ; preds = %1
%13 = call i32 @getline(i8** %2, i64* %3, i32* %10)
%14 = icmp eq i32 %13, -1
br i1 %14, label %46, label %17
15: ; preds = %1
%16 = tail call i32 @perror(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0))
br label %55
17: ; preds = %12, %37
%18 = phi i32 [ %38, %37 ], [ 0, %12 ]
%19 = load i8*, i8** %2
%20 = call i32 @strlen(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%21 = call i32 @strncmp(i8* %19, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %20)
%22 = icmp eq i32 %21, 0
br i1 %22, label %37, label %23
23: ; preds = %17
%24 = icmp eq i32 %18, 0
br i1 %24, label %37, label %25
25: ; preds = %23
%26 = load i8*, i8** %2
%27 = call i32 @sscanf(i8* %26, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32* %4, i8** %5)
%28 = icmp slt i32 %27, 2
br i1 %28, label %37, label %29
29: ; preds = %25
%30 = load i8*, i8** %5
%31 = call i32 @strlen(i8* %0)
%32 = call i32 @strncmp(i8* %30, i8* %0, i32 %31)
%33 = icmp eq i32 %32, 0
br i1 %33, label %41, label %34
34: ; preds = %29
%35 = load i8*, i8** %5
%36 = call i32 @free(i8* %35)
br label %37
37: ; preds = %34, %23, %17, %25
%38 = phi i32 [ 0, %25 ], [ 1, %34 ], [ 0, %23 ], [ 1, %17 ]
%39 = call i32 @getline(i8** %2, i64* %3, i32* %10)
%40 = icmp eq i32 %39, -1
br i1 %40, label %46, label %17
41: ; preds = %29
%42 = load i32, i32* %4
%43 = load i8*, i8** %5
%44 = call i32 @free(i8* %43)
%45 = icmp slt i32 %42, 0
br i1 %45, label %46, label %50
46: ; preds = %37, %12, %41
%47 = phi i32 [ %42, %41 ], [ -1, %12 ], [ -1, %37 ]
%48 = load i32, i32* @stderr
%49 = call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0), i8* %0)
br label %50
50: ; preds = %46, %41
%51 = phi i32 [ %47, %46 ], [ %42, %41 ]
%52 = load i8*, i8** %2
%53 = call i32 @free(i8* %52)
%54 = call i32 @fclose(i32* %10)
br label %55
55: ; preds = %50, %15
%56 = phi i32 [ %51, %50 ], [ -1, %15 ]
ret i32 %56
}