#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*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ helpcases(char *, char *, int, int);
int /*<<< orphan*/ stderr;
int openfile(char *match, char *file, int flag) {
  FILE *f;
  char str[500];
  f = fopen(file, "r");
  if (f == NULL) {
    fprintf(stderr, "Error: file not found\n");
    return 1;
  }
  int count = 0;
  while (fgets(str, 500, f) != NULL) {
    count++;
    helpcases(match, str, flag, count);
  }
  return 0;
}


x86 assembly (clang, -O0):
<openfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe9f(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1196 <openfile+0x56>
mov    0x2e64(%rip),%rax
mov    (%rax),%edi
lea    0xe7d(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11f3 <openfile+0xb3>
movl   $0x0,-0x224(%rbp)
lea    -0x220(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    $0x1f4,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     11ec <openfile+0xac>
mov    -0x224(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x224(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x220(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x224(%rbp),%ecx
call   1030 <helpcases@plt>
jmp    11a0 <openfile+0x60>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openfile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1f8,%rsp
mov    %edx,%r14d
mov    %rsi,%rax
mov    %rdi,%r12
lea    0xe9f(%rip),%rsi
mov    %rax,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11ca <openfile+0x8a>
mov    %rax,%r13
mov    %rsp,%rdi
mov    $0x1f4,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
xor    %r15d,%r15d
test   %rax,%rax
je     11e5 <openfile+0xa5>
mov    $0x1,%ebx
mov    %rsp,%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %rbp,%rsi
mov    %r14d,%edx
mov    %ebx,%ecx
call   1030 <helpcases@plt>
mov    %rbp,%rdi
mov    $0x1f4,%esi
mov    %r13,%rdx
call   1040 <fgets@plt>
add    $0x1,%ebx
test   %rax,%rax
jne    11a0 <openfile+0x60>
jmp    11e5 <openfile+0xa5>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
lea    0xe28(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%r15d
mov    %r15d,%eax
add    $0x1f8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"Error: file not found\0A\00"
define i32 @openfile(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca [500 x i8]
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = load i8*, i8** %6
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %8
%13 = load i32*, i32** %8
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %18
15: ; preds = %3
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %4
br label %33
18: ; preds = %3
store i32 0, i32* %10
br label %19
19: ; preds = %24, %18
%20 = getelementptr inbounds [500 x i8], [500 x i8]* %9, i64 0, i64 0
%21 = load i32*, i32** %8
%22 = call i32* @fgets(i8* %20, i32 500, i32* %21)
%23 = icmp ne i32* %22, null
br i1 %23, label %24, label %32
24: ; preds = %19
%25 = load i32, i32* %10
%26 = add nsw i32 %25, 1
store i32 %26, i32* %10
%27 = load i8*, i8** %5
%28 = getelementptr inbounds [500 x i8], [500 x i8]* %9, i64 0, i64 0
%29 = load i32, i32* %7
%30 = load i32, i32* %10
%31 = call i32 @helpcases(i8* %27, i8* %28, i32 %29, i32 %30)
br label %19
32: ; preds = %19
store i32 0, i32* %4
br label %33
33: ; preds = %32, %15
%34 = load i32, i32* %4
ret i32 %34
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"Error: file not found\0A\00"
define i32 @openfile(i8* %0, i8* nocapture %1, i32 %2) {
%4 = alloca [500 x i8]
%5 = getelementptr inbounds [500 x i8], [500 x i8]* %4, i64 0, i64 0
%6 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %11, label %8
8: ; preds = %3
%9 = call i32* @fgets(i8* %5, i32 500, i32* %6)
%10 = icmp eq i32* %9, null
br i1 %10, label %20, label %14
11: ; preds = %3
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %20
14: ; preds = %8, %14
%15 = phi i32 [ %16, %14 ], [ 0, %8 ]
%16 = add nuw nsw i32 %15, 1
%17 = call i32 @helpcases(i8* %0, i8* %5, i32 %2, i32 %16)
%18 = call i32* @fgets(i8* %5, i32 500, i32* %6)
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %14
20: ; preds = %14, %8, %11
%21 = phi i32 [ 1, %11 ], [ 0, %8 ], [ 0, %14 ]
ret i32 %21
}