#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 ENOENT;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, int *);
int chrdev_get_major(void) {
  FILE *f;
  char *filename = "/sys/bus/pseudo/drivers/mhvtl/major";
  int rc = 0;
  int x;
  int majno;
  f = fopen(filename, "r");
  if (!f) {
    ;
    return -ENOENT;
  }
  x = fscanf(f, "%d", &majno);
  if (!x) {
    ;
    rc = -1;
  } else
    rc = majno;
  fclose(f);
  return rc;
}


x86 assembly (clang, -O0):
<chrdev_get_major>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xec1(%rip),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xecd(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    117c <chrdev_get_major+0x4c>
xor    %eax,%eax
mov    0x2e66(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11be <chrdev_get_major+0x8e>
mov    -0x10(%rbp),%rdi
lea    0xe9d(%rip),%rsi
lea    -0x24(%rbp),%rdx
call   1040 <fscanf@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    11a9 <chrdev_get_major+0x79>
movl   $0xffffffff,-0x1c(%rbp)
jmp    11af <chrdev_get_major+0x7f>
mov    -0x24(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chrdev_get_major>:
push   %rbp
push   %rbx
push   %rax
lea    0xec6(%rip),%rdi
lea    0xee1(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1175 <chrdev_get_major+0x45>
mov    %rax,%rbx
lea    0xecf(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
cmp    $0x1,%eax
sbb    %ebp,%ebp
or     0x4(%rsp),%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    1180 <chrdev_get_major+0x50>
mov    0x2e5c(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [36 x i8] c"/sys/bus/pseudo/drivers/mhvtl/major\00"
@.str.1 = [2 x i8] c"r\00"
@ENOENT = global i32 0
@.str.2 = [3 x i8] c"%d\00"
define i32 @chrdev_get_major() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8** %3
store i32 0, i32* %4
%7 = load i8*, i8** %3
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** %2
%9 = load i32*, i32** %2
%10 = icmp ne i32* %9, null
br i1 %10, label %14, label %11
11: ; preds = %0
%12 = load i32, i32* @ENOENT
%13 = sub nsw i32 0, %12
store i32 %13, i32* %1
br label %26
14: ; preds = %0
%15 = load i32*, i32** %2
%16 = call i32 @fscanf(i32* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %6)
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = icmp ne i32 %17, 0
br i1 %18, label %20, label %19
19: ; preds = %14
store i32 -1, i32* %4
br label %22
20: ; preds = %14
%21 = load i32, i32* %6
store i32 %21, i32* %4
br label %22
22: ; preds = %20, %19
%23 = load i32*, i32** %2
%24 = call i32 @fclose(i32* %23)
%25 = load i32, i32* %4
store i32 %25, i32* %1
br label %26
26: ; preds = %22, %11
%27 = load i32, i32* %1
ret i32 %27
}

LLVM IR (-O2):
@.str = [36 x i8] c"/sys/bus/pseudo/drivers/mhvtl/major\00"
@.str.1 = [2 x i8] c"r\00"
@ENOENT = global i32 0
@.str.2 = [3 x i8] c"%d\00"
define i32 @chrdev_get_major() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([36 x i8], [36 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 %5, label %8
5: ; preds = %0
%6 = load i32, i32* @ENOENT
%7 = sub nsw i32 0, %6
br label %14
8: ; preds = %0
%9 = call i32 @fscanf(i32* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %1)
%10 = icmp eq i32 %9, 0
%11 = load i32, i32* %1
%12 = select i1 %10, i32 -1, i32 %11
%13 = call i32 @fclose(i32* %3)
br label %14
14: ; preds = %8, %5
%15 = phi i32 [ %12, %8 ], [ %7, %5 ]
ret i32 %15
}