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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ err(int /*<<< orphan*/, char *);
int /*<<< orphan*/ errx(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fprintf(int /*<<< orphan*/ *, char *, char const *);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
scalar_t__ strcmp(char *, char const *);
size_t strlen(char const *);
void fprintf__simple_string(void) {
  FILE *file;
  char buffer[128];
  size_t length;
  const char *contents = "This is a message\n";
  file = fopen("test.txt", "w+");
  if (fprintf(file, "%s", contents) <= 0)
    err(EXIT_FAILURE, "fprintf failed to write to file");
  rewind(file);
  length = fread(buffer, 1, sizeof(buffer) - 1, file);
  if (length != strlen(contents))
    err(EXIT_FAILURE, "fread failed");
  buffer[length] = '\0';
  fclose(file);
  if (strcmp(buffer, contents) != 0)
    errx(EXIT_FAILURE, "Written and read data differ");
}


x86 assembly (clang, -O0):
<fprintf__simple_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
lea    0xe5e(%rip),%rax
mov    %rax,-0xa0(%rbp)
lea    0xe63(%rip),%rdi
lea    0xe65(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xa0(%rbp),%rdx
lea    0xe4d(%rip),%rsi
call   1090 <fprintf@plt>
cmp    $0x0,%rax
jg     11f6 <fprintf__simple_string+0x66>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
lea    0xe31(%rip),%rsi
call   10a0 <err@plt>
mov    -0x8(%rbp),%rdi
call   1070 <rewind@plt>
lea    -0x90(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0x7f,%edx
call   1030 <fread@plt>
mov    %rax,-0x98(%rbp)
mov    -0x98(%rbp),%rax
mov    %rax,-0xa8(%rbp)
mov    -0xa0(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0xa8(%rbp),%rax
cmp    %rcx,%rax
je     1262 <fprintf__simple_string+0xd2>
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
lea    0xde5(%rip),%rsi
call   10a0 <err@plt>
mov    -0x98(%rbp),%rax
movb   $0x0,-0x90(%rbp,%rax,1)
mov    -0x8(%rbp),%rdi
call   1040 <fclose@plt>
lea    -0x90(%rbp),%rdi
mov    -0xa0(%rbp),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     12ac <fprintf__simple_string+0x11c>
mov    0x2d42(%rip),%rax
mov    (%rax),%edi
lea    0xda8(%rip),%rsi
call   1060 <errx@plt>
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fprintf__simple_string>:
push   %r14
push   %rbx
sub    $0x88,%rsp
lea    0xe82(%rip),%rdi
lea    0xe84(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%r14
lea    0xe78(%rip),%rsi
lea    0xe52(%rip),%rdx
mov    %rax,%rdi
call   1080 <fprintf@plt>
test   %rax,%rax
jg     11d0 <fprintf__simple_string+0x50>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
lea    0xe57(%rip),%rsi
call   1090 <err@plt>
mov    %r14,%rdi
call   1060 <rewind@plt>
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x7f,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
mov    %rax,%rbx
cmp    $0x12,%rax
je     120b <fprintf__simple_string+0x8b>
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
lea    0xe3c(%rip),%rsi
call   1090 <err@plt>
movb   $0x0,(%rsp,%rbx,1)
mov    %r14,%rdi
call   1040 <fclose@plt>
lea    0xde2(%rip),%rsi
mov    %rsp,%rdi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1240 <fprintf__simple_string+0xc0>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
lea    0xe14(%rip),%rsi
call   1050 <errx@plt>
add    $0x88,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [19 x i8] c"This is a message\0A\00"
@.str.1 = [9 x i8] c"test.txt\00"
@.str.2 = [3 x i8] c"w+\00"
@.str.3 = [3 x i8] c"%s\00"
@EXIT_FAILURE = global i32 0
@.str.4 = [32 x i8] c"fprintf failed to write to file\00"
@.str.5 = [13 x i8] c"fread failed\00"
@.str.6 = [29 x i8] c"Written and read data differ\00"
define void @fprintf__simple_string() {
%1 = alloca i32*
%2 = alloca [128 x i8]
%3 = alloca i64
%4 = alloca i8*
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8** %4
%5 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %5, i32** %1
%6 = load i32*, i32** %1
%7 = load i8*, i8** %4
%8 = call i64 @fprintf(i32* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %7)
%9 = icmp sle i64 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = load i32, i32* @EXIT_FAILURE
%12 = call i32 @err(i32 %11, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
br label %13
13: ; preds = %10, %0
%14 = load i32*, i32** %1
%15 = call i32 @rewind(i32* %14)
%16 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%17 = load i32*, i32** %1
%18 = call i64 @fread(i8* %16, i32 1, i32 127, i32* %17)
store i64 %18, i64* %3
%19 = load i64, i64* %3
%20 = load i8*, i8** %4
%21 = call i64 @strlen(i8* %20)
%22 = icmp ne i64 %19, %21
br i1 %22, label %23, label %26
23: ; preds = %13
%24 = load i32, i32* @EXIT_FAILURE
%25 = call i32 @err(i32 %24, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
br label %26
26: ; preds = %23, %13
%27 = load i64, i64* %3
%28 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 %27
store i8 0, i8* %28
%29 = load i32*, i32** %1
%30 = call i32 @fclose(i32* %29)
%31 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%32 = load i8*, i8** %4
%33 = call i64 @strcmp(i8* %31, i8* %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %26
%36 = load i32, i32* @EXIT_FAILURE
%37 = call i32 @errx(i32 %36, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0))
br label %38
38: ; preds = %35, %26
ret void
}

LLVM IR (-O2):
@.str = [19 x i8] c"This is a message\0A\00"
@.str.1 = [9 x i8] c"test.txt\00"
@.str.2 = [3 x i8] c"w+\00"
@.str.3 = [3 x i8] c"%s\00"
@EXIT_FAILURE = global i32 0
@.str.4 = [32 x i8] c"fprintf failed to write to file\00"
@.str.5 = [13 x i8] c"fread failed\00"
@.str.6 = [29 x i8] c"Written and read data differ\00"
define void @fprintf__simple_string() {
%1 = alloca [128 x i8]
%2 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%4 = tail call i64 @fprintf(i32* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%5 = icmp slt i64 %4, 1
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = load i32, i32* @EXIT_FAILURE
%8 = tail call i32 @err(i32 %7, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
br label %9
9: ; preds = %6, %0
%10 = tail call i32 @rewind(i32* %3)
%11 = call i64 @fread(i8* %2, i32 1, i32 127, i32* %3)
%12 = icmp eq i64 %11, 18
br i1 %12, label %16, label %13
13: ; preds = %9
%14 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @err(i32 %14, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
br label %16
16: ; preds = %13, %9
%17 = getelementptr inbounds [128 x i8], [128 x i8]* %1, i64 0, i64 %11
store i8 0, i8* %17
%18 = tail call i32 @fclose(i32* %3)
%19 = call i64 @strcmp(i8* %2, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%20 = icmp eq i64 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %16
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @errx(i32 %22, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0))
br label %24
24: ; preds = %21, %16
ret void
}