#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*/ 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(unsigned char *, int, int, int /*<<< orphan*/ *);
void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize,
              char *filename) {
  FILE *f;
  int i;
  f = fopen(filename, "w");
  fprintf(f, "P5\n%d %d\n%d\n", xsize, ysize, 255);
  for (i = 0; i < ysize; i++)
    fwrite(buf + i * wrap, 1, xsize, f);
  fclose(f);
}


x86 assembly (clang, -O0):
<pgm_save>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe9c(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%edx
mov    -0x14(%rbp),%ecx
lea    0xe84(%rip),%rsi
mov    $0xff,%r8d
call   1040 <fprintf@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11cb <pgm_save+0x8b>
mov    -0x8(%rbp),%rdi
mov    -0x2c(%rbp),%eax
imul   -0xc(%rbp),%eax
cltq
add    %rax,%rdi
mov    -0x10(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1060 <fwrite@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1190 <pgm_save+0x50>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pgm_save>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r15d
mov    %edx,%r14d
mov    %esi,%r12d
mov    %rdi,%rbx
lea    0xea2(%rip),%rsi
mov    %r8,%rdi
call   1050 <fopen@plt>
mov    %rax,%r13
lea    0xe92(%rip),%rsi
mov    %rax,%rdi
mov    %r14d,%edx
mov    %r15d,%ecx
mov    $0xff,%r8d
call   1040 <fprintf@plt>
test   %r15d,%r15d
jle    11ac <pgm_save+0x6c>
movslq %r12d,%r12
mov    %r15d,%ebp
nop
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %r13,%rcx
call   1060 <fwrite@plt>
add    %r12,%rbx
add    $0xffffffffffffffff,%rbp
jne    1190 <pgm_save+0x50>
mov    %r13,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [13 x i8] c"P5\0A%d %d\0A%d\0A\00"
define void @pgm_save(i8* %0, i32 %1, i32 %2, i32 %3, i8* %4) {
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32*
%12 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i8* %4, i8** %10
%13 = load i8*, i8** %10
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %11
%15 = load i32*, i32** %11
%16 = load i32, i32* %8
%17 = load i32, i32* %9
%18 = call i32 @fprintf(i32* %15, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %16, i32 %17, i32 255)
store i32 0, i32* %12
br label %19
19: ; preds = %33, %5
%20 = load i32, i32* %12
%21 = load i32, i32* %9
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %36
23: ; preds = %19
%24 = load i8*, i8** %6
%25 = load i32, i32* %12
%26 = load i32, i32* %7
%27 = mul nsw i32 %25, %26
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %24, i64 %28
%30 = load i32, i32* %8
%31 = load i32*, i32** %11
%32 = call i32 @fwrite(i8* %29, i32 1, i32 %30, i32* %31)
br label %33
33: ; preds = %23
%34 = load i32, i32* %12
%35 = add nsw i32 %34, 1
store i32 %35, i32* %12
br label %19
36: ; preds = %19
%37 = load i32*, i32** %11
%38 = call i32 @fclose(i32* %37)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [13 x i8] c"P5\0A%d %d\0A%d\0A\00"
define void @pgm_save(i8* nocapture %0, i32 %1, i32 %2, i32 %3, i8* nocapture %4) {
%6 = tail call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @fprintf(i32* %6, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %2, i32 %3, i32 255)
%8 = icmp sgt i32 %3, 0
br i1 %8, label %9, label %19
9: ; preds = %5
%10 = sext i32 %1 to i64
%11 = zext i32 %3 to i64
br label %12
12: ; preds = %9, %12
%13 = phi i64 [ 0, %9 ], [ %17, %12 ]
%14 = mul nsw i64 %13, %10
%15 = getelementptr inbounds i8, i8* %0, i64 %14
%16 = tail call i32 @fwrite(i8* %15, i32 1, i32 %2, i32* %6)
%17 = add nuw nsw i64 %13, 1
%18 = icmp eq i64 %17, %11
br i1 %18, label %19, label %12
19: ; preds = %12, %5
%20 = tail call i32 @fclose(i32* %6)
ret void
}