#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*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ ppm_get_token(int /*<<< orphan*/ *, char *, int);
int sscanf(int /*<<< orphan*/, char *, int *);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(int /*<<< orphan*/, char *);
int ppm_get_size(char *file, int *nx, int *ny) {
  char tok[20];
  FILE *fp;
  if ((fp = fopen(file, "r")) == NULL) {
    fprintf(stderr, "can't read PPM file %s\n", file);
    return 0;
  }
  if (strcmp(ppm_get_token(fp, tok, sizeof tok), "P6")) {
    fprintf(stderr, "%s is not a valid binary PPM file, bad magic#\n", file);
    fclose(fp);
    return 0;
  }
  if (sscanf(ppm_get_token(fp, tok, sizeof tok), "%d", nx) != 1 ||
      sscanf(ppm_get_token(fp, tok, sizeof tok), "%d", ny) != 1) {
    fprintf(stderr, "%s is not a valid PPM file: bad size\n", file);
    fclose(fp);
    return 0;
  }
  fclose(fp);
  return 1;
}


x86 assembly (clang, -O0):
<ppm_get_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe81(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x48(%rbp)
cmp    $0x0,%rax
jne    11b7 <ppm_get_size+0x57>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe5c(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    12a7 <ppm_get_size+0x147>
mov    -0x48(%rbp),%rdi
lea    -0x40(%rbp),%rsi
mov    $0x14,%edx
call   1070 <ppm_get_token@plt>
mov    %eax,%edi
lea    0xe48(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%rax
je     120f <ppm_get_size+0xaf>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe28(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x48(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    12a7 <ppm_get_size+0x147>
mov    -0x48(%rbp),%rdi
lea    -0x40(%rbp),%rsi
mov    $0x14,%edx
call   1070 <ppm_get_token@plt>
mov    %eax,%edi
mov    -0x18(%rbp),%rdx
lea    0xe1e(%rip),%rsi
call   1060 <sscanf@plt>
cmp    $0x1,%eax
jne    1269 <ppm_get_size+0x109>
mov    -0x48(%rbp),%rdi
lea    -0x40(%rbp),%rsi
mov    $0x14,%edx
call   1070 <ppm_get_token@plt>
mov    %eax,%edi
mov    -0x20(%rbp),%rdx
lea    0xdf1(%rip),%rsi
call   1060 <sscanf@plt>
cmp    $0x1,%eax
je     1297 <ppm_get_size+0x137>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdd2(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x48(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    12a7 <ppm_get_size+0x147>
mov    -0x48(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ppm_get_size>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r14
lea    0xe85(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11c0 <ppm_get_size+0x60>
mov    %rax,%rbx
mov    %rsp,%rsi
mov    %rax,%rdi
mov    $0x14,%edx
call   1070 <ppm_get_token@plt>
lea    0xe7b(%rip),%rsi
mov    %eax,%edi
call   1040 <strcmp@plt>
test   %rax,%rax
je     11da <ppm_get_size+0x7a>
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
lea    0xe62(%rip),%rsi
jmp    1245 <ppm_get_size+0xe5>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
lea    0xe32(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
jmp    1255 <ppm_get_size+0xf5>
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    $0x14,%edx
call   1070 <ppm_get_token@plt>
lea    0xe5b(%rip),%rsi
mov    %eax,%edi
mov    %r12,%rdx
call   1060 <sscanf@plt>
cmp    $0x1,%eax
jne    1235 <ppm_get_size+0xd5>
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    $0x14,%edx
call   1070 <ppm_get_token@plt>
lea    0xe35(%rip),%rsi
mov    %eax,%edi
mov    %r15,%rdx
call   1060 <sscanf@plt>
cmp    $0x1,%eax
jne    1235 <ppm_get_size+0xd5>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    $0x1,%eax
jmp    1257 <ppm_get_size+0xf7>
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
lea    0xe0a(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [24 x i8] c"can't read PPM file %s\0A\00"
@.str.2 = [3 x i8] c"P6\00"
@.str.3 = [47 x i8] c"%s is not a valid binary PPM file, bad magic#\0A\00"
@.str.4 = [3 x i8] c"%d\00"
@.str.5 = [38 x i8] c"%s is not a valid PPM file: bad size\0A\00"
define i32 @ppm_get_size(i8* %0, i32* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca [20 x i8]
%9 = alloca i32*
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32* %2, i32** %7
%10 = load i8*, i8** %5
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %9
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %17
13: ; preds = %3
%14 = load i32, i32* @stderr
%15 = load i8*, i8** %5
%16 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %15)
store i32 0, i32* %4
br label %52
17: ; preds = %3
%18 = load i32*, i32** %9
%19 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%20 = call i32 @ppm_get_token(i32* %18, i8* %19, i32 20)
%21 = call i64 @strcmp(i32 %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %17
%24 = load i32, i32* @stderr
%25 = load i8*, i8** %5
%26 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i8* %25)
%27 = load i32*, i32** %9
%28 = call i32 @fclose(i32* %27)
store i32 0, i32* %4
br label %52
29: ; preds = %17
%30 = load i32*, i32** %9
%31 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%32 = call i32 @ppm_get_token(i32* %30, i8* %31, i32 20)
%33 = load i32*, i32** %6
%34 = call i32 @sscanf(i32 %32, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %33)
%35 = icmp ne i32 %34, 1
br i1 %35, label %43, label %36
36: ; preds = %29
%37 = load i32*, i32** %9
%38 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%39 = call i32 @ppm_get_token(i32* %37, i8* %38, i32 20)
%40 = load i32*, i32** %7
%41 = call i32 @sscanf(i32 %39, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %40)
%42 = icmp ne i32 %41, 1
br i1 %42, label %43, label %49
43: ; preds = %36, %29
%44 = load i32, i32* @stderr
%45 = load i8*, i8** %5
%46 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i8* %45)
%47 = load i32*, i32** %9
%48 = call i32 @fclose(i32* %47)
store i32 0, i32* %4
br label %52
49: ; preds = %36
%50 = load i32*, i32** %9
%51 = call i32 @fclose(i32* %50)
store i32 1, i32* %4
br label %52
52: ; preds = %49, %43, %23, %13
%53 = load i32, i32* %4
ret i32 %53
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [24 x i8] c"can't read PPM file %s\0A\00"
@.str.2 = [3 x i8] c"P6\00"
@.str.3 = [47 x i8] c"%s is not a valid binary PPM file, bad magic#\0A\00"
@.str.4 = [3 x i8] c"%d\00"
@.str.5 = [38 x i8] c"%s is not a valid PPM file: bad size\0A\00"
define i32 @ppm_get_size(i8* %0, i32* %1, i32* %2) {
%4 = alloca [20 x i8]
%5 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %11
8: ; preds = %3
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %33
11: ; preds = %3
%12 = call i32 @ppm_get_token(i32* %6, i8* %5, i32 20)
%13 = call i64 @strcmp(i32 %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%14 = icmp eq i64 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %11
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%18 = call i32 @fclose(i32* %6)
br label %33
19: ; preds = %11
%20 = call i32 @ppm_get_token(i32* %6, i8* %5, i32 20)
%21 = call i32 @sscanf(i32 %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %1)
%22 = icmp eq i32 %21, 1
br i1 %22, label %23, label %27
23: ; preds = %19
%24 = call i32 @ppm_get_token(i32* %6, i8* %5, i32 20)
%25 = call i32 @sscanf(i32 %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %2)
%26 = icmp eq i32 %25, 1
br i1 %26, label %31, label %27
27: ; preds = %23, %19
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.5, i64 0, i64 0), i8* %0)
%30 = call i32 @fclose(i32* %6)
br label %33
31: ; preds = %23
%32 = call i32 @fclose(i32* %6)
br label %33
33: ; preds = %31, %27, %15, %8
%34 = phi i32 [ 0, %8 ], [ 0, %15 ], [ 0, %27 ], [ 1, %31 ]
ret i32 %34
}