#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 */
double **allocate2D(int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ initialize(double **, double **, int, int);
int /*<<< orphan*/ printf(char *, double);
int /*<<< orphan*/ putchar(char);
int /*<<< orphan*/ stderr;
double strtod(char *, int /*<<< orphan*/ *);
int strtol(char *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ sweep(double **, int, int, int, int, double **, double,
                         double *);
int main(int argc, char **argv) {
  int ni, nj;
  double h;
  double **F;
  double **U;
  const double tol = 1e-6;
  double maxerr;
  if (argc < 4) {
    fprintf(stderr, "usage: %s nrows ncols grid_spacing\n", argv[0]);
    return 1;
  }
  ni = strtol(argv[1], NULL, 10);
  nj = strtol(argv[2], NULL, 10);
  h = strtod(argv[3], NULL);
  F = allocate2D(ni + 2, nj + 2);
  U = allocate2D(ni + 2, nj + 2);
  if (!F || !U) {
    fprintf(stderr, "couldn't allocate memory\n");
    return 1;
  }
  initialize(F, U, ni, nj);
  do {
    maxerr = 0.0;
    sweep(U, ni, 1, 1, nj, F, h, &maxerr);
    sweep(U, ni, 1, nj, 1, F, h, &maxerr);
    sweep(U, 1, ni, nj, 1, F, h, &maxerr);
    sweep(U, 1, ni, 1, nj, F, h, &maxerr);
  } while (maxerr > tol);
  for (int i = 1; i <= ni; i++) {
    for (int j = 1; j <= nj; j++) {
      printf("%g ", U[i][j]);
    }
    putchar('\n');
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movsd  0xe62(%rip),%xmm0
movsd  %xmm0,-0x38(%rbp)
cmpl   $0x4,-0x8(%rbp)
jge    11d7 <main+0x57>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe44(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13ca <main+0x24a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   10a0 <strtol@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   10a0 <strtol@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <strtod@plt>
movsd  %xmm0,-0x20(%rbp)
mov    -0x14(%rbp),%edi
add    $0x2,%edi
mov    -0x18(%rbp),%esi
add    $0x2,%esi
call   1090 <allocate2D@plt>
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
add    $0x2,%edi
mov    -0x18(%rbp),%esi
add    $0x2,%esi
call   1090 <allocate2D@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     125f <main+0xdf>
cmpq   $0x0,-0x30(%rbp)
jne    1282 <main+0x102>
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
lea    0xdbd(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13ca <main+0x24a>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
call   1070 <initialize@plt>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x40(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%r8d
mov    -0x28(%rbp),%r9
movsd  -0x20(%rbp),%xmm0
mov    $0x1,%ecx
lea    -0x40(%rbp),%rax
mov    %ecx,%edx
mov    %rax,(%rsp)
call   1060 <sweep@plt>
mov    -0x30(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%ecx
mov    -0x28(%rbp),%r9
movsd  -0x20(%rbp),%xmm0
mov    $0x1,%r8d
lea    -0x40(%rbp),%rax
mov    %r8d,%edx
mov    %rax,(%rsp)
call   1060 <sweep@plt>
mov    -0x30(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
mov    -0x28(%rbp),%r9
movsd  -0x20(%rbp),%xmm0
mov    $0x1,%r8d
lea    -0x40(%rbp),%rax
mov    %r8d,%esi
mov    %rax,(%rsp)
call   1060 <sweep@plt>
mov    -0x30(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%r8d
mov    -0x28(%rbp),%r9
movsd  -0x20(%rbp),%xmm0
mov    $0x1,%ecx
lea    -0x40(%rbp),%rax
mov    %ecx,%esi
mov    %rax,(%rsp)
call   1060 <sweep@plt>
movsd  -0x40(%rbp),%xmm0
movsd  0xcb4(%rip),%xmm1
ucomisd %xmm1,%xmm0
ja     1295 <main+0x115>
movl   $0x1,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x14(%rbp),%eax
jg     13c3 <main+0x243>
movl   $0x1,-0x48(%rbp)
mov    -0x48(%rbp),%eax
cmp    -0x18(%rbp),%eax
jg     13ab <main+0x22b>
mov    -0x30(%rbp),%rax
movslq -0x44(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x48(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
lea    0xcae(%rip),%rdi
call   1050 <printf@plt>
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    1370 <main+0x1f0>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    135d <main+0x1dd>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
cmp    $0x3,%edi
jg     11ba <main+0x3a>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe5f(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%eax
jmp    138d <main+0x20d>
mov    0x8(%rbx),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   10a0 <strtol@plt>
mov    %eax,%r14d
mov    0x10(%rbx),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   10a0 <strtol@plt>
mov    %eax,%r15d
mov    0x18(%rbx),%rdi
xor    %esi,%esi
call   1040 <strtod@plt>
movsd  %xmm0,0x8(%rsp)
lea    0x2(%r14),%ebx
lea    0x2(%r15),%ebp
mov    %ebx,%edi
mov    %ebp,%esi
call   1090 <allocate2D@plt>
mov    %rax,%r12
mov    %ebx,%edi
mov    %ebp,%esi
call   1090 <allocate2D@plt>
test   %r12,%r12
je     136d <main+0x1ed>
mov    %rax,%rbx
test   %rax,%rax
je     136d <main+0x1ed>
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
mov    %r15d,%ecx
call   1070 <initialize@plt>
lea    0x10(%rsp),%rbp
nopl   0x0(%rax)
movq   $0x0,0x10(%rsp)
mov    %rbp,(%rsp)
mov    %rbx,%rdi
mov    %r14d,%esi
mov    $0x1,%edx
mov    $0x1,%ecx
mov    %r15d,%r8d
mov    %r12,%r9
movsd  0x8(%rsp),%xmm0
call   1060 <sweep@plt>
mov    %rbp,(%rsp)
mov    %rbx,%rdi
mov    %r14d,%esi
mov    $0x1,%edx
mov    %r15d,%ecx
mov    $0x1,%r8d
mov    %r12,%r9
movsd  0x8(%rsp),%xmm0
call   1060 <sweep@plt>
mov    %rbp,(%rsp)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %r15d,%ecx
mov    $0x1,%r8d
mov    %r12,%r9
movsd  0x8(%rsp),%xmm0
call   1060 <sweep@plt>
mov    %rbp,(%rsp)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    $0x1,%ecx
mov    %r15d,%r8d
mov    %r12,%r9
movsd  0x8(%rsp),%xmm0
call   1060 <sweep@plt>
movsd  0x10(%rsp),%xmm0
ucomisd 0xd13(%rip),%xmm0
ja     1240 <main+0xc0>
xor    %eax,%eax
test   %r14d,%r14d
jle    138d <main+0x20d>
lea    0x1(%r15),%ebp
add    $0x1,%r14d
mov    $0x1,%r12d
lea    0xd33(%rip),%r13
mov    %r14,0x8(%rsp)
jmp    1338 <main+0x1b8>
nopw   0x0(%rax,%rax,1)
mov    $0xa,%edi
call   1030 <putchar@plt>
add    $0x1,%r12
mov    0x8(%rsp),%r14
cmp    %r14,%r12
je     138b <main+0x20b>
test   %r15d,%r15d
jle    1320 <main+0x1a0>
mov    $0x1,%r14d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%rbx,%r12,8),%rax
movsd  (%rax,%r14,8),%xmm0
mov    %r13,%rdi
call   1050 <printf@plt>
add    $0x1,%r14
cmp    %r14,%rbp
jne    1350 <main+0x1d0>
jmp    1320 <main+0x1a0>
mov    0x2c6c(%rip),%rax
mov    (%rax),%edi
lea    0xcaf(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%eax
jmp    138d <main+0x20d>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [36 x i8] c"usage: %s nrows ncols grid_spacing\0A\00"
@.str.1 = [26 x i8] c"couldn't allocate memory\0A\00"
@.str.2 = [4 x i8] c"%g \00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca double
%9 = alloca double**
%10 = alloca double**
%11 = alloca double
%12 = alloca double
%13 = alloca i32
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store double 0x3EB0C6F7A0B5ED8D, double* %11
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 4
br i1 %16, label %17, label %23
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 0
%21 = load i8*, i8** %20
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %21)
store i32 1, i32* %3
br label %118
23: ; preds = %2
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i32 @strtol(i8* %26, i32* null, i32 10)
store i32 %27, i32* %6
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 2
%30 = load i8*, i8** %29
%31 = call i32 @strtol(i8* %30, i32* null, i32 10)
store i32 %31, i32* %7
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 3
%34 = load i8*, i8** %33
%35 = call double @strtod(i8* %34, i32* null)
store double %35, double* %8
%36 = load i32, i32* %6
%37 = add nsw i32 %36, 2
%38 = load i32, i32* %7
%39 = add nsw i32 %38, 2
%40 = call double** @allocate2D(i32 %37, i32 %39)
store double** %40, double*** %9
%41 = load i32, i32* %6
%42 = add nsw i32 %41, 2
%43 = load i32, i32* %7
%44 = add nsw i32 %43, 2
%45 = call double** @allocate2D(i32 %42, i32 %44)
store double** %45, double*** %10
%46 = load double**, double*** %9
%47 = icmp ne double** %46, null
br i1 %47, label %48, label %51
48: ; preds = %23
%49 = load double**, double*** %10
%50 = icmp ne double** %49, null
br i1 %50, label %54, label %51
51: ; preds = %48, %23
%52 = load i32, i32* @stderr
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %118
54: ; preds = %48
%55 = load double**, double*** %9
%56 = load double**, double*** %10
%57 = load i32, i32* %6
%58 = load i32, i32* %7
%59 = call i32 @initialize(double** %55, double** %56, i32 %57, i32 %58)
br label %60
60: ; preds = %85, %54
store double 0.000000e+00, double* %12
%61 = load double**, double*** %10
%62 = load i32, i32* %6
%63 = load i32, i32* %7
%64 = load double**, double*** %9
%65 = load double, double* %8
%66 = call i32 @sweep(double** %61, i32 %62, i32 1, i32 1, i32 %63, double** %64, double %65, double* %12)
%67 = load double**, double*** %10
%68 = load i32, i32* %6
%69 = load i32, i32* %7
%70 = load double**, double*** %9
%71 = load double, double* %8
%72 = call i32 @sweep(double** %67, i32 %68, i32 1, i32 %69, i32 1, double** %70, double %71, double* %12)
%73 = load double**, double*** %10
%74 = load i32, i32* %6
%75 = load i32, i32* %7
%76 = load double**, double*** %9
%77 = load double, double* %8
%78 = call i32 @sweep(double** %73, i32 1, i32 %74, i32 %75, i32 1, double** %76, double %77, double* %12)
%79 = load double**, double*** %10
%80 = load i32, i32* %6
%81 = load i32, i32* %7
%82 = load double**, double*** %9
%83 = load double, double* %8
%84 = call i32 @sweep(double** %79, i32 1, i32 %80, i32 1, i32 %81, double** %82, double %83, double* %12)
br label %85
85: ; preds = %60
%86 = load double, double* %12
%87 = fcmp ogt double %86, 0x3EB0C6F7A0B5ED8D
br i1 %87, label %60, label %88
88: ; preds = %85
store i32 1, i32* %13
br label %89
89: ; preds = %114, %88
%90 = load i32, i32* %13
%91 = load i32, i32* %6
%92 = icmp sle i32 %90, %91
br i1 %92, label %93, label %117
93: ; preds = %89
store i32 1, i32* %14
br label %94
94: ; preds = %109, %93
%95 = load i32, i32* %14
%96 = load i32, i32* %7
%97 = icmp sle i32 %95, %96
br i1 %97, label %98, label %112
98: ; preds = %94
%99 = load double**, double*** %10
%100 = load i32, i32* %13
%101 = sext i32 %100 to i64
%102 = getelementptr inbounds double*, double** %99, i64 %101
%103 = load double*, double** %102
%104 = load i32, i32* %14
%105 = sext i32 %104 to i64
%106 = getelementptr inbounds double, double* %103, i64 %105
%107 = load double, double* %106
%108 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), double %107)
br label %109
109: ; preds = %98
%110 = load i32, i32* %14
%111 = add nsw i32 %110, 1
store i32 %111, i32* %14
br label %94
112: ; preds = %94
%113 = call i32 @putchar(i8 signext 10)
br label %114
114: ; preds = %112
%115 = load i32, i32* %13
%116 = add nsw i32 %115, 1
store i32 %116, i32* %13
br label %89
117: ; preds = %89
store i32 0, i32* %3
br label %118
118: ; preds = %117, %51, %17
%119 = load i32, i32* %3
ret i32 %119
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [36 x i8] c"usage: %s nrows ncols grid_spacing\0A\00"
@.str.1 = [26 x i8] c"couldn't allocate memory\0A\00"
@.str.2 = [4 x i8] c"%g \00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca double
%4 = bitcast double* %3 to i8*
%5 = icmp slt i32 %0, 4
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %63
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i32 @strtol(i8* nocapture %12, i32* null, i32 10)
%14 = getelementptr inbounds i8*, i8** %1, i64 2
%15 = load i8*, i8** %14
%16 = tail call i32 @strtol(i8* nocapture %15, i32* null, i32 10)
%17 = getelementptr inbounds i8*, i8** %1, i64 3
%18 = load i8*, i8** %17
%19 = tail call double @strtod(i8* nocapture %18, i32* null)
%20 = add nsw i32 %13, 2
%21 = add nsw i32 %16, 2
%22 = tail call double** @allocate2D(i32 %20, i32 %21)
%23 = tail call double** @allocate2D(i32 %20, i32 %21)
%24 = icmp ne double** %22, null
%25 = icmp ne double** %23, null
%26 = select i1 %24, i1 %25, i1 false
br i1 %26, label %30, label %27
27: ; preds = %10
%28 = load i32, i32* @stderr
%29 = tail call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %63
30: ; preds = %10
%31 = tail call i32 @initialize(double** %22, double** %23, i32 %13, i32 %16)
br label %32
32: ; preds = %32, %30
store double 0.000000e+00, double* %3
%33 = call i32 @sweep(double** %23, i32 %13, i32 1, i32 1, i32 %16, double** %22, double %19, double* %3)
%34 = call i32 @sweep(double** %23, i32 %13, i32 1, i32 %16, i32 1, double** %22, double %19, double* %3)
%35 = call i32 @sweep(double** %23, i32 1, i32 %13, i32 %16, i32 1, double** %22, double %19, double* %3)
%36 = call i32 @sweep(double** %23, i32 1, i32 %13, i32 1, i32 %16, double** %22, double %19, double* %3)
%37 = load double, double* %3
%38 = fcmp ogt double %37, 0x3EB0C6F7A0B5ED8D
br i1 %38, label %32, label %39
39: ; preds = %32
%40 = icmp slt i32 %13, 1
br i1 %40, label %63, label %41
41: ; preds = %39
%42 = icmp slt i32 %16, 1
%43 = add i32 %16, 1
%44 = add nuw i32 %13, 1
%45 = zext i32 %44 to i64
%46 = zext i32 %43 to i64
br label %47
47: ; preds = %41, %51
%48 = phi i64 [ 1, %41 ], [ %53, %51 ]
br i1 %42, label %51, label %49
49: ; preds = %47
%50 = getelementptr inbounds double*, double** %23, i64 %48
br label %55
51: ; preds = %55, %47
%52 = call i32 @putchar(i8 signext 10)
%53 = add nuw nsw i64 %48, 1
%54 = icmp eq i64 %53, %45
br i1 %54, label %63, label %47
55: ; preds = %49, %55
%56 = phi i64 [ 1, %49 ], [ %61, %55 ]
%57 = load double*, double** %50
%58 = getelementptr inbounds double, double* %57, i64 %56
%59 = load double, double* %58
%60 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), double %59)
%61 = add nuw nsw i64 %56, 1
%62 = icmp eq i64 %61, %46
br i1 %62, label %51, label %55
63: ; preds = %51, %39, %27, %6
%64 = phi i32 [ 1, %6 ], [ 1, %27 ], [ 0, %39 ], [ 0, %51 ]
ret i32 %64
}