#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*/ *fopen(char const *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int read_str(const char *filename, char *val, int len) {
  FILE *fp;
  int ret;
  fp = fopen(filename, "r");
  if (!fp)
    return -1;
  ret = fread(val, 1, len - 1, fp);
  while (ret > 0 && val[ret - 1] == '\n')
    ret--;
  val[ret++] = 0;
  fclose(fp);
  return ret;
}


x86 assembly (clang, -O0):
<read_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeb2(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    116e <read_str+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f5 <read_str+0xc5>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
sub    $0x1,%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    %eax,-0x2c(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x2c(%rbp)
mov    %al,-0x2d(%rbp)
jle    11b2 <read_str+0x82>
mov    -0x18(%rbp),%rax
mov    -0x2c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
sete   %al
mov    %al,-0x2d(%rbp)
mov    -0x2d(%rbp),%al
test   $0x1,%al
jne    11c2 <read_str+0x92>
jmp    11d0 <read_str+0xa0>
mov    -0x2c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x2c(%rbp)
jmp    1189 <read_str+0x59>
mov    -0x18(%rbp),%rax
mov    -0x2c(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x2c(%rbp)
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_str>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebp
mov    %rsi,%rbx
lea    0xec0(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     119b <read_str+0x6b>
mov    %rax,%r14
add    $0xffffffff,%ebp
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rax,%rcx
call   1030 <fread@plt>
mov    %eax,%ecx
xor    %ebp,%ebp
test   %eax,%eax
cmovs  %eax,%ebp
nopl   0x0(%rax,%rax,1)
test   %ecx,%ecx
jle    1187 <read_str+0x57>
lea    -0x1(%rcx),%rax
cmpb   $0xa,-0x1(%rbx,%rcx,1)
mov    %rax,%rcx
je     1170 <read_str+0x40>
add    $0x1,%eax
mov    %eax,%ebp
movslq %ebp,%rax
add    $0x1,%ebp
movb   $0x0,(%rbx,%rax,1)
mov    %r14,%rdi
call   1040 <fclose@plt>
jmp    11a0 <read_str+0x70>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i32 @read_str(i8* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %28, label %6
6: ; preds = %3
%7 = add nsw i32 %2, -1
%8 = tail call i32 @fread(i8* %1, i32 1, i32 %7, i32* %4)
%9 = zext i32 %8 to i64
%10 = call i32 @llvm.smin.i32(i32 %8, i32 0)
br label %11
11: ; preds = %15, %6
%12 = phi i64 [ %16, %15 ], [ %9, %6 ]
%13 = trunc i64 %12 to i32
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %22
15: ; preds = %11
%16 = add nsw i64 %12, -1
%17 = getelementptr inbounds i8, i8* %1, i64 %16
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 10
br i1 %19, label %11, label %20
20: ; preds = %15
%21 = trunc i64 %12 to i32
br label %22
22: ; preds = %11, %20
%23 = phi i32 [ %21, %20 ], [ %10, %11 ]
%24 = add nsw i32 %23, 1
%25 = sext i32 %23 to i64
%26 = getelementptr inbounds i8, i8* %1, i64 %25
store i8 0, i8* %26
%27 = tail call i32 @fclose(i32* %4)
br label %28
28: ; preds = %3, %22
%29 = phi i32 [ %24, %22 ], [ -1, %3 ]
ret i32 %29
}