#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 */

/* Variables and functions */
int /*<<< orphan*/ error_handler(char *);
int /*<<< orphan*/ free(double *);
int get_dimlen(char *, char *);
int /*<<< orphan*/ get_file_dir(char const *, char *);
int /*<<< orphan*/ get_string_data_level(char const *, char *, char *, int *);
int /*<<< orphan*/ get_var_data(char *, char const *, double *);
scalar_t__ malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
void read_mosaic_grid_data(const char *mosaic_file, const char *name, int nx,
                           int ny, double *data, int level, int ioff,
                           int joff) {
  char tilefile[255], gridfile[255], dir[255];
  double *tmp;
  int ni, nj, nxp, nyp, i, j;
  get_file_dir(mosaic_file, dir);
  get_string_data_level(mosaic_file, "gridfiles", gridfile, &level);
  sprintf(tilefile, "%s/%s", dir, gridfile);
  ni = get_dimlen(tilefile, "nx");
  nj = get_dimlen(tilefile, "ny");
  if (ni != nx * 2 || nj != ny * 2)
    error_handler("supergrid size should be double of the model grid size");
  tmp = (double *)malloc((ni + 1) * (nj + 1) * sizeof(double));
  get_var_data(tilefile, name, tmp);
  nxp = nx + 1 - ioff;
  nyp = ny + 1 - joff;
  for (j = 0; j < nyp; j++)
    for (i = 0; i < nxp; i++)
      data[j * nxp + i] = tmp[(2 * j + joff) * (ni + 1) + 2 * i + ioff];
  free(tmp);
}


x86 assembly (clang, -O0):
<read_mosaic_grid_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x350,%rsp
mov    0x18(%rbp),%eax
mov    0x10(%rbp),%eax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    %r9d,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x330(%rbp),%rsi
call   1050 <get_file_dir@plt>
mov    -0x8(%rbp),%rdi
lea    -0x230(%rbp),%rdx
lea    0xe37(%rip),%rsi
lea    -0x24(%rbp),%rcx
call   1030 <get_string_data_level@plt>
lea    -0x130(%rbp),%rdi
lea    -0x330(%rbp),%rdx
lea    -0x230(%rbp),%rcx
lea    0xe1c(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x130(%rbp),%rdi
lea    0xe0f(%rip),%rsi
call   10a0 <get_dimlen@plt>
mov    %eax,-0x33c(%rbp)
lea    -0x130(%rbp),%rdi
lea    0xdf9(%rip),%rsi
call   10a0 <get_dimlen@plt>
mov    %eax,-0x340(%rbp)
mov    -0x33c(%rbp),%eax
mov    -0x14(%rbp),%ecx
shl    $0x1,%ecx
cmp    %ecx,%eax
jne    124d <read_mosaic_grid_data+0xcd>
mov    -0x340(%rbp),%eax
mov    -0x18(%rbp),%ecx
shl    $0x1,%ecx
cmp    %ecx,%eax
je     1259 <read_mosaic_grid_data+0xd9>
lea    0xdc2(%rip),%rdi
call   1060 <error_handler@plt>
mov    -0x33c(%rbp),%eax
add    $0x1,%eax
mov    -0x340(%rbp),%ecx
add    $0x1,%ecx
imul   %ecx,%eax
cltq
shl    $0x3,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x338(%rbp)
lea    -0x130(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x338(%rbp),%rdx
call   1080 <get_var_data@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
sub    0x10(%rbp),%eax
mov    %eax,-0x344(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
sub    0x18(%rbp),%eax
mov    %eax,-0x348(%rbp)
movl   $0x0,-0x350(%rbp)
mov    -0x350(%rbp),%eax
cmp    -0x348(%rbp),%eax
jge    1370 <read_mosaic_grid_data+0x1f0>
movl   $0x0,-0x34c(%rbp)
mov    -0x34c(%rbp),%eax
cmp    -0x344(%rbp),%eax
jge    1357 <read_mosaic_grid_data+0x1d7>
mov    -0x338(%rbp),%rax
mov    -0x350(%rbp),%ecx
shl    $0x1,%ecx
add    0x18(%rbp),%ecx
mov    -0x33c(%rbp),%edx
add    $0x1,%edx
imul   %edx,%ecx
mov    -0x34c(%rbp),%edx
shl    $0x1,%edx
add    %edx,%ecx
add    0x10(%rbp),%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
mov    -0x20(%rbp),%rax
mov    -0x350(%rbp),%ecx
imul   -0x344(%rbp),%ecx
add    -0x34c(%rbp),%ecx
movslq %ecx,%rcx
movsd  %xmm0,(%rax,%rcx,8)
mov    -0x34c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34c(%rbp)
jmp    12dd <read_mosaic_grid_data+0x15d>
jmp    135c <read_mosaic_grid_data+0x1dc>
mov    -0x350(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x350(%rbp)
jmp    12c1 <read_mosaic_grid_data+0x141>
mov    -0x338(%rbp),%rdi
call   1040 <free@plt>
add    $0x350,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_mosaic_grid_data>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x318,%rsp
mov    %r8,0x8(%rsp)
mov    %ecx,%r12d
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %r9d,0x4(%rsp)
lea    0x110(%rsp),%rbp
mov    %rbp,%rsi
call   1050 <get_file_dir@plt>
lea    0xe42(%rip),%rsi
lea    0x210(%rsp),%r13
lea    0x4(%rsp),%rcx
mov    %rbx,%rdi
mov    %r13,%rdx
call   1030 <get_string_data_level@plt>
lea    0xe2d(%rip),%rsi
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    %rbp,%rdx
mov    %r13,%rcx
call   1090 <sprintf@plt>
lea    0xe19(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <get_dimlen@plt>
mov    %eax,%ebp
lea    0xe0b(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <get_dimlen@plt>
mov    %eax,%ebx
lea    (%r15,%r15,1),%eax
cmp    %eax,%ebp
jne    1222 <read_mosaic_grid_data+0xa2>
lea    (%r12,%r12,1),%eax
cmp    %eax,%ebx
je     122e <read_mosaic_grid_data+0xae>
lea    0xded(%rip),%rdi
call   1060 <error_handler@plt>
lea    0x1(%rbp),%eax
add    $0x1,%ebx
mov    %eax,(%rsp)
imul   %eax,%ebx
shl    $0x3,%ebx
mov    %ebx,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
lea    0x10(%rsp),%rdi
mov    %r14,%rsi
mov    %rax,%rdx
call   1080 <get_var_data@plt>
mov    0x358(%rsp),%eax
sub    %eax,%r12d
add    $0x1,%r12d
test   %r12d,%r12d
jle    1371 <read_mosaic_grid_data+0x1f1>
mov    0x350(%rsp),%ecx
sub    %ecx,%r15d
add    $0x1,%r15d
movslq %r15d,%r9
mov    %r12d,%r11d
mov    %r9d,%r10d
mov    %r10d,%edi
and    $0xfffffffe,%edi
mov    0x358(%rsp),%eax
imul   (%rsp),%eax
add    %ecx,%eax
lea    0x2(%rbp,%rbp,1),%r12d
mov    0x8(%rsp),%rcx
add    $0x8,%rcx
lea    0x0(,%r9,8),%r13
xor    %edx,%edx
jmp    12d3 <read_mosaic_grid_data+0x153>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0x1,%rdx
add    %r12d,%eax
add    %r13,%rcx
cmp    %r11,%rdx
je     1371 <read_mosaic_grid_data+0x1f1>
test   %r15d,%r15d
jle    12c0 <read_mosaic_grid_data+0x140>
cmp    $0x1,%r15d
jne    12f0 <read_mosaic_grid_data+0x170>
xor    %ebp,%ebp
jmp    132d <read_mosaic_grid_data+0x1ad>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %eax,%r8d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
movslq %r8d,%r8
movsd  (%rbx,%r8,8),%xmm0
movsd  %xmm0,-0x8(%rcx,%rbp,8)
lea    0x2(%r8),%r14d
movslq %r14d,%rsi
movsd  (%rbx,%rsi,8),%xmm0
movsd  %xmm0,(%rcx,%rbp,8)
add    $0x2,%rbp
add    $0x4,%r8d
cmp    %rbp,%rdi
jne    1300 <read_mosaic_grid_data+0x180>
test   $0x1,%r10b
je     12c0 <read_mosaic_grid_data+0x140>
mov    0x358(%rsp),%esi
lea    (%rsi,%rdx,2),%r8d
imul   (%rsp),%r8d
add    0x350(%rsp),%r8d
mov    %rdx,%r14
imul   %r9,%r14
lea    (%r8,%rbp,2),%esi
movslq %esi,%rsi
movsd  (%rbx,%rsi,8),%xmm0
add    %rbp,%r14
mov    0x8(%rsp),%rsi
movsd  %xmm0,(%rsi,%r14,8)
jmp    12c0 <read_mosaic_grid_data+0x140>
mov    %rbx,%rdi
call   1040 <free@plt>
add    $0x318,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"gridfiles\00"
@.str.1 = [6 x i8] c"%s/%s\00"
@.str.2 = [3 x i8] c"nx\00"
@.str.3 = [3 x i8] c"ny\00"
@.str.4 = [55 x i8] c"supergrid size should be double of the model grid size\00"
define void @read_mosaic_grid_data(i8* %0, i8* %1, i32 %2, i32 %3, double* %4, i32 %5, i32 %6, i32 %7) {
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca double*
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca [255 x i8]
%18 = alloca [255 x i8]
%19 = alloca [255 x i8]
%20 = alloca double*
%21 = alloca i32
%22 = alloca i32
%23 = alloca i32
%24 = alloca i32
%25 = alloca i32
%26 = alloca i32
store i8* %0, i8** %9
store i8* %1, i8** %10
store i32 %2, i32* %11
store i32 %3, i32* %12
store double* %4, double** %13
store i32 %5, i32* %14
store i32 %6, i32* %15
store i32 %7, i32* %16
%27 = load i8*, i8** %9
%28 = getelementptr inbounds [255 x i8], [255 x i8]* %19, i64 0, i64 0
%29 = call i32 @get_file_dir(i8* %27, i8* %28)
%30 = load i8*, i8** %9
%31 = getelementptr inbounds [255 x i8], [255 x i8]* %18, i64 0, i64 0
%32 = call i32 @get_string_data_level(i8* %30, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %31, i32* %14)
%33 = getelementptr inbounds [255 x i8], [255 x i8]* %17, i64 0, i64 0
%34 = getelementptr inbounds [255 x i8], [255 x i8]* %19, i64 0, i64 0
%35 = getelementptr inbounds [255 x i8], [255 x i8]* %18, i64 0, i64 0
%36 = call i32 @sprintf(i8* %33, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %34, i8* %35)
%37 = getelementptr inbounds [255 x i8], [255 x i8]* %17, i64 0, i64 0
%38 = call i32 @get_dimlen(i8* %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32 %38, i32* %21
%39 = getelementptr inbounds [255 x i8], [255 x i8]* %17, i64 0, i64 0
%40 = call i32 @get_dimlen(i8* %39, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32 %40, i32* %22
%41 = load i32, i32* %21
%42 = load i32, i32* %11
%43 = mul nsw i32 %42, 2
%44 = icmp ne i32 %41, %43
br i1 %44, label %50, label %45
45: ; preds = %8
%46 = load i32, i32* %22
%47 = load i32, i32* %12
%48 = mul nsw i32 %47, 2
%49 = icmp ne i32 %46, %48
br i1 %49, label %50, label %52
50: ; preds = %45, %8
%51 = call i32 @error_handler(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.4, i64 0, i64 0))
br label %52
52: ; preds = %50, %45
%53 = load i32, i32* %21
%54 = add nsw i32 %53, 1
%55 = load i32, i32* %22
%56 = add nsw i32 %55, 1
%57 = mul nsw i32 %54, %56
%58 = sext i32 %57 to i64
%59 = mul i64 %58, 8
%60 = trunc i64 %59 to i32
%61 = call i64 @malloc(i32 %60)
%62 = inttoptr i64 %61 to double*
store double* %62, double** %20
%63 = getelementptr inbounds [255 x i8], [255 x i8]* %17, i64 0, i64 0
%64 = load i8*, i8** %10
%65 = load double*, double** %20
%66 = call i32 @get_var_data(i8* %63, i8* %64, double* %65)
%67 = load i32, i32* %11
%68 = add nsw i32 %67, 1
%69 = load i32, i32* %15
%70 = sub nsw i32 %68, %69
store i32 %70, i32* %23
%71 = load i32, i32* %12
%72 = add nsw i32 %71, 1
%73 = load i32, i32* %16
%74 = sub nsw i32 %72, %73
store i32 %74, i32* %24
store i32 0, i32* %26
br label %75
75: ; preds = %113, %52
%76 = load i32, i32* %26
%77 = load i32, i32* %24
%78 = icmp slt i32 %76, %77
br i1 %78, label %79, label %116
79: ; preds = %75
store i32 0, i32* %25
br label %80
80: ; preds = %109, %79
%81 = load i32, i32* %25
%82 = load i32, i32* %23
%83 = icmp slt i32 %81, %82
br i1 %83, label %84, label %112
84: ; preds = %80
%85 = load double*, double** %20
%86 = load i32, i32* %26
%87 = mul nsw i32 2, %86
%88 = load i32, i32* %16
%89 = add nsw i32 %87, %88
%90 = load i32, i32* %21
%91 = add nsw i32 %90, 1
%92 = mul nsw i32 %89, %91
%93 = load i32, i32* %25
%94 = mul nsw i32 2, %93
%95 = add nsw i32 %92, %94
%96 = load i32, i32* %15
%97 = add nsw i32 %95, %96
%98 = sext i32 %97 to i64
%99 = getelementptr inbounds double, double* %85, i64 %98
%100 = load double, double* %99
%101 = load double*, double** %13
%102 = load i32, i32* %26
%103 = load i32, i32* %23
%104 = mul nsw i32 %102, %103
%105 = load i32, i32* %25
%106 = add nsw i32 %104, %105
%107 = sext i32 %106 to i64
%108 = getelementptr inbounds double, double* %101, i64 %107
store double %100, double* %108
br label %109
109: ; preds = %84
%110 = load i32, i32* %25
%111 = add nsw i32 %110, 1
store i32 %111, i32* %25
br label %80
112: ; preds = %80
br label %113
113: ; preds = %112
%114 = load i32, i32* %26
%115 = add nsw i32 %114, 1
store i32 %115, i32* %26
br label %75
116: ; preds = %75
%117 = load double*, double** %20
%118 = call i32 @free(double* %117)
ret void
}

LLVM IR (-O2):
@.str = [10 x i8] c"gridfiles\00"
@.str.1 = [6 x i8] c"%s/%s\00"
@.str.2 = [3 x i8] c"nx\00"
@.str.3 = [3 x i8] c"ny\00"
@.str.4 = [55 x i8] c"supergrid size should be double of the model grid size\00"
define void @read_mosaic_grid_data(i8* %0, i8* %1, i32 %2, i32 %3, double* nocapture writeonly %4, i32 %5, i32 %6, i32 %7) {
%9 = alloca i32
%10 = alloca [255 x i8]
%11 = alloca [255 x i8]
%12 = alloca [255 x i8]
store i32 %5, i32* %9
%13 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%14 = getelementptr inbounds [255 x i8], [255 x i8]* %11, i64 0, i64 0
%15 = getelementptr inbounds [255 x i8], [255 x i8]* %12, i64 0, i64 0
%16 = call i32 @get_file_dir(i8* %0, i8* %15)
%17 = call i32 @get_string_data_level(i8* %0, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %14, i32* %9)
%18 = call i32 @sprintf(i8* %13, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %15, i8* %14)
%19 = call i32 @get_dimlen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%20 = call i32 @get_dimlen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%21 = shl nsw i32 %2, 1
%22 = icmp eq i32 %19, %21
%23 = shl nsw i32 %3, 1
%24 = icmp eq i32 %20, %23
%25 = select i1 %22, i1 %24, i1 false
br i1 %25, label %28, label %26
26: ; preds = %8
%27 = call i32 @error_handler(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.4, i64 0, i64 0))
br label %28
28: ; preds = %8, %26
%29 = add nsw i32 %19, 1
%30 = add nsw i32 %20, 1
%31 = shl i32 %29, 3
%32 = mul i32 %31, %30
%33 = call i64 @malloc(i32 %32)
%34 = inttoptr i64 %33 to double*
%35 = call i32 @get_var_data(i8* %13, i8* %1, double* %34)
%36 = add nsw i32 %2, 1
%37 = sub i32 %36, %6
%38 = add nsw i32 %3, 1
%39 = sub i32 %38, %7
%40 = icmp sgt i32 %39, 0
br i1 %40, label %41, label %96
41: ; preds = %28
%42 = icmp sgt i32 %37, 0
%43 = sext i32 %37 to i64
%44 = zext i32 %39 to i64
%45 = zext i32 %37 to i64
%46 = and i64 %45, 1
%47 = icmp eq i32 %37, 1
%48 = and i64 %45, 4294967294
%49 = icmp eq i64 %46, 0
br label %50
50: ; preds = %41, %93
%51 = phi i64 [ 0, %41 ], [ %94, %93 ]
br i1 %42, label %52, label %93
52: ; preds = %50
%53 = trunc i64 %51 to i32
%54 = shl i32 %53, 1
%55 = add nsw i32 %54, %7
%56 = mul nsw i32 %55, %29
%57 = add i32 %56, %6
%58 = mul nsw i64 %51, %43
br i1 %47, label %82, label %59
59: ; preds = %52, %59
%60 = phi i64 [ %79, %59 ], [ 0, %52 ]
%61 = phi i64 [ %80, %59 ], [ 0, %52 ]
%62 = trunc i64 %60 to i32
%63 = shl i32 %62, 1
%64 = add i32 %57, %63
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds double, double* %34, i64 %65
%67 = load double, double* %66
%68 = add nsw i64 %60, %58
%69 = getelementptr inbounds double, double* %4, i64 %68
store double %67, double* %69
%70 = or i64 %60, 1
%71 = trunc i64 %70 to i32
%72 = shl i32 %71, 1
%73 = add i32 %57, %72
%74 = sext i32 %73 to i64
%75 = getelementptr inbounds double, double* %34, i64 %74
%76 = load double, double* %75
%77 = add nsw i64 %70, %58
%78 = getelementptr inbounds double, double* %4, i64 %77
store double %76, double* %78
%79 = add nuw nsw i64 %60, 2
%80 = add i64 %61, 2
%81 = icmp eq i64 %80, %48
br i1 %81, label %82, label %59
82: ; preds = %59, %52
%83 = phi i64 [ 0, %52 ], [ %79, %59 ]
br i1 %49, label %93, label %84
84: ; preds = %82
%85 = trunc i64 %83 to i32
%86 = shl i32 %85, 1
%87 = add i32 %57, %86
%88 = sext i32 %87 to i64
%89 = getelementptr inbounds double, double* %34, i64 %88
%90 = load double, double* %89
%91 = add nsw i64 %83, %58
%92 = getelementptr inbounds double, double* %4, i64 %91
store double %90, double* %92
br label %93
93: ; preds = %84, %82, %50
%94 = add nuw nsw i64 %51, 1
%95 = icmp eq i64 %94, %44
br i1 %95, label %96, label %50
96: ; preds = %93, %28
%97 = call i32 @free(double* %34)
ret void
}