#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*/ uint8_t;
typedef int /*<<< orphan*/ devname;
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int O_NONBLOCK;
int O_RDWR;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, int /*<<< orphan*/);
int sscanf(char *, char *, int *, char *);
int /*<<< orphan*/ strcmp(char *, char *);
char *strerror(int /*<<< orphan*/);
int chrdev_open(char *name, uint8_t minor) {
  FILE *f;
  char devname[256];
  char buf[256];
  int devn;
  int ctlfd;
  f = fopen("/proc/devices", "r");
  if (!f) {
    printf("Cannot open control path to the driver: %s\n", strerror(errno));
    return -1;
  }
  devn = 0;
  while (!feof(f)) {
    if (!fgets(buf, sizeof(buf), f))
      break;
    if (sscanf(buf, "%d %s", &devn, devname) != 2)
      continue;
    if (!strcmp(devname, name))
      break;
    devn = 0;
  }
  fclose(f);
  if (!devn) {
    printf("Cannot find %s in /proc/devices - "
           "make sure the module is loaded\n",
           name);
    return -1;
  }
  snprintf(devname, sizeof(devname), "/dev/%s%d", name, minor);
  ctlfd = open(devname, O_RDWR | O_NONBLOCK);
  if (ctlfd < 0) {
    printf("Cannot open %s %s\n", devname, strerror(errno));
    fflush(NULL);
    printf("\n\n");
    return -1;
  }
  return ctlfd;
}


x86 assembly (clang, -O0):
<chrdev_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    0xe37(%rip),%rdi
lea    0xe3e(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    120f <chrdev_open+0x5f>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    %rax,%rsi
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    139b <chrdev_open+0x1eb>
movl   $0x0,-0x224(%rbp)
mov    -0x20(%rbp),%rdi
call   1080 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1237 <chrdev_open+0x87>
jmp    12b6 <chrdev_open+0x106>
lea    -0x220(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x100,%esi
call   1060 <fgets@plt>
cmp    $0x0,%eax
jne    125a <chrdev_open+0xaa>
jmp    12b6 <chrdev_open+0x106>
lea    -0x220(%rbp),%rdi
lea    -0x120(%rbp),%rcx
lea    0xdcd(%rip),%rsi
lea    -0x224(%rbp),%rdx
call   10a0 <sscanf@plt>
cmp    $0x2,%eax
je     1289 <chrdev_open+0xd9>
jmp    1219 <chrdev_open+0x69>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    12a7 <chrdev_open+0xf7>
jmp    12b6 <chrdev_open+0x106>
movl   $0x0,-0x224(%rbp)
jmp    1219 <chrdev_open+0x69>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
cmpl   $0x0,-0x224(%rbp)
jne    12ea <chrdev_open+0x13a>
mov    -0x10(%rbp),%rsi
lea    0xd6b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    139b <chrdev_open+0x1eb>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x14(%rbp),%r8d
mov    $0x100,%esi
lea    0xd7f(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x120(%rbp),%rdi
mov    0x2cd0(%rip),%rax
mov    (%rax),%esi
mov    0x2cbf(%rip),%rax
or     (%rax),%esi
call   10b0 <open@plt>
mov    %eax,-0x228(%rbp)
cmpl   $0x0,-0x228(%rbp)
jge    1392 <chrdev_open+0x1e2>
lea    -0x120(%rbp),%rax
mov    %rax,-0x230(%rbp)
mov    0x2c80(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    -0x230(%rbp),%rsi
mov    %rax,%rdx
lea    0xd26(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <fflush@plt>
lea    0xd22(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    139b <chrdev_open+0x1eb>
mov    -0x228(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chrdev_open>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %esi,%ebx
mov    %rdi,%r15
lea    0xe23(%rip),%rdi
lea    0xe2a(%rip),%rsi
call   10d0 <fopen@plt>
test   %rax,%rax
je     12a3 <chrdev_open+0xe3>
mov    %rax,%rbp
mov    %ebx,0xc(%rsp)
movl   $0x0,0x8(%rsp)
mov    %rax,%rdi
call   1090 <feof@plt>
test   %eax,%eax
je     12c0 <chrdev_open+0x100>
mov    %rbp,%rdi
call   1040 <fclose@plt>
cmpl   $0x0,0x8(%rsp)
je     133b <chrdev_open+0x17b>
lea    0xe59(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %r15,%rcx
mov    0xc(%rsp),%r8d
call   1060 <snprintf@plt>
mov    0x2d9c(%rip),%rax
mov    0x2d8d(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   10c0 <open@plt>
test   %eax,%eax
jns    1351 <chrdev_open+0x191>
mov    0x2d62(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xe12(%rip),%rdi
lea    0x10(%rsp),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   10a0 <fflush@plt>
lea    0xde9(%rip),%rdi
call   1030 <puts@plt>
jmp    134c <chrdev_open+0x18c>
mov    0x2d26(%rip),%rax
mov    (%rax),%edi
call   10e0 <strerror@plt>
lea    0xd58(%rip),%rdi
mov    %rax,%rsi
jmp    1345 <chrdev_open+0x185>
lea    0x110(%rsp),%rbx
lea    0xd6d(%rip),%r12
lea    0x8(%rsp),%r13
lea    0x10(%rsp),%r14
jmp    12f0 <chrdev_open+0x130>
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   1090 <feof@plt>
test   %eax,%eax
jne    1211 <chrdev_open+0x51>
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %rbp,%rdx
call   1070 <fgets@plt>
test   %eax,%eax
je     1211 <chrdev_open+0x51>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
mov    %r14,%rcx
call   10b0 <sscanf@plt>
cmp    $0x2,%eax
jne    12e0 <chrdev_open+0x120>
mov    %r14,%rdi
mov    %r15,%rsi
call   1080 <strcmp@plt>
test   %eax,%eax
je     1211 <chrdev_open+0x51>
movl   $0x0,0x8(%rsp)
jmp    12e0 <chrdev_open+0x120>
lea    0xd00(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%eax
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/proc/devices\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [44 x i8] c"Cannot open control path to the driver: %s\0A\00"
@errno = global i32 0
@.str.3 = [6 x i8] c"%d %s\00"
@.str.4 = [66 x i8] c"Cannot find %s in /proc/devices - make sure the module is loaded\0A\00"
@.str.5 = [10 x i8] c"/dev/%s%d\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str.6 = [19 x i8] c"Cannot open %s %s\0A\00"
@.str.7 = [3 x i8] c"\0A\0A\00"
define i32 @chrdev_open(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca [256 x i8]
%8 = alloca [256 x i8]
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%11 = 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* %11, i32** %6
%12 = load i32*, i32** %6
%13 = icmp ne i32* %12, null
br i1 %13, label %18, label %14
14: ; preds = %2
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i8* %16)
store i32 -1, i32* %3
br label %72
18: ; preds = %2
store i32 0, i32* %9
br label %19
19: ; preds = %42, %35, %18
%20 = load i32*, i32** %6
%21 = call i32 @feof(i32* %20)
%22 = icmp ne i32 %21, 0
%23 = xor i1 %22, true
br i1 %23, label %24, label %43
24: ; preds = %19
%25 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%26 = load i32*, i32** %6
%27 = call i32 @fgets(i8* %25, i32 256, i32* %26)
%28 = icmp ne i32 %27, 0
br i1 %28, label %30, label %29
29: ; preds = %24
br label %43
30: ; preds = %24
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%33 = call i32 @sscanf(i8* %31, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32* %9, i8* %32)
%34 = icmp ne i32 %33, 2
br i1 %34, label %35, label %36
35: ; preds = %30
br label %19
36: ; preds = %30
%37 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%38 = load i8*, i8** %4
%39 = call i32 @strcmp(i8* %37, i8* %38)
%40 = icmp ne i32 %39, 0
br i1 %40, label %42, label %41
41: ; preds = %36
br label %43
42: ; preds = %36
store i32 0, i32* %9
br label %19
43: ; preds = %41, %29, %19
%44 = load i32*, i32** %6
%45 = call i32 @fclose(i32* %44)
%46 = load i32, i32* %9
%47 = icmp ne i32 %46, 0
br i1 %47, label %51, label %48
48: ; preds = %43
%49 = load i8*, i8** %4
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([66 x i8], [66 x i8]* @.str.4, i64 0, i64 0), i8* %49)
store i32 -1, i32* %3
br label %72
51: ; preds = %43
%52 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%53 = load i8*, i8** %4
%54 = load i32, i32* %5
%55 = call i32 @snprintf(i8* %52, i32 256, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %53, i32 %54)
%56 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%57 = load i32, i32* @O_RDWR
%58 = load i32, i32* @O_NONBLOCK
%59 = or i32 %57, %58
%60 = call i32 @open(i8* %56, i32 %59)
store i32 %60, i32* %10
%61 = load i32, i32* %10
%62 = icmp slt i32 %61, 0
br i1 %62, label %63, label %70
63: ; preds = %51
%64 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%65 = load i32, i32* @errno
%66 = call i8* @strerror(i32 %65)
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i8* %64, i8* %66)
%68 = call i32 @fflush(i32* null)
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0))
store i32 -1, i32* %3
br label %72
70: ; preds = %51
%71 = load i32, i32* %10
store i32 %71, i32* %3
br label %72
72: ; preds = %70, %63, %48, %14
%73 = load i32, i32* %3
ret i32 %73
}

LLVM IR (-O2):
@.str = [14 x i8] c"/proc/devices\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [44 x i8] c"Cannot open control path to the driver: %s\0A\00"
@errno = global i32 0
@.str.3 = [6 x i8] c"%d %s\00"
@.str.4 = [66 x i8] c"Cannot find %s in /proc/devices - make sure the module is loaded\0A\00"
@.str.5 = [10 x i8] c"/dev/%s%d\00"
@O_RDWR = global i32 0
@O_NONBLOCK = global i32 0
@.str.6 = [19 x i8] c"Cannot open %s %s\0A\00"
@str = [2 x i8] c"\0A\00"
define i32 @chrdev_open(i8* %0, i32 %1) {
%3 = alloca [256 x i8]
%4 = alloca [256 x i8]
%5 = alloca i32
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%8 = bitcast i32* %5 to i8*
%9 = 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))
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i32, i32* @errno
%13 = tail call i8* @strerror(i32 %12)
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0), i8* %13)
br label %50
15: ; preds = %2
store i32 0, i32* %5
%16 = call i32 @feof(i32* %9)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %31
18: ; preds = %15, %28
%19 = call i32 @fgets(i8* %7, i32 256, i32* %9)
%20 = icmp eq i32 %19, 0
br i1 %20, label %31, label %21
21: ; preds = %18
%22 = call i32 @sscanf(i8* %7, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32* %5, i8* %6)
%23 = icmp eq i32 %22, 2
br i1 %23, label %24, label %28
24: ; preds = %21
%25 = call i32 @strcmp(i8* %6, i8* dereferenceable(1) %0)
%26 = icmp eq i32 %25, 0
br i1 %26, label %31, label %27
27: ; preds = %24
store i32 0, i32* %5
br label %28
28: ; preds = %27, %21
%29 = call i32 @feof(i32* %9)
%30 = icmp eq i32 %29, 0
br i1 %30, label %18, label %31
31: ; preds = %28, %18, %24, %15
%32 = call i32 @fclose(i32* %9)
%33 = load i32, i32* %5
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([66 x i8], [66 x i8]* @.str.4, i64 0, i64 0), i8* %0)
br label %50
37: ; preds = %31
%38 = call i32 @snprintf(i8* %6, i32 256, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), i8* %0, i32 %1)
%39 = load i32, i32* @O_RDWR
%40 = load i32, i32* @O_NONBLOCK
%41 = or i32 %40, %39
%42 = call i32 @open(i8* %6, i32 %41)
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %50
44: ; preds = %37
%45 = load i32, i32* @errno
%46 = call i8* @strerror(i32 %45)
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), i8* %6, i8* %46)
%48 = call i32 @fflush(i32* null)
%49 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str, i64 0, i64 0))
br label %50
50: ; preds = %37, %44, %35, %11
%51 = phi i32 [ -1, %44 ], [ -1, %35 ], [ -1, %11 ], [ %42, %37 ]
ret i32 %51
}