#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 uint8_t;
typedef int uint32_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int, int, int);
int /*<<< orphan*/ fwrite(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int);
void save_image(int index, double field[][80], uint32_t palet[], int max_col,
                double scale) {
  FILE *file;
  char filename[128];
  int row, col;
  uint32_t rgb;
  uint8_t b;
  sprintf(filename, "metaballs%03d.ppm", index);
  file = fopen(filename, "wb");
  if (file != NULL) {
    fprintf(file, "P6\n%d %d\n%d\n", 80, 8, 255);
    for (row = 0; row < 8; row++) {
      for (col = 0; col < 80; col++) {
        double v = field[row][col];
        int j = max_col * v / scale;
        if (j > max_col) {
          j = max_col;
        }
        rgb = palet[j];
        b = (rgb >> 16) & 0xFF;
        fwrite(&b, 1, 1, file);
        b = (rgb >> 8) & 0xFF;
        fwrite(&b, 1, 1, file);
        b = (rgb >> 0) & 0xFF;
        fwrite(&b, 1, 1, file);
      }
    }
    fclose(file);
  }
}


x86 assembly (clang, -O0):
<save_image>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movsd  %xmm0,-0x28(%rbp)
lea    -0xb0(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe81(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0xb0(%rbp),%rdi
lea    0xe80(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     131c <save_image+0x1cc>
mov    -0x30(%rbp),%rdi
lea    0xe64(%rip),%rsi
mov    $0x50,%edx
mov    $0x8,%ecx
mov    $0xff,%r8d
call   1040 <fprintf@plt>
movl   $0x0,-0xb4(%rbp)
cmpl   $0x8,-0xb4(%rbp)
jge    1313 <save_image+0x1c3>
movl   $0x0,-0xb8(%rbp)
cmpl   $0x50,-0xb8(%rbp)
jge    12fa <save_image+0x1aa>
mov    -0x10(%rbp),%rax
movslq -0xb4(%rbp),%rcx
imul   $0x280,%rcx,%rcx
add    %rcx,%rax
movslq -0xb8(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
movsd  %xmm0,-0xc8(%rbp)
ff
cvtsi2sdl -0x1c(%rbp),%xmm0
mulsd  -0xc8(%rbp),%xmm0
ff
divsd  -0x28(%rbp),%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0xcc(%rbp)
mov    -0xcc(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jle    1251 <save_image+0x101>
mov    -0x1c(%rbp),%eax
mov    %eax,-0xcc(%rbp)
mov    -0x18(%rbp),%rax
movslq -0xcc(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0xbc(%rbp)
mov    -0xbc(%rbp),%eax
sar    $0x10,%eax
and    $0xff,%eax
mov    %eax,-0xc0(%rbp)
mov    -0x30(%rbp),%rcx
lea    -0xc0(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1070 <fwrite@plt>
mov    -0xbc(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %eax,-0xc0(%rbp)
mov    -0x30(%rbp),%rcx
lea    -0xc0(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1070 <fwrite@plt>
mov    -0xbc(%rbp),%eax
sar    $0x0,%eax
and    $0xff,%eax
mov    %eax,-0xc0(%rbp)
mov    -0x30(%rbp),%rcx
lea    -0xc0(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1070 <fwrite@plt>
mov    -0xb8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xb8(%rbp)
jmp    11e7 <save_image+0x97>
jmp    12ff <save_image+0x1af>
mov    -0xb4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xb4(%rbp)
jmp    11d0 <save_image+0x80>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
add    $0xd0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_image>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xa8,%rsp
movsd  %xmm0,0x18(%rsp)
mov    %ecx,%r13d
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%edx
lea    0xe87(%rip),%rsi
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <sprintf@plt>
lea    0xe85(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     127a <save_image+0x12a>
mov    %rax,%rbx
lea    0xe6d(%rip),%rsi
mov    %rax,%rdi
mov    $0x50,%edx
mov    $0x8,%ecx
mov    $0xff,%r8d
call   1050 <fprintf@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %r13d,%xmm0
movsd  %xmm0,0x10(%rsp)
xor    %eax,%eax
jmp    11f6 <save_image+0xa6>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x8(%rsp),%rax
add    $0x1,%rax
add    $0x280,%r15
cmp    $0x8,%rax
je     1261 <save_image+0x111>
mov    %rax,0x8(%rsp)
xor    %ebp,%ebp
jmp    123f <save_image+0xef>
nop
movslq %ecx,%rax
mov    (%r14,%rax,4),%r12d
mov    %r12d,%eax
shr    $0x10,%eax
movsbl %al,%edi
mov    %rbx,%rsi
call   1040 <fputc@plt>
mov    %r12d,%eax
shr    $0x8,%eax
movsbl %al,%edi
mov    %rbx,%rsi
call   1040 <fputc@plt>
movsbl %r12b,%edi
mov    %rbx,%rsi
call   1040 <fputc@plt>
add    $0x1,%rbp
cmp    $0x50,%rbp
je     11e0 <save_image+0x90>
movsd  (%r15,%rbp,8),%xmm0
mulsd  0x10(%rsp),%xmm0
divsd  0x18(%rsp),%xmm0
cvttsd2si %xmm0,%eax
mov    %r13d,%ecx
cmp    %r13d,%eax
jg     1200 <save_image+0xb0>
mov    %eax,%ecx
jmp    1200 <save_image+0xb0>
mov    %rbx,%rdi
add    $0xa8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>
add    $0xa8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [18 x i8] c"metaballs%03d.ppm\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [13 x i8] c"P6\0A%d %d\0A%d\0A\00"
define void @save_image(i32 %0, [80 x double]* %1, i32* %2, i32 %3, double %4) {
%6 = alloca i32
%7 = alloca [80 x double]*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca double
%11 = alloca i32*
%12 = alloca [128 x i8]
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca double
%18 = alloca i32
store i32 %0, i32* %6
store [80 x double]* %1, [80 x double]** %7
store i32* %2, i32** %8
store i32 %3, i32* %9
store double %4, double* %10
%19 = getelementptr inbounds [128 x i8], [128 x i8]* %12, i64 0, i64 0
%20 = load i32, i32* %6
%21 = call i32 @sprintf(i8* %19, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %20)
%22 = getelementptr inbounds [128 x i8], [128 x i8]* %12, i64 0, i64 0
%23 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %23, i32** %11
%24 = load i32*, i32** %11
%25 = icmp ne i32* %24, null
br i1 %25, label %26, label %88
26: ; preds = %5
%27 = load i32*, i32** %11
%28 = call i32 @fprintf(i32* %27, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 80, i32 8, i32 255)
store i32 0, i32* %13
br label %29
29: ; preds = %82, %26
%30 = load i32, i32* %13
%31 = icmp slt i32 %30, 8
br i1 %31, label %32, label %85
32: ; preds = %29
store i32 0, i32* %14
br label %33
33: ; preds = %78, %32
%34 = load i32, i32* %14
%35 = icmp slt i32 %34, 80
br i1 %35, label %36, label %81
36: ; preds = %33
%37 = load [80 x double]*, [80 x double]** %7
%38 = load i32, i32* %13
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [80 x double], [80 x double]* %37, i64 %39
%41 = load i32, i32* %14
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [80 x double], [80 x double]* %40, i64 0, i64 %42
%44 = load double, double* %43
store double %44, double* %17
%45 = load i32, i32* %9
%46 = sitofp i32 %45 to double
%47 = load double, double* %17
%48 = fmul double %46, %47
%49 = load double, double* %10
%50 = fdiv double %48, %49
%51 = fptosi double %50 to i32
store i32 %51, i32* %18
%52 = load i32, i32* %18
%53 = load i32, i32* %9
%54 = icmp sgt i32 %52, %53
br i1 %54, label %55, label %57
55: ; preds = %36
%56 = load i32, i32* %9
store i32 %56, i32* %18
br label %57
57: ; preds = %55, %36
%58 = load i32*, i32** %8
%59 = load i32, i32* %18
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i32, i32* %58, i64 %60
%62 = load i32, i32* %61
store i32 %62, i32* %15
%63 = load i32, i32* %15
%64 = ashr i32 %63, 16
%65 = and i32 %64, 255
store i32 %65, i32* %16
%66 = load i32*, i32** %11
%67 = call i32 @fwrite(i32* %16, i32 1, i32 1, i32* %66)
%68 = load i32, i32* %15
%69 = ashr i32 %68, 8
%70 = and i32 %69, 255
store i32 %70, i32* %16
%71 = load i32*, i32** %11
%72 = call i32 @fwrite(i32* %16, i32 1, i32 1, i32* %71)
%73 = load i32, i32* %15
%74 = ashr i32 %73, 0
%75 = and i32 %74, 255
store i32 %75, i32* %16
%76 = load i32*, i32** %11
%77 = call i32 @fwrite(i32* %16, i32 1, i32 1, i32* %76)
br label %78
78: ; preds = %57
%79 = load i32, i32* %14
%80 = add nsw i32 %79, 1
store i32 %80, i32* %14
br label %33
81: ; preds = %33
br label %82
82: ; preds = %81
%83 = load i32, i32* %13
%84 = add nsw i32 %83, 1
store i32 %84, i32* %13
br label %29
85: ; preds = %29
%86 = load i32*, i32** %11
%87 = call i32 @fclose(i32* %86)
br label %88
88: ; preds = %85, %5
ret void
}

LLVM IR (-O2):
@.str = [18 x i8] c"metaballs%03d.ppm\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [13 x i8] c"P6\0A%d %d\0A%d\0A\00"
define void @save_image(i32 %0, [80 x double]* nocapture %1, i32* nocapture %2, i32 %3, double %4) {
%6 = alloca [128 x i8]
%7 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%8 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %0)
%9 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %44, label %11
11: ; preds = %5
%12 = tail call i32 @fprintf(i32* %9, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i32 80, i32 8, i32 255)
%13 = sitofp i32 %3 to double
br label %14
14: ; preds = %11, %39
%15 = phi i64 [ 0, %11 ], [ %40, %39 ]
br label %16
16: ; preds = %14, %16
%17 = phi i64 [ 0, %14 ], [ %37, %16 ]
%18 = getelementptr inbounds [80 x double], [80 x double]* %1, i64 %15, i64 %17
%19 = load double, double* %18
%20 = fmul double %19, %13
%21 = fdiv double %20, %4
%22 = fptosi double %21 to i32
%23 = icmp sgt i32 %22, %3
%24 = select i1 %23, i32 %3, i32 %22
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i32, i32* %2, i64 %25
%27 = load i32, i32* %26
%28 = shl i32 %27, 8
%29 = ashr i32 %28, 24
%30 = tail call i32 @fputc(i32 %29, i32* %9)
%31 = shl i32 %27, 16
%32 = ashr i32 %31, 24
%33 = tail call i32 @fputc(i32 %32, i32* %9)
%34 = shl i32 %27, 24
%35 = ashr exact i32 %34, 24
%36 = tail call i32 @fputc(i32 %35, i32* %9)
%37 = add nuw nsw i64 %17, 1
%38 = icmp eq i64 %37, 80
br i1 %38, label %39, label %16
39: ; preds = %16
%40 = add nuw nsw i64 %15, 1
%41 = icmp eq i64 %40, 8
br i1 %41, label %42, label %14
42: ; preds = %39
%43 = tail call i32 @fclose(i32* %9)
br label %44
44: ; preds = %42, %5
ret void
}