#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 */
char EOF;
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ perror(char *);
void read_matrix(char *ret, char *file_name, int chars_to_read) {
  FILE *fs;
  fs = fopen(file_name, "r");
  char c;
  int i = 0;
  if (fs == NULL) {
    perror("Error when opening file");
    return;
  }
  for (i = 0; i < chars_to_read; i++) {
    c = fgetc(fs);
    if (c == EOF)
      break;
    ret[i] = c;
  }
}


x86 assembly (clang, -O0):
<read_matrix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeb2(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    117a <read_matrix+0x4a>
lea    0xe92(%rip),%rdi
call   1050 <perror@plt>
jmp    11d0 <read_matrix+0xa0>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11d0 <read_matrix+0xa0>
mov    -0x20(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %al,-0x21(%rbp)
movsbl -0x21(%rbp),%eax
mov    0x2e44(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
jne    11b4 <read_matrix+0x84>
jmp    11d0 <read_matrix+0xa0>
mov    -0x21(%rbp),%dl
mov    -0x8(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1181 <read_matrix+0x51>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_matrix>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%ebp
mov    %rsi,%rax
mov    %rdi,%r14
lea    0xeb9(%rip),%rsi
mov    %rax,%rdi
call   1040 <fopen@plt>
test   %rax,%rax
je     1194 <read_matrix+0x64>
test   %ebp,%ebp
jle    118b <read_matrix+0x5b>
mov    %rax,%r15
mov    %ebp,%ebp
xor    %ebx,%ebx
mov    0x2e82(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1030 <fgetc@plt>
cmp    (%r12),%al
je     118b <read_matrix+0x5b>
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1170 <read_matrix+0x40>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe67(%rip),%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <perror@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [24 x i8] c"Error when opening file\00"
@EOF = global i8 0
define void @read_matrix(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i8
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%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** %7
store i32 0, i32* %9
%12 = load i32*, i32** %7
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %39
16: ; preds = %3
store i32 0, i32* %9
br label %17
17: ; preds = %36, %16
%18 = load i32, i32* %9
%19 = load i32, i32* %6
%20 = icmp slt i32 %18, %19
br i1 %20, label %21, label %39
21: ; preds = %17
%22 = load i32*, i32** %7
%23 = call signext i8 @fgetc(i32* %22)
store i8 %23, i8* %8
%24 = load i8, i8* %8
%25 = sext i8 %24 to i32
%26 = load i8, i8* @EOF
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %25, %27
br i1 %28, label %29, label %30
29: ; preds = %21
br label %39
30: ; preds = %21
%31 = load i8, i8* %8
%32 = load i8*, i8** %4
%33 = load i32, i32* %9
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
store i8 %31, i8* %35
br label %36
36: ; preds = %30
%37 = load i32, i32* %9
%38 = add nsw i32 %37, 1
store i32 %38, i32* %9
br label %17
39: ; preds = %14, %29, %17
ret void
}

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