#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 EOF;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fputc(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, char *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  if (argc != 2) {
    fprintf(stderr, "Usage: %s file.h264\n", *argv);
    exit(1);
  }
  argv++;
  FILE *f;
  f = fopen(*argv, "r+");
  if (f == NULL) {
    fprintf(stderr, "Error opening file %s\n", *argv);
    exit(1);
  }
  int c, n = 0;
  do {
    c = fgetc(f);
    if (c == 0x32) {
      fseek(f, -1, SEEK_CUR);
      printf("Token 0x32 found at offset %d. Proceed? [y/N] ", n);
      char c;
      scanf("%c", &c);
      if (c == 'y') {
        fputc(0x29, f);
        printf("0x32 changed to 0x29. Have a nice day!\n");
        break;
      }
    }
    n++;
  } while (c != EOF);
  if (c == EOF)
    printf("Token 0x32 not found. Exiting.\n");
  fclose(f);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11d6 <main+0x46>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe39(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
lea    0xe25(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    122a <main+0x9a>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdfd(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x32,-0x1c(%rbp)
jne    12b2 <main+0x122>
mov    -0x18(%rbp),%rdi
mov    0x2d96(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%esi
call   1080 <fseek@plt>
mov    -0x20(%rbp),%esi
lea    0xdc7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xde8(%rip),%rdi
lea    -0x21(%rbp),%rsi
call   1090 <scanf@plt>
movsbl -0x21(%rbp),%eax
cmp    $0x79,%eax
jne    12ad <main+0x11d>
mov    -0x18(%rbp),%rsi
mov    $0x29,%edi
call   1060 <fputc@plt>
lea    0xdc0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12cd <main+0x13d>
jmp    12b2 <main+0x122>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2d1b(%rip),%rcx
cmp    (%rcx),%eax
jne    1231 <main+0xa1>
mov    -0x1c(%rbp),%eax
mov    0x2d09(%rip),%rcx
cmp    (%rcx),%eax
jne    12ed <main+0x15d>
lea    0xda3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    1283 <main+0xe3>
mov    0x8(%rbx),%rdi
lea    0xe53(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1298 <main+0xf8>
mov    %rax,%r13
lea    0xe84(%rip),%r15
lea    0x7(%rsp),%r12
mov    0x2dfa(%rip),%r14
xor    %ebp,%ebp
jmp    11f8 <main+0x58>
nopw   0x0(%rax,%rax,1)
add    $0x1,%ebp
cmp    (%r14),%ebx
je     125e <main+0xbe>
mov    %r13,%rdi
call   1060 <fgetc@plt>
mov    %eax,%ebx
cmp    $0x32,%eax
jne    11f0 <main+0x50>
mov    0x2dda(%rip),%rax
mov    (%rax),%edx
mov    %r13,%rdi
mov    $0xffffffff,%esi
call   1090 <fseek@plt>
lea    0xe0b(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   10a0 <scanf@plt>
cmpb   $0x79,0x7(%rsp)
jne    11f0 <main+0x50>
mov    $0x29,%edi
mov    %r13,%rsi
call   1070 <fputc@plt>
lea    0xe2d(%rip),%rdi
call   1030 <puts@plt>
cmpl   $0x32,(%r14)
jne    126a <main+0xca>
lea    0xdfc(%rip),%rdi
call   1030 <puts@plt>
mov    %r13,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d46(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xd6a(%rip),%rsi
jmp    12ac <main+0x10c>
mov    0x2d31(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
lea    0xd6c(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [21 x i8] c"Usage: %s file.h264\0A\00"
@.str.1 = [3 x i8] c"r+\00"
@.str.2 = [23 x i8] c"Error opening file %s\0A\00"
@SEEK_CUR = global i32 0
@.str.3 = [47 x i8] c"Token 0x32 found at offset %d. Proceed? [y/N] \00"
@.str.4 = [3 x i8] c"%c\00"
@.str.5 = [40 x i8] c"0x32 changed to 0x29. Have a nice day!\0A\00"
@EOF = global i32 0
@.str.6 = [32 x i8] c"Token 0x32 not found. Exiting.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %18
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8**, i8*** %5
%15 = load i8*, i8** %14
%16 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i32 1
store i8** %20, i8*** %5
%21 = load i8**, i8*** %5
%22 = load i8*, i8** %21
%23 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %23, i32** %6
%24 = load i32*, i32** %6
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %32
26: ; preds = %18
%27 = load i32, i32* @stderr
%28 = load i8**, i8*** %5
%29 = load i8*, i8** %28
%30 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %29)
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %18
store i32 0, i32* %8
br label %33
33: ; preds = %56, %32
%34 = load i32*, i32** %6
%35 = call i32 @fgetc(i32* %34)
store i32 %35, i32* %7
%36 = load i32, i32* %7
%37 = icmp eq i32 %36, 50
br i1 %37, label %38, label %53
38: ; preds = %33
%39 = load i32*, i32** %6
%40 = load i32, i32* @SEEK_CUR
%41 = call i32 @fseek(i32* %39, i32 -1, i32 %40)
%42 = load i32, i32* %8
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i32 %42)
%44 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %9)
%45 = load i8, i8* %9
%46 = sext i8 %45 to i32
%47 = icmp eq i32 %46, 121
br i1 %47, label %48, label %52
48: ; preds = %38
%49 = load i32*, i32** %6
%50 = call i32 @fputc(i32 41, i32* %49)
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.5, i64 0, i64 0))
br label %60
52: ; preds = %38
br label %53
53: ; preds = %52, %33
%54 = load i32, i32* %8
%55 = add nsw i32 %54, 1
store i32 %55, i32* %8
br label %56
56: ; preds = %53
%57 = load i32, i32* %7
%58 = load i32, i32* @EOF
%59 = icmp ne i32 %57, %58
br i1 %59, label %33, label %60
60: ; preds = %56, %48
%61 = load i32, i32* %7
%62 = load i32, i32* @EOF
%63 = icmp eq i32 %61, %62
br i1 %63, label %64, label %66
64: ; preds = %60
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0))
br label %66
66: ; preds = %64, %60
%67 = load i32*, i32** %6
%68 = call i32 @fclose(i32* %67)
%69 = load i32, i32* %3
ret i32 %69
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [21 x i8] c"Usage: %s file.h264\0A\00"
@.str.1 = [3 x i8] c"r+\00"
@.str.2 = [23 x i8] c"Error opening file %s\0A\00"
@SEEK_CUR = global i32 0
@.str.3 = [47 x i8] c"Token 0x32 found at offset %d. Proceed? [y/N] \00"
@.str.4 = [3 x i8] c"%c\00"
@EOF = global i32 0
@str = [31 x i8] c"Token 0x32 not found. Exiting.\00"
@str.7 = [39 x i8] c"0x32 changed to 0x29. Have a nice day!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8
%4 = icmp eq i32 %0, 2
br i1 %4, label %10, label %5
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = load i8*, i8** %1
%8 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %20
15: ; preds = %10
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %11
%18 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %17)
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %10, %32
%21 = phi i32 [ %33, %32 ], [ 0, %10 ]
%22 = call i32 @fgetc(i32* %13)
%23 = icmp eq i32 %22, 50
br i1 %23, label %24, label %32
24: ; preds = %20
%25 = load i32, i32* @SEEK_CUR
%26 = call i32 @fseek(i32* %13, i32 -1, i32 %25)
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i32 %21)
%28 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %3)
%29 = load i8, i8* %3
%30 = icmp eq i8 %29, 121
br i1 %30, label %36, label %31
31: ; preds = %24
br label %32
32: ; preds = %31, %20
%33 = add nuw nsw i32 %21, 1
%34 = load i32, i32* @EOF
%35 = icmp eq i32 %22, %34
br i1 %35, label %41, label %20
36: ; preds = %24
%37 = call i32 @fputc(i32 41, i32* %13)
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @str.7, i64 0, i64 0))
%39 = load i32, i32* @EOF
%40 = icmp eq i32 %39, 50
br i1 %40, label %41, label %43
41: ; preds = %32, %36
%42 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
br label %43
43: ; preds = %41, %36
%44 = call i32 @fclose(i32* %13)
ret i32 0
}