#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*/ uint8_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 /*<<< orphan*/ fwrite(int /*<<< orphan*/ *const, int, int,
                          int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int);
void save_pgm(int width, int height, uint8_t *const *buf, int num) {
  char filename[100];
  FILE *pgmfile;
  int i;
  sprintf(filename, "%d.pgm", num);
  pgmfile = fopen(filename, "wb");
  if (!pgmfile)
    return;
  fprintf(pgmfile, "P5\n%d %d\n255\n", width, height * 3 / 2);
  fwrite(buf[0], width, height, pgmfile);
  width >>= 1;
  height >>= 1;
  for (i = 0; i < height; i++) {
    fwrite(buf[1] + i * width, width, 1, pgmfile);
    fwrite(buf[2] + i * width, width, 1, pgmfile);
  }
  fclose(pgmfile);
}


x86 assembly (clang, -O0):
<save_pgm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
lea    -0x80(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xe8a(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x80(%rbp),%rdi
lea    0xe81(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x88(%rbp)
cmpq   $0x0,-0x88(%rbp)
jne    11a5 <save_pgm+0x55>
jmp    1297 <save_pgm+0x147>
mov    -0x88(%rbp),%rdi
mov    -0x4(%rbp),%eax
mov    %eax,-0x90(%rbp)
imul   $0x3,-0x8(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    -0x90(%rbp),%edx
mov    %eax,%ecx
lea    0xe3a(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x4(%rbp),%esi
mov    -0x8(%rbp),%edx
mov    -0x88(%rbp),%rcx
call   1070 <fwrite@plt>
mov    -0x4(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x8(%rbp),%eax
sar    $0x1,%eax
mov    %eax,-0x8(%rbp)
movl   $0x0,-0x8c(%rbp)
mov    -0x8c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    128b <save_pgm+0x13b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x8c(%rbp),%eax
imul   -0x4(%rbp),%eax
cltq
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x4(%rbp),%esi
mov    -0x88(%rbp),%rcx
mov    $0x1,%edx
call   1070 <fwrite@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x8c(%rbp),%eax
imul   -0x4(%rbp),%eax
cltq
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x4(%rbp),%esi
mov    -0x88(%rbp),%rcx
mov    $0x1,%edx
call   1070 <fwrite@plt>
mov    -0x8c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8c(%rbp)
jmp    120a <save_pgm+0xba>
mov    -0x88(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_pgm>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    %rdx,%r14
mov    %esi,%ebx
mov    %edi,%r15d
lea    0xe93(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %ecx,%edx
call   1060 <sprintf@plt>
lea    0xe86(%rip),%rsi
mov    %rbp,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     122d <save_pgm+0xdd>
mov    %rax,%r13
lea    (%rbx,%rbx,2),%eax
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
sar    %ecx
lea    0xe62(%rip),%rsi
mov    %r13,%rdi
mov    %r15d,%edx
call   1040 <fprintf@plt>
mov    (%r14),%rdi
mov    %r15d,%esi
mov    %ebx,%edx
mov    %r13,%rcx
call   1070 <fwrite@plt>
cmp    $0x2,%ebx
jl     1217 <save_pgm+0xc7>
sar    %r15d
shr    %ebx
movslq %r15d,%r12
shl    $0x2,%r12
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
mov    0x8(%r14),%rdi
add    %rbp,%rdi
mov    %r15d,%esi
mov    $0x1,%edx
mov    %r13,%rcx
call   1070 <fwrite@plt>
mov    0x10(%r14),%rdi
add    %rbp,%rdi
mov    %r15d,%esi
mov    $0x1,%edx
mov    %r13,%rcx
call   1070 <fwrite@plt>
add    %r12,%rbp
add    $0xffffffffffffffff,%rbx
jne    11e0 <save_pgm+0x90>
mov    %r13,%rdi
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"%d.pgm\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [14 x i8] c"P5\0A%d %d\0A255\0A\00"
define void @save_pgm(i32 %0, i32 %1, i32** %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32**
%8 = alloca i32
%9 = alloca [100 x i8]
%10 = alloca i32*
%11 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32** %2, i32*** %7
store i32 %3, i32* %8
%12 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%13 = load i32, i32* %8
%14 = call i32 @sprintf(i8* %12, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %16, i32** %10
%17 = load i32*, i32** %10
%18 = icmp ne i32* %17, null
br i1 %18, label %20, label %19
19: ; preds = %4
br label %71
20: ; preds = %4
%21 = load i32*, i32** %10
%22 = load i32, i32* %5
%23 = load i32, i32* %6
%24 = mul nsw i32 %23, 3
%25 = sdiv i32 %24, 2
%26 = call i32 @fprintf(i32* %21, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %22, i32 %25)
%27 = load i32**, i32*** %7
%28 = getelementptr inbounds i32*, i32** %27, i64 0
%29 = load i32*, i32** %28
%30 = load i32, i32* %5
%31 = load i32, i32* %6
%32 = load i32*, i32** %10
%33 = call i32 @fwrite(i32* %29, i32 %30, i32 %31, i32* %32)
%34 = load i32, i32* %5
%35 = ashr i32 %34, 1
store i32 %35, i32* %5
%36 = load i32, i32* %6
%37 = ashr i32 %36, 1
store i32 %37, i32* %6
store i32 0, i32* %11
br label %38
38: ; preds = %65, %20
%39 = load i32, i32* %11
%40 = load i32, i32* %6
%41 = icmp slt i32 %39, %40
br i1 %41, label %42, label %68
42: ; preds = %38
%43 = load i32**, i32*** %7
%44 = getelementptr inbounds i32*, i32** %43, i64 1
%45 = load i32*, i32** %44
%46 = load i32, i32* %11
%47 = load i32, i32* %5
%48 = mul nsw i32 %46, %47
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i32, i32* %45, i64 %49
%51 = load i32, i32* %5
%52 = load i32*, i32** %10
%53 = call i32 @fwrite(i32* %50, i32 %51, i32 1, i32* %52)
%54 = load i32**, i32*** %7
%55 = getelementptr inbounds i32*, i32** %54, i64 2
%56 = load i32*, i32** %55
%57 = load i32, i32* %11
%58 = load i32, i32* %5
%59 = mul nsw i32 %57, %58
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i32, i32* %56, i64 %60
%62 = load i32, i32* %5
%63 = load i32*, i32** %10
%64 = call i32 @fwrite(i32* %61, i32 %62, i32 1, i32* %63)
br label %65
65: ; preds = %42
%66 = load i32, i32* %11
%67 = add nsw i32 %66, 1
store i32 %67, i32* %11
br label %38
68: ; preds = %38
%69 = load i32*, i32** %10
%70 = call i32 @fclose(i32* %69)
br label %71
71: ; preds = %68, %19
ret void
}

LLVM IR (-O2):
@.str = [7 x i8] c"%d.pgm\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [14 x i8] c"P5\0A%d %d\0A255\0A\00"
define void @save_pgm(i32 %0, i32 %1, i32** nocapture %2, i32 %3) {
%5 = alloca [100 x i8]
%6 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%7 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %3)
%8 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %37, label %10
10: ; preds = %4
%11 = mul nsw i32 %1, 3
%12 = sdiv i32 %11, 2
%13 = tail call i32 @fprintf(i32* %8, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %0, i32 %12)
%14 = load i32*, i32** %2
%15 = tail call i32 @fwrite(i32* %14, i32 %0, i32 %1, i32* %8)
%16 = ashr i32 %0, 1
%17 = icmp sgt i32 %1, 1
br i1 %17, label %18, label %35
18: ; preds = %10
%19 = lshr i32 %1, 1
%20 = getelementptr inbounds i32*, i32** %2, i64 1
%21 = getelementptr inbounds i32*, i32** %2, i64 2
%22 = sext i32 %16 to i64
%23 = zext i32 %19 to i64
br label %24
24: ; preds = %18, %24
%25 = phi i64 [ 0, %18 ], [ %33, %24 ]
%26 = load i32*, i32** %20
%27 = mul nsw i64 %25, %22
%28 = getelementptr inbounds i32, i32* %26, i64 %27
%29 = tail call i32 @fwrite(i32* %28, i32 %16, i32 1, i32* %8)
%30 = load i32*, i32** %21
%31 = getelementptr inbounds i32, i32* %30, i64 %27
%32 = tail call i32 @fwrite(i32* %31, i32 %16, i32 1, i32* %8)
%33 = add nuw nsw i64 %25, 1
%34 = icmp eq i64 %33, %23
br i1 %34, label %35, label %24
35: ; preds = %24, %10
%36 = tail call i32 @fclose(i32* %8)
br label %37
37: ; preds = %4, %35
ret void
}