#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 uint8_t;
typedef int uint32_t;
typedef int /*<<< orphan*/ buf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int, int);
int /*<<< orphan*/ fwrite(int *, int, int, int /*<<< orphan*/ *);
void save_ppm(FILE *f, uint32_t *buffer, uint32_t width, uint32_t height,
              uint32_t pitch) {
  fprintf(f, "P6\n%d %d\n255\n", width, height);
  for (uint32_t y = 0; y < height; y++) {
    uint32_t *line = buffer;
    for (uint32_t x = 0; x < width; x++, line++) {
      uint8_t buf[3] = {*line >> 16, *line >> 8, *line};
      fwrite(buf, 1, sizeof(buf), f);
    }
    buffer = buffer + pitch / sizeof(uint32_t);
  }
}


x86 assembly (clang, -O0):
<save_ppm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xeb5(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    11f5 <save_ppm+0xd5>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11d0 <save_ppm+0xb0>
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
sar    $0x10,%eax
mov    %eax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
sar    $0x8,%eax
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
lea    -0x38(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
mov    $0xc,%edx
call   1040 <fwrite@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x28(%rbp)
jmp    1172 <save_ppm+0x52>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
shr    $0x2,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1157 <save_ppm+0x37>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_ppm>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8d,%r12d
mov    %ecx,%r14d
mov    %rsi,%rbp
mov    %rdi,%rbx
lea    0xebf(%rip),%rsi
mov    %edx,0xc(%rsp)
call   1030 <fprintf@plt>
mov    %r14d,0x10(%rsp)
test   %r14d,%r14d
jle    11cb <save_ppm+0xab>
movslq %r12d,%rax
and    $0xfffffffffffffffc,%rax
mov    %rax,0x20(%rsp)
mov    0xc(%rsp),%r14d
xor    %r15d,%r15d
lea    0x14(%rsp),%r12
jmp    1180 <save_ppm+0x60>
nop
add    $0x1,%r15d
add    0x20(%rsp),%rbp
cmp    0x10(%rsp),%r15d
je     11cb <save_ppm+0xab>
cmpl   $0x0,0xc(%rsp)
jle    1170 <save_ppm+0x50>
xor    %r13d,%r13d
nopw   0x0(%rax,%rax,1)
mov    0x0(%rbp,%r13,4),%eax
mov    %eax,%ecx
sar    $0x10,%ecx
mov    %ecx,0x14(%rsp)
mov    %eax,%ecx
sar    $0x8,%ecx
mov    %ecx,0x18(%rsp)
mov    %eax,0x1c(%rsp)
mov    %r12,%rdi
mov    $0x1,%esi
mov    $0xc,%edx
mov    %rbx,%rcx
call   1040 <fwrite@plt>
add    $0x1,%r13
cmp    %r13d,%r14d
jne    1190 <save_ppm+0x70>
jmp    1170 <save_ppm+0x50>
add    $0x28,%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"
define void @save_ppm(i32* %0, i32* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32*
%13 = alloca i32
%14 = alloca [3 x i32]
store i32* %0, i32** %6
store i32* %1, i32** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 %4, i32* %10
%15 = load i32*, i32** %6
%16 = load i32, i32* %8
%17 = load i32, i32* %9
%18 = call i32 @fprintf(i32* %15, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %16, i32 %17)
store i32 0, i32* %11
br label %19
19: ; preds = %55, %5
%20 = load i32, i32* %11
%21 = load i32, i32* %9
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %58
23: ; preds = %19
%24 = load i32*, i32** %7
store i32* %24, i32** %12
store i32 0, i32* %13
br label %25
25: ; preds = %44, %23
%26 = load i32, i32* %13
%27 = load i32, i32* %8
%28 = icmp slt i32 %26, %27
br i1 %28, label %29, label %49
29: ; preds = %25
%30 = getelementptr inbounds [3 x i32], [3 x i32]* %14, i64 0, i64 0
%31 = load i32*, i32** %12
%32 = load i32, i32* %31
%33 = ashr i32 %32, 16
store i32 %33, i32* %30
%34 = getelementptr inbounds i32, i32* %30, i64 1
%35 = load i32*, i32** %12
%36 = load i32, i32* %35
%37 = ashr i32 %36, 8
store i32 %37, i32* %34
%38 = getelementptr inbounds i32, i32* %34, i64 1
%39 = load i32*, i32** %12
%40 = load i32, i32* %39
store i32 %40, i32* %38
%41 = getelementptr inbounds [3 x i32], [3 x i32]* %14, i64 0, i64 0
%42 = load i32*, i32** %6
%43 = call i32 @fwrite(i32* %41, i32 1, i32 12, i32* %42)
br label %44
44: ; preds = %29
%45 = load i32, i32* %13
%46 = add nsw i32 %45, 1
store i32 %46, i32* %13
%47 = load i32*, i32** %12
%48 = getelementptr inbounds i32, i32* %47, i32 1
store i32* %48, i32** %12
br label %25
49: ; preds = %25
%50 = load i32*, i32** %7
%51 = load i32, i32* %10
%52 = sext i32 %51 to i64
%53 = udiv i64 %52, 4
%54 = getelementptr inbounds i32, i32* %50, i64 %53
store i32* %54, i32** %7
br label %55
55: ; preds = %49
%56 = load i32, i32* %11
%57 = add nsw i32 %56, 1
store i32 %57, i32* %11
br label %19
58: ; preds = %19
ret void
}

LLVM IR (-O2):
@.str = [14 x i8] c"P6\0A%d %d\0A255\0A\00"
define void @save_ppm(i32* nocapture %0, i32* nocapture %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca [3 x i32]
%7 = tail call i32 @fprintf(i32* %0, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %2, i32 %3)
%8 = icmp sgt i32 %3, 0
br i1 %8, label %9, label %20
9: ; preds = %5
%10 = icmp sgt i32 %2, 0
%11 = bitcast [3 x i32]* %6 to i8*
%12 = getelementptr inbounds [3 x i32], [3 x i32]* %6, i64 0, i64 0
%13 = getelementptr inbounds [3 x i32], [3 x i32]* %6, i64 0, i64 1
%14 = getelementptr inbounds [3 x i32], [3 x i32]* %6, i64 0, i64 2
%15 = sext i32 %4 to i64
%16 = lshr i64 %15, 2
br label %17
17: ; preds = %9, %21
%18 = phi i32 [ 0, %9 ], [ %23, %21 ]
%19 = phi i32* [ %1, %9 ], [ %22, %21 ]
br i1 %10, label %25, label %21
20: ; preds = %21, %5
ret void
21: ; preds = %25, %17
%22 = getelementptr inbounds i32, i32* %19, i64 %16
%23 = add nuw nsw i32 %18, 1
%24 = icmp eq i32 %23, %3
br i1 %24, label %20, label %17
25: ; preds = %17, %25
%26 = phi i32 [ %32, %25 ], [ 0, %17 ]
%27 = phi i32* [ %33, %25 ], [ %19, %17 ]
%28 = load i32, i32* %27
%29 = ashr i32 %28, 16
store i32 %29, i32* %12
%30 = ashr i32 %28, 8
store i32 %30, i32* %13
store i32 %28, i32* %14
%31 = call i32 @fwrite(i32* %12, i32 1, i32 12, i32* %0)
%32 = add nuw nsw i32 %26, 1
%33 = getelementptr inbounds i32, i32* %27, i64 1
%34 = icmp eq i32 %32, %2
br i1 %34, label %21, label %25
}