#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fscanf(int /*<<< orphan*/ *, char *, int *, ...);
int /*<<< orphan*/ stderr;
void pgmread(char *filename, void *vx, int nx, int ny) {
  FILE *fp;
  int nxt, nyt, i, j, t;
  char dummy[128];
  int n = 128;
  char *cret;
  int iret;
  float *x = (float *)vx;
  if (NULL == (fp = fopen(filename, "r"))) {
    fprintf(stderr, "pgmread: cannot open <%s>\n", filename);
    exit(-1);
  }
  cret = fgets(dummy, n, fp);
  cret = fgets(dummy, n, fp);
  iret = fscanf(fp, "%d %d", &nxt, &nyt);
  if (nx != nxt || ny != nyt) {
    fprintf(stderr,
            "pgmread: size mismatch, (nx,ny) = (%d,%d) expected (%d,%d)\n", nxt,
            nyt, nx, ny);
    exit(-1);
  }
  iret = fscanf(fp, "%d", &i);
  for (j = 0; j < ny; j++) {
    for (i = 0; i < nx; i++) {
      iret = fscanf(fp, "%d", &t);
      x[(ny - j - 1) + ny * i] = t;
    }
  }
  fclose(fp);
}


x86 assembly (clang, -O0):
<pgmread>:
push   %rbp
mov    %rsp,%rbp
sub    $0xe0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0x80,-0xc4(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0xe0(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe67(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rcx
mov    %rcx,-0x20(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    11d5 <pgmread+0x75>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe3e(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
lea    -0xc0(%rbp),%rdi
mov    -0xc4(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1050 <fgets@plt>
mov    %rax,-0xd0(%rbp)
lea    -0xc0(%rbp),%rdi
mov    -0xc4(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1050 <fgets@plt>
mov    %rax,-0xd0(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe03(%rip),%rsi
lea    -0x24(%rbp),%rdx
lea    -0x28(%rbp),%rcx
mov    $0x0,%al
call   1040 <fscanf@plt>
mov    %eax,-0xd4(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x24(%rbp),%eax
jne    1247 <pgmread+0xe7>
mov    -0x18(%rbp),%eax
cmp    -0x28(%rbp),%eax
je     1276 <pgmread+0x116>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
mov    -0x24(%rbp),%edx
mov    -0x28(%rbp),%ecx
mov    -0x14(%rbp),%r8d
mov    -0x18(%rbp),%r9d
lea    0xdbe(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%rdi
lea    0xd9f(%rip),%rsi
lea    -0x2c(%rbp),%rdx
mov    $0x0,%al
call   1040 <fscanf@plt>
mov    %eax,-0xd4(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    131b <pgmread+0x1bb>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1308 <pgmread+0x1a8>
mov    -0x20(%rbp),%rdi
lea    0xd5d(%rip),%rsi
lea    -0x34(%rbp),%rdx
mov    $0x0,%al
call   1040 <fscanf@plt>
mov    %eax,-0xd4(%rbp)
cvtsi2ssl -0x34(%rbp),%xmm0
mov    -0xe0(%rbp),%rax
mov    -0x18(%rbp),%ecx
sub    -0x30(%rbp),%ecx
sub    $0x1,%ecx
mov    -0x18(%rbp),%edx
imul   -0x2c(%rbp),%edx
add    %edx,%ecx
movslq %ecx,%rcx
movss  %xmm0,(%rax,%rcx,4)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    12ac <pgmread+0x14c>
jmp    130d <pgmread+0x1ad>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1299 <pgmread+0x139>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
add    $0xe0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pgmread>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xa8,%rsp
mov    %ecx,0x10(%rsp)
mov    %edx,%r14d
mov    %rsi,%rbp
mov    %rdi,%r12
lea    0xe7b(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     12cd <pgmread+0x16d>
mov    %rax,%rbx
lea    0x20(%rsp),%r12
mov    %r12,%rdi
mov    $0x80,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
mov    %r12,%rdi
mov    $0x80,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
lea    0xe5b(%rip),%rsi
lea    0x1c(%rsp),%rdx
lea    0x18(%rsp),%rcx
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <fscanf@plt>
mov    0x1c(%rsp),%edx
mov    0x18(%rsp),%ecx
mov    %r14d,%r12d
cmp    %r14d,%edx
mov    0x10(%rsp),%r14d
jne    12f1 <pgmread+0x191>
cmp    %r14d,%ecx
jne    12f1 <pgmread+0x191>
lea    0xe21(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <fscanf@plt>
test   %r14d,%r14d
jle    12b3 <pgmread+0x153>
mov    %r12d,%edx
mov    %r14d,%esi
lea    0xdfc(%rip),%r15
lea    0x14(%rsp),%r13
movl   $0x0,0x8(%rsp)
jmp    124f <pgmread+0xef>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x8(%rsp),%eax
add    $0x1,%eax
mov    %eax,0x8(%rsp)
cmp    %esi,%eax
je     12b3 <pgmread+0x153>
movl   $0x0,0xc(%rsp)
test   %edx,%edx
jle    1240 <pgmread+0xe0>
mov    0x8(%rsp),%r14d
not    %r14d
add    %esi,%r14d
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r13,%rdx
xor    %eax,%eax
call   1040 <fscanf@plt>
mov    0x10(%rsp),%esi
mov    %r12d,%edx
xorps  %xmm0,%xmm0
cvtsi2ssl 0x14(%rsp),%xmm0
mov    0xc(%rsp),%eax
mov    %eax,%ecx
imul   %esi,%ecx
add    %r14d,%ecx
movslq %ecx,%rcx
movss  %xmm0,0x0(%rbp,%rcx,4)
add    $0x1,%eax
mov    %eax,0xc(%rsp)
cmp    %r12d,%eax
jl     1270 <pgmread+0x110>
jmp    1240 <pgmread+0xe0>
mov    %rbx,%rdi
call   1030 <fclose@plt>
add    $0xa8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d0c(%rip),%rax
mov    (%rax),%edi
lea    0xd25(%rip),%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    0x2ce8(%rip),%rax
mov    (%rax),%edi
lea    0xd22(%rip),%rsi
mov    %r12d,%r8d
mov    %r14d,%r9d
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"pgmread: cannot open <%s>\0A\00"
@.str.2 = [6 x i8] c"%d %d\00"
@.str.3 = [60 x i8] c"pgmread: size mismatch, (nx,ny) = (%d,%d) expected (%d,%d)\0A\00"
@.str.4 = [3 x i8] c"%d\00"
define void @pgmread(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca [128 x i8]
%16 = alloca i32
%17 = alloca i8*
%18 = alloca i32
%19 = alloca float*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
store i32 128, i32* %16
%20 = load i8*, i8** %6
%21 = bitcast i8* %20 to float*
store float* %21, float** %19
%22 = load i8*, i8** %5
%23 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %23, i32** %9
%24 = icmp eq i32* null, %23
br i1 %24, label %25, label %30
25: ; preds = %4
%26 = load i32, i32* @stderr
%27 = load i8*, i8** %5
%28 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %27)
%29 = call i32 @exit(i32 -1)
unreachable
30: ; preds = %4
%31 = getelementptr inbounds [128 x i8], [128 x i8]* %15, i64 0, i64 0
%32 = load i32, i32* %16
%33 = load i32*, i32** %9
%34 = call i8* @fgets(i8* %31, i32 %32, i32* %33)
store i8* %34, i8** %17
%35 = getelementptr inbounds [128 x i8], [128 x i8]* %15, i64 0, i64 0
%36 = load i32, i32* %16
%37 = load i32*, i32** %9
%38 = call i8* @fgets(i8* %35, i32 %36, i32* %37)
store i8* %38, i8** %17
%39 = load i32*, i32** %9
%40 = call i32 (i32*, i8*, i32*, ...) @fscanf(i32* %39, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32* %10, i32* %11)
store i32 %40, i32* %18
%41 = load i32, i32* %7
%42 = load i32, i32* %10
%43 = icmp ne i32 %41, %42
br i1 %43, label %48, label %44
44: ; preds = %30
%45 = load i32, i32* %8
%46 = load i32, i32* %11
%47 = icmp ne i32 %45, %46
br i1 %47, label %48, label %56
48: ; preds = %44, %30
%49 = load i32, i32* @stderr
%50 = load i32, i32* %10
%51 = load i32, i32* %11
%52 = load i32, i32* %7
%53 = load i32, i32* %8
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.3, i64 0, i64 0), i32 %50, i32 %51, i32 %52, i32 %53)
%55 = call i32 @exit(i32 -1)
unreachable
56: ; preds = %44
%57 = load i32*, i32** %9
%58 = call i32 (i32*, i8*, i32*, ...) @fscanf(i32* %57, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %12)
store i32 %58, i32* %18
store i32 0, i32* %13
br label %59
59: ; preds = %88, %56
%60 = load i32, i32* %13
%61 = load i32, i32* %8
%62 = icmp slt i32 %60, %61
br i1 %62, label %63, label %91
63: ; preds = %59
store i32 0, i32* %12
br label %64
64: ; preds = %84, %63
%65 = load i32, i32* %12
%66 = load i32, i32* %7
%67 = icmp slt i32 %65, %66
br i1 %67, label %68, label %87
68: ; preds = %64
%69 = load i32*, i32** %9
%70 = call i32 (i32*, i8*, i32*, ...) @fscanf(i32* %69, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %14)
store i32 %70, i32* %18
%71 = load i32, i32* %14
%72 = sitofp i32 %71 to float
%73 = load float*, float** %19
%74 = load i32, i32* %8
%75 = load i32, i32* %13
%76 = sub nsw i32 %74, %75
%77 = sub nsw i32 %76, 1
%78 = load i32, i32* %8
%79 = load i32, i32* %12
%80 = mul nsw i32 %78, %79
%81 = add nsw i32 %77, %80
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds float, float* %73, i64 %82
store float %72, float* %83
br label %84
84: ; preds = %68
%85 = load i32, i32* %12
%86 = add nsw i32 %85, 1
store i32 %86, i32* %12
br label %64
87: ; preds = %64
br label %88
88: ; preds = %87
%89 = load i32, i32* %13
%90 = add nsw i32 %89, 1
store i32 %90, i32* %13
br label %59
91: ; preds = %59
%92 = load i32*, i32** %9
%93 = call i32 @fclose(i32* %92)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.1 = [27 x i8] c"pgmread: cannot open <%s>\0A\00"
@.str.2 = [6 x i8] c"%d %d\00"
@.str.3 = [60 x i8] c"pgmread: size mismatch, (nx,ny) = (%d,%d) expected (%d,%d)\0A\00"
@.str.4 = [3 x i8] c"%d\00"
define void @pgmread(i8* %0, i8* nocapture writeonly %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [128 x i8]
%10 = bitcast i32* %5 to i8*
%11 = bitcast i32* %6 to i8*
%12 = bitcast i32* %7 to i8*
%13 = bitcast i32* %8 to i8*
%14 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%15 = bitcast i8* %1 to float*
%16 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %22
18: ; preds = %4
%19 = load i32, i32* @stderr
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%21 = tail call i32 @exit(i32 -1)
unreachable
22: ; preds = %4
%23 = call i8* @fgets(i8* %14, i32 128, i32* %16)
%24 = call i8* @fgets(i8* %14, i32 128, i32* %16)
%25 = call i32 (i32*, i8*, i32*, ...) @fscanf(i32* %16, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32* %5, i32* %6)
%26 = load i32, i32* %5
%27 = icmp eq i32 %26, %2
%28 = load i32, i32* %6
%29 = icmp eq i32 %28, %3
%30 = select i1 %27, i1 %29, i1 false
br i1 %30, label %35, label %31
31: ; preds = %22
%32 = load i32, i32* @stderr
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.3, i64 0, i64 0), i32 %26, i32 %28, i32 %2, i32 %3)
%34 = call i32 @exit(i32 -1)
unreachable
35: ; preds = %22
%36 = call i32 (i32*, i8*, i32*, ...) @fscanf(i32* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %7)
%37 = icmp sgt i32 %3, 0
br i1 %37, label %38, label %59
38: ; preds = %35
%39 = icmp sgt i32 %2, 0
br label %40
40: ; preds = %38, %56
%41 = phi i32 [ 0, %38 ], [ %57, %56 ]
store i32 0, i32* %7
br i1 %39, label %42, label %56
42: ; preds = %40
%43 = xor i32 %41, -1
%44 = add i32 %43, %3
br label %45
45: ; preds = %42, %45
%46 = call i32 (i32*, i8*, i32*, ...) @fscanf(i32* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32* %8)
%47 = load i32, i32* %8
%48 = sitofp i32 %47 to float
%49 = load i32, i32* %7
%50 = mul nsw i32 %49, %3
%51 = add nsw i32 %44, %50
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds float, float* %15, i64 %52
store float %48, float* %53
%54 = add nsw i32 %49, 1
store i32 %54, i32* %7
%55 = icmp slt i32 %54, %2
br i1 %55, label %45, label %56
56: ; preds = %45, %40
%57 = add nuw nsw i32 %41, 1
%58 = icmp eq i32 %57, %3
br i1 %58, label %59, label %40
59: ; preds = %56, %35
%60 = call i32 @fclose(i32* %16)
ret void
}