#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*/ fgets(char *, scalar_t__, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(char *, scalar_t__, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ strlen(char *);
int main(void) {
  FILE *stream;
  char string[] = "This is a test";
  char msg[20];
  stream = fopen("a.txt", "w+");
  fwrite(string, strlen(string), 1, stream);
  fseek(stream, 0, SEEK_SET);
  fgets(msg, strlen(string) + 1, stream);
  printf("%s", msg);
  fclose(stream);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe7a(%rip),%rax
mov    %rax,-0x1f(%rbp)
mov    0xe78(%rip),%eax
mov    %eax,-0x17(%rbp)
mov    0xe72(%rip),%ax
mov    %ax,-0x13(%rbp)
mov    0xe6a(%rip),%al
mov    %al,-0x11(%rbp)
lea    0xe61(%rip),%rdi
lea    0xe60(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
lea    -0x1f(%rbp),%rax
mov    %rax,-0x50(%rbp)
lea    -0x1f(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x50(%rbp),%rdi
mov    %rax,%rsi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edx
call   1090 <fwrite@plt>
mov    -0x10(%rbp),%rdi
mov    0x2df1(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <fseek@plt>
lea    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
lea    -0x1f(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x48(%rbp),%rdi
mov    %rax,%rsi
add    $0x1,%rsi
mov    -0x10(%rbp),%rdx
call   1060 <fgets@plt>
lea    -0x40(%rbp),%rsi
lea    0xdf0(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x28,%rsp
movabs $0x74736574206120,%rax
mov    %rax,0x7(%rsp)
movabs $0x2073692073696854,%rax
69 73 20
mov    %rax,(%rsp)
lea    0xe74(%rip),%rdi
lea    0xe73(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%rbx
mov    %rsp,%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %r14,%rdi
mov    %rax,%rsi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1090 <fwrite@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <fseek@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rsi
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    %rbx,%rdx
call   1060 <fgets@plt>
lea    0xe1a(%rip),%rdi
mov    %r14,%rsi
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@__const.main.string = [15 x i8] c"This is a test\00"
@.str = [6 x i8] c"a.txt\00"
@.str.1 = [3 x i8] c"w+\00"
@SEEK_SET = global i32 0
@.str.2 = [3 x i8] c"%s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [15 x i8]
%4 = alloca [20 x i8]
store i32 0, i32* %1
%5 = bitcast [15 x i8]* %3 to i8*
%6 = call i32* @fopen(i8* getelementptr inbounds ([6 x i8], [6 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 = getelementptr inbounds [15 x i8], [15 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [15 x i8], [15 x i8]* %3, i64 0, i64 0
%9 = call i64 @strlen(i8* %8)
%10 = load i32*, i32** %2
%11 = call i32 @fwrite(i8* %7, i64 %9, i32 1, i32* %10)
%12 = load i32*, i32** %2
%13 = load i32, i32* @SEEK_SET
%14 = call i32 @fseek(i32* %12, i32 0, i32 %13)
%15 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%16 = getelementptr inbounds [15 x i8], [15 x i8]* %3, i64 0, i64 0
%17 = call i64 @strlen(i8* %16)
%18 = add nsw i64 %17, 1
%19 = load i32*, i32** %2
%20 = call i32 @fgets(i8* %15, i64 %18, i32* %19)
%21 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%22 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %21)
%23 = load i32*, i32** %2
%24 = call i32 @fclose(i32* %23)
ret i32 0
}

LLVM IR (-O2):
@__const.main.string = [15 x i8] c"This is a test\00"
@.str = [6 x i8] c"a.txt\00"
@.str.1 = [3 x i8] c"w+\00"
@SEEK_SET = global i32 0
@.str.2 = [3 x i8] c"%s\00"
define i32 @main() {
%1 = alloca [15 x i8]
%2 = alloca [20 x i8]
%3 = getelementptr inbounds [15 x i8], [15 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 ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%6 = call i64 @strlen(i8* %3)
%7 = call i32 @fwrite(i8* %3, i64 %6, i32 1, i32* %5)
%8 = load i32, i32* @SEEK_SET
%9 = tail call i32 @fseek(i32* %5, i32 0, i32 %8)
%10 = call i64 @strlen(i8* %3)
%11 = add nsw i64 %10, 1
%12 = call i32 @fgets(i8* %4, i64 %11, i32* %5)
%13 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %4)
%14 = call i32 @fclose(i32* %5)
ret i32 0
}