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

/* Variables and functions */
int errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int getline(char **, size_t *, int /*<<< orphan*/ *);
int strlen(char *);
char *mu_badv_kmod_version(int *const error) {
  if (error) {
    *error = 0;
  };
  FILE *fp;
  char *line = NULL;
  size_t len = 0;
  ssize_t read;
  fp = fopen("/sys/module/batman_adv/version", "r");
  if (!fp) {
    if (error) {
      *error = errno;
    };
    return NULL;
  }
  if ((read = getline(&line, &len, fp)) != -1) {
    fclose(fp);
    line[strlen(line) - 1] = '\0';
    return line;
  } else {
    if (error) {
      *error = errno;
    };
    fclose(fp);
    return NULL;
  }
}


x86 assembly (clang, -O0):
<mu_badv_kmod_version>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1161 <mu_badv_kmod_version+0x21>
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
lea    0xe88(%rip),%rdi
lea    0xea0(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11ba <mu_badv_kmod_version+0x7a>
cmpq   $0x0,-0x10(%rbp)
je     11ad <mu_badv_kmod_version+0x6d>
mov    0x2e3b(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1239 <mu_badv_kmod_version+0xf9>
mov    -0x18(%rbp),%rdx
lea    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1060 <getline@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0xffffffff,%eax
je     120e <mu_badv_kmod_version+0xce>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1239 <mu_badv_kmod_version+0xf9>
cmpq   $0x0,-0x10(%rbp)
je     1228 <mu_badv_kmod_version+0xe8>
mov    0x2dc0(%rip),%rax
mov    (%rax),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mu_badv_kmod_version>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
test   %rdi,%rdi
je     1156 <mu_badv_kmod_version+0x16>
movl   $0x0,(%r14)
movq   $0x0,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0xe91(%rip),%rdi
lea    0xea9(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     11bd <mu_badv_kmod_version+0x7d>
mov    %rax,%rbx
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
mov    %rax,%rdx
call   1060 <getline@plt>
cmp    $0xffffffff,%eax
je     11d0 <mu_badv_kmod_version+0x90>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    0x8(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    0x8(%rsp),%rax
jmp    11eb <mu_badv_kmod_version+0xab>
test   %r14,%r14
je     11e9 <mu_badv_kmod_version+0xa9>
mov    0x2e17(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%r14)
jmp    11e9 <mu_badv_kmod_version+0xa9>
test   %r14,%r14
je     11e1 <mu_badv_kmod_version+0xa1>
mov    0x2e04(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%r14)
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [31 x i8] c"/sys/module/batman_adv/version\00"
@.str.1 = [2 x i8] c"r\00"
@errno = global i32 0
define i8* @mu_badv_kmod_version(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
store i32* %0, i32** %3
%8 = load i32*, i32** %3
%9 = icmp ne i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = load i32*, i32** %3
store i32 0, i32* %11
br label %12
12: ; preds = %10, %1
store i8* null, i8** %5
store i64 0, i64* %6
%13 = call i32* @fopen(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %4
%14 = load i32*, i32** %4
%15 = icmp ne i32* %14, null
br i1 %15, label %23, label %16
16: ; preds = %12
%17 = load i32*, i32** %3
%18 = icmp ne i32* %17, null
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = load i32, i32* @errno
%21 = load i32*, i32** %3
store i32 %20, i32* %21
br label %22
22: ; preds = %19, %16
store i8* null, i8** %2
br label %46
23: ; preds = %12
%24 = load i32*, i32** %4
%25 = call i32 @getline(i8** %5, i64* %6, i32* %24)
store i32 %25, i32* %7
%26 = icmp ne i32 %25, -1
br i1 %26, label %27, label %37
27: ; preds = %23
%28 = load i32*, i32** %4
%29 = call i32 @fclose(i32* %28)
%30 = load i8*, i8** %5
%31 = load i8*, i8** %5
%32 = call i32 @strlen(i8* %31)
%33 = sub nsw i32 %32, 1
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %30, i64 %34
store i8 0, i8* %35
%36 = load i8*, i8** %5
store i8* %36, i8** %2
br label %46
37: ; preds = %23
%38 = load i32*, i32** %3
%39 = icmp ne i32* %38, null
br i1 %39, label %40, label %43
40: ; preds = %37
%41 = load i32, i32* @errno
%42 = load i32*, i32** %3
store i32 %41, i32* %42
br label %43
43: ; preds = %40, %37
%44 = load i32*, i32** %4
%45 = call i32 @fclose(i32* %44)
store i8* null, i8** %2
br label %46
46: ; preds = %43, %27, %22
%47 = load i8*, i8** %2
ret i8* %47
}

LLVM IR (-O2):
@.str = [31 x i8] c"/sys/module/batman_adv/version\00"
@.str.1 = [2 x i8] c"r\00"
@errno = global i32 0
define i8* @mu_badv_kmod_version(i32* writeonly %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = icmp eq i32* %0, null
br i1 %4, label %6, label %5
5: ; preds = %1
store i32 0, i32* %0
br label %6
6: ; preds = %5, %1
%7 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%8 = bitcast i64* %3 to i8*
store i64 0, i64* %3
%9 = tail call i32* @fopen(i8* getelementptr inbounds ([31 x i8], [31 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 %14
11: ; preds = %6
br i1 %4, label %30, label %12
12: ; preds = %11
%13 = load i32, i32* @errno
store i32 %13, i32* %0
br label %30
14: ; preds = %6
%15 = call i32 @getline(i8** %2, i64* %3, i32* %9)
%16 = icmp eq i32 %15, -1
br i1 %16, label %25, label %17
17: ; preds = %14
%18 = call i32 @fclose(i32* %9)
%19 = load i8*, i8** %2
%20 = call i32 @strlen(i8* %19)
%21 = add nsw i32 %20, -1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %19, i64 %22
store i8 0, i8* %23
%24 = load i8*, i8** %2
br label %30
25: ; preds = %14
br i1 %4, label %28, label %26
26: ; preds = %25
%27 = load i32, i32* @errno
store i32 %27, i32* %0
br label %28
28: ; preds = %26, %25
%29 = call i32 @fclose(i32* %9)
br label %30
30: ; preds = %11, %12, %28, %17
%31 = phi i8* [ %24, %17 ], [ null, %28 ], [ null, %12 ], [ null, %11 ]
ret i8* %31
}