#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_CUR;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ fputc(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ unlink(char *);
void example(int fixUndefinedBehavior) {
  FILE *fs = fopen("fileio-readwrite.temp", "w+");
  fprintf(fs, "123456789012345678901234567890");
  fseek(fs, 0L, SEEK_SET);
  for (int i = 0; i < 5; i++) {
    fputc('x', fs);
    fputc('x', fs);
    fputc('x', fs);
    if (fixUndefinedBehavior)
      fseek(fs, 0L, SEEK_CUR);
    int in = fgetc(fs);
    if (fixUndefinedBehavior)
      fseek(fs, 0L, SEEK_CUR);
    printf("read a byte; in = '%c'\n", (char)in);
  }
  fseek(fs, 0L, SEEK_SET);
  char buf[256] = "";
  fread(buf, 1, 256, fs);
  printf("buf: '%s'\n", buf);
  fclose(fs);
  unlink("fileio-readwrite.temp");
}


x86 assembly (clang, -O0):
<example>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %edi,-0x4(%rbp)
lea    0xe4b(%rip),%rdi
lea    0xe5a(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe49(%rip),%rsi
call   10a0 <fprintf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2df8(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10b0 <fseek@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x5,-0x14(%rbp)
jge    1294 <example+0xf4>
mov    -0x10(%rbp),%rsi
mov    $0x78,%edi
call   1090 <fputc@plt>
mov    -0x10(%rbp),%rsi
mov    $0x78,%edi
call   1090 <fputc@plt>
mov    -0x10(%rbp),%rsi
mov    $0x78,%edi
call   1090 <fputc@plt>
cmpl   $0x0,-0x4(%rbp)
je     1246 <example+0xa6>
mov    -0x10(%rbp),%rdi
mov    0x2dad(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10b0 <fseek@plt>
mov    -0x10(%rbp),%rdi
call   1080 <fgetc@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     1272 <example+0xd2>
mov    -0x10(%rbp),%rdi
mov    0x2d81(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10b0 <fseek@plt>
mov    -0x18(%rbp),%eax
movsbl %al,%esi
lea    0xdb9(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11f2 <example+0x52>
mov    -0x10(%rbp),%rdi
mov    0x2d39(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10b0 <fseek@plt>
lea    -0x120(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1070 <memset@plt>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x100,%edx
call   1040 <fread@plt>
lea    -0x120(%rbp),%rsi
lea    0xd6b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
lea    0xd04(%rip),%rdi
call   1030 <unlink@plt>
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<example>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    %edi,%r15d
lea    0xe55(%rip),%rdi
lea    0xe64(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%r13
lea    0xe58(%rip),%rdi
mov    $0x1e,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10b0 <fwrite@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%edx
mov    %r13,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    $0x5,%ebx
lea    0xe46(%rip),%r14
mov    0x2def(%rip),%r12
jmp    1239 <example+0xa9>
nopl   0x0(%rax,%rax,1)
mov    (%r12),%edx
mov    %r13,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %r13,%rdi
call   1070 <fgetc@plt>
mov    %eax,%ebp
mov    (%r12),%edx
mov    %r13,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
movsbl %bpl,%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0xffffffff,%ebx
je     1271 <example+0xe1>
mov    $0x78,%edi
mov    %r13,%rsi
call   1080 <fputc@plt>
mov    $0x78,%edi
mov    %r13,%rsi
call   1080 <fputc@plt>
mov    $0x78,%edi
mov    %r13,%rsi
call   1080 <fputc@plt>
test   %r15d,%r15d
jne    1200 <example+0x70>
mov    %r13,%rdi
call   1070 <fgetc@plt>
mov    %eax,%ebp
jmp    1226 <example+0x96>
mov    0x2d60(%rip),%rax
mov    (%rax),%edx
mov    %r13,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    $0x1,%esi
mov    $0x100,%edx
mov    %r13,%rcx
call   1040 <fread@plt>
lea    0xd43(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r13,%rdi
call   1050 <fclose@plt>
lea    0xcda(%rip),%rdi
call   1030 <unlink@plt>
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [22 x i8] c"fileio-readwrite.temp\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [31 x i8] c"123456789012345678901234567890\00"
@SEEK_SET = global i32 0
@SEEK_CUR = global i32 0
@.str.3 = [24 x i8] c"read a byte; in = '%c'\0A\00"
@.str.4 = [11 x i8] c"buf: '%s'\0A\00"
define void @example(i32 %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca [256 x i8]
store i32 %0, i32* %2
%7 = call i32* @fopen(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %3
%8 = load i32*, i32** %3
%9 = call i32 @fprintf(i32* %8, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%10 = load i32*, i32** %3
%11 = load i32, i32* @SEEK_SET
%12 = call i32 @fseek(i32* %10, i64 0, i32 %11)
store i32 0, i32* %4
br label %13
13: ; preds = %43, %1
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 5
br i1 %15, label %16, label %46
16: ; preds = %13
%17 = load i32*, i32** %3
%18 = call i32 @fputc(i8 signext 120, i32* %17)
%19 = load i32*, i32** %3
%20 = call i32 @fputc(i8 signext 120, i32* %19)
%21 = load i32*, i32** %3
%22 = call i32 @fputc(i8 signext 120, i32* %21)
%23 = load i32, i32* %2
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %16
%26 = load i32*, i32** %3
%27 = load i32, i32* @SEEK_CUR
%28 = call i32 @fseek(i32* %26, i64 0, i32 %27)
br label %29
29: ; preds = %25, %16
%30 = load i32*, i32** %3
%31 = call i32 @fgetc(i32* %30)
store i32 %31, i32* %5
%32 = load i32, i32* %2
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %29
%35 = load i32*, i32** %3
%36 = load i32, i32* @SEEK_CUR
%37 = call i32 @fseek(i32* %35, i64 0, i32 %36)
br label %38
38: ; preds = %34, %29
%39 = load i32, i32* %5
%40 = trunc i32 %39 to i8
%41 = sext i8 %40 to i32
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %41)
br label %43
43: ; preds = %38
%44 = load i32, i32* %4
%45 = add nsw i32 %44, 1
store i32 %45, i32* %4
br label %13
46: ; preds = %13
%47 = load i32*, i32** %3
%48 = load i32, i32* @SEEK_SET
%49 = call i32 @fseek(i32* %47, i64 0, i32 %48)
%50 = bitcast [256 x i8]* %6 to i8*
%51 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%52 = load i32*, i32** %3
%53 = call i32 @fread(i8* %51, i32 1, i32 256, i32* %52)
%54 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* %54)
%56 = load i32*, i32** %3
%57 = call i32 @fclose(i32* %56)
%58 = call i32 @unlink(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str = [22 x i8] c"fileio-readwrite.temp\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [31 x i8] c"123456789012345678901234567890\00"
@SEEK_SET = global i32 0
@SEEK_CUR = global i32 0
@.str.3 = [24 x i8] c"read a byte; in = '%c'\0A\00"
@.str.4 = [11 x i8] c"buf: '%s'\0A\00"
define void @example(i32 %0) {
%2 = alloca [256 x i8]
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%4 = tail call i64 @fwrite(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i64 30, i64 1, i32* %3)
%5 = load i32, i32* @SEEK_SET
%6 = tail call i32 @fseek(i32* %3, i64 0, i32 %5)
%7 = icmp eq i32 %0, 0
br label %16
8: ; preds = %29
%9 = load i32, i32* @SEEK_SET
%10 = tail call i32 @fseek(i32* %3, i64 0, i32 %9)
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%12 = call i32 @fread(i8* %11, i32 1, i32 256, i32* %3)
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* %11)
%14 = call i32 @fclose(i32* %3)
%15 = call i32 @unlink(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
ret void
16: ; preds = %1, %29
%17 = phi i32 [ 0, %1 ], [ %34, %29 ]
%18 = tail call i32 @fputc(i8 signext 120, i32* %3)
%19 = tail call i32 @fputc(i8 signext 120, i32* %3)
%20 = tail call i32 @fputc(i8 signext 120, i32* %3)
br i1 %7, label %21, label %23
21: ; preds = %16
%22 = tail call i32 @fgetc(i32* %3)
br label %29
23: ; preds = %16
%24 = load i32, i32* @SEEK_CUR
%25 = tail call i32 @fseek(i32* %3, i64 0, i32 %24)
%26 = tail call i32 @fgetc(i32* %3)
%27 = load i32, i32* @SEEK_CUR
%28 = tail call i32 @fseek(i32* %3, i64 0, i32 %27)
br label %29
29: ; preds = %21, %23
%30 = phi i32 [ %22, %21 ], [ %26, %23 ]
%31 = shl i32 %30, 24
%32 = ashr exact i32 %31, 24
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %32)
%34 = add nuw nsw i32 %17, 1
%35 = icmp eq i32 %34, 5
br i1 %35, label %8, label %16
}