#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*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ strlen(char *);
int read_file(char *sp, char *fname, int len) {
  FILE *fd;
  int bytes;
  bytes = 0;
  fd = fopen(fname, "r");
  if (fd) {
    while (fgets(sp, len, fd)) {
      len -= strlen(sp);
      bytes += strlen(sp);
      sp += strlen(sp);
    }
    fclose(fd);
  } else {
    bytes = -1;
  }
  return bytes;
}


x86 assembly (clang, -O0):
<read_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe9b(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11e8 <read_file+0xa8>
jmp    117e <read_file+0x3e>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     11da <read_file+0x9a>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
movslq -0x14(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
movslq -0x24(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
add    -0x8(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    117e <read_file+0x3e>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
jmp    11ef <read_file+0xaf>
movl   $0xffffffff,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%rax
mov    %rdi,%rbx
lea    0xeaa(%rip),%rsi
mov    %rax,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11ac <read_file+0x6c>
mov    %rax,%r14
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
xor    %r15d,%r15d
test   %rax,%rax
je     11a2 <read_file+0x62>
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1040 <strlen@plt>
sub    %eax,%ebp
add    %eax,%r15d
add    %rax,%rbx
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    1180 <read_file+0x40>
mov    %r14,%rdi
call   1030 <fclose@plt>
jmp    11b2 <read_file+0x72>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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