#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*/ *);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int read_file(char *filename, char content[], int size) {
  int i;
  FILE *file;
  file = fopen(filename, "r");
  if (file == NULL) {
    return 1;
  }
  for (i = 0; i < size; i++) {
    content[i] = fgetc(file);
    if (content[i] == '\n') {
      content[i] = '\0';
      i = size;
    }
  }
  fclose(file);
  return 0;
}


x86 assembly (clang, -O0):
<read_file>:
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_file+0x3e>
movl   $0x1,-0x4(%rbp)
jmp    11e1 <read_file+0xb1>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    11d1 <read_file+0xa1>
mov    -0x28(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %al,%dl
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11be <read_file+0x8e>
mov    -0x18(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    11c3 <read_file+0x93>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1175 <read_file+0x45>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%ebx
mov    %rsi,%r14
lea    0xebc(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1190 <read_file+0x60>
mov    %rax,%r15
test   %ebx,%ebx
jle    1184 <read_file+0x54>
xor    %r12d,%r12d
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1040 <fgetc@plt>
movslq %ebp,%rcx
cmp    $0xa,%al
movzbl %al,%eax
cmove  %r12d,%eax
mov    %al,(%r14,%rcx,1)
mov    %ecx,%ebp
cmove  %ebx,%ebp
add    $0x1,%ebp
cmp    %ebx,%ebp
jl     1160 <read_file+0x30>
mov    %r15,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    1195 <read_file+0x65>
mov    $0x1,%eax
pop    %rbx
pop    %r12
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 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** %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 %47
15: ; preds = %3
store i32 0, i32* %8
br label %16
16: ; preds = %41, %15
%17 = load i32, i32* %8
%18 = load i32, i32* %7
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %44
20: ; preds = %16
%21 = load i32*, i32** %9
%22 = call signext i8 @fgetc(i32* %21)
%23 = load i8*, i8** %6
%24 = load i32, i32* %8
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
store i8 %22, i8* %26
%27 = load i8*, i8** %6
%28 = load i32, i32* %8
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = icmp eq i32 %32, 10
br i1 %33, label %34, label %40
34: ; preds = %20
%35 = load i8*, i8** %6
%36 = load i32, i32* %8
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %35, i64 %37
store i8 0, i8* %38
%39 = load i32, i32* %7
store i32 %39, i32* %8
br label %40
40: ; preds = %34, %20
br label %41
41: ; preds = %40
%42 = load i32, i32* %8
%43 = add nsw i32 %42, 1
store i32 %43, i32* %8
br label %16
44: ; preds = %16
%45 = load i32*, i32** %9
%46 = call i32 @fclose(i32* %45)
store i32 0, i32* %4
br label %47
47: ; preds = %44, %14
%48 = load i32, i32* %4
ret i32 %48
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i32 @read_file(i8* nocapture %0, i8* nocapture writeonly %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 %20, label %6
6: ; preds = %3
%7 = icmp sgt i32 %2, 0
br i1 %7, label %8, label %18
8: ; preds = %6, %8
%9 = phi i32 [ %16, %8 ], [ 0, %6 ]
%10 = tail call signext i8 @fgetc(i32* %4)
%11 = sext i32 %9 to i64
%12 = getelementptr inbounds i8, i8* %1, i64 %11
%13 = icmp eq i8 %10, 10
%14 = select i1 %13, i8 0, i8 %10
%15 = select i1 %13, i32 %2, i32 %9
store i8 %14, i8* %12
%16 = add nsw i32 %15, 1
%17 = icmp slt i32 %16, %2
br i1 %17, label %8, label %18
18: ; preds = %8, %6
%19 = tail call i32 @fclose(i32* %4)
br label %20
20: ; preds = %3, %18
%21 = phi i32 [ 0, %18 ], [ 1, %3 ]
ret i32 %21
}