#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*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, scalar_t__, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, scalar_t__, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
scalar_t__ strlen(char *);
int main() {
  FILE *fp;
  char c[] = "ejemplo fwrite/fread";
  char buffer[20];
  fp = fopen("datos.dat", "w+");
  if (fp == NULL)
    return -1;
  fwrite(c, strlen(c) + 1, 1, fp);
  rewind(fp);
  fread(buffer, strlen(c) + 1, 1, fp);
  printf("%s\n", buffer);
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe7a(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe77(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xe75(%rip),%eax
mov    %eax,-0x20(%rbp)
mov    0xe70(%rip),%al
mov    %al,-0x1c(%rbp)
lea    0xe67(%rip),%rdi
lea    0xe6a(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d5 <main+0x65>
movl   $0xffffffff,-0x4(%rbp)
jmp    1252 <main+0xe2>
lea    -0x30(%rbp),%rax
mov    %rax,-0x60(%rbp)
lea    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x60(%rbp),%rdi
mov    %rax,%rsi
add    $0x1,%rsi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edx
call   1090 <fwrite@plt>
mov    -0x10(%rbp),%rdi
call   1070 <rewind@plt>
lea    -0x50(%rbp),%rax
mov    %rax,-0x58(%rbp)
lea    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x58(%rbp),%rdi
mov    %rax,%rsi
add    $0x1,%rsi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
lea    -0x50(%rbp),%rsi
lea    0xde5(%rip),%rdi
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x38,%rsp
movaps 0xe82(%rip),%xmm0
movaps %xmm0,(%rsp)
movabs $0x64616572662f65,%rax
mov    %rax,0xd(%rsp)
lea    0xe7d(%rip),%rdi
lea    0xe80(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     120f <main+0x9f>
mov    %rax,%rbx
mov    %rsp,%r14
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rsi
mov    %r14,%rdi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1090 <fwrite@plt>
mov    %rbx,%rdi
call   1070 <rewind@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rsi
lea    0x20(%rsp),%r14
mov    %r14,%rdi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
lea    0xe27(%rip),%rdi
mov    %r14,%rsi
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    1214 <main+0xa4>
mov    $0xffffffff,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@__const.main.c = [21 x i8] c"ejemplo fwrite/fread\00"
@.str = [10 x i8] c"datos.dat\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [21 x i8]
%4 = alloca [20 x i8]
store i32 0, i32* %1
%5 = bitcast [21 x i8]* %3 to i8*
%6 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %2
%7 = load i32*, i32** %2
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %10
9: ; preds = %0
store i32 -1, i32* %1
br label %29
10: ; preds = %0
%11 = getelementptr inbounds [21 x i8], [21 x i8]* %3, i64 0, i64 0
%12 = getelementptr inbounds [21 x i8], [21 x i8]* %3, i64 0, i64 0
%13 = call i64 @strlen(i8* %12)
%14 = add nsw i64 %13, 1
%15 = load i32*, i32** %2
%16 = call i32 @fwrite(i8* %11, i64 %14, i32 1, i32* %15)
%17 = load i32*, i32** %2
%18 = call i32 @rewind(i32* %17)
%19 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%20 = getelementptr inbounds [21 x i8], [21 x i8]* %3, i64 0, i64 0
%21 = call i64 @strlen(i8* %20)
%22 = add nsw i64 %21, 1
%23 = load i32*, i32** %2
%24 = call i32 @fread(i8* %19, i64 %22, i32 1, i32* %23)
%25 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%26 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %25)
%27 = load i32*, i32** %2
%28 = call i32 @fclose(i32* %27)
store i32 0, i32* %1
br label %29
29: ; preds = %10, %9
%30 = load i32, i32* %1
ret i32 %30
}

LLVM IR (-O2):
@__const.main.c = [21 x i8] c"ejemplo fwrite/fread\00"
@.str = [10 x i8] c"datos.dat\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [4 x i8] c"%s\0A\00"
define i32 @main() {
%1 = alloca [21 x i8]
%2 = alloca [20 x i8]
%3 = getelementptr inbounds [21 x i8], [21 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [20 x i8], [20 x i8]* %2, i64 0, i64 0
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %17, label %7
7: ; preds = %0
%8 = call i64 @strlen(i8* %3)
%9 = add nsw i64 %8, 1
%10 = call i32 @fwrite(i8* %3, i64 %9, i32 1, i32* %5)
%11 = tail call i32 @rewind(i32* %5)
%12 = call i64 @strlen(i8* %3)
%13 = add nsw i64 %12, 1
%14 = call i32 @fread(i8* %4, i64 %13, i32 1, i32* %5)
%15 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %4)
%16 = call i32 @fclose(i32* %5)
br label %17
17: ; preds = %0, %7
%18 = phi i32 [ 0, %7 ], [ -1, %0 ]
ret i32 %18
}