#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 /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
char *strchr(char *, char);
int strlen(char *);
int sensors_read_line(const char *filename, int max, char *out) {
  char *p;
  FILE *f;
  f = fopen(filename, "rt");
  if (f == NULL) {
    return -1;
  }
  if (fgets(out, max, f) == NULL) {
    fclose(f);
    return -1;
  }
  fclose(f);
  p = strchr(out, '\n');
  if (p != NULL) {
    *p = '\0';
    return p - out;
  }
  return strlen(out);
}


x86 assembly (clang, -O0):
<sensors_read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe92(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    118e <sensors_read_line+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1209 <sensors_read_line+0xb9>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x30(%rbp),%rdx
call   1060 <fgets@plt>
cmp    $0x0,%rax
jne    11bd <sensors_read_line+0x6d>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1209 <sensors_read_line+0xb9>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11fd <sensors_read_line+0xad>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x4(%rbp)
jmp    1209 <sensors_read_line+0xb9>
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sensors_read_line>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%r15d
lea    0xe9c(%rip),%rsi
call   1070 <fopen@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
je     11af <sensors_read_line+0x5f>
mov    %rax,%rbp
mov    %r14,%rdi
mov    %r15d,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
mov    %rax,%r15
mov    %rbp,%rdi
call   1030 <fclose@plt>
test   %r15,%r15
je     11af <sensors_read_line+0x5f>
mov    %r14,%rdi
mov    $0xa,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     11bc <sensors_read_line+0x6c>
mov    %rax,%rbx
movb   $0x0,(%rax)
sub    %r14d,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <strlen@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"rt\00"
define i32 @sensors_read_line(i8* %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%10 = load i8*, i8** %5
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %9
%12 = load i32*, i32** %9
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 -1, i32* %4
br label %42
15: ; preds = %3
%16 = load i8*, i8** %7
%17 = load i32, i32* %6
%18 = load i32*, i32** %9
%19 = call i32* @fgets(i8* %16, i32 %17, i32* %18)
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %24
21: ; preds = %15
%22 = load i32*, i32** %9
%23 = call i32 @fclose(i32* %22)
store i32 -1, i32* %4
br label %42
24: ; preds = %15
%25 = load i32*, i32** %9
%26 = call i32 @fclose(i32* %25)
%27 = load i8*, i8** %7
%28 = call i8* @strchr(i8* %27, i8 signext 10)
store i8* %28, i8** %8
%29 = load i8*, i8** %8
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %39
31: ; preds = %24
%32 = load i8*, i8** %8
store i8 0, i8* %32
%33 = load i8*, i8** %8
%34 = load i8*, i8** %7
%35 = ptrtoint i8* %33 to i64
%36 = ptrtoint i8* %34 to i64
%37 = sub i64 %35, %36
%38 = trunc i64 %37 to i32
store i32 %38, i32* %4
br label %42
39: ; preds = %24
%40 = load i8*, i8** %7
%41 = call i32 @strlen(i8* %40)
store i32 %41, i32* %4
br label %42
42: ; preds = %39, %31, %21, %14
%43 = load i32, i32* %4
ret i32 %43
}

LLVM IR (-O2):
@.str = [3 x i8] c"rt\00"
define i32 @sensors_read_line(i8* nocapture %0, i32 %1, i8* %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %20, label %6
6: ; preds = %3
%7 = tail call i32* @fgets(i8* %2, i32 %1, i32* %4)
%8 = icmp eq i32* %7, null
%9 = tail call i32 @fclose(i32* %4)
br i1 %8, label %20, label %10
10: ; preds = %6
%11 = tail call i8* @strchr(i8* dereferenceable(1) %2, i8 signext 10)
%12 = icmp eq i8* %11, null
br i1 %12, label %18, label %13
13: ; preds = %10
store i8 0, i8* %11
%14 = ptrtoint i8* %11 to i64
%15 = ptrtoint i8* %2 to i64
%16 = sub i64 %14, %15
%17 = trunc i64 %16 to i32
br label %20
18: ; preds = %10
%19 = tail call i32 @strlen(i8* %2)
br label %20
20: ; preds = %6, %3, %18, %13
%21 = phi i32 [ %17, %13 ], [ %19, %18 ], [ -1, %3 ], [ -1, %6 ]
ret i32 %21
}