#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*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ putc(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ remove(char const *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
scalar_t__ strcmp(char *, char *);
int main(void) {
  FILE *f;
  int i;
  const char filename[] = "/tmp/bug3.test";
  f = fopen(filename, "w+");
  for (i = 0; i < 9000; i++)
    putc('x', f);
  fseek(f, 8180L, 0);
  fwrite("Where does this text go?", 1, 24, f);
  fflush(f);
  rewind(f);
  for (i = 0; i < 9000; i++) {
    int j;
    if ((j = getc(f)) != 'x') {
      if (i != 8180) {
        printf("Test FAILED!");
        return 1;
      } else {
        char buf[25];
        buf[0] = j;
        fread(buf + 1, 1, 23, f);
        buf[24] = '\0';
        if (strcmp(buf, "Where does this text go?") != 0) {
          printf("%s\nTest FAILED!\n", buf);
          return 1;
        }
        i += 23;
      }
    }
  }
  fclose(f);
  remove(filename);
  puts("Test succeeded.");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe1a(%rip),%rax
mov    %rax,-0x23(%rbp)
mov    0xe18(%rip),%eax
mov    %eax,-0x1b(%rbp)
mov    0xe12(%rip),%ax
mov    %ax,-0x17(%rbp)
mov    0xe0a(%rip),%al
mov    %al,-0x15(%rbp)
lea    -0x23(%rbp),%rdi
lea    0xdfd(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x2328,-0x14(%rbp)
jge    124b <main+0x7b>
mov    -0x10(%rbp),%rsi
mov    $0x78,%edi
call   10a0 <putc@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1222 <main+0x52>
mov    -0x10(%rbp),%rdi
mov    $0x1ff4,%esi
xor    %edx,%edx
call   10c0 <fseek@plt>
mov    -0x10(%rbp),%rcx
lea    0xdac(%rip),%rdi
mov    $0x1,%esi
mov    $0x18,%edx
call   10e0 <fwrite@plt>
mov    -0x10(%rbp),%rdi
call   10b0 <fflush@plt>
mov    -0x10(%rbp),%rdi
call   1080 <rewind@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x2328,-0x14(%rbp)
jge    1355 <main+0x185>
mov    -0x10(%rbp),%rdi
call   10f0 <getc@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x78,%eax
je     1342 <main+0x172>
cmpl   $0x1ff4,-0x14(%rbp)
je     12d7 <main+0x107>
lea    0xd67(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    137a <main+0x1aa>
mov    -0x28(%rbp),%eax
mov    %al,-0x50(%rbp)
lea    -0x50(%rbp),%rdi
add    $0x1,%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0x17,%edx
call   1050 <fread@plt>
movb   $0x0,-0x38(%rbp)
lea    -0x50(%rbp),%rdi
lea    0xd0b(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     1334 <main+0x164>
lea    -0x50(%rbp),%rsi
lea    0xd17(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    137a <main+0x1aa>
mov    -0x14(%rbp),%eax
add    $0x17,%eax
mov    %eax,-0x14(%rbp)
jmp    1342 <main+0x172>
jmp    1347 <main+0x177>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    128e <main+0xbe>
mov    -0x10(%rbp),%rdi
call   1060 <fclose@plt>
lea    -0x23(%rbp),%rdi
call   1030 <remove@plt>
lea    0xcdb(%rip),%rdi
call   1040 <puts@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x30,%rsp
movabs $0x747365742e3367,%rax
mov    %rax,0x7(%rsp)
movabs $0x6775622f706d742f,%rax
62 75 67
mov    %rax,(%rsp)
lea    0xe0e(%rip),%rsi
mov    %rsp,%rdi
call   10d0 <fopen@plt>
mov    %rax,%r13
mov    $0x2328,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    $0x78,%edi
mov    %r13,%rsi
call   10a0 <putc@plt>
add    $0xffffffff,%ebx
jne    1220 <main+0x50>
xor    %ebx,%ebx
mov    $0x1ff4,%esi
mov    %r13,%rdi
xor    %edx,%edx
call   10c0 <fseek@plt>
lea    0xdc8(%rip),%r14
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x18,%edx
mov    %r13,%rcx
call   10e0 <fwrite@plt>
mov    %r13,%rdi
call   10b0 <fflush@plt>
mov    %r13,%rdi
call   1080 <rewind@plt>
lea    0x11(%rsp),%r15
lea    0x10(%rsp),%r12
jmp    128d <main+0xbd>
nopl   0x0(%rax,%rax,1)
lea    0x1(%rbx),%eax
cmp    $0x2327,%ebx
mov    %eax,%ebx
jge    12d7 <main+0x107>
mov    %r13,%rdi
call   10f0 <getc@plt>
cmp    $0x78,%eax
je     1280 <main+0xb0>
cmp    $0x1ff4,%ebx
jne    12f7 <main+0x127>
mov    %al,0x10(%rsp)
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x17,%edx
mov    %r13,%rcx
call   1050 <fread@plt>
movb   $0x0,0x28(%rsp)
mov    %r12,%rdi
mov    %r14,%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
jne    1307 <main+0x137>
mov    $0x200b,%ebx
jmp    1280 <main+0xb0>
mov    %r13,%rdi
call   1060 <fclose@plt>
mov    %rsp,%rdi
call   1030 <remove@plt>
lea    0xd5b(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
jmp    131f <main+0x14f>
lea    0xd2d(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    131a <main+0x14a>
lea    0xd2a(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@__const.main.filename = [15 x i8] c"/tmp/bug3.test\00"
@.str = [3 x i8] c"w+\00"
@.str.1 = [25 x i8] c"Where does this text go?\00"
@.str.2 = [13 x i8] c"Test FAILED!\00"
@.str.3 = [17 x i8] c"%s\0ATest FAILED!\0A\00"
@.str.4 = [16 x i8] c"Test succeeded.\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca [15 x i8]
%5 = alloca i32
%6 = alloca [25 x i8]
store i32 0, i32* %1
%7 = bitcast [15 x i8]* %4 to i8*
%8 = getelementptr inbounds [15 x i8], [15 x i8]* %4, i64 0, i64 0
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %2
store i32 0, i32* %3
br label %10
10: ; preds = %16, %0
%11 = load i32, i32* %3
%12 = icmp slt i32 %11, 9000
br i1 %12, label %13, label %19
13: ; preds = %10
%14 = load i32*, i32** %2
%15 = call i32 @putc(i8 signext 120, i32* %14)
br label %16
16: ; preds = %13
%17 = load i32, i32* %3
%18 = add nsw i32 %17, 1
store i32 %18, i32* %3
br label %10
19: ; preds = %10
%20 = load i32*, i32** %2
%21 = call i32 @fseek(i32* %20, i64 8180, i32 0)
%22 = load i32*, i32** %2
%23 = call i32 @fwrite(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 1, i32 24, i32* %22)
%24 = load i32*, i32** %2
%25 = call i32 @fflush(i32* %24)
%26 = load i32*, i32** %2
%27 = call i32 @rewind(i32* %26)
store i32 0, i32* %3
br label %28
28: ; preds = %60, %19
%29 = load i32, i32* %3
%30 = icmp slt i32 %29, 9000
br i1 %30, label %31, label %63
31: ; preds = %28
%32 = load i32*, i32** %2
%33 = call i32 @getc(i32* %32)
store i32 %33, i32* %5
%34 = icmp ne i32 %33, 120
br i1 %34, label %35, label %59
35: ; preds = %31
%36 = load i32, i32* %3
%37 = icmp ne i32 %36, 8180
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %69
40: ; preds = %35
%41 = load i32, i32* %5
%42 = trunc i32 %41 to i8
%43 = getelementptr inbounds [25 x i8], [25 x i8]* %6, i64 0, i64 0
store i8 %42, i8* %43
%44 = getelementptr inbounds [25 x i8], [25 x i8]* %6, i64 0, i64 0
%45 = getelementptr inbounds i8, i8* %44, i64 1
%46 = load i32*, i32** %2
%47 = call i32 @fread(i8* %45, i32 1, i32 23, i32* %46)
%48 = getelementptr inbounds [25 x i8], [25 x i8]* %6, i64 0, i64 24
store i8 0, i8* %48
%49 = getelementptr inbounds [25 x i8], [25 x i8]* %6, i64 0, i64 0
%50 = call i64 @strcmp(i8* %49, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %40
%53 = getelementptr inbounds [25 x i8], [25 x i8]* %6, i64 0, i64 0
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %53)
store i32 1, i32* %1
br label %69
55: ; preds = %40
%56 = load i32, i32* %3
%57 = add nsw i32 %56, 23
store i32 %57, i32* %3
br label %58
58: ; preds = %55
br label %59
59: ; preds = %58, %31
br label %60
60: ; preds = %59
%61 = load i32, i32* %3
%62 = add nsw i32 %61, 1
store i32 %62, i32* %3
br label %28
63: ; preds = %28
%64 = load i32*, i32** %2
%65 = call i32 @fclose(i32* %64)
%66 = getelementptr inbounds [15 x i8], [15 x i8]* %4, i64 0, i64 0
%67 = call i32 @remove(i8* %66)
%68 = call i32 @puts(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %1
br label %69
69: ; preds = %63, %52, %38
%70 = load i32, i32* %1
ret i32 %70
}

LLVM IR (-O2):
@__const.main.filename = [15 x i8] c"/tmp/bug3.test\00"
@.str = [3 x i8] c"w+\00"
@.str.1 = [25 x i8] c"Where does this text go?\00"
@.str.2 = [13 x i8] c"Test FAILED!\00"
@.str.3 = [17 x i8] c"%s\0ATest FAILED!\0A\00"
@.str.4 = [16 x i8] c"Test succeeded.\00"
define i32 @main() {
%1 = alloca [15 x i8]
%2 = alloca [25 x i8]
%3 = getelementptr inbounds [15 x i8], [15 x i8]* %1, i64 0, i64 0
%4 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
br label %5
5: ; preds = %0, %5
%6 = phi i32 [ 0, %0 ], [ %8, %5 ]
%7 = tail call i32 @putc(i8 signext 120, i32* %4)
%8 = add nuw nsw i32 %6, 1
%9 = icmp eq i32 %8, 9000
br i1 %9, label %10, label %5
10: ; preds = %5
%11 = tail call i32 @fseek(i32* %4, i64 8180, i32 0)
%12 = tail call i32 @fwrite(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 1, i32 24, i32* %4)
%13 = tail call i32 @fflush(i32* %4)
%14 = tail call i32 @rewind(i32* %4)
%15 = getelementptr inbounds [25 x i8], [25 x i8]* %2, i64 0, i64 0
%16 = getelementptr inbounds [25 x i8], [25 x i8]* %2, i64 0, i64 1
%17 = getelementptr inbounds [25 x i8], [25 x i8]* %2, i64 0, i64 24
br label %18
18: ; preds = %10, %34
%19 = phi i32 [ 0, %10 ], [ %36, %34 ]
%20 = call i32 @getc(i32* %4)
%21 = icmp eq i32 %20, 120
br i1 %21, label %34, label %22
22: ; preds = %18
%23 = icmp eq i32 %19, 8180
br i1 %23, label %26, label %24
24: ; preds = %22
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %42
26: ; preds = %22
%27 = trunc i32 %20 to i8
store i8 %27, i8* %15
%28 = call i32 @fread(i8* %16, i32 1, i32 23, i32* %4)
store i8 0, i8* %17
%29 = call i64 @strcmp(i8* %15, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %26
br label %34
32: ; preds = %26
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %15)
br label %42
34: ; preds = %31, %18
%35 = phi i32 [ 8203, %31 ], [ %19, %18 ]
%36 = add nsw i32 %35, 1
%37 = icmp slt i32 %35, 8999
br i1 %37, label %18, label %38
38: ; preds = %34
%39 = call i32 @fclose(i32* %4)
%40 = call i32 @remove(i8* %3)
%41 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
br label %42
42: ; preds = %24, %32, %38
%43 = phi i32 [ 0, %38 ], [ 1, %24 ], [ 1, %32 ]
ret i32 %43
}