#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 */
long INT_MAX;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ isdigit(char);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
long strtol(char *, int /*<<< orphan*/ *, int);
int find_major(void) {
  long num;
  FILE *df;
  enum { LEN = 50 };
  char buff[LEN], c, *p;
  char *major, *mname;
  if ((df = fopen("/proc/devices", "r")) == NULL) {
    fprintf(stderr, "usbmon"
                    ": Can't open /proc/devices\n");
    exit(1);
  }
  num = -1;
  while (fgets(buff, LEN, df) != NULL) {
    p = buff;
    major = NULL;
    mname = NULL;
    for (p = buff; (c = *p) != 0; p++) {
      if (major == NULL) {
        if (c != ' ') {
          major = p;
        }
      } else if (mname == NULL) {
        if (!isdigit(c) && c != ' ') {
          mname = p;
        }
      } else {
        if (c == '\n') {
          *p = 0;
          break;
        }
      }
    }
    if (major != NULL && mname != NULL) {
      if (strcmp(mname, "usbmon") == 0) {
        errno = 0;
        num = strtol(major, NULL, 10);
        if (errno != 0) {
          fprintf(stderr, "usbmon"
                          ": Syntax error "
                          "in /proc/devices\n");
          exit(1);
        }
        break;
      }
    }
  }
  fclose(df);
  if (num == -1) {
    fprintf(stderr, "usbmon"
                    ": Can't find usbmon in /proc/devices\n");
    exit(1);
  }
  if (num <= 0 || num > INT_MAX) {
    fprintf(stderr,
            "usbmon"
            ": Weird major %ld in /proc/devices\n",
            num);
    exit(1);
  }
  return (int)num;
}


x86 assembly (clang, -O0):
<find_major>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
lea    0xe71(%rip),%rdi
lea    0xe78(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11ca <find_major+0x4a>
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
lea    0xe57(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
lea    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x32,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     134d <find_major+0x1cd>
lea    -0x50(%rbp),%rax
mov    %rax,-0x60(%rbp)
movq   $0x0,-0x68(%rbp)
movq   $0x0,-0x70(%rbp)
lea    -0x50(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x51(%rbp)
movsbl %al,%eax
cmp    $0x0,%eax
je     12b8 <find_major+0x138>
cmpq   $0x0,-0x68(%rbp)
jne    1248 <find_major+0xc8>
movsbl -0x51(%rbp),%eax
cmp    $0x20,%eax
je     1243 <find_major+0xc3>
mov    -0x60(%rbp),%rax
mov    %rax,-0x68(%rbp)
jmp    12a2 <find_major+0x122>
cmpq   $0x0,-0x70(%rbp)
jne    127f <find_major+0xff>
movsbl -0x51(%rbp),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%eax
jne    127a <find_major+0xfa>
movsbl -0x51(%rbp),%eax
cmp    $0x20,%eax
je     127a <find_major+0xfa>
mov    -0x60(%rbp),%rax
mov    %rax,-0x70(%rbp)
jmp    129d <find_major+0x11d>
movsbl -0x51(%rbp),%eax
cmp    $0xa,%eax
jne    1298 <find_major+0x118>
mov    -0x60(%rbp),%rax
movb   $0x0,(%rax)
jmp    12b8 <find_major+0x138>
jmp    129d <find_major+0x11d>
jmp    12a2 <find_major+0x122>
jmp    12a7 <find_major+0x127>
mov    -0x60(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x60(%rbp)
jmp    120e <find_major+0x8e>
cmpq   $0x0,-0x68(%rbp)
je     1348 <find_major+0x1c8>
cmpq   $0x0,-0x70(%rbp)
je     1348 <find_major+0x1c8>
mov    -0x70(%rbp),%rdi
lea    0xd59(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1343 <find_major+0x1c3>
mov    0x2ce1(%rip),%rax
movq   $0x0,(%rax)
mov    -0x68(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1080 <strtol@plt>
mov    %rax,-0x8(%rbp)
mov    0x2cbd(%rip),%rax
cmpq   $0x0,(%rax)
je     133e <find_major+0x1be>
mov    0x2cb4(%rip),%rax
mov    (%rax),%edi
lea    0xd0c(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    134d <find_major+0x1cd>
jmp    1348 <find_major+0x1c8>
jmp    11d2 <find_major+0x52>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
cmpq   $0xffffffffffffffff,-0x8(%rbp)
jne    1382 <find_major+0x202>
mov    0x2c70(%rip),%rax
mov    (%rax),%edi
lea    0xcef(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x8(%rbp)
jle    13a1 <find_major+0x221>
mov    -0x8(%rbp),%rax
mov    0x2c50(%rip),%rcx
cmp    (%rcx),%rax
jle    13c6 <find_major+0x246>
mov    0x2c30(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xcd7(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x8(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_major>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
lea    0xe6b(%rip),%rdi
lea    0xe72(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     12fa <find_major+0x17a>
mov    %rax,%r12
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x32,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     12d1 <find_major+0x151>
xor    %r13d,%r13d
jmp    11e9 <find_major+0x69>
xchg   %ax,%ax
mov    %r14,%rdi
mov    $0x32,%esi
mov    %r12,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     12d1 <find_major+0x151>
mov    (%rsp),%r15b
test   %r15b,%r15b
je     11d0 <find_major+0x50>
xor    %edi,%edi
xor    %ebx,%ebx
mov    %r14,%rbp
jmp    1219 <find_major+0x99>
nopl   0x0(%rax,%rax,1)
cmp    $0x20,%r15b
mov    %rbp,%rbx
cmove  %r13,%rbx
movzbl 0x1(%rbp),%r15d
add    $0x1,%rbp
test   %r15b,%r15b
je     1252 <find_major+0xd2>
test   %rbx,%rbx
je     1200 <find_major+0x80>
test   %rdi,%rdi
je     1230 <find_major+0xb0>
cmp    $0xa,%r15b
jne    120b <find_major+0x8b>
jmp    124e <find_major+0xce>
nopl   0x0(%rax,%rax,1)
movsbl %r15b,%edi
call   1030 <isdigit@plt>
cmp    $0x20,%r15b
mov    $0x0,%edi
cmovne %rbp,%rdi
test   %eax,%eax
cmovne %r13,%rdi
jmp    120b <find_major+0x8b>
movb   $0x0,0x0(%rbp)
test   %rbx,%rbx
je     11d0 <find_major+0x50>
test   %rdi,%rdi
je     11d0 <find_major+0x50>
lea    0xdc7(%rip),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    11d0 <find_major+0x50>
mov    0x2d50(%rip),%rbp
movq   $0x0,0x0(%rbp)
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1080 <strtol@plt>
cmpq   $0x0,0x0(%rbp)
jne    130c <find_major+0x18c>
mov    %rax,%rbx
mov    %r12,%rdi
call   1040 <fclose@plt>
cmp    $0xffffffffffffffff,%rbx
je     12d9 <find_major+0x159>
test   %rbx,%rbx
jle    131e <find_major+0x19e>
mov    0x2d2d(%rip),%rax
cmp    (%rax),%rbx
jg     131e <find_major+0x19e>
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r12,%rdi
call   1040 <fclose@plt>
mov    0x2cf8(%rip),%rax
mov    (%rax),%edi
lea    0xd77(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2cd7(%rip),%rax
mov    (%rax),%edi
lea    0xd06(%rip),%rsi
jmp    12e9 <find_major+0x169>
mov    0x2cc5(%rip),%rax
mov    (%rax),%edi
lea    0xd1d(%rip),%rsi
jmp    12e9 <find_major+0x169>
mov    0x2cb3(%rip),%rax
mov    (%rax),%edi
lea    0xd5e(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/devices\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [34 x i8] c"usbmon: Can't open /proc/devices\0A\00"
@.str.3 = [7 x i8] c"usbmon\00"
@errno = global i64 0
@.str.4 = [39 x i8] c"usbmon: Syntax error in /proc/devices\0A\00"
@.str.5 = [44 x i8] c"usbmon: Can't find usbmon in /proc/devices\0A\00"
@INT_MAX = global i64 0
@.str.6 = [42 x i8] c"usbmon: Weird major %ld in /proc/devices\0A\00"
define i32 @find_major() {
%1 = alloca i64
%2 = alloca i32*
%3 = alloca [50 x i8]
%4 = alloca i8
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** %2
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %14
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %0
store i64 -1, i64* %1
br label %15
15: ; preds = %85, %14
%16 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
%17 = load i32*, i32** %2
%18 = call i32* @fgets(i8* %16, i32 50, i32* %17)
%19 = icmp ne i32* %18, null
br i1 %19, label %20, label %86
20: ; preds = %15
%21 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
store i8* %21, i8** %5
store i8* null, i8** %6
store i8* null, i8** %7
%22 = getelementptr inbounds [50 x i8], [50 x i8]* %3, i64 0, i64 0
store i8* %22, i8** %5
br label %23
23: ; preds = %61, %20
%24 = load i8*, i8** %5
%25 = load i8, i8* %24
store i8 %25, i8* %4
%26 = sext i8 %25 to i32
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %64
28: ; preds = %23
%29 = load i8*, i8** %6
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %38
31: ; preds = %28
%32 = load i8, i8* %4
%33 = sext i8 %32 to i32
%34 = icmp ne i32 %33, 32
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = load i8*, i8** %5
store i8* %36, i8** %6
br label %37
37: ; preds = %35, %31
br label %60
38: ; preds = %28
%39 = load i8*, i8** %7
%40 = icmp eq i8* %39, null
br i1 %40, label %41, label %52
41: ; preds = %38
%42 = load i8, i8* %4
%43 = call i32 @isdigit(i8 signext %42)
%44 = icmp ne i32 %43, 0
br i1 %44, label %51, label %45
45: ; preds = %41
%46 = load i8, i8* %4
%47 = sext i8 %46 to i32
%48 = icmp ne i32 %47, 32
br i1 %48, label %49, label %51
49: ; preds = %45
%50 = load i8*, i8** %5
store i8* %50, i8** %7
br label %51
51: ; preds = %49, %45, %41
br label %59
52: ; preds = %38
%53 = load i8, i8* %4
%54 = sext i8 %53 to i32
%55 = icmp eq i32 %54, 10
br i1 %55, label %56, label %58
56: ; preds = %52
%57 = load i8*, i8** %5
store i8 0, i8* %57
br label %64
58: ; preds = %52
br label %59
59: ; preds = %58, %51
br label %60
60: ; preds = %59, %37
br label %61
61: ; preds = %60
%62 = load i8*, i8** %5
%63 = getelementptr inbounds i8, i8* %62, i32 1
store i8* %63, i8** %5
br label %23
64: ; preds = %56, %23
%65 = load i8*, i8** %6
%66 = icmp ne i8* %65, null
br i1 %66, label %67, label %85
67: ; preds = %64
%68 = load i8*, i8** %7
%69 = icmp ne i8* %68, null
br i1 %69, label %70, label %85
70: ; preds = %67
%71 = load i8*, i8** %7
%72 = call i64 @strcmp(i8* %71, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%73 = icmp eq i64 %72, 0
br i1 %73, label %74, label %84
74: ; preds = %70
store i64 0, i64* @errno
%75 = load i8*, i8** %6
%76 = call i64 @strtol(i8* %75, i32* null, i32 10)
store i64 %76, i64* %1
%77 = load i64, i64* @errno
%78 = icmp ne i64 %77, 0
br i1 %78, label %79, label %83
79: ; preds = %74
%80 = load i32, i32* @stderr
%81 = call i32 (i32, i8*, ...) @fprintf(i32 %80, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0))
%82 = call i32 @exit(i32 1)
unreachable
83: ; preds = %74
br label %86
84: ; preds = %70
br label %85
85: ; preds = %84, %67, %64
br label %15
86: ; preds = %83, %15
%87 = load i32*, i32** %2
%88 = call i32 @fclose(i32* %87)
%89 = load i64, i64* %1
%90 = icmp eq i64 %89, -1
br i1 %90, label %91, label %95
91: ; preds = %86
%92 = load i32, i32* @stderr
%93 = call i32 (i32, i8*, ...) @fprintf(i32 %92, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.5, i64 0, i64 0))
%94 = call i32 @exit(i32 1)
unreachable
95: ; preds = %86
%96 = load i64, i64* %1
%97 = icmp sle i64 %96, 0
br i1 %97, label %102, label %98
98: ; preds = %95
%99 = load i64, i64* %1
%100 = load i64, i64* @INT_MAX
%101 = icmp sgt i64 %99, %100
br i1 %101, label %102, label %107
102: ; preds = %98, %95
%103 = load i32, i32* @stderr
%104 = load i64, i64* %1
%105 = call i32 (i32, i8*, ...) @fprintf(i32 %103, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.6, i64 0, i64 0), i64 %104)
%106 = call i32 @exit(i32 1)
unreachable
107: ; preds = %98
%108 = load i64, i64* %1
%109 = trunc i64 %108 to i32
ret i32 %109
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/devices\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [34 x i8] c"usbmon: Can't open /proc/devices\0A\00"
@.str.3 = [7 x i8] c"usbmon\00"
@errno = global i64 0
@.str.4 = [39 x i8] c"usbmon: Syntax error in /proc/devices\0A\00"
@.str.5 = [44 x i8] c"usbmon: Can't find usbmon in /proc/devices\0A\00"
@INT_MAX = global i64 0
@.str.6 = [42 x i8] c"usbmon: Weird major %ld in /proc/devices\0A\00"
define i32 @find_major() {
%1 = alloca [50 x i8]
%2 = getelementptr inbounds [50 x i8], [50 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %8, label %5
5: ; preds = %0
%6 = call i32* @fgets(i8* %2, i32 50, i32* %3)
%7 = icmp eq i32* %6, null
br i1 %7, label %15, label %12
8: ; preds = %0
%9 = load i32, i32* @stderr
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %5, %60
%13 = load i8, i8* %2
%14 = icmp eq i8 %13, 0
br i1 %14, label %60, label %17
15: ; preds = %60, %5
%16 = call i32 @fclose(i32* %3)
br label %66
17: ; preds = %12, %37
%18 = phi i8 [ %41, %37 ], [ %13, %12 ]
%19 = phi i8* [ %39, %37 ], [ null, %12 ]
%20 = phi i8* [ %38, %37 ], [ null, %12 ]
%21 = phi i8* [ %40, %37 ], [ %2, %12 ]
%22 = icmp eq i8* %20, null
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = icmp eq i8 %18, 32
%25 = select i1 %24, i8* null, i8* %21
br label %37
26: ; preds = %17
%27 = icmp eq i8* %19, null
br i1 %27, label %28, label %34
28: ; preds = %26
%29 = call i32 @isdigit(i8 signext %18)
%30 = icmp eq i32 %29, 0
%31 = icmp ne i8 %18, 32
%32 = select i1 %30, i1 %31, i1 false
%33 = select i1 %32, i8* %21, i8* null
br label %37
34: ; preds = %26
%35 = icmp eq i8 %18, 10
br i1 %35, label %36, label %37
36: ; preds = %34
store i8 0, i8* %21
br label %43
37: ; preds = %28, %23, %34
%38 = phi i8* [ %20, %34 ], [ %25, %23 ], [ %20, %28 ]
%39 = phi i8* [ %19, %34 ], [ %19, %23 ], [ %33, %28 ]
%40 = getelementptr inbounds i8, i8* %21, i64 1
%41 = load i8, i8* %40
%42 = icmp eq i8 %41, 0
br i1 %42, label %43, label %17
43: ; preds = %37, %36
%44 = phi i8* [ %20, %36 ], [ %38, %37 ]
%45 = phi i8* [ %19, %36 ], [ %39, %37 ]
%46 = icmp ne i8* %44, null
%47 = icmp ne i8* %45, null
%48 = select i1 %46, i1 %47, i1 false
br i1 %48, label %49, label %60
49: ; preds = %43
%50 = call i64 @strcmp(i8* %45, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%51 = icmp eq i64 %50, 0
br i1 %51, label %52, label %60
52: ; preds = %49
store i64 0, i64* @errno
%53 = call i64 @strtol(i8* nocapture %44, i32* null, i32 10)
%54 = load i64, i64* @errno
%55 = icmp eq i64 %54, 0
br i1 %55, label %63, label %56
56: ; preds = %52
%57 = load i32, i32* @stderr
%58 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0))
%59 = call i32 @exit(i32 1)
unreachable
60: ; preds = %12, %49, %43
%61 = call i32* @fgets(i8* %2, i32 50, i32* %3)
%62 = icmp eq i32* %61, null
br i1 %62, label %15, label %12
63: ; preds = %52
%64 = call i32 @fclose(i32* %3)
%65 = icmp eq i64 %53, -1
br i1 %65, label %66, label %70
66: ; preds = %15, %63
%67 = load i32, i32* @stderr
%68 = call i32 (i32, i8*, ...) @fprintf(i32 %67, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.5, i64 0, i64 0))
%69 = call i32 @exit(i32 1)
unreachable
70: ; preds = %63
%71 = icmp slt i64 %53, 1
%72 = load i64, i64* @INT_MAX
%73 = icmp sgt i64 %53, %72
%74 = select i1 %71, i1 true, i1 %73
br i1 %74, label %75, label %79
75: ; preds = %70
%76 = load i32, i32* @stderr
%77 = call i32 (i32, i8*, ...) @fprintf(i32 %76, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.6, i64 0, i64 0), i64 %53)
%78 = call i32 @exit(i32 1)
unreachable
79: ; preds = %70
%80 = trunc i64 %53 to i32
ret i32 %80
}