#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*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ fputs(char const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int main() {
  FILE *out_stream = fopen("example_rewriting.txt", "w");
  if (out_stream != NULL) {
    const char phrase[] = "This is an apple.";
    fputs(phrase, out_stream);
    long position = ftell(out_stream);
    fseek(out_stream, position - 8, SEEK_SET);
    fprintf(out_stream, "%4s", " sam");
    fclose(out_stream);
    printf("Запись завершена\n");
  } else {
    perror("Ошибка открытия файла");
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe6a(%rip),%rdi
lea    0xe79(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1237 <main+0xb7>
mov    0xeb8(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xeb5(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xeb2(%rip),%ax
mov    %ax,-0x20(%rbp)
lea    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1050 <fputs@plt>
mov    -0x10(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
sub    $0x8,%rsi
mov    0x2de1(%rip),%rax
mov    (%rax),%edx
call   1080 <fseek@plt>
mov    -0x10(%rbp),%rdi
lea    0xe07(%rip),%rsi
lea    0xe04(%rip),%rdx
call   1060 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xdf4(%rip),%rdi
call   1040 <printf@plt>
jmp    1243 <main+0xc3>
lea    0xe04(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
lea    0xe74(%rip),%rdi
lea    0xe83(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     120c <main+0x8c>
mov    %rax,%rbx
movaps 0xec9(%rip),%xmm0
movaps %xmm0,(%rsp)
movw   $0x2e,0x10(%rsp)
mov    %rsp,%rdi
mov    %rax,%rsi
call   1050 <fputs@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
lea    -0x8(%rax),%rsi
mov    0x2e10(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
call   1080 <fseek@plt>
lea    0xe37(%rip),%rsi
lea    0xe34(%rip),%rdx
mov    %rbx,%rdi
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xe4b(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret
lea    0xe0e(%rip),%rdi
call   10a0 <perror@plt>
jmp    1204 <main+0x84>

LLVM IR (-O0):
@.str = [22 x i8] c"example_rewriting.txt\00"
@.str.1 = [2 x i8] c"w\00"
@__const.main.phrase = [18 x i8] c"This is an apple.\00"
@SEEK_SET = global i32 0
@.str.2 = [4 x i8] c"%4s\00"
@.str.3 = [5 x i8] c" sam\00"
@.str.4 = [33 x i8] c"\D0\97\D0\B0\D0\BF\D0\B8\D1\81\D1\8C \D0\B7\D0\B0\D0\B2\D0\B5\D1\80\D1\88\D0\B5\D0\BD\D0\B0\0A\00"
@.str.5 = [41 x i8] c"\D0\9E\D1\88\D0\B8\D0\B1\D0\BA\D0\B0 \D0\BE\D1\82\D0\BA\D1\80\D1\8B\D1\82\D0\B8\D1\8F \D1\84\D0\B0\D0\B9\D0\BB\D0\B0\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [18 x i8]
%4 = alloca i64
store i32 0, i32* %1
%5 = call i32* @fopen(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %2
%6 = load i32*, i32** %2
%7 = icmp ne i32* %6, null
br i1 %7, label %8, label %25
8: ; preds = %0
%9 = bitcast [18 x i8]* %3 to i8*
%10 = getelementptr inbounds [18 x i8], [18 x i8]* %3, i64 0, i64 0
%11 = load i32*, i32** %2
%12 = call i32 @fputs(i8* %10, i32* %11)
%13 = load i32*, i32** %2
%14 = call i64 @ftell(i32* %13)
store i64 %14, i64* %4
%15 = load i32*, i32** %2
%16 = load i64, i64* %4
%17 = sub nsw i64 %16, 8
%18 = load i32, i32* @SEEK_SET
%19 = call i32 @fseek(i32* %15, i64 %17, i32 %18)
%20 = load i32*, i32** %2
%21 = call i32 @fprintf(i32* %20, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%22 = load i32*, i32** %2
%23 = call i32 @fclose(i32* %22)
%24 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.4, i64 0, i64 0))
br label %27
25: ; preds = %0
%26 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0))
br label %27
27: ; preds = %25, %8
%28 = load i32, i32* %1
ret i32 %28
}

LLVM IR (-O2):
@.str = [22 x i8] c"example_rewriting.txt\00"
@.str.1 = [2 x i8] c"w\00"
@__const.main.phrase = [18 x i8] c"This is an apple.\00"
@SEEK_SET = global i32 0
@.str.2 = [4 x i8] c"%4s\00"
@.str.3 = [5 x i8] c" sam\00"
@.str.5 = [41 x i8] c"\D0\9E\D1\88\D0\B8\D0\B1\D0\BA\D0\B0 \D0\BE\D1\82\D0\BA\D1\80\D1\8B\D1\82\D0\B8\D1\8F \D1\84\D0\B0\D0\B9\D0\BB\D0\B0\00"
@str = [32 x i8] c"\D0\97\D0\B0\D0\BF\D0\B8\D1\81\D1\8C \D0\B7\D0\B0\D0\B2\D0\B5\D1\80\D1\88\D0\B5\D0\BD\D0\B0\00"
define i32 @main() {
%1 = alloca [18 x i8]
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %14, label %4
4: ; preds = %0
%5 = getelementptr inbounds [18 x i8], [18 x i8]* %1, i64 0, i64 0
%6 = call i32 @fputs(i8* %5, i32* %2)
%7 = tail call i64 @ftell(i32* %2)
%8 = add nsw i64 %7, -8
%9 = load i32, i32* @SEEK_SET
%10 = tail call i32 @fseek(i32* %2, i64 %8, i32 %9)
%11 = tail call i32 @fprintf(i32* %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%12 = tail call i32 @fclose(i32* %2)
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
br label %16
14: ; preds = %0
%15 = tail call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0))
br label %16
16: ; preds = %14, %4
ret i32 0
}