#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ fwrite(int /*<<< orphan*/ *const, int, int,
                          int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
void save_pgm(int width, int height, int chroma_width, int chroma_height,
              uint8_t *const *buf, int num) {
  char filename[100];
  FILE *pgmfile;
  int i;
  static uint8_t black[16384] = {0};
  sprintf(filename, "%d.pgm", num);
  pgmfile = fopen(filename, "wb");
  if (!pgmfile) {
    fprintf(stderr, "Could not open file \"%s\".\n", filename);
    exit(1);
  }
  fprintf(pgmfile, "P5\n%d %d\n255\n", 2 * chroma_width,
          height + chroma_height);
  for (i = 0; i < height; i++) {
    fwrite(buf[0] + i * width, width, 1, pgmfile);
    fwrite(black, 2 * chroma_width - width, 1, pgmfile);
  }
  for (i = 0; i < chroma_height; i++) {
    fwrite(buf[1] + i * chroma_width, chroma_width, 1, pgmfile);
    fwrite(buf[2] + i * chroma_width, chroma_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    %edx,-0xc(%rbp)
mov    %ecx,-0x10(%rbp)
mov    %r8,-0x18(%rbp)
mov    %r9d,-0x1c(%rbp)
lea    -0x80(%rbp),%rdi
mov    -0x1c(%rbp),%edx
lea    0xe73(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x80(%rbp),%rdi
lea    0xe6a(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x88(%rbp)
cmpq   $0x0,-0x88(%rbp)
jne    11dd <save_pgm+0x7d>
mov    0x2e22(%rip),%rax
movslq (%rax),%rdi
lea    -0x80(%rbp),%rdx
lea    0xe3e(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x88(%rbp),%rdi
mov    -0xc(%rbp),%edx
shl    $0x1,%edx
mov    -0x8(%rbp),%ecx
add    -0x10(%rbp),%ecx
lea    0xe2e(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x0,-0x8c(%rbp)
mov    -0x8c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    127a <save_pgm+0x11a>
mov    -0x18(%rbp),%rax
mov    (%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   1080 <fwrite@plt>
mov    -0xc(%rbp),%esi
shl    $0x1,%esi
sub    -0x4(%rbp),%esi
mov    -0x88(%rbp),%rcx
lea    0x2e04(%rip),%rdi
mov    $0x1,%edx
call   1080 <fwrite@plt>
mov    -0x8c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8c(%rbp)
jmp    1208 <save_pgm+0xa8>
movl   $0x0,-0x8c(%rbp)
mov    -0x8c(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    1305 <save_pgm+0x1a5>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x8c(%rbp),%eax
imul   -0xc(%rbp),%eax
cltq
shl    $0x2,%rax
add    %rax,%rdi
mov    -0xc(%rbp),%esi
mov    -0x88(%rbp),%rcx
mov    $0x1,%edx
call   1080 <fwrite@plt>
mov    -0x18(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x8c(%rbp),%eax
imul   -0xc(%rbp),%eax
cltq
shl    $0x2,%rax
add    %rax,%rdi
mov    -0xc(%rbp),%esi
mov    -0x88(%rbp),%rcx
mov    $0x1,%edx
call   1080 <fwrite@plt>
mov    -0x8c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8c(%rbp)
jmp    1284 <save_pgm+0x124>
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    $0x88,%rsp
mov    %r8,%r15
mov    %ecx,%ebx
mov    %edx,%ebp
mov    %esi,%r13d
mov    %edi,0xc(%rsp)
lea    0xe7a(%rip),%rsi
lea    0x20(%rsp),%r14
mov    %r14,%rdi
mov    %r9d,%edx
call   1060 <sprintf@plt>
lea    0xe6a(%rip),%rsi
mov    %r14,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     1290 <save_pgm+0x130>
mov    %rax,%r14
mov    %rbp,0x18(%rsp)
add    %ebp,%ebp
mov    %rbx,0x10(%rsp)
lea    (%rbx,%r13,1),%ecx
lea    0xe5d(%rip),%rsi
mov    %rax,%rdi
mov    %ebp,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
test   %r13d,%r13d
mov    0xc(%rsp),%eax
jle    1226 <save_pgm+0xc6>
sub    %eax,%ebp
movslq %eax,%r12
mov    %r13d,%r13d
shl    $0x2,%r12
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    (%r15),%rdi
add    %rbx,%rdi
mov    0xc(%rsp),%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1080 <fwrite@plt>
lea    0x2e52(%rip),%rdi
mov    %ebp,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1080 <fwrite@plt>
add    %r12,%rbx
add    $0xffffffffffffffff,%r13
jne    11f0 <save_pgm+0x90>
mov    0x10(%rsp),%rax
test   %eax,%eax
mov    0x18(%rsp),%r13
jle    1277 <save_pgm+0x117>
movslq %r13d,%r12
mov    %eax,%ebp
shl    $0x2,%r12
xor    %ebx,%ebx
nop
mov    0x8(%r15),%rdi
add    %rbx,%rdi
mov    %r13d,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1080 <fwrite@plt>
mov    0x10(%r15),%rdi
add    %rbx,%rdi
mov    %r13d,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1080 <fwrite@plt>
add    %r12,%rbx
add    $0xffffffffffffffff,%rbp
jne    1240 <save_pgm+0xe0>
mov    %r14,%rdi
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>
mov    0x2d49(%rip),%rax
movslq (%rax),%rdi
lea    0xd69(%rip),%rsi
lea    0x20(%rsp),%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

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

LLVM IR (-O2):
@save_pgm.black = internal global [16384 x i32] zeroinitializer
@.str = [7 x i8] c"%d.pgm\00"
@.str.1 = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.2 = [27 x i8] c"Could not open file \22%s\22.\0A\00"
@.str.3 = [14 x i8] c"P5\0A%d %d\0A255\0A\00"
define void @save_pgm(i32 %0, i32 %1, i32 %2, i32 %3, i32** nocapture %4, i32 %5) {
%7 = alloca [100 x i8]
%8 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%9 = call i32 @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %5)
%10 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %18
12: ; preds = %6
%13 = load i32, i32* @stderr
%14 = sext i32 %13 to i64
%15 = inttoptr i64 %14 to i32*
%16 = call i32 (i32*, i8*, ...) @fprintf(i32* %15, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %8)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %6
%19 = shl nsw i32 %2, 1
%20 = add nsw i32 %3, %1
%21 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %10, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %19, i32 %20)
%22 = icmp sgt i32 %1, 0
br i1 %22, label %23, label %27
23: ; preds = %18
%24 = sub nsw i32 %19, %0
%25 = sext i32 %0 to i64
%26 = zext i32 %1 to i64
br label %34
27: ; preds = %34, %18
%28 = icmp sgt i32 %3, 0
br i1 %28, label %29, label %54
29: ; preds = %27
%30 = getelementptr inbounds i32*, i32** %4, i64 1
%31 = getelementptr inbounds i32*, i32** %4, i64 2
%32 = sext i32 %2 to i64
%33 = zext i32 %3 to i64
br label %43
34: ; preds = %23, %34
%35 = phi i64 [ 0, %23 ], [ %41, %34 ]
%36 = load i32*, i32** %4
%37 = mul nsw i64 %35, %25
%38 = getelementptr inbounds i32, i32* %36, i64 %37
%39 = tail call i32 @fwrite(i32* %38, i32 %0, i32 1, i32* %10)
%40 = tail call i32 @fwrite(i32* getelementptr inbounds ([16384 x i32], [16384 x i32]* @save_pgm.black, i64 0, i64 0), i32 %24, i32 1, i32* %10)
%41 = add nuw nsw i64 %35, 1
%42 = icmp eq i64 %41, %26
br i1 %42, label %27, label %34
43: ; preds = %29, %43
%44 = phi i64 [ 0, %29 ], [ %52, %43 ]
%45 = load i32*, i32** %30
%46 = mul nsw i64 %44, %32
%47 = getelementptr inbounds i32, i32* %45, i64 %46
%48 = tail call i32 @fwrite(i32* %47, i32 %2, i32 1, i32* %10)
%49 = load i32*, i32** %31
%50 = getelementptr inbounds i32, i32* %49, i64 %46
%51 = tail call i32 @fwrite(i32* %50, i32 %2, i32 1, i32* %10)
%52 = add nuw nsw i64 %44, 1
%53 = icmp eq i64 %52, %33
br i1 %53, label %54, label %43
54: ; preds = %43, %27
%55 = tail call i32 @fclose(i32* %10)
ret void
}