#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 EOF;
int /*<<< orphan*/ error(char *);
int getc(int /*<<< orphan*/ *);
scalar_t__ isdigit(int);
scalar_t__ isspace(int);
int ungetc(int, int /*<<< orphan*/ *);
unsigned int get_num(FILE *f) {
  unsigned int num;
  int c;
  while (isspace(c = getc(f)))
    ;
  if (!isdigit(c))
    error("Error: corrupted PGM file.");
  num = (unsigned int)(c - '0');
  while (isdigit(c = getc(f)))
    num = 10 * num + c - '0';
  if ((c != EOF) && (ungetc(c, f) == EOF))
    error("get_num: unable to 'ungetc' while reading PGM file.");
  return num;
}


x86 assembly (clang, -O0):
<get_num>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <getc@plt>
mov    %eax,%edi
mov    %edi,-0x10(%rbp)
call   1040 <isspace@plt>
cmp    $0x0,%rax
je     117e <get_num+0x2e>
jmp    115c <get_num+0xc>
mov    -0x10(%rbp),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%rax
jne    119c <get_num+0x4c>
lea    0xe69(%rip),%rdi
call   1060 <error@plt>
mov    -0x10(%rbp),%eax
sub    $0x30,%eax
mov    %eax,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <getc@plt>
mov    %eax,%edi
mov    %edi,-0x10(%rbp)
call   1030 <isdigit@plt>
cmp    $0x0,%rax
je     11d4 <get_num+0x84>
imul   $0xa,-0xc(%rbp),%eax
add    -0x10(%rbp),%eax
sub    $0x30,%eax
mov    %eax,-0xc(%rbp)
jmp    11a5 <get_num+0x55>
mov    -0x10(%rbp),%eax
mov    0x2e0a(%rip),%rcx
cmp    (%rcx),%eax
je     120d <get_num+0xbd>
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%rsi
call   1050 <ungetc@plt>
mov    0x2def(%rip),%rcx
cmp    (%rcx),%eax
jne    120d <get_num+0xbd>
lea    0xe13(%rip),%rdi
call   1060 <error@plt>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_num>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1070 <getc@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1040 <isspace@plt>
test   %rax,%rax
jne    1160 <get_num+0x10>
mov    %ebx,%edi
call   1030 <isdigit@plt>
test   %rax,%rax
jne    118e <get_num+0x3e>
lea    0xe77(%rip),%rdi
call   1060 <error@plt>
add    $0xffffffd0,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1070 <getc@plt>
mov    %eax,%r15d
mov    %eax,%edi
call   1030 <isdigit@plt>
test   %rax,%rax
je     11c3 <get_num+0x73>
lea    (%rbx,%rbx,4),%eax
lea    (%r15,%rax,2),%ebx
add    $0xffffffd0,%ebx
jmp    11a0 <get_num+0x50>
mov    0x2e1e(%rip),%r12
cmp    (%r12),%r15d
je     11ed <get_num+0x9d>
mov    %r15d,%edi
mov    %r14,%rsi
call   1050 <ungetc@plt>
cmp    (%r12),%eax
jne    11ed <get_num+0x9d>
lea    0xe33(%rip),%rdi
call   1060 <error@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [27 x i8] c"Error: corrupted PGM file.\00"
@EOF = global i32 0
@.str.1 = [52 x i8] c"get_num: unable to 'ungetc' while reading PGM file.\00"
define i32 @get_num(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
store i32* %0, i32** %2
br label %5
5: ; preds = %10, %1
%6 = load i32*, i32** %2
%7 = call i32 @getc(i32* %6)
store i32 %7, i32* %4
%8 = call i64 @isspace(i32 %7)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %11
10: ; preds = %5
br label %5
11: ; preds = %5
%12 = load i32, i32* %4
%13 = call i64 @isdigit(i32 %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %11
%16 = call i32 @error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %11
%18 = load i32, i32* %4
%19 = sub nsw i32 %18, 48
store i32 %19, i32* %3
br label %20
20: ; preds = %25, %17
%21 = load i32*, i32** %2
%22 = call i32 @getc(i32* %21)
store i32 %22, i32* %4
%23 = call i64 @isdigit(i32 %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %20
%26 = load i32, i32* %3
%27 = mul i32 10, %26
%28 = load i32, i32* %4
%29 = add i32 %27, %28
%30 = sub i32 %29, 48
store i32 %30, i32* %3
br label %20
31: ; preds = %20
%32 = load i32, i32* %4
%33 = load i32, i32* @EOF
%34 = icmp ne i32 %32, %33
br i1 %34, label %35, label %43
35: ; preds = %31
%36 = load i32, i32* %4
%37 = load i32*, i32** %2
%38 = call i32 @ungetc(i32 %36, i32* %37)
%39 = load i32, i32* @EOF
%40 = icmp eq i32 %38, %39
br i1 %40, label %41, label %43
41: ; preds = %35
%42 = call i32 @error(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0))
br label %43
43: ; preds = %41, %35, %31
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
@.str = [27 x i8] c"Error: corrupted PGM file.\00"
@EOF = global i32 0
@.str.1 = [52 x i8] c"get_num: unable to 'ungetc' while reading PGM file.\00"
define i32 @get_num(i32* nocapture %0) {
br label %2
2: ; preds = %2, %1
%3 = tail call i32 @getc(i32* %0)
%4 = tail call i64 @isspace(i32 %3)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %2
6: ; preds = %2
%7 = tail call i64 @isdigit(i32 %3)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 @error(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %6
%12 = add i32 %3, -48
%13 = tail call i32 @getc(i32* %0)
%14 = tail call i64 @isdigit(i32 %13)
%15 = icmp eq i64 %14, 0
br i1 %15, label %25, label %16
16: ; preds = %11, %16
%17 = phi i32 [ %22, %16 ], [ %13, %11 ]
%18 = phi i32 [ %21, %16 ], [ %12, %11 ]
%19 = mul i32 %18, 10
%20 = add i32 %19, %17
%21 = add i32 %20, -48
%22 = tail call i32 @getc(i32* %0)
%23 = tail call i64 @isdigit(i32 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %16
25: ; preds = %16, %11
%26 = phi i32 [ %12, %11 ], [ %21, %16 ]
%27 = phi i32 [ %13, %11 ], [ %22, %16 ]
%28 = load i32, i32* @EOF
%29 = icmp eq i32 %27, %28
br i1 %29, label %36, label %30
30: ; preds = %25
%31 = tail call i32 @ungetc(i32 %27, i32* %0)
%32 = load i32, i32* @EOF
%33 = icmp eq i32 %31, %32
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = tail call i32 @error(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.1, i64 0, i64 0))
br label %36
36: ; preds = %34, %30, %25
ret i32 %26
}