#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*/ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int);
void save_ppm(int width, int height, uint8_t *buf, int num) {
  char filename[100];
  FILE *ppmfile;
  sprintf(filename, "%d.ppm", num);
  ppmfile = fopen(filename, "wb");
  if (!ppmfile)
    return;
  fprintf(ppmfile, "P6\n%d %d\n255\n", width, height);
  fwrite(buf, 3 * width, height, ppmfile);
  fclose(ppmfile);
}


x86 assembly (clang, -O0):
<save_ppm>:
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_ppm+0x55>
jmp    11e1 <save_ppm+0x91>
mov    -0x88(%rbp),%rdi
mov    -0x4(%rbp),%edx
mov    -0x8(%rbp),%ecx
lea    0xe51(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x10(%rbp),%rdi
imul   $0x3,-0x4(%rbp),%esi
mov    -0x8(%rbp),%edx
mov    -0x88(%rbp),%rcx
call   1070 <fwrite@plt>
mov    -0x88(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_ppm>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x68,%rsp
mov    %rdx,%r14
mov    %esi,%r15d
mov    %edi,%ebx
lea    0xe97(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %ecx,%edx
call   1060 <sprintf@plt>
lea    0xe8a(%rip),%rsi
mov    %rbp,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     11c4 <save_ppm+0x74>
mov    %rax,%rbp
lea    0xe76(%rip),%rsi
mov    %rax,%rdi
mov    %ebx,%edx
mov    %r15d,%ecx
call   1040 <fprintf@plt>
lea    (%rbx,%rbx,2),%esi
mov    %r14,%rdi
mov    %r15d,%edx
mov    %rbp,%rcx
call   1070 <fwrite@plt>
mov    %rbp,%rdi
add    $0x68,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>
add    $0x68,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"%d.ppm\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [14 x i8] c"P6\0A%d %d\0A255\0A\00"
define void @save_ppm(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*
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32* %2, i32** %7
store i32 %3, i32* %8
%11 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%12 = load i32, i32* %8
%13 = call i32 @sprintf(i8* %11, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %15, i32** %10
%16 = load i32*, i32** %10
%17 = icmp ne i32* %16, null
br i1 %17, label %19, label %18
18: ; preds = %4
br label %32
19: ; preds = %4
%20 = load i32*, i32** %10
%21 = load i32, i32* %5
%22 = load i32, i32* %6
%23 = call i32 @fprintf(i32* %20, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %21, i32 %22)
%24 = load i32*, i32** %7
%25 = load i32, i32* %5
%26 = mul nsw i32 3, %25
%27 = load i32, i32* %6
%28 = load i32*, i32** %10
%29 = call i32 @fwrite(i32* %24, i32 %26, i32 %27, i32* %28)
%30 = load i32*, i32** %10
%31 = call i32 @fclose(i32* %30)
br label %32
32: ; preds = %19, %18
ret void
}

LLVM IR (-O2):
@.str = [7 x i8] c"%d.ppm\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [14 x i8] c"P6\0A%d %d\0A255\0A\00"
define void @save_ppm(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 %15, label %10
10: ; preds = %4
%11 = tail call i32 @fprintf(i32* %8, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %0, i32 %1)
%12 = mul nsw i32 %0, 3
%13 = tail call i32 @fwrite(i32* %2, i32 %12, i32 %1, i32* %8)
%14 = tail call i32 @fclose(i32* %8)
br label %15
15: ; preds = %4, %10
ret void
}