#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_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int find(char *, char *, int);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ fread(char *, int, long, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
char *malloc(long);
int /*<<< orphan*/ printf(char *, ...);
int rand();
int /*<<< orphan*/ replaceChar(char *, char, char, int);
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ *stderr;
int /*<<< orphan*/ stdin;
char *strtok(char *, char *);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main() {
  srand(time(NULL));
  char *filename;
  char *buffer;
  long int length;
  FILE *fp;
  int svgPos;
  int startPos;
  int digit1;
  int digit2;
  char replace;
  char with;
  filename = malloc(1024);
  printf("File to destroy: ");
  fgets(filename, 1024, stdin);
  filename = strtok(filename, "\n");
  fp = fopen(filename, "rb");
  if (fp) {
    fseek(fp, 0, SEEK_END);
    length = ftell(fp);
    fseek(fp, 0, SEEK_SET);
    buffer = malloc(length);
    fread(buffer, 1, length, fp);
    fclose(fp);
  } else {
    fprintf(stderr, "File not found: %s\n", filename);
    return 1;
  }
  svgPos = find(buffer, "<svg", 0);
  startPos = find(buffer, ">", svgPos);
  if (svgPos == -1 || startPos == -1) {
    fprintf(stderr, "Error: Is this an SVG file?\n");
    return 1;
  }
  digit1 = rand() % 10;
  do {
    digit2 = rand() % 10;
  } while (digit1 == digit2);
  replace = (char)(digit1 + 48);
  with = (char)(digit2 + 48);
  replaceChar(buffer, replace, with, startPos);
  FILE *output = fopen(filename, "w");
  fprintf(output, "%s", buffer);
  fclose(output);
  printf("Replaced %c with %c.\n", replace, with);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   10b0 <time@plt>
mov    %eax,%edi
call   1060 <srand@plt>
mov    $0x400,%edi
call   10d0 <malloc@plt>
mov    %rax,-0x10(%rbp)
lea    0xddc(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2daa(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   1070 <fgets@plt>
mov    -0x10(%rbp),%rdi
lea    0xe19(%rip),%rsi
call   1100 <strtok@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xdb1(%rip),%rsi
call   10f0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     12db <main+0xeb>
mov    -0x28(%rbp),%rdi
mov    0x2d68(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10e0 <fseek@plt>
mov    -0x28(%rbp),%rdi
call   10a0 <ftell@plt>
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2d27(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10e0 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   10d0 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
jmp    1303 <main+0x113>
mov    0x2cee(%rip),%rax
mov    (%rax),%rdi
mov    -0x10(%rbp),%rdx
lea    0xd25(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1410 <main+0x220>
mov    -0x18(%rbp),%rdi
lea    0xd1b(%rip),%rsi
xor    %edx,%edx
call   10c0 <find@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x2c(%rbp),%edx
lea    0xd08(%rip),%rsi
call   10c0 <find@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
je     1342 <main+0x152>
cmpl   $0xffffffff,-0x30(%rbp)
jne    1366 <main+0x176>
mov    0x2c87(%rip),%rax
mov    (%rax),%rdi
lea    0xcdd(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1410 <main+0x220>
mov    $0x0,%al
call   1110 <rand@plt>
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %edx,-0x34(%rbp)
mov    $0x0,%al
call   1110 <rand@plt>
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %edx,-0x38(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x38(%rbp),%eax
je     1378 <main+0x188>
mov    -0x34(%rbp),%eax
add    $0x30,%eax
mov    %al,-0x39(%rbp)
mov    -0x38(%rbp),%eax
add    $0x30,%eax
mov    %al,-0x3a(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x39(%rbp),%dl
mov    -0x3a(%rbp),%al
mov    -0x30(%rbp),%ecx
movsbl %dl,%esi
movsbl %al,%edx
call   1080 <replaceChar@plt>
mov    -0x10(%rbp),%rdi
lea    0xc82(%rip),%rsi
call   10f0 <fopen@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xc6c(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x48(%rbp),%rdi
call   1040 <fclose@plt>
movsbl -0x39(%rbp),%esi
movsbl -0x3a(%rbp),%edx
lea    0xc50(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %edi,%edi
call   10c0 <time@plt>
mov    %eax,%edi
call   1070 <srand@plt>
mov    $0x400,%edi
call   10e0 <malloc@plt>
mov    %rax,%rbx
lea    0xdc3(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d95(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0x400,%esi
call   1080 <fgets@plt>
lea    0xe02(%rip),%rsi
mov    %rbx,%rdi
call   1110 <strtok@plt>
mov    %rax,%r14
lea    0xd9f(%rip),%rsi
mov    %rax,%rdi
call   1100 <fopen@plt>
test   %rax,%rax
je     13d3 <main+0x1c3>
mov    %rax,%rbx
mov    0x2d5a(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10f0 <fseek@plt>
mov    %rbx,%rdi
call   10b0 <ftell@plt>
mov    %rax,%rbp
mov    0x2d1c(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10f0 <fseek@plt>
mov    %rbp,%rdi
call   10e0 <malloc@plt>
mov    %rax,%r12
mov    $0x1,%r13d
mov    %rax,%rdi
mov    $0x1,%esi
mov    %rbp,%rdx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xd3e(%rip),%rsi
mov    %r12,%rdi
xor    %edx,%edx
call   10d0 <find@plt>
mov    %eax,%ebx
lea    0xd30(%rip),%rsi
mov    %r12,%rdi
mov    %eax,%edx
call   10d0 <find@plt>
cmp    $0xffffffff,%ebx
je     13f6 <main+0x1e6>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
je     13f6 <main+0x1e6>
xor    %eax,%eax
call   1130 <rand@plt>
movslq %eax,%rbx
imul   $0x66666667,%rbx,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x22,%rax
add    %ecx,%eax
add    %eax,%eax
lea    (%rax,%rax,4),%eax
sub    %eax,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   1130 <rand@plt>
cltq
imul   $0x66666667,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x22,%rcx
add    %edx,%ecx
add    %ecx,%ecx
lea    (%rcx,%rcx,4),%ecx
sub    %ecx,%eax
cmp    %eax,%ebx
je     1350 <main+0x140>
add    $0x30,%bl
add    $0x30,%al
movsbl %bl,%ebx
movsbl %al,%r13d
mov    %r12,%rdi
mov    %ebx,%esi
mov    %r13d,%edx
mov    %r15d,%ecx
call   1090 <replaceChar@plt>
lea    0xcb2(%rip),%rsi
mov    %r14,%rdi
call   1100 <fopen@plt>
mov    %rax,%rbp
mov    %r12,%rdi
mov    %rax,%rsi
call   1060 <fputs@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
movzbl %bl,%esi
movzbl %r13b,%edx
lea    0xc88(%rip),%rdi
xor    %r13d,%r13d
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1416 <main+0x206>
mov    0x2bf6(%rip),%rax
mov    (%rax),%rdi
lea    0xc31(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    $0x1,%r13d
jmp    1416 <main+0x206>
mov    0x2bd3(%rip),%rax
mov    (%rax),%rcx
lea    0xc29(%rip),%rdi
mov    $0x1c,%esi
mov    $0x1,%edx
call   1120 <fwrite@plt>
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"File to destroy: \00"
@stdin = global i32 0
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@stderr = global i32* null
@.str.3 = [20 x i8] c"File not found: %s\0A\00"
@.str.4 = [5 x i8] c"<svg\00"
@.str.5 = [2 x i8] c">\00"
@.str.6 = [29 x i8] c"Error: Is this an SVG file?\0A\00"
@.str.7 = [2 x i8] c"w\00"
@.str.8 = [3 x i8] c"%s\00"
@.str.9 = [22 x i8] c"Replaced %c with %c.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8
%11 = alloca i8
%12 = alloca i32*
store i32 0, i32* %1
%13 = call i32 @time(i32* null)
%14 = call i32 @srand(i32 %13)
%15 = call i8* @malloc(i64 1024)
store i8* %15, i8** %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%17 = load i8*, i8** %2
%18 = load i32, i32* @stdin
%19 = call i32 @fgets(i8* %17, i32 1024, i32 %18)
%20 = load i8*, i8** %2
%21 = call i8* @strtok(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %21, i8** %2
%22 = load i8*, i8** %2
%23 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %23, i32** %5
%24 = load i32*, i32** %5
%25 = icmp ne i32* %24, null
br i1 %25, label %26, label %43
26: ; preds = %0
%27 = load i32*, i32** %5
%28 = load i32, i32* @SEEK_END
%29 = call i32 @fseek(i32* %27, i32 0, i32 %28)
%30 = load i32*, i32** %5
%31 = call i64 @ftell(i32* %30)
store i64 %31, i64* %4
%32 = load i32*, i32** %5
%33 = load i32, i32* @SEEK_SET
%34 = call i32 @fseek(i32* %32, i32 0, i32 %33)
%35 = load i64, i64* %4
%36 = call i8* @malloc(i64 %35)
store i8* %36, i8** %3
%37 = load i8*, i8** %3
%38 = load i64, i64* %4
%39 = load i32*, i32** %5
%40 = call i32 @fread(i8* %37, i32 1, i64 %38, i32* %39)
%41 = load i32*, i32** %5
%42 = call i32 @fclose(i32* %41)
br label %47
43: ; preds = %0
%44 = load i32*, i32** @stderr
%45 = load i8*, i8** %2
%46 = call i32 (i32*, i8*, ...) @fprintf(i32* %44, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %45)
store i32 1, i32* %1
br label %95
47: ; preds = %26
%48 = load i8*, i8** %3
%49 = call i32 @find(i8* %48, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 0)
store i32 %49, i32* %6
%50 = load i8*, i8** %3
%51 = load i32, i32* %6
%52 = call i32 @find(i8* %50, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 %51)
store i32 %52, i32* %7
%53 = load i32, i32* %6
%54 = icmp eq i32 %53, -1
br i1 %54, label %58, label %55
55: ; preds = %47
%56 = load i32, i32* %7
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %61
58: ; preds = %55, %47
%59 = load i32*, i32** @stderr
%60 = call i32 (i32*, i8*, ...) @fprintf(i32* %59, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %1
br label %95
61: ; preds = %55
%62 = call i32 (...) @rand()
%63 = srem i32 %62, 10
store i32 %63, i32* %8
br label %64
64: ; preds = %67, %61
%65 = call i32 (...) @rand()
%66 = srem i32 %65, 10
store i32 %66, i32* %9
br label %67
67: ; preds = %64
%68 = load i32, i32* %8
%69 = load i32, i32* %9
%70 = icmp eq i32 %68, %69
br i1 %70, label %64, label %71
71: ; preds = %67
%72 = load i32, i32* %8
%73 = add nsw i32 %72, 48
%74 = trunc i32 %73 to i8
store i8 %74, i8* %10
%75 = load i32, i32* %9
%76 = add nsw i32 %75, 48
%77 = trunc i32 %76 to i8
store i8 %77, i8* %11
%78 = load i8*, i8** %3
%79 = load i8, i8* %10
%80 = load i8, i8* %11
%81 = load i32, i32* %7
%82 = call i32 @replaceChar(i8* %78, i8 signext %79, i8 signext %80, i32 %81)
%83 = load i8*, i8** %2
%84 = call i32* @fopen(i8* %83, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
store i32* %84, i32** %12
%85 = load i32*, i32** %12
%86 = load i8*, i8** %3
%87 = call i32 (i32*, i8*, ...) @fprintf(i32* %85, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i8* %86)
%88 = load i32*, i32** %12
%89 = call i32 @fclose(i32* %88)
%90 = load i8, i8* %10
%91 = sext i8 %90 to i32
%92 = load i8, i8* %11
%93 = sext i8 %92 to i32
%94 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.9, i64 0, i64 0), i32 %91, i32 %93)
store i32 0, i32* %1
br label %95
95: ; preds = %71, %58, %43
%96 = load i32, i32* %1
ret i32 %96
}

LLVM IR (-O2):
@.str = [18 x i8] c"File to destroy: \00"
@stdin = global i32 0
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@stderr = global i32* null
@.str.3 = [20 x i8] c"File not found: %s\0A\00"
@.str.4 = [5 x i8] c"<svg\00"
@.str.5 = [2 x i8] c">\00"
@.str.6 = [29 x i8] c"Error: Is this an SVG file?\0A\00"
@.str.7 = [2 x i8] c"w\00"
@.str.9 = [22 x i8] c"Replaced %c with %c.\0A\00"
define i32 @main() {
%1 = tail call i32 @time(i32* null)
%2 = tail call i32 @srand(i32 %1)
%3 = tail call dereferenceable_or_null(1024) i8* @malloc(i64 1024)
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0))
%5 = load i32, i32* @stdin
%6 = tail call i32 @fgets(i8* %3, i32 1024, i32 %5)
%7 = tail call i8* @strtok(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32* @fopen(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %24, label %10
10: ; preds = %0
%11 = load i32, i32* @SEEK_END
%12 = tail call i32 @fseek(i32* %8, i32 0, i32 %11)
%13 = tail call i64 @ftell(i32* %8)
%14 = load i32, i32* @SEEK_SET
%15 = tail call i32 @fseek(i32* %8, i32 0, i32 %14)
%16 = tail call i8* @malloc(i64 %13)
%17 = tail call i32 @fread(i8* %16, i32 1, i64 %13, i32* %8)
%18 = tail call i32 @fclose(i32* %8)
%19 = tail call i32 @find(i8* %16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i32 0)
%20 = tail call i32 @find(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0), i32 %19)
%21 = icmp eq i32 %19, -1
%22 = icmp eq i32 %20, -1
%23 = select i1 %21, i1 true, i1 %22
br i1 %23, label %27, label %30
24: ; preds = %0
%25 = load i32*, i32** @stderr
%26 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %25, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %7)
br label %49
27: ; preds = %10
%28 = load i32*, i32** @stderr
%29 = tail call i64 @fwrite(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i64 28, i64 1, i32* %28)
br label %49
30: ; preds = %10
%31 = tail call i32 (...) @rand()
%32 = srem i32 %31, 10
br label %33
33: ; preds = %33, %30
%34 = tail call i32 (...) @rand()
%35 = srem i32 %34, 10
%36 = icmp eq i32 %32, %35
br i1 %36, label %33, label %37
37: ; preds = %33
%38 = trunc i32 %32 to i8
%39 = add i8 %38, 48
%40 = trunc i32 %35 to i8
%41 = add nsw i8 %40, 48
%42 = tail call i32 @replaceChar(i8* %16, i8 signext %39, i8 signext %41, i32 %20)
%43 = tail call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
%44 = tail call i32 @fputs(i8* %16, i32* %43)
%45 = tail call i32 @fclose(i32* %43)
%46 = zext i8 %39 to i32
%47 = zext i8 %41 to i32
%48 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.9, i64 0, i64 0), i32 %46, i32 %47)
br label %49
49: ; preds = %37, %27, %24
%50 = phi i32 [ 1, %27 ], [ 0, %37 ], [ 1, %24 ]
ret i32 %50
}