#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__ malloc(int);
int /*<<< orphan*/ to_lower(char *);
int readfile(char *filename) {
  char *line;
  FILE *file = NULL;
  file = fopen(filename, "r");
  if (file != NULL) {
    line = (char *)malloc(1024);
    while (1) {
      if (fgets(line, 1024, file))
        to_lower(line);
      else
        break;
    }
    fclose(file);
  }
  return (0);
}


x86 assembly (clang, -O0):
<readfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe91(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11ce <readfile+0x7e>
mov    $0x400,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x400,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     11bb <readfile+0x6b>
mov    -0x10(%rbp),%rdi
call   1040 <to_lower@plt>
jmp    11c0 <readfile+0x70>
jmp    11c5 <readfile+0x75>
jmp    1191 <readfile+0x41>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readfile>:
push   %r14
push   %rbx
push   %rax
lea    0xea5(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11a7 <readfile+0x57>
mov    %rax,%r14
mov    $0x400,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
nopl   0x0(%rax,%rax,1)
mov    $0x400,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     119f <readfile+0x4f>
mov    %rbx,%rdi
call   1040 <to_lower@plt>
mov    %rbx,%rdi
jmp    1180 <readfile+0x30>
mov    %r14,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i32 @readfile(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32*
store i8* %0, i8** %2
store i32* null, i32** %4
%5 = load i8*, i8** %2
%6 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %6, i32** %4
%7 = load i32*, i32** %4
%8 = icmp ne i32* %7, null
br i1 %8, label %9, label %25
9: ; preds = %1
%10 = call i64 @malloc(i32 1024)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %3
br label %12
12: ; preds = %9, %21
%13 = load i8*, i8** %3
%14 = load i32*, i32** %4
%15 = call i64 @fgets(i8* %13, i32 1024, i32* %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = load i8*, i8** %3
%19 = call i32 @to_lower(i8* %18)
br label %21
20: ; preds = %12
br label %22
21: ; preds = %17
br label %12
22: ; preds = %20
%23 = load i32*, i32** %4
%24 = call i32 @fclose(i32* %23)
br label %25
25: ; preds = %22, %1
ret i32 0
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i32 @readfile(i8* nocapture %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %15, label %4
4: ; preds = %1
%5 = tail call i64 @malloc(i32 1024)
%6 = inttoptr i64 %5 to i8*
%7 = tail call i64 @fgets(i8* %6, i32 1024, i32* %2)
%8 = icmp eq i64 %7, 0
br i1 %8, label %13, label %9
9: ; preds = %4, %9
%10 = tail call i32 @to_lower(i8* %6)
%11 = tail call i64 @fgets(i8* %6, i32 1024, i32* %2)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %9
13: ; preds = %9, %4
%14 = tail call i32 @fclose(i32* %2)
br label %15
15: ; preds = %13, %1
ret i32 0
}