#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*/ filebuff;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int fread(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ sprintf(char *, char *, char *);
int /*<<< orphan*/ stderr;
int injetar_arquivo(char *buff, const char *arq) {
  int escrito = 0;
  FILE *f = fopen(arq, "r");
  if (f == NULL) {
    fprintf(stderr, "Erro ao abrir arquivo %s.\r\n", arq);
    return escrito;
  }
  char filebuff[1025] = {0};
  int read;
  filebuff[sizeof(filebuff) - 1] = 0;
  while ((read = fread(filebuff, sizeof(char), 1024, f)) > 0) {
    escrito += sprintf(buff + escrito, "%s", filebuff);
  }
  fclose(f);
  return escrito;
}


x86 assembly (clang, -O0):
<injetar_arquivo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe7b(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11bd <injetar_arquivo+0x5d>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe55(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    123c <injetar_arquivo+0xdc>
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0x401,%edx
call   1050 <memset@plt>
movb   $0x0,-0x30(%rbp)
lea    -0x430(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1030 <fread@plt>
mov    %eax,-0x434(%rbp)
cmp    $0x0,%eax
jle    122d <injetar_arquivo+0xcd>
mov    -0x10(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
lea    -0x430(%rbp),%rdx
lea    0xe08(%rip),%rsi
call   1080 <sprintf@plt>
mov    %rax,%rcx
movslq -0x1c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x1c(%rbp)
jmp    11d4 <injetar_arquivo+0x74>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<injetar_arquivo>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x410,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xe83(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     1207 <injetar_arquivo+0xa7>
mov    %rax,%r15
mov    %rsp,%r12
xor    %ebx,%ebx
mov    $0x401,%edx
mov    %r12,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jle    11fd <injetar_arquivo+0x9d>
xor    %ebx,%ebx
lea    0xe5b(%rip),%r12
mov    %rsp,%r13
cs nopw 0x0(%rax,%rax,1)
movslq %ebx,%rbx
lea    (%r14,%rbx,1),%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1080 <sprintf@plt>
add    %eax,%ebx
mov    %r13,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r15,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jg     11d0 <injetar_arquivo+0x70>
mov    %r15,%rdi
call   1040 <fclose@plt>
jmp    1221 <injetar_arquivo+0xc1>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
lea    0xdeb(%rip),%rsi
mov    %rbx,%rdx
call   1060 <fprintf@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x410,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"Erro ao abrir arquivo %s.\0D\0A\00"
@.str.2 = [3 x i8] c"%s\00"
define i32 @injetar_arquivo(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca [1025 x i8]
%9 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, 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
%12 = load i32*, i32** %7
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i8*, i8** %5
%17 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%18 = load i32, i32* %6
store i32 %18, i32* %3
br label %42
19: ; preds = %2
%20 = bitcast [1025 x i8]* %8 to i8*
%21 = getelementptr inbounds [1025 x i8], [1025 x i8]* %8, i64 0, i64 1024
store i8 0, i8* %21
br label %22
22: ; preds = %27, %19
%23 = getelementptr inbounds [1025 x i8], [1025 x i8]* %8, i64 0, i64 0
%24 = load i32*, i32** %7
%25 = call i32 @fread(i8* %23, i32 1, i32 1024, i32* %24)
store i32 %25, i32* %9
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %38
27: ; preds = %22
%28 = load i8*, i8** %4
%29 = load i32, i32* %6
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
%32 = getelementptr inbounds [1025 x i8], [1025 x i8]* %8, i64 0, i64 0
%33 = call i64 @sprintf(i8* %31, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %32)
%34 = load i32, i32* %6
%35 = sext i32 %34 to i64
%36 = add nsw i64 %35, %33
%37 = trunc i64 %36 to i32
store i32 %37, i32* %6
br label %22
38: ; preds = %22
%39 = load i32*, i32** %7
%40 = call i32 @fclose(i32* %39)
%41 = load i32, i32* %6
store i32 %41, i32* %3
br label %42
42: ; preds = %38, %14
%43 = load i32, i32* %3
ret i32 %43
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"Erro ao abrir arquivo %s.\0D\0A\00"
@.str.2 = [3 x i8] c"%s\00"
define i32 @injetar_arquivo(i8* %0, i8* %1) {
%3 = alloca [1025 x i8]
%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 %6, label %9
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %26
9: ; preds = %2
%10 = getelementptr inbounds [1025 x i8], [1025 x i8]* %3, i64 0, i64 0
%11 = getelementptr inbounds [1025 x i8], [1025 x i8]* %3, i64 0, i64 0
%12 = call i32 @fread(i8* %10, i32 1, i32 1024, i32* %4)
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %23
14: ; preds = %9, %14
%15 = phi i32 [ %20, %14 ], [ 0, %9 ]
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %0, i64 %16
%18 = call i64 @sprintf(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %10)
%19 = trunc i64 %18 to i32
%20 = add i32 %15, %19
%21 = call i32 @fread(i8* %10, i32 1, i32 1024, i32* %4)
%22 = icmp sgt i32 %21, 0
br i1 %22, label %14, label %23
23: ; preds = %14, %9
%24 = phi i32 [ 0, %9 ], [ %20, %14 ]
%25 = call i32 @fclose(i32* %4)
br label %26
26: ; preds = %23, %6
%27 = phi i32 [ 0, %6 ], [ %24, %23 ]
ret i32 %27
}