#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*/ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
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) {
    fprintf(stderr, "Could not open file \"%s\".\n", filename);
    exit(1);
  }
  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    0xe7a(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x80(%rbp),%rdi
lea    0xe71(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x88(%rbp)
cmpq   $0x0,-0x88(%rbp)
jne    11d6 <save_ppm+0x76>
mov    0x2e29(%rip),%rax
movslq (%rax),%rdi
lea    -0x80(%rbp),%rdx
lea    0xe45(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x88(%rbp),%rdi
mov    -0x4(%rbp),%edx
mov    -0x8(%rbp),%ecx
lea    0xe3b(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x10(%rbp),%rdi
imul   $0x3,-0x4(%rbp),%esi
mov    -0x8(%rbp),%edx
mov    -0x88(%rbp),%rcx
call   1080 <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    0xe87(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %ecx,%edx
call   1060 <sprintf@plt>
lea    0xe7a(%rip),%rsi
mov    %rbp,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     11d6 <save_ppm+0x76>
mov    %rax,%rbp
lea    0xe81(%rip),%rsi
mov    %rax,%rdi
mov    %ebx,%edx
mov    %r15d,%ecx
xor    %eax,%eax
call   1040 <fprintf@plt>
lea    (%rbx,%rbx,2),%esi
mov    %r14,%rdi
mov    %r15d,%edx
mov    %rbp,%rcx
call   1080 <fwrite@plt>
mov    %rbp,%rdi
add    $0x68,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>
mov    0x2e03(%rip),%rax
movslq (%rax),%rdi
lea    0xe23(%rip),%rsi
mov    %rsp,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"%d.ppm\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"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 %25, label %18
18: ; preds = %4
%19 = load i32, i32* @stderr
%20 = sext i32 %19 to i64
%21 = inttoptr i64 %20 to i32*
%22 = getelementptr inbounds [100 x i8], [100 x i8]* %9, i64 0, i64 0
%23 = call i32 (i32*, i8*, ...) @fprintf(i32* %21, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %22)
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %4
%26 = load i32*, i32** %10
%27 = load i32, i32* %5
%28 = load i32, i32* %6
%29 = call i32 (i32*, i8*, ...) @fprintf(i32* %26, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %27, i32 %28)
%30 = load i32*, i32** %7
%31 = load i32, i32* %5
%32 = mul nsw i32 3, %31
%33 = load i32, i32* %6
%34 = load i32*, i32** %10
%35 = call i32 @fwrite(i32* %30, i32 %32, i32 %33, i32* %34)
%36 = load i32*, i32** %10
%37 = call i32 @fclose(i32* %36)
ret void
}

LLVM IR (-O2):
@.str = [7 x i8] c"%d.ppm\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"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 %10, label %16
10: ; preds = %4
%11 = load i32, i32* @stderr
%12 = sext i32 %11 to i64
%13 = inttoptr i64 %12 to i32*
%14 = call i32 (i32*, i8*, ...) @fprintf(i32* %13, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %6)
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %4
%17 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %8, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %0, i32 %1)
%18 = mul nsw i32 %0, 3
%19 = tail call i32 @fwrite(i32* %2, i32 %18, i32 %1, i32* %8)
%20 = tail call i32 @fclose(i32* %8)
ret void
}