#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 */

/* Variables and functions */
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int, int);
int /*<<< orphan*/ sprintf(char *, char *, int, int);
int strlen(char *);
int /*<<< orphan*/ write(int, char *, int);
void ppm_save(char *path, char *s, int h, int w) {
  char sig[128];
  int fd;
  sprintf(sig, "P6\n%d %d\n255\n", w, h);
  fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0600);
  write(fd, sig, strlen(sig));
  write(fd, s, h * w * 3);
  close(fd);
}


x86 assembly (clang, -O0):
<ppm_save>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
lea    -0xa0(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    -0x14(%rbp),%ecx
lea    0xe83(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
mov    0x2e5a(%rip),%rax
or     (%rax),%esi
mov    0x2e59(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   1060 <open@plt>
mov    %eax,-0xa4(%rbp)
mov    -0xa4(%rbp),%eax
mov    %eax,-0xb4(%rbp)
lea    -0xa0(%rbp),%rax
mov    %rax,-0xb0(%rbp)
lea    -0xa0(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xb4(%rbp),%edi
mov    -0xb0(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0xa4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%eax
imul   -0x18(%rbp),%eax
imul   $0x3,%eax,%edx
call   1030 <write@plt>
mov    -0xa4(%rbp),%edi
call   1050 <close@plt>
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ppm_save>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    %ecx,%r13d
mov    %edx,%ebx
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe8d(%rip),%rsi
mov    %rsp,%r12
mov    %r12,%rdi
mov    %ecx,%edx
mov    %ebx,%ecx
call   1070 <sprintf@plt>
mov    0x2e3f(%rip),%rax
mov    0x2e60(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e5d(%rip),%rax
or     (%rax),%esi
mov    %r15,%rdi
mov    $0x180,%edx
call   1060 <open@plt>
mov    %eax,%ebp
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1030 <write@plt>
imul   %r13d,%ebx
lea    (%rbx,%rbx,2),%edx
mov    %ebp,%edi
mov    %r14,%rsi
call   1030 <write@plt>
mov    %ebp,%edi
call   1050 <close@plt>
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"P6\0A%d %d\0A255\0A\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
define void @ppm_save(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca [128 x i8]
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%11 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%12 = load i32, i32* %8
%13 = load i32, i32* %7
%14 = call i32 @sprintf(i8* %11, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %13)
%15 = load i8*, i8** %5
%16 = load i32, i32* @O_WRONLY
%17 = load i32, i32* @O_TRUNC
%18 = or i32 %16, %17
%19 = load i32, i32* @O_CREAT
%20 = or i32 %18, %19
%21 = call i32 @open(i8* %15, i32 %20, i32 384)
store i32 %21, i32* %10
%22 = load i32, i32* %10
%23 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%24 = getelementptr inbounds [128 x i8], [128 x i8]* %9, i64 0, i64 0
%25 = call i32 @strlen(i8* %24)
%26 = call i32 @write(i32 %22, i8* %23, i32 %25)
%27 = load i32, i32* %10
%28 = load i8*, i8** %6
%29 = load i32, i32* %7
%30 = load i32, i32* %8
%31 = mul nsw i32 %29, %30
%32 = mul nsw i32 %31, 3
%33 = call i32 @write(i32 %27, i8* %28, i32 %32)
%34 = load i32, i32* %10
%35 = call i32 @close(i32 %34)
ret void
}

LLVM IR (-O2):
@.str = [14 x i8] c"P6\0A%d %d\0A255\0A\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
define void @ppm_save(i8* nocapture %0, i8* nocapture %1, i32 %2, i32 %3) {
%5 = alloca [128 x i8]
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%7 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %3, i32 %2)
%8 = load i32, i32* @O_WRONLY
%9 = load i32, i32* @O_TRUNC
%10 = or i32 %9, %8
%11 = load i32, i32* @O_CREAT
%12 = or i32 %10, %11
%13 = tail call i32 @open(i8* %0, i32 %12, i32 384)
%14 = call i32 @strlen(i8* %6)
%15 = call i32 @write(i32 %13, i8* %6, i32 %14)
%16 = mul i32 %2, 3
%17 = mul i32 %16, %3
%18 = call i32 @write(i32 %13, i8* %1, i32 %17)
%19 = call i32 @close(i32 %13)
ret void
}