#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 */
struct stat {
  int dummy;
};
typedef int /*<<< orphan*/ filename;
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 /*<<< orphan*/ snprintf(char *, int, char *);
int stat(char *, struct stat *);
int chrdev_get_major(void) {
  FILE *f;
  char filename[256];
  int pseudo;
  int rc = 0;
  int x;
  int majno;
  struct stat km;
  snprintf(filename, (sizeof(filename) / sizeof((filename)[0])),
           "/sys/bus/pseudo9/drivers/mhvtl/major");
  pseudo = stat(filename, &km);
  if (pseudo < 0)
    snprintf(filename, (sizeof(filename) / sizeof((filename)[0])),
             "/sys/bus/pseudo/drivers/mhvtl/major");
  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    $0x130,%rsp
movl   $0x0,-0x118(%rbp)
lea    -0x110(%rbp),%rdi
mov    $0x100,%esi
lea    0xe88(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x110(%rbp),%rdi
lea    -0x128(%rbp),%rsi
call   1060 <stat@plt>
mov    %eax,-0x114(%rbp)
cmpl   $0x0,-0x114(%rbp)
jge    11bb <chrdev_get_major+0x6b>
lea    -0x110(%rbp),%rdi
mov    $0x100,%esi
lea    0xe6f(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x110(%rbp),%rdi
lea    0xe5a(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11f0 <chrdev_get_major+0xa0>
xor    %eax,%eax
mov    0x2df2(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1247 <chrdev_get_major+0xf7>
mov    -0x10(%rbp),%rdi
lea    0xe4e(%rip),%rsi
lea    -0x120(%rbp),%rdx
call   1050 <fscanf@plt>
mov    %eax,-0x11c(%rbp)
cmpl   $0x0,-0x11c(%rbp)
jne    1229 <chrdev_get_major+0xd9>
movl   $0xffffffff,-0x118(%rbp)
ff ff ff
jmp    1235 <chrdev_get_major+0xe5>
mov    -0x120(%rbp),%eax
mov    %eax,-0x118(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x118(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chrdev_get_major>:
push   %rbp
push   %rbx
sub    $0x118,%rsp
lea    0xea0(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1040 <snprintf@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1060 <stat@plt>
test   %eax,%eax
jns    1199 <chrdev_get_major+0x49>
lea    0xe9b(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x100,%esi
call   1040 <snprintf@plt>
lea    0xe83(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11d9 <chrdev_get_major+0x89>
mov    %rax,%rbx
lea    0xe90(%rip),%rsi
lea    0x4(%rsp),%rdx
mov    %rax,%rdi
call   1050 <fscanf@plt>
cmp    $0x1,%eax
sbb    %ebp,%ebp
or     0x4(%rsp),%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    11e4 <chrdev_get_major+0x94>
mov    0x2df8(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
mov    %ebp,%eax
add    $0x118,%rsp
pop    %rbx
pop    %rbp
ret

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

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