#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*/ uint8_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EOF;
int /*<<< orphan*/ F_OK;
int access(char *, int /*<<< orphan*/);
int /*<<< orphan*/ concat_path(char *, char *, char *);
char *dirname(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, char *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ isvalid_effect(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
char *strrchr(char *, char);
int pfan_read_cfg(char *path, char img_paths[8][1024], uint8_t effects[8][3]) {
  FILE *file = fopen(path, "r");
  if (!file) {
    fprintf(stderr, "Can not open '%s'.\n", path);
    return -1;
  }
  char *dirpath = dirname(path);
  char imgname[256];
  int n = 0;
  int matchs;
  while (n < 8 && (matchs = fscanf(file, "%s%*[ \t]+%hhu-%hhu-%hhu\n", imgname,
                                   &effects[n][0], &effects[n][1],
                                   &effects[n][2])) == 4) {
    char *ext = strrchr(imgname, '.');
    if (!ext || strcmp(ext, ".pbm") != 0) {
      fprintf(stderr, "%s: invalid extension, expected '.pbm'.\n", imgname);
      break;
    }
    concat_path(img_paths[n], dirpath, imgname);
    if (access(img_paths[n], F_OK) == -1) {
      fprintf(stderr, "Image '%s' does not exist.\n", img_paths[n]);
      break;
    }
    if (!isvalid_effect(effects[n])) {
      fprintf(stderr, "Invalid effect value for '%s'.\n", img_paths[n]);
      break;
    }
    n++;
  }
  fclose(file);
  if (matchs != EOF && n < 8)
    return -1;
  return n;
}


x86 assembly (clang, -O0):
<pfan_read_cfg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe3e(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11fb <pfan_read_cfg+0x5b>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe18(%rip),%rsi
call   1090 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    141c <pfan_read_cfg+0x27c>
mov    -0x10(%rbp),%rdi
call   1060 <dirname@plt>
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x134(%rbp)
xor    %eax,%eax
cmpl   $0x8,-0x134(%rbp)
mov    %al,-0x141(%rbp)
jge    128e <pfan_read_cfg+0xee>
mov    -0x28(%rbp),%rdi
lea    -0x130(%rbp),%rdx
mov    -0x20(%rbp),%rcx
movslq -0x134(%rbp),%rax
imul   $0xc,%rax,%rax
add    %rax,%rcx
mov    -0x20(%rbp),%r8
movslq -0x134(%rbp),%rax
imul   $0xc,%rax,%rax
add    %rax,%r8
add    $0x4,%r8
mov    -0x20(%rbp),%r9
movslq -0x134(%rbp),%rax
imul   $0xc,%rax,%rax
add    %rax,%r9
add    $0x8,%r9
lea    0xd9f(%rip),%rsi
call   1070 <fscanf@plt>
mov    %eax,-0x138(%rbp)
cmp    $0x4,%eax
sete   %al
mov    %al,-0x141(%rbp)
mov    -0x141(%rbp),%al
test   $0x1,%al
jne    12a1 <pfan_read_cfg+0x101>
jmp    13dc <pfan_read_cfg+0x23c>
lea    -0x130(%rbp),%rdi
mov    $0x2e,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x140(%rbp)
cmpq   $0x0,-0x140(%rbp)
je     12e4 <pfan_read_cfg+0x144>
mov    -0x140(%rbp),%rdi
lea    0xd5a(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
je     1305 <pfan_read_cfg+0x165>
mov    0x2ced(%rip),%rax
mov    (%rax),%edi
lea    -0x130(%rbp),%rdx
lea    0xd39(%rip),%rsi
call   1090 <fprintf@plt>
jmp    13dc <pfan_read_cfg+0x23c>
mov    -0x18(%rbp),%rdi
movslq -0x134(%rbp),%rax
shl    $0xa,%rax
add    %rax,%rdi
mov    -0x30(%rbp),%rsi
lea    -0x130(%rbp),%rdx
call   1030 <concat_path@plt>
mov    -0x18(%rbp),%rdi
movslq -0x134(%rbp),%rax
shl    $0xa,%rax
add    %rax,%rdi
mov    0x2c88(%rip),%rax
mov    (%rax),%esi
call   10b0 <access@plt>
cmp    $0xffffffff,%eax
jne    137c <pfan_read_cfg+0x1dc>
mov    0x2c81(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
movslq -0x134(%rbp),%rax
shl    $0xa,%rax
add    %rax,%rdx
lea    0xceb(%rip),%rsi
call   1090 <fprintf@plt>
jmp    13dc <pfan_read_cfg+0x23c>
mov    -0x20(%rbp),%rdi
movslq -0x134(%rbp),%rax
imul   $0xc,%rax,%rax
add    %rax,%rdi
call   10a0 <isvalid_effect@plt>
cmp    $0x0,%eax
jne    13c8 <pfan_read_cfg+0x228>
mov    0x2c35(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
movslq -0x134(%rbp),%rax
shl    $0xa,%rax
add    %rax,%rdx
lea    0xcbb(%rip),%rsi
call   1090 <fprintf@plt>
jmp    13dc <pfan_read_cfg+0x23c>
mov    -0x134(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x134(%rbp)
jmp    1212 <pfan_read_cfg+0x72>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x138(%rbp),%eax
mov    0x2bf6(%rip),%rcx
cmp    (%rcx),%eax
je     1413 <pfan_read_cfg+0x273>
cmpl   $0x8,-0x134(%rbp)
jge    1413 <pfan_read_cfg+0x273>
movl   $0xffffffff,-0x4(%rbp)
jmp    141c <pfan_read_cfg+0x27c>
mov    -0x134(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pfan_read_cfg>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %rdx,%rbp
mov    %rsi,%rbx
mov    %rdi,%r15
lea    0xe3f(%rip),%rsi
call   10c0 <fopen@plt>
test   %rax,%rax
je     1296 <pfan_read_cfg+0xf6>
mov    %rax,%r14
mov    %r15,%rdi
call   1060 <dirname@plt>
mov    %rax,0x8(%rsp)
xor    %r12d,%r12d
lea    0x10(%rsp),%r13
nopw   0x0(%rax,%rax,1)
lea    0x4(%rbp),%r8
lea    0x8(%rbp),%r9
mov    %r14,%rdi
lea    0xe14(%rip),%rsi
mov    %r13,%rdx
mov    %rbp,%rcx
call   1070 <fscanf@plt>
mov    %eax,%r15d
cmp    $0x4,%eax
jne    12f6 <pfan_read_cfg+0x156>
mov    %r13,%rdi
mov    $0x2e,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     12b5 <pfan_read_cfg+0x115>
mov    %rax,%rdi
lea    0xdf6(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
jne    12b5 <pfan_read_cfg+0x115>
mov    %rbx,%rdi
mov    0x8(%rsp),%rsi
mov    %r13,%rdx
call   1030 <concat_path@plt>
mov    0x2d6e(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   10b0 <access@plt>
cmp    $0xffffffff,%eax
je     12cc <pfan_read_cfg+0x12c>
mov    %rbp,%rdi
call   10a0 <isvalid_effect@plt>
test   %eax,%eax
je     12de <pfan_read_cfg+0x13e>
add    $0x1,%r12
add    $0xc,%rbp
add    $0x400,%rbx
cmp    $0x8,%r12
jne    11f0 <pfan_read_cfg+0x50>
mov    $0x8,%r12d
jmp    12f6 <pfan_read_cfg+0x156>
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
lea    0xd5c(%rip),%rsi
mov    %r15,%rdx
call   1090 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    1319 <pfan_read_cfg+0x179>
mov    0x2d1c(%rip),%rax
mov    (%rax),%edi
lea    0xd6f(%rip),%rsi
lea    0x10(%rsp),%rdx
jmp    12f1 <pfan_read_cfg+0x151>
mov    0x2d05(%rip),%rax
mov    (%rax),%edi
lea    0xd81(%rip),%rsi
jmp    12ee <pfan_read_cfg+0x14e>
mov    0x2cf3(%rip),%rax
mov    (%rax),%edi
lea    0xd8b(%rip),%rsi
mov    %rbx,%rdx
call   1090 <fprintf@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    0x2ce3(%rip),%rcx
cmp    $0x8,%r12d
mov    $0xffffffff,%eax
cmovae %r12d,%eax
cmp    (%rcx),%r15d
cmove  %r12d,%eax
add    $0x118,%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 = [20 x i8] c"Can not open '%s'.\0A\00"
@.str.2 = [25 x i8] c"%s%*[ \09]+%hhu-%hhu-%hhu\0A\00"
@.str.3 = [5 x i8] c".pbm\00"
@.str.4 = [41 x i8] c"%s: invalid extension, expected '.pbm'.\0A\00"
@F_OK = global i32 0
@.str.5 = [28 x i8] c"Image '%s' does not exist.\0A\00"
@.str.6 = [32 x i8] c"Invalid effect value for '%s'.\0A\00"
@EOF = global i32 0
define i32 @pfan_read_cfg(i8* %0, [1024 x i8]* %1, [3 x i32]* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [1024 x i8]*
%7 = alloca [3 x i32]*
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca [256 x i8]
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
store i8* %0, i8** %5
store [1024 x i8]* %1, [1024 x i8]** %6
store [3 x i32]* %2, [3 x i32]** %7
%14 = load i8*, i8** %5
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %8
%16 = load i32*, i32** %8
%17 = icmp ne i32* %16, null
br i1 %17, label %22, label %18
18: ; preds = %3
%19 = load i32, i32* @stderr
%20 = load i8*, i8** %5
%21 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %20)
store i32 -1, i32* %4
br label %119
22: ; preds = %3
%23 = load i8*, i8** %5
%24 = call i8* @dirname(i8* %23)
store i8* %24, i8** %9
store i32 0, i32* %11
br label %25
25: ; preds = %104, %22
%26 = load i32, i32* %11
%27 = icmp slt i32 %26, 8
br i1 %27, label %28, label %48
28: ; preds = %25
%29 = load i32*, i32** %8
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%31 = load [3 x i32]*, [3 x i32]** %7
%32 = load i32, i32* %11
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [3 x i32], [3 x i32]* %31, i64 %33
%35 = getelementptr inbounds [3 x i32], [3 x i32]* %34, i64 0, i64 0
%36 = load [3 x i32]*, [3 x i32]** %7
%37 = load i32, i32* %11
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [3 x i32], [3 x i32]* %36, i64 %38
%40 = getelementptr inbounds [3 x i32], [3 x i32]* %39, i64 0, i64 1
%41 = load [3 x i32]*, [3 x i32]** %7
%42 = load i32, i32* %11
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [3 x i32], [3 x i32]* %41, i64 %43
%45 = getelementptr inbounds [3 x i32], [3 x i32]* %44, i64 0, i64 2
%46 = call i32 @fscanf(i32* %29, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %30, i32* %35, i32* %40, i32* %45)
store i32 %46, i32* %12
%47 = icmp eq i32 %46, 4
br label %48
48: ; preds = %28, %25
%49 = phi i1 [ false, %25 ], [ %47, %28 ]
br i1 %49, label %50, label %107
50: ; preds = %48
%51 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%52 = call i8* @strrchr(i8* %51, i8 signext 46)
store i8* %52, i8** %13
%53 = load i8*, i8** %13
%54 = icmp ne i8* %53, null
br i1 %54, label %55, label %59
55: ; preds = %50
%56 = load i8*, i8** %13
%57 = call i64 @strcmp(i8* %56, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %63
59: ; preds = %55, %50
%60 = load i32, i32* @stderr
%61 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%62 = call i32 @fprintf(i32 %60, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i8* %61)
br label %107
63: ; preds = %55
%64 = load [1024 x i8]*, [1024 x i8]** %6
%65 = load i32, i32* %11
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds [1024 x i8], [1024 x i8]* %64, i64 %66
%68 = getelementptr inbounds [1024 x i8], [1024 x i8]* %67, i64 0, i64 0
%69 = load i8*, i8** %9
%70 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%71 = call i32 @concat_path(i8* %68, i8* %69, i8* %70)
%72 = load [1024 x i8]*, [1024 x i8]** %6
%73 = load i32, i32* %11
%74 = sext i32 %73 to i64
%75 = getelementptr inbounds [1024 x i8], [1024 x i8]* %72, i64 %74
%76 = getelementptr inbounds [1024 x i8], [1024 x i8]* %75, i64 0, i64 0
%77 = load i32, i32* @F_OK
%78 = call i32 @access(i8* %76, i32 %77)
%79 = icmp eq i32 %78, -1
br i1 %79, label %80, label %88
80: ; preds = %63
%81 = load i32, i32* @stderr
%82 = load [1024 x i8]*, [1024 x i8]** %6
%83 = load i32, i32* %11
%84 = sext i32 %83 to i64
%85 = getelementptr inbounds [1024 x i8], [1024 x i8]* %82, i64 %84
%86 = getelementptr inbounds [1024 x i8], [1024 x i8]* %85, i64 0, i64 0
%87 = call i32 @fprintf(i32 %81, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i8* %86)
br label %107
88: ; preds = %63
%89 = load [3 x i32]*, [3 x i32]** %7
%90 = load i32, i32* %11
%91 = sext i32 %90 to i64
%92 = getelementptr inbounds [3 x i32], [3 x i32]* %89, i64 %91
%93 = getelementptr inbounds [3 x i32], [3 x i32]* %92, i64 0, i64 0
%94 = call i32 @isvalid_effect(i32* %93)
%95 = icmp ne i32 %94, 0
br i1 %95, label %104, label %96
96: ; preds = %88
%97 = load i32, i32* @stderr
%98 = load [1024 x i8]*, [1024 x i8]** %6
%99 = load i32, i32* %11
%100 = sext i32 %99 to i64
%101 = getelementptr inbounds [1024 x i8], [1024 x i8]* %98, i64 %100
%102 = getelementptr inbounds [1024 x i8], [1024 x i8]* %101, i64 0, i64 0
%103 = call i32 @fprintf(i32 %97, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0), i8* %102)
br label %107
104: ; preds = %88
%105 = load i32, i32* %11
%106 = add nsw i32 %105, 1
store i32 %106, i32* %11
br label %25
107: ; preds = %96, %80, %59, %48
%108 = load i32*, i32** %8
%109 = call i32 @fclose(i32* %108)
%110 = load i32, i32* %12
%111 = load i32, i32* @EOF
%112 = icmp ne i32 %110, %111
br i1 %112, label %113, label %117
113: ; preds = %107
%114 = load i32, i32* %11
%115 = icmp slt i32 %114, 8
br i1 %115, label %116, label %117
116: ; preds = %113
store i32 -1, i32* %4
br label %119
117: ; preds = %113, %107
%118 = load i32, i32* %11
store i32 %118, i32* %4
br label %119
119: ; preds = %117, %116, %18
%120 = load i32, i32* %4
ret i32 %120
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [20 x i8] c"Can not open '%s'.\0A\00"
@.str.2 = [25 x i8] c"%s%*[ \09]+%hhu-%hhu-%hhu\0A\00"
@.str.3 = [5 x i8] c".pbm\00"
@.str.4 = [41 x i8] c"%s: invalid extension, expected '.pbm'.\0A\00"
@F_OK = global i32 0
@.str.5 = [28 x i8] c"Image '%s' does not exist.\0A\00"
@.str.6 = [32 x i8] c"Invalid effect value for '%s'.\0A\00"
@EOF = global i32 0
define i32 @pfan_read_cfg(i8* %0, [1024 x i8]* %1, [3 x i32]* %2) {
%4 = alloca [256 x i8]
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %3
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %60
10: ; preds = %3
%11 = tail call i8* @dirname(i8* %0)
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
br label %13
13: ; preds = %10, %47
%14 = phi i64 [ 0, %10 ], [ %48, %47 ]
%15 = getelementptr inbounds [3 x i32], [3 x i32]* %2, i64 %14, i64 0
%16 = getelementptr inbounds [3 x i32], [3 x i32]* %2, i64 %14, i64 1
%17 = getelementptr inbounds [3 x i32], [3 x i32]* %2, i64 %14, i64 2
%18 = call i32 @fscanf(i32* %5, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %12, i32* %15, i32* %16, i32* %17)
%19 = icmp eq i32 %18, 4
br i1 %19, label %20, label %50
20: ; preds = %13
%21 = call i8* @strrchr(i8* %12, i8 signext 46)
%22 = icmp eq i8* %21, null
br i1 %22, label %26, label %23
23: ; preds = %20
%24 = call i64 @strcmp(i8* %21, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%25 = icmp eq i64 %24, 0
br i1 %25, label %30, label %26
26: ; preds = %23, %20
%27 = trunc i64 %14 to i32
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i8* %12)
br label %52
30: ; preds = %23
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 %14, i64 0
%32 = call i32 @concat_path(i8* %31, i8* %11, i8* %12)
%33 = load i32, i32* @F_OK
%34 = call i32 @access(i8* %31, i32 %33)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %40
36: ; preds = %30
%37 = trunc i64 %14 to i32
%38 = load i32, i32* @stderr
%39 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0), i8* %31)
br label %52
40: ; preds = %30
%41 = call i32 @isvalid_effect(i32* %15)
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %40
%44 = trunc i64 %14 to i32
%45 = load i32, i32* @stderr
%46 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.6, i64 0, i64 0), i8* %31)
br label %52
47: ; preds = %40
%48 = add nuw nsw i64 %14, 1
%49 = icmp eq i64 %48, 8
br i1 %49, label %52, label %13
50: ; preds = %13
%51 = trunc i64 %14 to i32
br label %52
52: ; preds = %47, %50, %43, %36, %26
%53 = phi i32 [ %27, %26 ], [ %37, %36 ], [ %44, %43 ], [ %51, %50 ], [ 8, %47 ]
%54 = call i32 @fclose(i32* %5)
%55 = load i32, i32* @EOF
%56 = icmp ne i32 %18, %55
%57 = icmp ult i32 %53, 8
%58 = select i1 %56, i1 %57, i1 false
%59 = select i1 %58, i32 -1, i32 %53
br label %60
60: ; preds = %52, %7
%61 = phi i32 [ %59, %52 ], [ -1, %7 ]
ret i32 %61
}