#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ sprintf(char *, char *, char const *);
size_t strlen(char *);
char *strstr(char *, char *);
int strtol(char *, int /*<<< orphan*/ *, int);
int scanmodifications(const char *filename, const char *basename) {
  FILE *fp;
  char str[512];
  char constant[128];
  char *ptr;
  size_t constlen;
  int modifcount = 0;
  if ((fp = fopen(filename, "r")) == NULL)
    return 0;
  sprintf(constant, "%sMODIFIED", basename);
  constlen = strlen(constant);
  while (fgets(str, sizeof str, fp) != NULL) {
    if ((ptr = strstr(str, constant)) != NULL && ptr > str &&
        *(ptr - 1) <= ' ' && *(ptr + constlen) <= ' ')
      modifcount = strtol(ptr + constlen, NULL, 10);
  }
  fclose(fp);
  return modifcount;
}


x86 assembly (clang, -O0):
<scanmodifications>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2c0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x2b4(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe68(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11b7 <scanmodifications+0x47>
movl   $0x0,-0x4(%rbp)
jmp    12a3 <scanmodifications+0x133>
lea    -0x2a0(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe39(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x2a0(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x2b0(%rbp)
lea    -0x220(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0x200,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     1291 <scanmodifications+0x121>
lea    -0x220(%rbp),%rdi
lea    -0x2a0(%rbp),%rsi
call   1090 <strstr@plt>
mov    %rax,-0x2a8(%rbp)
cmp    $0x0,%rax
je     128c <scanmodifications+0x11c>
mov    -0x2a8(%rbp),%rax
lea    -0x220(%rbp),%rcx
cmp    %rcx,%rax
jbe    128c <scanmodifications+0x11c>
mov    -0x2a8(%rbp),%rax
movsbl -0x1(%rax),%eax
cmp    $0x20,%eax
jg     128c <scanmodifications+0x11c>
mov    -0x2a8(%rbp),%rax
mov    -0x2b0(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x20,%eax
jg     128c <scanmodifications+0x11c>
mov    -0x2a8(%rbp),%rdi
add    -0x2b0(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %eax,-0x2b4(%rbp)
jmp    11e1 <scanmodifications+0x71>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x2b4(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2c0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scanmodifications>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x280,%rsp
mov    %rsi,%rbx
lea    0xe76(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1233 <scanmodifications+0xc3>
mov    %rax,%r15
lea    0xe60(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
mov    %rbx,%rdx
call   1080 <sprintf@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,%r14
lea    0x80(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x200,%esi
mov    %r15,%rdx
call   1050 <fgets@plt>
xor    %r12d,%r12d
test   %rax,%rax
je     1229 <scanmodifications+0xb9>
mov    %rsp,%r13
jmp    11f5 <scanmodifications+0x85>
mov    %rbx,%rdi
mov    $0x200,%esi
mov    %r15,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1229 <scanmodifications+0xb9>
mov    %rbx,%rdi
mov    %r13,%rsi
call   1090 <strstr@plt>
cmp    %rbx,%rax
jbe    11e0 <scanmodifications+0x70>
cmpb   $0x20,-0x1(%rax)
jg     11e0 <scanmodifications+0x70>
cmpb   $0x20,(%rax,%r14,1)
jg     11e0 <scanmodifications+0x70>
add    %r14,%rax
mov    %rax,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1060 <strtol@plt>
mov    %eax,%r12d
jmp    11e0 <scanmodifications+0x70>
mov    %r15,%rdi
call   1030 <fclose@plt>
jmp    1236 <scanmodifications+0xc6>
xor    %r12d,%r12d
mov    %r12d,%eax
add    $0x280,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [11 x i8] c"%sMODIFIED\00"
define i32 @scanmodifications(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca [512 x i8]
%8 = alloca [128 x i8]
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %11
%12 = load i8*, i8** %4
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %6
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 0, i32* %3
br label %59
16: ; preds = %2
%17 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%18 = load i8*, i8** %5
%19 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%20 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%21 = call i64 @strlen(i8* %20)
store i64 %21, i64* %10
br label %22
22: ; preds = %54, %16
%23 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%24 = load i32*, i32** %6
%25 = call i32* @fgets(i8* %23, i32 512, i32* %24)
%26 = icmp ne i32* %25, null
br i1 %26, label %27, label %55
27: ; preds = %22
%28 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%29 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%30 = call i8* @strstr(i8* %28, i8* %29)
store i8* %30, i8** %9
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %54
32: ; preds = %27
%33 = load i8*, i8** %9
%34 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%35 = icmp ugt i8* %33, %34
br i1 %35, label %36, label %54
36: ; preds = %32
%37 = load i8*, i8** %9
%38 = getelementptr inbounds i8, i8* %37, i64 -1
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp sle i32 %40, 32
br i1 %41, label %42, label %54
42: ; preds = %36
%43 = load i8*, i8** %9
%44 = load i64, i64* %10
%45 = getelementptr inbounds i8, i8* %43, i64 %44
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = icmp sle i32 %47, 32
br i1 %48, label %49, label %54
49: ; preds = %42
%50 = load i8*, i8** %9
%51 = load i64, i64* %10
%52 = getelementptr inbounds i8, i8* %50, i64 %51
%53 = call i32 @strtol(i8* %52, i32* null, i32 10)
store i32 %53, i32* %11
br label %54
54: ; preds = %49, %42, %36, %32, %27
br label %22
55: ; preds = %22
%56 = load i32*, i32** %6
%57 = call i32 @fclose(i32* %56)
%58 = load i32, i32* %11
store i32 %58, i32* %3
br label %59
59: ; preds = %55, %15
%60 = load i32, i32* %3
ret i32 %60
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [11 x i8] c"%sMODIFIED\00"
define i32 @scanmodifications(i8* nocapture %0, i8* %1) {
%3 = alloca [512 x i8]
%4 = alloca [128 x i8]
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%7 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %35, label %9
9: ; preds = %2
%10 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%11 = call i64 @strlen(i8* %6)
%12 = call i32* @fgets(i8* %5, i32 512, i32* %7)
%13 = icmp eq i32* %12, null
br i1 %13, label %32, label %14
14: ; preds = %9, %28
%15 = phi i32 [ %29, %28 ], [ 0, %9 ]
%16 = call i8* @strstr(i8* %5, i8* %6)
%17 = icmp ugt i8* %16, %5
br i1 %17, label %18, label %28
18: ; preds = %14
%19 = getelementptr inbounds i8, i8* %16, i64 -1
%20 = load i8, i8* %19
%21 = icmp slt i8 %20, 33
br i1 %21, label %22, label %28
22: ; preds = %18
%23 = getelementptr inbounds i8, i8* %16, i64 %11
%24 = load i8, i8* %23
%25 = icmp slt i8 %24, 33
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = call i32 @strtol(i8* nocapture %23, i32* null, i32 10)
br label %28
28: ; preds = %26, %22, %18, %14
%29 = phi i32 [ %27, %26 ], [ %15, %22 ], [ %15, %18 ], [ %15, %14 ]
%30 = call i32* @fgets(i8* %5, i32 512, i32* %7)
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %14
32: ; preds = %28, %9
%33 = phi i32 [ 0, %9 ], [ %29, %28 ]
%34 = call i32 @fclose(i32* %7)
br label %35
35: ; preds = %2, %32
%36 = phi i32 [ %33, %32 ], [ 0, %2 ]
ret i32 %36
}