#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 */
scalar_t__ EOF;
int atoi(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ isdigit(char);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int strlen(char *);
int /*<<< orphan*/ unlink(char *);
int ensureValid(char *file, int *width, int *height) {
  FILE *filechk;
  FILE *sysout;
  char buff[1024], syscmd[1024];
  filechk = fopen(file, "r");
  if (!filechk || fgetc(filechk) == EOF) {
    if (filechk) {
      fclose(filechk);
      unlink(file);
    }
    return 0;
  }
  fclose(filechk);
  sprintf(syscmd, "identify %s", file);
  sysout = popen(syscmd, "r");
  if (!sysout)
    return 0;
  if (!fgets(buff, 1024, sysout)) {
    pclose(sysout);
    return 0;
  }
  pclose(sysout);
  printf(buff);
  if (!isdigit(buff[strlen(file) + 6]) && buff[strlen(file) + 6] > '0')
    return 0;
  int mywidth = atoi(buff + strlen(file) + 6);
  int myheight = atoi(
      buff + strlen(file) + 6 +
      (mywidth < 100 ? (mywidth < 10 ? 1 : 2) : (mywidth >= 1000 ? 4 : 3)) + 1);
  printf("%d, %d\n", mywidth, myheight);
  if (*width == 0) {
    *width = mywidth;
    *height = myheight;
  }
  if (*width != mywidth || *height != myheight)
    return 0;
  return 1;
}


x86 assembly (clang, -O0):
<ensureValid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x860,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe1e(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     120f <ensureValid+0x4f>
mov    -0x28(%rbp),%rdi
call   1090 <fgetc@plt>
mov    0x2de2(%rip),%rcx
cmp    (%rcx),%rax
jne    1238 <ensureValid+0x78>
cmpq   $0x0,-0x28(%rbp)
je     122c <ensureValid+0x6c>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
movl   $0x0,-0x4(%rbp)
jmp    1466 <ensureValid+0x2a6>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
lea    -0x830(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xdaf(%rip),%rsi
call   10e0 <sprintf@plt>
lea    -0x830(%rbp),%rdi
lea    0xd9a(%rip),%rsi
call   10b0 <popen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1286 <ensureValid+0xc6>
movl   $0x0,-0x4(%rbp)
jmp    1466 <ensureValid+0x2a6>
lea    -0x430(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    $0x400,%esi
call   10a0 <fgets@plt>
cmp    $0x0,%eax
jne    12b9 <ensureValid+0xf9>
mov    -0x30(%rbp),%rdi
call   1080 <pclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    1466 <ensureValid+0x2a6>
mov    -0x30(%rbp),%rdi
call   1080 <pclose@plt>
lea    -0x430(%rbp),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
add    $0x6,%eax
cltq
movsbl -0x430(%rbp,%rax,1),%edi
ff
call   1040 <isdigit@plt>
cmp    $0x0,%eax
jne    131f <ensureValid+0x15f>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
add    $0x6,%eax
cltq
movsbl -0x430(%rbp,%rax,1),%eax
ff
cmp    $0x30,%eax
jle    131f <ensureValid+0x15f>
movl   $0x0,-0x4(%rbp)
jmp    1466 <ensureValid+0x2a6>
lea    -0x430(%rbp),%rax
mov    %rax,-0x850(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x850(%rbp),%rdi
cltq
add    %rax,%rdi
add    $0x6,%rdi
call   10d0 <atoi@plt>
mov    %eax,-0x834(%rbp)
lea    -0x430(%rbp),%rax
mov    %rax,-0x848(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x848(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
add    $0x6,%rax
mov    %rax,-0x840(%rbp)
cmpl   $0x64,-0x834(%rbp)
jge    13b0 <ensureValid+0x1f0>
mov    -0x834(%rbp),%edx
mov    $0x2,%eax
mov    $0x1,%ecx
cmp    $0xa,%edx
cmovl  %ecx,%eax
mov    %eax,-0x854(%rbp)
jmp    13cf <ensureValid+0x20f>
mov    -0x834(%rbp),%edx
mov    $0x3,%eax
mov    $0x4,%ecx
cmp    $0x3e8,%edx
cmovge %ecx,%eax
mov    %eax,-0x854(%rbp)
mov    -0x840(%rbp),%rdi
mov    -0x854(%rbp),%eax
cltq
add    %rax,%rdi
add    $0x1,%rdi
call   10d0 <atoi@plt>
mov    %eax,-0x838(%rbp)
mov    -0x834(%rbp),%esi
mov    -0x838(%rbp),%edx
lea    0xc0b(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x18(%rbp),%rax
cmpl   $0x0,(%rax)
jne    142f <ensureValid+0x26f>
mov    -0x834(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x838(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
cmp    -0x834(%rbp),%eax
jne    1453 <ensureValid+0x293>
mov    -0x20(%rbp),%rax
mov    (%rax),%eax
cmp    -0x838(%rbp),%eax
je     145f <ensureValid+0x29f>
movl   $0x0,-0x4(%rbp)
jmp    1466 <ensureValid+0x2a6>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x860,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ensureValid>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x818,%rsp
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r14
lea    0xe1f(%rip),%rsi
call   10c0 <fopen@plt>
xor    %r13d,%r13d
test   %rax,%rax
je     1361 <ensureValid+0x1a1>
mov    %rax,%rbp
mov    %rbx,0x8(%rsp)
mov    %rax,%rdi
call   1090 <fgetc@plt>
mov    %rax,%r12
mov    0x2ddc(%rip),%rax
mov    (%rax),%rbx
mov    %rbp,%rdi
call   1050 <fclose@plt>
cmp    %rbx,%r12
jne    1229 <ensureValid+0x69>
mov    %r14,%rdi
call   1030 <unlink@plt>
jmp    1361 <ensureValid+0x1a1>
lea    0xdd2(%rip),%rsi
lea    0x410(%rsp),%rbx
mov    %rbx,%rdi
mov    %r14,%rdx
call   10e0 <sprintf@plt>
lea    0xdb6(%rip),%rsi
mov    %rbx,%rdi
call   10b0 <popen@plt>
test   %rax,%rax
je     1361 <ensureValid+0x1a1>
mov    %rax,%rbp
lea    0x10(%rsp),%r12
mov    %r12,%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   10a0 <fgets@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1080 <pclose@plt>
test   %ebx,%ebx
je     1361 <ensureValid+0x1a1>
xor    %r13d,%r13d
mov    %r12,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r14,%rdi
call   1060 <strlen@plt>
cltq
movsbl 0x6(%rax,%r12,1),%edi
call   1040 <isdigit@plt>
test   %eax,%eax
jne    12c1 <ensureValid+0x101>
mov    %r14,%rdi
call   1060 <strlen@plt>
cltq
cmpb   $0x30,0x6(%rax,%r12,1)
jg     1361 <ensureValid+0x1a1>
mov    %r14,%rdi
call   1060 <strlen@plt>
cltq
lea    (%rsp,%rax,1),%rdi
add    $0x16,%rdi
call   10d0 <atoi@plt>
mov    %eax,%r12d
mov    %r14,%rdi
call   1060 <strlen@plt>
cltq
add    %rsp,%rax
add    $0x10,%rax
xor    %ecx,%ecx
cmp    $0xa,%r12d
setl   %cl
mov    $0x2,%edx
sub    %rcx,%rdx
xor    %ecx,%ecx
cmp    $0x3e8,%r12d
setge  %cl
add    $0x3,%rcx
cmp    $0x64,%r12d
cmovl  %rdx,%rcx
lea    (%rcx,%rax,1),%rdi
add    $0x7,%rdi
call   10d0 <atoi@plt>
mov    %eax,%ebx
lea    0xce3(%rip),%rdi
xor    %r13d,%r13d
mov    %r12d,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1070 <printf@plt>
mov    (%r15),%eax
test   %eax,%eax
jne    134e <ensureValid+0x18e>
mov    %r12d,(%r15)
mov    0x8(%rsp),%rax
mov    %ebx,(%rax)
mov    (%r15),%eax
cmp    %r12d,%eax
jne    1361 <ensureValid+0x1a1>
xor    %r13d,%r13d
mov    0x8(%rsp),%rax
cmp    %ebx,(%rax)
sete   %r13b
mov    %r13d,%eax
add    $0x818,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@EOF = global i64 0
@.str.1 = [12 x i8] c"identify %s\00"
@.str.2 = [8 x i8] c"%d, %d\0A\00"
define i32 @ensureValid(i8* %0, i32* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca [1024 x i8]
%11 = alloca [1024 x i8]
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32* %2, 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 %18, label %23
18: ; preds = %3
%19 = load i32*, i32** %8
%20 = call i64 @fgetc(i32* %19)
%21 = load i64, i64* @EOF
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %32
23: ; preds = %18, %3
%24 = load i32*, i32** %8
%25 = icmp ne i32* %24, null
br i1 %25, label %26, label %31
26: ; preds = %23
%27 = load i32*, i32** %8
%28 = call i32 @fclose(i32* %27)
%29 = load i8*, i8** %5
%30 = call i32 @unlink(i8* %29)
br label %31
31: ; preds = %26, %23
store i32 0, i32* %4
br label %129
32: ; preds = %18
%33 = load i32*, i32** %8
%34 = call i32 @fclose(i32* %33)
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%36 = load i8*, i8** %5
%37 = call i32 @sprintf(i8* %35, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %36)
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%39 = call i32* @popen(i8* %38, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %39, i32** %9
%40 = load i32*, i32** %9
%41 = icmp ne i32* %40, null
br i1 %41, label %43, label %42
42: ; preds = %32
store i32 0, i32* %4
br label %129
43: ; preds = %32
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%45 = load i32*, i32** %9
%46 = call i32 @fgets(i8* %44, i32 1024, i32* %45)
%47 = icmp ne i32 %46, 0
br i1 %47, label %51, label %48
48: ; preds = %43
%49 = load i32*, i32** %9
%50 = call i32 @pclose(i32* %49)
store i32 0, i32* %4
br label %129
51: ; preds = %43
%52 = load i32*, i32** %9
%53 = call i32 @pclose(i32* %52)
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%55 = call i32 (i8*, ...) @printf(i8* %54)
%56 = load i8*, i8** %5
%57 = call i32 @strlen(i8* %56)
%58 = add nsw i32 %57, 6
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 %59
%61 = load i8, i8* %60
%62 = call i32 @isdigit(i8 signext %61)
%63 = icmp ne i32 %62, 0
br i1 %63, label %74, label %64
64: ; preds = %51
%65 = load i8*, i8** %5
%66 = call i32 @strlen(i8* %65)
%67 = add nsw i32 %66, 6
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 %68
%70 = load i8, i8* %69
%71 = sext i8 %70 to i32
%72 = icmp sgt i32 %71, 48
br i1 %72, label %73, label %74
73: ; preds = %64
store i32 0, i32* %4
br label %129
74: ; preds = %64, %51
%75 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%76 = load i8*, i8** %5
%77 = call i32 @strlen(i8* %76)
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds i8, i8* %75, i64 %78
%80 = getelementptr inbounds i8, i8* %79, i64 6
%81 = call i32 @atoi(i8* %80)
store i32 %81, i32* %12
%82 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%83 = load i8*, i8** %5
%84 = call i32 @strlen(i8* %83)
%85 = sext i32 %84 to i64
%86 = getelementptr inbounds i8, i8* %82, i64 %85
%87 = getelementptr inbounds i8, i8* %86, i64 6
%88 = load i32, i32* %12
%89 = icmp slt i32 %88, 100
br i1 %89, label %90, label %95
90: ; preds = %74
%91 = load i32, i32* %12
%92 = icmp slt i32 %91, 10
%93 = zext i1 %92 to i64
%94 = select i1 %92, i32 1, i32 2
br label %100
95: ; preds = %74
%96 = load i32, i32* %12
%97 = icmp sge i32 %96, 1000
%98 = zext i1 %97 to i64
%99 = select i1 %97, i32 4, i32 3
br label %100
100: ; preds = %95, %90
%101 = phi i32 [ %94, %90 ], [ %99, %95 ]
%102 = sext i32 %101 to i64
%103 = getelementptr inbounds i8, i8* %87, i64 %102
%104 = getelementptr inbounds i8, i8* %103, i64 1
%105 = call i32 @atoi(i8* %104)
store i32 %105, i32* %13
%106 = load i32, i32* %12
%107 = load i32, i32* %13
%108 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %106, i32 %107)
%109 = load i32*, i32** %6
%110 = load i32, i32* %109
%111 = icmp eq i32 %110, 0
br i1 %111, label %112, label %117
112: ; preds = %100
%113 = load i32, i32* %12
%114 = load i32*, i32** %6
store i32 %113, i32* %114
%115 = load i32, i32* %13
%116 = load i32*, i32** %7
store i32 %115, i32* %116
br label %117
117: ; preds = %112, %100
%118 = load i32*, i32** %6
%119 = load i32, i32* %118
%120 = load i32, i32* %12
%121 = icmp ne i32 %119, %120
br i1 %121, label %127, label %122
122: ; preds = %117
%123 = load i32*, i32** %7
%124 = load i32, i32* %123
%125 = load i32, i32* %13
%126 = icmp ne i32 %124, %125
br i1 %126, label %127, label %128
127: ; preds = %122, %117
store i32 0, i32* %4
br label %129
128: ; preds = %122
store i32 1, i32* %4
br label %129
129: ; preds = %128, %127, %73, %48, %42, %31
%130 = load i32, i32* %4
ret i32 %130
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@EOF = global i64 0
@.str.1 = [12 x i8] c"identify %s\00"
@.str.2 = [8 x i8] c"%d, %d\0A\00"
define i32 @ensureValid(i8* %0, i32* nocapture %1, i32* nocapture %2) {
%4 = alloca [1024 x i8]
%5 = alloca [1024 x i8]
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%8 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %72, label %10
10: ; preds = %3
%11 = tail call i64 @fgetc(i32* %8)
%12 = load i64, i64* @EOF
%13 = icmp eq i64 %11, %12
%14 = tail call i32 @fclose(i32* %8)
br i1 %13, label %15, label %17
15: ; preds = %10
%16 = tail call i32 @unlink(i8* %0)
br label %72
17: ; preds = %10
%18 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%19 = call i32* @popen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%20 = icmp eq i32* %19, null
br i1 %20, label %72, label %21
21: ; preds = %17
%22 = call i32 @fgets(i8* %6, i32 1024, i32* %19)
%23 = icmp eq i32 %22, 0
%24 = call i32 @pclose(i32* %19)
br i1 %23, label %72, label %25
25: ; preds = %21
%26 = call i32 (i8*, ...) @printf(i8* %6)
%27 = call i32 @strlen(i8* %0)
%28 = add nsw i32 %27, 6
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %29
%31 = load i8, i8* %30
%32 = call i32 @isdigit(i8 signext %31)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %41
34: ; preds = %25
%35 = call i32 @strlen(i8* %0)
%36 = add nsw i32 %35, 6
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %37
%39 = load i8, i8* %38
%40 = icmp sgt i8 %39, 48
br i1 %40, label %72, label %41
41: ; preds = %34, %25
%42 = call i32 @strlen(i8* %0)
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %43
%45 = getelementptr inbounds i8, i8* %44, i64 6
%46 = call i32 @atoi(i8* %45)
%47 = call i32 @strlen(i8* %0)
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %48
%50 = getelementptr inbounds i8, i8* %49, i64 6
%51 = icmp slt i32 %46, 100
%52 = icmp slt i32 %46, 10
%53 = select i1 %52, i64 1, i64 2
%54 = icmp sgt i32 %46, 999
%55 = select i1 %54, i64 4, i64 3
%56 = select i1 %51, i64 %53, i64 %55
%57 = getelementptr inbounds i8, i8* %50, i64 %56
%58 = getelementptr inbounds i8, i8* %57, i64 1
%59 = call i32 @atoi(i8* %58)
%60 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %46, i32 %59)
%61 = load i32, i32* %1
%62 = icmp eq i32 %61, 0
br i1 %62, label %63, label %65
63: ; preds = %41
store i32 %46, i32* %1
store i32 %59, i32* %2
%64 = load i32, i32* %1
br label %65
65: ; preds = %63, %41
%66 = phi i32 [ %64, %63 ], [ %61, %41 ]
%67 = icmp eq i32 %66, %46
br i1 %67, label %68, label %72
68: ; preds = %65
%69 = load i32, i32* %2
%70 = icmp eq i32 %69, %59
%71 = zext i1 %70 to i32
br label %72
72: ; preds = %21, %68, %65, %34, %17, %15, %3
%73 = phi i32 [ 0, %3 ], [ 0, %15 ], [ 0, %17 ], [ 0, %34 ], [ 0, %65 ], [ %71, %68 ], [ 0, %21 ]
ret i32 %73
}