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

/* Variables and functions */
int BUFSIZ;
int EOF;
int EXIT_FAILURE;
int EXIT_SUCCESS;
int FILENAME_MAX;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char const *, int /*<<< orphan*/ *);
long ftell(int /*<<< orphan*/ *);
int getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int putc(char const, int /*<<< orphan*/ *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ remove(char *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
scalar_t__ strcmp(char *, char const *);
char *strerror(int /*<<< orphan*/);
char *strrchr(char *, char);
int main(int argc, char **argv) {
  static const char hello[] = "Hello, world.\n";
  static const char replace[] = "Hewwo, world.\n";
  static const size_t replace_from = 2, replace_to = 4;
  char filename[FILENAME_MAX];
  char *name = strrchr(*argv, '/');
  char buf[BUFSIZ];
  FILE *f;
  int lose = 0;
  if (name != NULL)
    ++name;
  else
    name = *argv;
  (void)sprintf(filename, "/tmp/%s.test", name);
  f = fopen(filename, "w+");
  if (f == NULL) {
    perror(filename);
    exit(1);
  }
  (void)fputs(hello, f);
  rewind(f);
  (void)fgets(buf, sizeof(buf), f);
  rewind(f);
  (void)fputs(buf, f);
  rewind(f);
  {
    size_t i;
    for (i = 0; i < replace_from; ++i) {
      int c = getc(f);
      if (c == EOF) {
        printf("EOF at %Zu.\n", i);
        lose = 1;
        break;
      } else if (c != hello[i]) {
        printf("Got '%c' instead of '%c' at %Zu.\n", (unsigned char)c, hello[i],
               i);
        lose = 1;
        break;
      }
    }
  }
  {
    long int where = ftell(f);
    if (where == (long int)replace_from) {
      size_t i;
      for (i = replace_from; i < replace_to; ++i)
        if (putc(replace[i], f) == EOF) {
          printf("putc('%c') got %s at %Zu.\n", replace[i], strerror(errno), i);
          lose = 1;
          break;
        }
    } else if (where == -1L) {
      printf("ftell got %s (should be at %Zu).\n", strerror(errno),
             replace_from);
      lose = 1;
    } else {
      printf("ftell returns %lu; should be %Zu.\n", where, replace_from);
      lose = 1;
    }
  }
  if (!lose) {
    rewind(f);
    if (fgets(buf, sizeof(buf), f) == NULL) {
      printf("fgets got %s.\n", strerror(errno));
      lose = 1;
    } else if (strcmp(buf, replace)) {
      printf("Read \"%s\" instead of \"%s\".\n", buf, replace);
      lose = 1;
    }
  }
  if (lose)
    printf("Test FAILED!  Losing file is \"%s\".\n", filename);
  else {
    (void)remove(filename);
    puts("Test succeeded.");
  }
  return lose ? EXIT_FAILURE : EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2dc0(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x78(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    $0x2f,%esi
call   1070 <strrchr@plt>
mov    %rax,-0x28(%rbp)
mov    0x2d6f(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x70(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x68(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x3c(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     12a6 <main+0xa6>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    12b1 <main+0xb1>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x78(%rbp),%rdi
mov    -0x28(%rbp),%rdx
lea    0xd70(%rip),%rsi
call   10f0 <sprintf@plt>
mov    -0x78(%rbp),%rdi
lea    0xd6d(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12f7 <main+0xf7>
mov    -0x78(%rbp),%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>
mov    -0x38(%rbp),%rsi
lea    0xcfe(%rip),%rdi
call   1080 <fputs@plt>
mov    -0x38(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x68(%rbp),%rdi
mov    -0x70(%rbp),%rax
mov    %eax,%esi
mov    -0x38(%rbp),%rdx
call   1090 <fgets@plt>
mov    -0x38(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x68(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1080 <fputs@plt>
mov    -0x38(%rbp),%rdi
call   1060 <rewind@plt>
movq   $0x0,-0x48(%rbp)
cmpq   $0x2,-0x48(%rbp)
jae    13f9 <main+0x1f9>
mov    -0x38(%rbp),%rdi
call   1120 <getc@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x4c(%rbp),%eax
mov    0x2c7d(%rip),%rcx
cmp    (%rcx),%eax
jne    1391 <main+0x191>
mov    -0x48(%rbp),%rsi
lea    0xcc2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x3c(%rbp)
jmp    13f9 <main+0x1f9>
mov    -0x4c(%rbp),%eax
mov    -0x48(%rbp),%rdx
lea    0xc61(%rip),%rcx
movsbl (%rcx,%rdx,1),%ecx
cmp    %ecx,%eax
je     13de <main+0x1de>
mov    -0x4c(%rbp),%eax
movzbl %al,%esi
mov    -0x48(%rbp),%rcx
lea    0xc44(%rip),%rax
movsbl (%rax,%rcx,1),%edx
mov    -0x48(%rbp),%rcx
lea    0xc82(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x3c(%rbp)
jmp    13f9 <main+0x1f9>
jmp    13e3 <main+0x1e3>
jmp    13e8 <main+0x1e8>
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
jmp    134a <main+0x14a>
mov    -0x38(%rbp),%rdi
call   10c0 <ftell@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x2,-0x58(%rbp)
jne    14ac <main+0x2ac>
movq   $0x2,-0x60(%rbp)
cmpq   $0x4,-0x60(%rbp)
jae    14a7 <main+0x2a7>
mov    -0x60(%rbp),%rcx
lea    0xbe0(%rip),%rax
mov    (%rax,%rcx,1),%al
mov    -0x38(%rbp),%rsi
movsbl %al,%edi
call   10b0 <putc@plt>
mov    0x2ba3(%rip),%rcx
cmp    (%rcx),%eax
jne    1491 <main+0x291>
mov    -0x60(%rbp),%rcx
lea    0xbb7(%rip),%rax
movsbl (%rax,%rcx,1),%eax
mov    %eax,-0x7c(%rbp)
mov    0x2b62(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
mov    -0x7c(%rbp),%esi
mov    %rax,%rdx
mov    -0x60(%rbp),%rcx
lea    0xbf1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x3c(%rbp)
jmp    14a7 <main+0x2a7>
jmp    1496 <main+0x296>
mov    -0x60(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x60(%rbp)
jmp    1419 <main+0x219>
jmp    150a <main+0x30a>
cmpq   $0xffffffffffffffff,-0x58(%rbp)
jne    14e7 <main+0x2e7>
mov    0x2b0a(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
mov    %rax,%rsi
lea    0xbbb(%rip),%rdi
mov    $0x2,%edx
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x3c(%rbp)
jmp    1505 <main+0x305>
mov    -0x58(%rbp),%rsi
lea    0xbba(%rip),%rdi
mov    $0x2,%edx
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x3c(%rbp)
jmp    150a <main+0x30a>
cmpl   $0x0,-0x3c(%rbp)
jne    15a9 <main+0x3a9>
mov    -0x38(%rbp),%rdi
call   1060 <rewind@plt>
mov    -0x68(%rbp),%rdi
mov    -0x70(%rbp),%rax
mov    %eax,%esi
mov    -0x38(%rbp),%rdx
call   1090 <fgets@plt>
cmp    $0x0,%rax
jne    1565 <main+0x365>
mov    0x2a87(%rip),%rax
mov    (%rax),%edi
call   1110 <strerror@plt>
mov    %rax,%rsi
lea    0xb7d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x3c(%rbp)
jmp    15a4 <main+0x3a4>
mov    -0x68(%rbp),%rdi
lea    0xa9f(%rip),%rsi
call   10a0 <strcmp@plt>
cmp    $0x0,%rax
je     159f <main+0x39f>
mov    -0x68(%rbp),%rsi
lea    0xb54(%rip),%rdi
lea    0xa7e(%rip),%rdx
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x3c(%rbp)
jmp    15a4 <main+0x3a4>
jmp    15a9 <main+0x3a9>
cmpl   $0x0,-0x3c(%rbp)
je     15ca <main+0x3ca>
mov    -0x78(%rbp),%rsi
lea    0xb3c(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    15df <main+0x3df>
mov    -0x78(%rbp),%rdi
call   1030 <remove@plt>
lea    0xb44(%rip),%rdi
call   1040 <puts@plt>
cmpl   $0x0,-0x3c(%rbp)
je     15fa <main+0x3fa>
mov    0x29d0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x80(%rbp)
jmp    1606 <main+0x406>
mov    0x29b7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x80(%rbp)
mov    -0x80(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2dbb(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r14
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
mov    (%rsi),%rbx
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1070 <strrchr@plt>
mov    0x2d81(%rip),%rcx
mov    (%rcx),%r15d
mov    %rsp,%r12
lea    0xf(%r15),%rcx
and    $0xfffffffffffffff0,%rcx
sub    %rcx,%r12
mov    %r12,%rsp
lea    0x1(%rax),%rdx
test   %rax,%rax
cmove  %rbx,%rdx
lea    0xda9(%rip),%rsi
mov    %r14,%rdi
call   10f0 <sprintf@plt>
lea    0xda7(%rip),%rsi
mov    %r14,%rdi
call   10d0 <fopen@plt>
test   %rax,%rax
je     14b6 <main+0x2a6>
mov    %rax,%rbx
lea    0xd61(%rip),%rdi
mov    $0xe,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1110 <fwrite@plt>
mov    %rbx,%rdi
call   1060 <rewind@plt>
mov    %r12,%rdi
mov    %r15d,%esi
mov    %rbx,%rdx
call   1090 <fgets@plt>
mov    %rbx,%rdi
call   1060 <rewind@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
call   1080 <fputs@plt>
mov    %rbx,%rdi
call   1060 <rewind@plt>
mov    %rbx,%rdi
call   1130 <getc@plt>
mov    0x2cf7(%rip),%r13
cmp    0x0(%r13),%eax
jne    12fb <main+0xeb>
xor    %esi,%esi
jmp    1313 <main+0x103>
cmp    $0x48,%eax
jne    132a <main+0x11a>
mov    %rbx,%rdi
call   1130 <getc@plt>
mov    $0x1,%esi
cmp    0x0(%r13),%eax
jne    1333 <main+0x123>
lea    0xd14(%rip),%rdi
movl   $0x0,-0x2c(%rbp)
xor    %eax,%eax
call   1050 <printf@plt>
jmp    135f <main+0x14f>
mov    $0x48,%edx
xor    %ecx,%ecx
jmp    1347 <main+0x137>
mov    $0x1,%cl
mov    %ecx,-0x2c(%rbp)
cmp    $0x65,%eax
je     135f <main+0x14f>
mov    $0x1,%ecx
mov    $0x65,%edx
movzbl %al,%esi
lea    0xcea(%rip),%rdi
movl   $0x0,-0x2c(%rbp)
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   10c0 <ftell@plt>
cmp    $0xffffffffffffffff,%rax
je     13d4 <main+0x1c4>
cmp    $0x2,%rax
jne    13eb <main+0x1db>
mov    $0x77,%edi
mov    %rbx,%rsi
call   10b0 <putc@plt>
mov    %r13,%rcx
mov    $0x2,%r13d
cmp    (%rcx),%eax
je     13ab <main+0x19b>
mov    $0x77,%edi
mov    %rbx,%rsi
call   10b0 <putc@plt>
mov    $0x3,%r13d
mov    0x2c41(%rip),%rcx
cmp    (%rcx),%eax
jne    142a <main+0x21a>
mov    0x2c16(%rip),%rax
mov    (%rax),%edi
call   1120 <strerror@plt>
lea    0xc9d(%rip),%rdi
mov    $0x77,%esi
mov    %rax,%rdx
mov    %r13,%rcx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1401 <main+0x1f1>
mov    0x2bed(%rip),%rax
mov    (%rax),%edi
call   1120 <strerror@plt>
lea    0xc8f(%rip),%rdi
jmp    13f2 <main+0x1e2>
lea    0xca8(%rip),%rdi
mov    $0x2,%edx
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xce0(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2ba7(%rip),%rax
mov    (%rax),%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
cmpb   $0x0,-0x2c(%rbp)
je     1401 <main+0x1f1>
mov    %rbx,%rdi
call   1060 <rewind@plt>
mov    %r12,%rdi
mov    %r15d,%esi
mov    %rbx,%rdx
call   1090 <fgets@plt>
test   %rax,%rax
je     1472 <main+0x262>
lea    0xbbd(%rip),%rsi
mov    %r12,%rdi
call   10a0 <strcmp@plt>
test   %rax,%rax
je     1496 <main+0x286>
lea    0xc66(%rip),%rdi
lea    0xba2(%rip),%rdx
mov    %r12,%rsi
jmp    13fa <main+0x1ea>
mov    0x2b4f(%rip),%rax
mov    (%rax),%edi
call   1120 <strerror@plt>
lea    0xc36(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1401 <main+0x1f1>
mov    %r14,%rdi
call   1030 <remove@plt>
lea    0xc67(%rip),%rdi
call   1040 <puts@plt>
mov    0x2b07(%rip),%rax
jmp    1419 <main+0x209>
mov    %r14,%rdi
call   10e0 <perror@plt>
mov    $0x1,%edi
call   1100 <exit@plt>

LLVM IR (-O0):
@main.hello = internal constant [15 x i8] c"Hello, world.\0A\00"
@main.replace = internal constant [15 x i8] c"Hewwo, world.\0A\00"
@main.replace_from = internal constant i64 2
@main.replace_to = internal constant i64 4
@FILENAME_MAX = global i32 0
@BUFSIZ = global i32 0
@.str = [13 x i8] c"/tmp/%s.test\00"
@.str.1 = [3 x i8] c"w+\00"
@EOF = global i32 0
@.str.2 = [13 x i8] c"EOF at %Zu.\0A\00"
@.str.3 = [34 x i8] c"Got '%c' instead of '%c' at %Zu.\0A\00"
@.str.4 = [27 x i8] c"putc('%c') got %s at %Zu.\0A\00"
@errno = global i32 0
@.str.5 = [34 x i8] c"ftell got %s (should be at %Zu).\0A\00"
@.str.6 = [35 x i8] c"ftell returns %lu; should be %Zu.\0A\00"
@.str.7 = [15 x i8] c"fgets got %s.\0A\00"
@.str.8 = [28 x i8] c"Read \22%s\22 instead of \22%s\22.\0A\00"
@.str.9 = [36 x i8] c"Test FAILED! Losing file is \22%s\22.\0A\00"
@.str.10 = [16 x i8] c"Test succeeded.\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i64
%13 = alloca i32
%14 = alloca i64
%15 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = load i32, i32* @FILENAME_MAX
%17 = zext i32 %16 to i64
%18 = call i8* @llvm.stacksave()
store i8* %18, i8** %6
%19 = alloca i8, i64 %17
store i64 %17, i64* %7
%20 = load i8**, i8*** %5
%21 = load i8*, i8** %20
%22 = call i8* @strrchr(i8* %21, i8 signext 47)
store i8* %22, i8** %8
%23 = load i32, i32* @BUFSIZ
%24 = zext i32 %23 to i64
%25 = alloca i8, i64 %24
store i64 %24, i64* %9
store i32 0, i32* %11
%26 = load i8*, i8** %8
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %31
28: ; preds = %2
%29 = load i8*, i8** %8
%30 = getelementptr inbounds i8, i8* %29, i32 1
store i8* %30, i8** %8
br label %34
31: ; preds = %2
%32 = load i8**, i8*** %5
%33 = load i8*, i8** %32
store i8* %33, i8** %8
br label %34
34: ; preds = %31, %28
%35 = load i8*, i8** %8
%36 = call i32 @sprintf(i8* %19, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %35)
%37 = call i32* @fopen(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %37, i32** %10
%38 = load i32*, i32** %10
%39 = icmp eq i32* %38, null
br i1 %39, label %40, label %43
40: ; preds = %34
%41 = call i32 @perror(i8* %19)
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %34
%44 = load i32*, i32** %10
%45 = call i32 @fputs(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @main.hello, i64 0, i64 0), i32* %44)
%46 = load i32*, i32** %10
%47 = call i32 @rewind(i32* %46)
%48 = trunc i64 %24 to i32
%49 = load i32*, i32** %10
%50 = call i32* @fgets(i8* %25, i32 %48, i32* %49)
%51 = load i32*, i32** %10
%52 = call i32 @rewind(i32* %51)
%53 = load i32*, i32** %10
%54 = call i32 @fputs(i8* %25, i32* %53)
%55 = load i32*, i32** %10
%56 = call i32 @rewind(i32* %55)
store i64 0, i64* %12
br label %57
57: ; preds = %88, %43
%58 = load i64, i64* %12
%59 = icmp ult i64 %58, 2
br i1 %59, label %60, label %91
60: ; preds = %57
%61 = load i32*, i32** %10
%62 = call i32 @getc(i32* %61)
store i32 %62, i32* %13
%63 = load i32, i32* %13
%64 = load i32, i32* @EOF
%65 = icmp eq i32 %63, %64
br i1 %65, label %66, label %69
66: ; preds = %60
%67 = load i64, i64* %12
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i64 %67)
store i32 1, i32* %11
br label %91
69: ; preds = %60
%70 = load i32, i32* %13
%71 = load i64, i64* %12
%72 = getelementptr inbounds [15 x i8], [15 x i8]* @main.hello, i64 0, i64 %71
%73 = load i8, i8* %72
%74 = sext i8 %73 to i32
%75 = icmp ne i32 %70, %74
br i1 %75, label %76, label %86
76: ; preds = %69
%77 = load i32, i32* %13
%78 = trunc i32 %77 to i8
%79 = zext i8 %78 to i32
%80 = load i64, i64* %12
%81 = getelementptr inbounds [15 x i8], [15 x i8]* @main.hello, i64 0, i64 %80
%82 = load i8, i8* %81
%83 = sext i8 %82 to i32
%84 = load i64, i64* %12
%85 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i32 %79, i32 %83, i64 %84)
store i32 1, i32* %11
br label %91
86: ; preds = %69
br label %87
87: ; preds = %86
br label %88
88: ; preds = %87
%89 = load i64, i64* %12
%90 = add i64 %89, 1
store i64 %90, i64* %12
br label %57
91: ; preds = %76, %66, %57
%92 = load i32*, i32** %10
%93 = call i64 @ftell(i32* %92)
store i64 %93, i64* %14
%94 = load i64, i64* %14
%95 = icmp eq i64 %94, 2
br i1 %95, label %96, label %122
96: ; preds = %91
store i64 2, i64* %15
br label %97
97: ; preds = %118, %96
%98 = load i64, i64* %15
%99 = icmp ult i64 %98, 4
br i1 %99, label %100, label %121
100: ; preds = %97
%101 = load i64, i64* %15
%102 = getelementptr inbounds [15 x i8], [15 x i8]* @main.replace, i64 0, i64 %101
%103 = load i8, i8* %102
%104 = load i32*, i32** %10
%105 = call i32 @putc(i8 signext %103, i32* %104)
%106 = load i32, i32* @EOF
%107 = icmp eq i32 %105, %106
br i1 %107, label %108, label %117
108: ; preds = %100
%109 = load i64, i64* %15
%110 = getelementptr inbounds [15 x i8], [15 x i8]* @main.replace, i64 0, i64 %109
%111 = load i8, i8* %110
%112 = sext i8 %111 to i32
%113 = load i32, i32* @errno
%114 = call i8* @strerror(i32 %113)
%115 = load i64, i64* %15
%116 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i32 %112, i8* %114, i64 %115)
store i32 1, i32* %11
br label %121
117: ; preds = %100
br label %118
118: ; preds = %117
%119 = load i64, i64* %15
%120 = add i64 %119, 1
store i64 %120, i64* %15
br label %97
121: ; preds = %108, %97
br label %133
122: ; preds = %91
%123 = load i64, i64* %14
%124 = icmp eq i64 %123, -1
br i1 %124, label %125, label %129
125: ; preds = %122
%126 = load i32, i32* @errno
%127 = call i8* @strerror(i32 %126)
%128 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i8* %127, i64 2)
store i32 1, i32* %11
br label %132
129: ; preds = %122
%130 = load i64, i64* %14
%131 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0), i64 %130, i64 2)
store i32 1, i32* %11
br label %132
132: ; preds = %129, %125
br label %133
133: ; preds = %132, %121
%134 = load i32, i32* %11
%135 = icmp ne i32 %134, 0
br i1 %135, label %154, label %136
136: ; preds = %133
%137 = load i32*, i32** %10
%138 = call i32 @rewind(i32* %137)
%139 = trunc i64 %24 to i32
%140 = load i32*, i32** %10
%141 = call i32* @fgets(i8* %25, i32 %139, i32* %140)
%142 = icmp eq i32* %141, null
br i1 %142, label %143, label %147
143: ; preds = %136
%144 = load i32, i32* @errno
%145 = call i8* @strerror(i32 %144)
%146 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0), i8* %145)
store i32 1, i32* %11
br label %153
147: ; preds = %136
%148 = call i64 @strcmp(i8* %25, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @main.replace, i64 0, i64 0))
%149 = icmp ne i64 %148, 0
br i1 %149, label %150, label %152
150: ; preds = %147
%151 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.8, i64 0, i64 0), i8* %25, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @main.replace, i64 0, i64 0))
store i32 1, i32* %11
br label %152
152: ; preds = %150, %147
br label %153
153: ; preds = %152, %143
br label %154
154: ; preds = %153, %133
%155 = load i32, i32* %11
%156 = icmp ne i32 %155, 0
br i1 %156, label %157, label %159
157: ; preds = %154
%158 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.9, i64 0, i64 0), i8* %19)
br label %162
159: ; preds = %154
%160 = call i32 @remove(i8* %19)
%161 = call i32 @puts(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.10, i64 0, i64 0))
br label %162
162: ; preds = %159, %157
%163 = load i32, i32* %11
%164 = icmp ne i32 %163, 0
br i1 %164, label %165, label %167
165: ; preds = %162
%166 = load i32, i32* @EXIT_FAILURE
br label %169
167: ; preds = %162
%168 = load i32, i32* @EXIT_SUCCESS
br label %169
169: ; preds = %167, %165
%170 = phi i32 [ %166, %165 ], [ %168, %167 ]
store i32 %170, i32* %3
%171 = load i8*, i8** %6
%172 = load i32, i32* %3
ret i32 %172
}

LLVM IR (-O2):
@main.hello = internal constant [15 x i8] c"Hello, world.\0A\00"
@main.replace = internal constant [15 x i8] c"Hewwo, world.\0A\00"
@FILENAME_MAX = global i32 0
@BUFSIZ = global i32 0
@.str = [13 x i8] c"/tmp/%s.test\00"
@.str.1 = [3 x i8] c"w+\00"
@EOF = global i32 0
@.str.2 = [13 x i8] c"EOF at %Zu.\0A\00"
@.str.3 = [34 x i8] c"Got '%c' instead of '%c' at %Zu.\0A\00"
@.str.4 = [27 x i8] c"putc('%c') got %s at %Zu.\0A\00"
@errno = global i32 0
@.str.5 = [34 x i8] c"ftell got %s (should be at %Zu).\0A\00"
@.str.6 = [35 x i8] c"ftell returns %lu; should be %Zu.\0A\00"
@.str.7 = [15 x i8] c"fgets got %s.\0A\00"
@.str.8 = [28 x i8] c"Read \22%s\22 instead of \22%s\22.\0A\00"
@.str.9 = [36 x i8] c"Test FAILED! Losing file is \22%s\22.\0A\00"
@.str.10 = [16 x i8] c"Test succeeded.\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = load i32, i32* @FILENAME_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = load i8*, i8** %1
%7 = tail call i8* @strrchr(i8* dereferenceable(1) %6, i8 signext 47)
%8 = load i32, i32* @BUFSIZ
%9 = zext i32 %8 to i64
%10 = alloca i8, i64 %9
%11 = icmp eq i8* %7, null
%12 = getelementptr inbounds i8, i8* %7, i64 1
%13 = select i1 %11, i8* %6, i8* %12
%14 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = call i32 @perror(i8* %5)
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = tail call i64 @fwrite(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @main.hello, i64 0, i64 0), i64 14, i64 1, i32* %15)
%22 = tail call i32 @rewind(i32* %15)
%23 = call i32* @fgets(i8* %10, i32 %8, i32* %15)
%24 = call i32 @rewind(i32* %15)
%25 = call i32 @fputs(i8* %10, i32* %15)
%26 = call i32 @rewind(i32* %15)
%27 = call i32 @getc(i32* %15)
%28 = load i32, i32* @EOF
%29 = icmp eq i32 %27, %28
br i1 %29, label %30, label %33
30: ; preds = %41, %20
%31 = phi i64 [ 0, %20 ], [ 1, %41 ]
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i64 %31)
br label %47
33: ; preds = %20
%34 = icmp eq i32 %27, 72
br i1 %34, label %41, label %35
35: ; preds = %45, %33
%36 = phi i32 [ 72, %33 ], [ 101, %45 ]
%37 = phi i64 [ 0, %33 ], [ 1, %45 ]
%38 = phi i32 [ %27, %33 ], [ %42, %45 ]
%39 = and i32 %38, 255
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0), i32 %39, i32 %36, i64 %37)
br label %47
41: ; preds = %33
%42 = call i32 @getc(i32* %15)
%43 = load i32, i32* @EOF
%44 = icmp eq i32 %42, %43
br i1 %44, label %30, label %45
45: ; preds = %41
%46 = icmp eq i32 %42, 101
br i1 %46, label %47, label %35
47: ; preds = %45, %35, %30
%48 = phi i1 [ false, %35 ], [ false, %30 ], [ true, %45 ]
%49 = call i64 @ftell(i32* %15)
switch i64 %49, label %68 [
i64 2, label %50
i64 -1, label %64
]
50: ; preds = %47
%51 = call i32 @putc(i8 signext 119, i32* %15)
%52 = load i32, i32* @EOF
%53 = icmp eq i32 %51, %52
br i1 %53, label %54, label %59
54: ; preds = %59, %50
%55 = phi i64 [ 2, %50 ], [ 3, %59 ]
%56 = load i32, i32* @errno
%57 = call i8* @strerror(i32 %56)
%58 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i32 119, i8* %57, i64 %55)
br label %83
59: ; preds = %50
%60 = call i32 @putc(i8 signext 119, i32* %15)
%61 = load i32, i32* @EOF
%62 = icmp eq i32 %60, %61
br i1 %62, label %54, label %63
63: ; preds = %59
br i1 %48, label %70, label %83
64: ; preds = %47
%65 = load i32, i32* @errno
%66 = call i8* @strerror(i32 %65)
%67 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i8* %66, i64 2)
br label %83
68: ; preds = %47
%69 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0), i64 %49, i64 2)
br label %83
70: ; preds = %63
%71 = call i32 @rewind(i32* %15)
%72 = call i32* @fgets(i8* %10, i32 %8, i32* %15)
%73 = icmp eq i32* %72, null
br i1 %73, label %74, label %78
74: ; preds = %70
%75 = load i32, i32* @errno
%76 = call i8* @strerror(i32 %75)
%77 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0), i8* %76)
br label %83
78: ; preds = %70
%79 = call i64 @strcmp(i8* %10, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @main.replace, i64 0, i64 0))
%80 = icmp eq i64 %79, 0
br i1 %80, label %85, label %81
81: ; preds = %78
%82 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.8, i64 0, i64 0), i8* %10, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @main.replace, i64 0, i64 0))
br label %83
83: ; preds = %54, %68, %64, %63, %81, %74
%84 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.9, i64 0, i64 0), i8* %5)
br label %88
85: ; preds = %78
%86 = call i32 @remove(i8* %5)
%87 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.10, i64 0, i64 0))
br label %88
88: ; preds = %83, %85
%89 = phi i32* [ @EXIT_SUCCESS, %85 ], [ @EXIT_FAILURE, %83 ]
%90 = load i32, i32* %89
ret i32 %90
}