#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_SET;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputc(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  FILE *fin, *fout;
  size_t mysize;
  int val, i;
  int audio[8] = {0x40, 0x9E, 0x69, 0xF8, 0x4D, 0x5B, 0xCF, 0x11};
  int audioi = 0;
  int audiofound = 0;
  int offset = 0;
  int lastaudio = 0;
  if (argc == 2) {
    printf("[inputfile=%s]\n", argv[1]);
  } else {
    printf("more arguments needed... %s <file_to_patch>\n", argv[0]);
    exit(1);
  }
  fin = fopen(argv[1], "rb");
  for (i = 0; i < 20; i) {
    val = fgetc(fin);
    if (val == EOF)
      break;
    if (val == audio[audioi]) {
      if (audioi == 7) {
        printf("found audio stream at offset 0x%x\n", offset - 7);
        audioi = 0;
        lastaudio = offset - 7;
        audiofound++;
      } else
        audioi++;
    } else
      audioi = 0;
    offset++;
    if (offset == 0xA000)
      break;
  }
  fclose(fin);
  if (lastaudio != 0) {
    if (audiofound != 1) {
      printf("Will now patch at offset 0x%x.", lastaudio);
      fout = fopen(argv[1], "r+b");
      fseek(fout, lastaudio + 1, SEEK_SET);
      fputc(0, fout);
      fclose(fout);
      printf(" DONE! OK! \n");
    } else
      printf("Only one audio stream found, no need to patch!\n");
  } else
    printf("found no audio, no patching done!\n");
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe73(%rip),%rax
mov    %rax,-0x50(%rbp)
mov    0xe70(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    0xe6d(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xe6a(%rip),%rax
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x54(%rbp)
movl   $0x0,-0x58(%rbp)
movl   $0x0,-0x5c(%rbp)
movl   $0x0,-0x60(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    11f3 <main+0x83>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1212 <main+0xa2>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe3c(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x30(%rbp)
cmpl   $0x14,-0x30(%rbp)
jge    12e5 <main+0x175>
mov    -0x18(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    0x2d97(%rip),%rcx
cmp    (%rcx),%eax
jne    125e <main+0xee>
jmp    12e5 <main+0x175>
mov    -0x2c(%rbp),%eax
movslq -0x54(%rbp),%rcx
cmp    -0x50(%rbp,%rcx,4),%eax
jne    12b9 <main+0x149>
cmpl   $0x7,-0x54(%rbp)
jne    12ab <main+0x13b>
mov    -0x5c(%rbp),%esi
sub    $0x7,%esi
lea    0xdda(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x54(%rbp)
mov    -0x5c(%rbp),%eax
sub    $0x7,%eax
mov    %eax,-0x60(%rbp)
mov    -0x58(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x58(%rbp)
jmp    12b4 <main+0x144>
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    12c0 <main+0x150>
movl   $0x0,-0x54(%rbp)
mov    -0x5c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x5c(%rbp)
cmpl   $0xa000,-0x5c(%rbp)
jne    12db <main+0x16b>
jmp    12e5 <main+0x175>
jmp    12e0 <main+0x170>
jmp    1231 <main+0xc1>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
cmpl   $0x0,-0x60(%rbp)
je     137d <main+0x20d>
cmpl   $0x1,-0x58(%rbp)
je     136a <main+0x1fa>
mov    -0x60(%rbp),%esi
lea    0xd77(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xd80(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x60(%rbp),%esi
add    $0x1,%esi
mov    0x2c9c(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
mov    -0x20(%rbp),%rsi
xor    %edi,%edi
call   1060 <fputc@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
lea    0xd48(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1378 <main+0x208>
lea    0xd42(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    138b <main+0x21b>
lea    0xd5f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    12dd <main+0x15d>
mov    0x8(%rbx),%rsi
lea    0xe7b(%rip),%rdi
xor    %r15d,%r15d
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,0x10(%rsp)
mov    0x8(%rbx),%rdi
lea    0xe9e(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%rbx
mov    $0xfffffff9,%ebp
mov    0x2e15(%rip),%r12
lea    0xe26(%rip),%r14
movl   $0x0,0x8(%rsp)
movl   $0x0,0xc(%rsp)
jmp    121e <main+0x9e>
xor    %r13d,%r13d
lea    0xe6a(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
addl   $0x1,0x8(%rsp)
mov    %ebp,0xc(%rsp)
nopl   0x0(%rax,%rax,1)
add    $0x1,%ebp
mov    %r13d,%r15d
cmp    $0x9ff9,%ebp
je     1247 <main+0xc7>
mov    %rbx,%rdi
call   1060 <fgetc@plt>
cmp    (%r12),%eax
je     1247 <main+0xc7>
movslq %r15d,%rcx
xor    %r13d,%r13d
cmp    (%r14,%rcx,4),%eax
jne    1210 <main+0x90>
cmp    $0x7,%r15d
je     11ec <main+0x6c>
add    $0x1,%r15d
mov    %r15d,%r13d
jmp    1210 <main+0x90>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0xc(%rsp),%ebp
test   %ebp,%ebp
je     1267 <main+0xe7>
cmpl   $0x1,0x8(%rsp)
jne    1270 <main+0xf0>
lea    0xe63(%rip),%rdi
jmp    12c7 <main+0x147>
lea    0xe38(%rip),%rdi
jmp    12c7 <main+0x147>
lea    0xe0c(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x10(%rsp),%rax
mov    0x8(%rax),%rdi
lea    0xe12(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%rbx
add    $0x1,%ebp
mov    0x2d36(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %ebp,%esi
call   1080 <fseek@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   1070 <fputc@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xe30(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rbx),%rsi
lea    0xd49(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@__const.main.audio = [8 x i32] [i32 64, i32 158, i32 105, i32 248, i32 77, i32 91, i32 207, i32 17]
@.str = [16 x i8] c"[inputfile=%s]\0A\00"
@.str.1 = [45 x i8] c"more arguments needed... %s <file_to_patch>\0A\00"
@.str.2 = [3 x i8] c"rb\00"
@EOF = global i32 0
@.str.3 = [35 x i8] c"found audio stream at offset 0x%x\0A\00"
@.str.4 = [31 x i8] c"Will now patch at offset 0x%x.\00"
@.str.5 = [4 x i8] c"r+b\00"
@SEEK_SET = global i32 0
@.str.6 = [13 x i8] c" DONE! OK! \0A\00"
@.str.7 = [48 x i8] c"Only one audio stream found, no need to patch!\0A\00"
@.str.8 = [35 x i8] c"found no audio, no patching done!\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 i64
%9 = alloca i32
%10 = alloca i32
%11 = alloca [8 x i32]
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = bitcast [8 x i32]* %11 to i8*
store i32 0, i32* %12
store i32 0, i32* %13
store i32 0, i32* %14
store i32 0, i32* %15
%17 = load i32, i32* %4
%18 = icmp eq i32 %17, 2
br i1 %18, label %19, label %24
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %22)
br label %30
24: ; preds = %2
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 0
%27 = load i8*, i8** %26
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i8* %27)
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %19
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = call i32* @fopen(i8* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %34, i32** %6
store i32 0, i32* %10
br label %35
35: ; preds = %75, %30
%36 = load i32, i32* %10
%37 = icmp slt i32 %36, 20
br i1 %37, label %38, label %77
38: ; preds = %35
%39 = load i32*, i32** %6
%40 = call i32 @fgetc(i32* %39)
store i32 %40, i32* %9
%41 = load i32, i32* %9
%42 = load i32, i32* @EOF
%43 = icmp eq i32 %41, %42
br i1 %43, label %44, label %45
44: ; preds = %38
br label %77
45: ; preds = %38
%46 = load i32, i32* %9
%47 = load i32, i32* %12
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds [8 x i32], [8 x i32]* %11, i64 0, i64 %48
%50 = load i32, i32* %49
%51 = icmp eq i32 %46, %50
br i1 %51, label %52, label %67
52: ; preds = %45
%53 = load i32, i32* %12
%54 = icmp eq i32 %53, 7
br i1 %54, label %55, label %63
55: ; preds = %52
%56 = load i32, i32* %14
%57 = sub nsw i32 %56, 7
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %57)
store i32 0, i32* %12
%59 = load i32, i32* %14
%60 = sub nsw i32 %59, 7
store i32 %60, i32* %15
%61 = load i32, i32* %13
%62 = add nsw i32 %61, 1
store i32 %62, i32* %13
br label %66
63: ; preds = %52
%64 = load i32, i32* %12
%65 = add nsw i32 %64, 1
store i32 %65, i32* %12
br label %66
66: ; preds = %63, %55
br label %68
67: ; preds = %45
store i32 0, i32* %12
br label %68
68: ; preds = %67, %66
%69 = load i32, i32* %14
%70 = add nsw i32 %69, 1
store i32 %70, i32* %14
%71 = load i32, i32* %14
%72 = icmp eq i32 %71, 40960
br i1 %72, label %73, label %74
73: ; preds = %68
br label %77
74: ; preds = %68
br label %75
75: ; preds = %74
%76 = load i32, i32* %10
br label %35
77: ; preds = %73, %44, %35
%78 = load i32*, i32** %6
%79 = call i32 @fclose(i32* %78)
%80 = load i32, i32* %15
%81 = icmp ne i32 %80, 0
br i1 %81, label %82, label %105
82: ; preds = %77
%83 = load i32, i32* %13
%84 = icmp ne i32 %83, 1
br i1 %84, label %85, label %102
85: ; preds = %82
%86 = load i32, i32* %15
%87 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i32 %86)
%88 = load i8**, i8*** %5
%89 = getelementptr inbounds i8*, i8** %88, i64 1
%90 = load i8*, i8** %89
%91 = call i32* @fopen(i8* %90, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0))
store i32* %91, i32** %7
%92 = load i32*, i32** %7
%93 = load i32, i32* %15
%94 = add nsw i32 %93, 1
%95 = load i32, i32* @SEEK_SET
%96 = call i32 @fseek(i32* %92, i32 %94, i32 %95)
%97 = load i32*, i32** %7
%98 = call i32 @fputc(i32 0, i32* %97)
%99 = load i32*, i32** %7
%100 = call i32 @fclose(i32* %99)
%101 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
br label %104
102: ; preds = %82
%103 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.7, i64 0, i64 0))
br label %104
104: ; preds = %102, %85
br label %107
105: ; preds = %77
%106 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.8, i64 0, i64 0))
br label %107
107: ; preds = %105, %104
%108 = load i32, i32* %3
ret i32 %108
}

LLVM IR (-O2):
@__const.main.audio = [8 x i32] [i32 64, i32 158, i32 105, i32 248, i32 77, i32 91, i32 207, i32 17]
@.str = [16 x i8] c"[inputfile=%s]\0A\00"
@.str.1 = [45 x i8] c"more arguments needed... %s <file_to_patch>\0A\00"
@.str.2 = [3 x i8] c"rb\00"
@EOF = global i32 0
@.str.3 = [35 x i8] c"found audio stream at offset 0x%x\0A\00"
@.str.4 = [31 x i8] c"Will now patch at offset 0x%x.\00"
@.str.5 = [4 x i8] c"r+b\00"
@SEEK_SET = global i32 0
@str = [34 x i8] c"found no audio, no patching done!\00"
@str.9 = [47 x i8] c"Only one audio stream found, no need to patch!\00"
@str.10 = [12 x i8] c" DONE! OK! \00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %4, label %10
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = load i8*, i8** %5
%9 = tail call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %14
10: ; preds = %2
%11 = load i8*, i8** %1
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i8* %11)
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %35, %4
%15 = phi i32 [ 0, %4 ], [ %36, %35 ]
%16 = phi i32 [ 0, %4 ], [ %37, %35 ]
%17 = phi i32 [ 0, %4 ], [ %39, %35 ]
%18 = phi i32 [ 0, %4 ], [ %38, %35 ]
%19 = tail call i32 @fgetc(i32* %9)
%20 = load i32, i32* @EOF
%21 = icmp eq i32 %19, %20
br i1 %21, label %41, label %22
22: ; preds = %14
%23 = sext i32 %15 to i64
%24 = getelementptr inbounds [8 x i32], [8 x i32]* @__const.main.audio, i64 0, i64 %23
%25 = load i32, i32* %24
%26 = icmp eq i32 %19, %25
br i1 %26, label %27, label %35
27: ; preds = %22
%28 = icmp eq i32 %15, 7
br i1 %28, label %29, label %33
29: ; preds = %27
%30 = add nsw i32 %17, -7
%31 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %30)
%32 = add nsw i32 %16, 1
br label %35
33: ; preds = %27
%34 = add nsw i32 %15, 1
br label %35
35: ; preds = %22, %29, %33
%36 = phi i32 [ 0, %29 ], [ %34, %33 ], [ 0, %22 ]
%37 = phi i32 [ %32, %29 ], [ %16, %33 ], [ %16, %22 ]
%38 = phi i32 [ %30, %29 ], [ %18, %33 ], [ %18, %22 ]
%39 = add nuw nsw i32 %17, 1
%40 = icmp eq i32 %39, 40960
br i1 %40, label %41, label %14
41: ; preds = %35, %14
%42 = phi i32 [ %16, %14 ], [ %37, %35 ]
%43 = phi i32 [ %18, %14 ], [ %38, %35 ]
%44 = tail call i32 @fclose(i32* %9)
%45 = icmp eq i32 %43, 0
br i1 %45, label %57, label %46
46: ; preds = %41
%47 = icmp eq i32 %42, 1
br i1 %47, label %57, label %48
48: ; preds = %46
%49 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0), i32 %43)
%50 = load i8*, i8** %5
%51 = tail call i32* @fopen(i8* %50, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0))
%52 = add nsw i32 %43, 1
%53 = load i32, i32* @SEEK_SET
%54 = tail call i32 @fseek(i32* %51, i32 %52, i32 %53)
%55 = tail call i32 @fputc(i32 0, i32* %51)
%56 = tail call i32 @fclose(i32* %51)
br label %57
57: ; preds = %41, %46, %48
%58 = phi i8* [ getelementptr inbounds ([12 x i8], [12 x i8]* @str.10, i64 0, i64 0), %48 ], [ getelementptr inbounds ([47 x i8], [47 x i8]* @str.9, i64 0, i64 0), %46 ], [ getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0), %41 ]
%59 = tail call i32 @puts(i8* dereferenceable(1) %58)
ret i32 0
}