#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 */
struct stat {
  int st_size;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EOF;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, long);
int stat(char *, struct stat *);
int main(int argc, char *argv[]) {
  int j, i;
  struct stat data;
  char size[8];
  char string[] = "This emulator executable has a size of N";
  FILE *fd;
  if (stat(argv[1], &data) == -1) {
    printf("Usage: %s <file>\n", argv[0]);
    return -1;
  }
  if (data.st_size >= 10000000) {
    printf("Error: Size exceeding 7 digits\n");
    return -1;
  }
  if ((fd = fopen(argv[1], "r+b")) == NULL) {
    printf("Error: Unable to open %s for reading and writing", argv[0]);
    return -1;
  }
  sprintf(size, "%7li", (long)data.st_size);
  i = 0;
  for (j = getc(fd); j != EOF; j = getc(fd)) {
    if (j == string[i])
      i++;
    else
      i = 0;
    if (string[i] == 'N') {
      fseek(fd, 0, SEEK_CUR);
      fwrite(size, 1, 7, fd);
      fflush(fd);
    }
  }
  fclose(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x60(%rbp),%rdi
lea    0xe3f(%rip),%rsi
mov    $0x29,%edx
call   1060 <memcpy@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x20(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0xffffffff,%eax
jne    1206 <main+0x66>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133e <main+0x19e>
cmpl   $0x989680,-0x20(%rbp)
jl     122d <main+0x8d>
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133e <main+0x19e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe1f(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x68(%rbp)
cmp    $0x0,%rax
jne    1270 <main+0xd0>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe02(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133e <main+0x19e>
lea    -0x28(%rbp),%rdi
movslq -0x20(%rbp),%rdx
lea    0xe11(%rip),%rsi
call   10a0 <sprintf@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x68(%rbp),%rdi
call   10c0 <getc@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2d3f(%rip),%rcx
cmp    (%rcx),%eax
je     132e <main+0x18e>
mov    -0x14(%rbp),%eax
movslq -0x18(%rbp),%rcx
movsbl -0x60(%rbp,%rcx,1),%ecx
cmp    %ecx,%eax
jne    12cb <main+0x12b>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    12d2 <main+0x132>
movl   $0x0,-0x18(%rbp)
movslq -0x18(%rbp),%rax
movsbl -0x60(%rbp,%rax,1),%eax
cmp    $0x4e,%eax
jne    1318 <main+0x178>
mov    -0x68(%rbp),%rdi
mov    0x2cf9(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <fseek@plt>
lea    -0x28(%rbp),%rdi
mov    -0x68(%rbp),%rcx
mov    $0x1,%esi
mov    $0x7,%edx
call   10b0 <fwrite@plt>
mov    -0x68(%rbp),%rdi
call   1070 <fflush@plt>
jmp    131d <main+0x17d>
mov    -0x68(%rbp),%rdi
call   10c0 <getc@plt>
mov    %eax,-0x14(%rbp)
jmp    1297 <main+0xf7>
mov    -0x68(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%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,%r14
mov    0x8(%rsi),%rdi
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
cmp    $0xffffffff,%eax
je     11e0 <main+0x40>
movslq 0x8(%rsp),%r15
cmp    $0x989680,%r15
jl     11ec <main+0x4c>
lea    0xe9c(%rip),%rdi
call   1030 <puts@plt>
jmp    1249 <main+0xa9>
mov    (%r14),%rsi
lea    0xe3f(%rip),%rdi
jmp    1242 <main+0xa2>
mov    0x8(%r14),%rdi
lea    0xe44(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1238 <main+0x98>
mov    %rax,%rbx
lea    0xe65(%rip),%rsi
lea    0x10(%rsp),%rdi
mov    %r15,%rdx
call   10a0 <sprintf@plt>
mov    %rbx,%rdi
call   10c0 <getc@plt>
mov    0x2db9(%rip),%r15
cmp    (%r15),%eax
jne    125d <main+0xbd>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    124e <main+0xae>
mov    (%r14),%rsi
lea    0xdfd(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %r12d,%r12d
lea    0xd99(%rip),%rbp
lea    0x10(%rsp),%r14
xor    %r13d,%r13d
jmp    128d <main+0xed>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   10c0 <getc@plt>
cmp    (%r15),%eax
je     122c <main+0x8c>
movslq %r13d,%r13
movsbl 0x0(%r13,%rbp,1),%ecx
add    $0x1,%r13d
cmp    %ecx,%eax
cmovne %r12d,%r13d
cmp    $0x27,%r13d
jne    1280 <main+0xe0>
mov    0x2d3b(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <fseek@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x7,%edx
mov    %rbx,%rcx
call   10b0 <fwrite@plt>
mov    %rbx,%rdi
call   1070 <fflush@plt>
jmp    1280 <main+0xe0>

LLVM IR (-O0):
%struct.stat = type { i32 }
@__const.main.string = [41 x i8] c"This emulator executable has a size of N\00"
@.str = [18 x i8] c"Usage: %s <file>\0A\00"
@.str.1 = [32 x i8] c"Error: Size exceeding 7 digits\0A\00"
@.str.2 = [4 x i8] c"r+b\00"
@.str.3 = [49 x i8] c"Error: Unable to open %s for reading and writing\00"
@.str.4 = [5 x i8] c"%7li\00"
@EOF = global i32 0
@SEEK_CUR = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.stat
%9 = alloca [8 x i8]
%10 = alloca [41 x i8]
%11 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = bitcast [41 x i8]* %10 to i8*
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = call i32 @stat(i8* %15, %struct.stat* %8)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %23
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 0
%21 = load i8*, i8** %20
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %21)
store i32 -1, i32* %3
br label %87
23: ; preds = %2
%24 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%25 = load i32, i32* %24
%26 = icmp sge i32 %25, 10000000
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %87
29: ; preds = %23
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = call i32* @fopen(i8* %32, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
store i32* %33, i32** %11
%34 = icmp eq i32* %33, null
br i1 %34, label %35, label %40
35: ; preds = %29
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 0
%38 = load i8*, i8** %37
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0), i8* %38)
store i32 -1, i32* %3
br label %87
40: ; preds = %29
%41 = getelementptr inbounds [8 x i8], [8 x i8]* %9, i64 0, i64 0
%42 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%43 = load i32, i32* %42
%44 = sext i32 %43 to i64
%45 = call i32 @sprintf(i8* %41, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i64 %44)
store i32 0, i32* %7
%46 = load i32*, i32** %11
%47 = call i32 @getc(i32* %46)
store i32 %47, i32* %6
br label %48
48: ; preds = %81, %40
%49 = load i32, i32* %6
%50 = load i32, i32* @EOF
%51 = icmp ne i32 %49, %50
br i1 %51, label %52, label %84
52: ; preds = %48
%53 = load i32, i32* %6
%54 = load i32, i32* %7
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds [41 x i8], [41 x i8]* %10, i64 0, i64 %55
%57 = load i8, i8* %56
%58 = sext i8 %57 to i32
%59 = icmp eq i32 %53, %58
br i1 %59, label %60, label %63
60: ; preds = %52
%61 = load i32, i32* %7
%62 = add nsw i32 %61, 1
store i32 %62, i32* %7
br label %64
63: ; preds = %52
store i32 0, i32* %7
br label %64
64: ; preds = %63, %60
%65 = load i32, i32* %7
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds [41 x i8], [41 x i8]* %10, i64 0, i64 %66
%68 = load i8, i8* %67
%69 = sext i8 %68 to i32
%70 = icmp eq i32 %69, 78
br i1 %70, label %71, label %80
71: ; preds = %64
%72 = load i32*, i32** %11
%73 = load i32, i32* @SEEK_CUR
%74 = call i32 @fseek(i32* %72, i32 0, i32 %73)
%75 = getelementptr inbounds [8 x i8], [8 x i8]* %9, i64 0, i64 0
%76 = load i32*, i32** %11
%77 = call i32 @fwrite(i8* %75, i32 1, i32 7, i32* %76)
%78 = load i32*, i32** %11
%79 = call i32 @fflush(i32* %78)
br label %80
80: ; preds = %71, %64
br label %81
81: ; preds = %80
%82 = load i32*, i32** %11
%83 = call i32 @getc(i32* %82)
store i32 %83, i32* %6
br label %48
84: ; preds = %48
%85 = load i32*, i32** %11
%86 = call i32 @fclose(i32* %85)
store i32 0, i32* %3
br label %87
87: ; preds = %84, %35, %27, %18
%88 = load i32, i32* %3
ret i32 %88
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@__const.main.string = [41 x i8] c"This emulator executable has a size of N\00"
@.str = [18 x i8] c"Usage: %s <file>\0A\00"
@.str.2 = [4 x i8] c"r+b\00"
@.str.3 = [49 x i8] c"Error: Unable to open %s for reading and writing\00"
@.str.4 = [5 x i8] c"%7li\00"
@EOF = global i32 0
@SEEK_CUR = global i32 0
@str = [31 x i8] c"Error: Size exceeding 7 digits\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = alloca [8 x i8]
%5 = bitcast %struct.stat* %3 to i8*
%6 = getelementptr inbounds [8 x i8], [8 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = call i32 @stat(i8* %8, %struct.stat* %3)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = load i8*, i8** %1
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %12)
br label %55
14: ; preds = %2
%15 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%16 = load i32, i32* %15
%17 = icmp sgt i32 %16, 9999999
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
br label %55
20: ; preds = %14
%21 = load i8*, i8** %7
%22 = tail call i32* @fopen(i8* %21, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = load i8*, i8** %1
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0), i8* %25)
br label %55
27: ; preds = %20
%28 = sext i32 %16 to i64
%29 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i64 %28)
%30 = tail call i32 @getc(i32* %22)
%31 = load i32, i32* @EOF
%32 = icmp eq i32 %30, %31
br i1 %32, label %53, label %33
33: ; preds = %27, %49
%34 = phi i32 [ %42, %49 ], [ 0, %27 ]
%35 = phi i32 [ %50, %49 ], [ %30, %27 ]
%36 = sext i32 %34 to i64
%37 = getelementptr inbounds [41 x i8], [41 x i8]* @__const.main.string, i64 0, i64 %36
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp eq i32 %35, %39
%41 = add nsw i32 %34, 1
%42 = select i1 %40, i32 %41, i32 0
%43 = icmp eq i32 %42, 39
br i1 %43, label %44, label %49
44: ; preds = %33
%45 = load i32, i32* @SEEK_CUR
%46 = tail call i32 @fseek(i32* %22, i32 0, i32 %45)
%47 = call i32 @fwrite(i8* %6, i32 1, i32 7, i32* %22)
%48 = tail call i32 @fflush(i32* %22)
br label %49
49: ; preds = %33, %44
%50 = tail call i32 @getc(i32* %22)
%51 = load i32, i32* @EOF
%52 = icmp eq i32 %50, %51
br i1 %52, label %53, label %33
53: ; preds = %49, %27
%54 = tail call i32 @fclose(i32* %22)
br label %55
55: ; preds = %53, %24, %18, %11
%56 = phi i32 [ -1, %11 ], [ -1, %18 ], [ -1, %24 ], [ 0, %53 ]
ret i32 %56
}