#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*/ time_t;
typedef int /*<<< orphan*/ fn;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, unsigned char,
                           unsigned char, ...);
int /*<<< orphan*/ localtime(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcpy(char *, char const *);
int /*<<< orphan*/ strftime(char *, int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
void write_ppm_ascii(const char *filename, unsigned char *pixels, int width,
                     int height) {
  unsigned char rgb[3];
  unsigned char *loc;
  char fn[256];
  FILE *fp;
  int i, num_of_pixels;
  time_t tm;
  if (filename != NULL) {
    strcpy(fn, filename);
  } else {
    tm = time(NULL);
    strftime(fn, sizeof(fn), "SCRN_%Y_%m_%d_%H_%M_%S.ppm", localtime(&tm));
  }
  fp = fopen(fn, "w");
  if (fp == NULL)
    return;
  fprintf(fp, "P3\n%d %d\n255\n", width, height);
  loc = pixels;
  num_of_pixels = width * height;
  for (i = 0; i < num_of_pixels; ++i) {
    rgb[2] = *loc++;
    rgb[1] = *loc++;
    rgb[0] = *loc++;
    loc++;
    fprintf(fp, "\n%d\n%d\n%d", rgb[0], rgb[1], rgb[2]);
  }
  fclose(fp);
}


x86 assembly (clang, -O0):
<write_ppm_ascii>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     11a9 <write_ppm_ascii+0x39>
lea    -0x130(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcpy@plt>
jmp    11ec <write_ppm_ascii+0x7c>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <time@plt>
mov    %eax,-0x144(%rbp)
lea    -0x130(%rbp),%rax
mov    %rax,-0x150(%rbp)
lea    -0x144(%rbp),%rdi
call   1030 <localtime@plt>
mov    -0x150(%rbp),%rdi
mov    %eax,%ecx
mov    $0x100,%esi
lea    0xe19(%rip),%rdx
call   1080 <strftime@plt>
lea    -0x130(%rbp),%rdi
lea    0xe21(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x138(%rbp)
cmpq   $0x0,-0x138(%rbp)
jne    1219 <write_ppm_ascii+0xa9>
jmp    12fb <write_ppm_ascii+0x18b>
mov    -0x138(%rbp),%rdi
mov    -0x14(%rbp),%eax
mov    %al,%cl
mov    -0x18(%rbp),%eax
lea    0xdee(%rip),%rsi
movzbl %cl,%edx
movzbl %al,%ecx
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
imul   -0x18(%rbp),%eax
mov    %eax,-0x140(%rbp)
movl   $0x0,-0x13c(%rbp)
mov    -0x13c(%rbp),%eax
cmp    -0x140(%rbp),%eax
jge    12ef <write_ppm_ascii+0x17f>
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
mov    (%rax),%al
mov    %al,-0x19(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
mov    (%rax),%al
mov    %al,-0x1a(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x28(%rbp)
mov    (%rax),%al
mov    %al,-0x1b(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x138(%rbp),%rdi
mov    -0x1b(%rbp),%cl
mov    -0x1a(%rbp),%al
movzbl -0x19(%rbp),%r8d
lea    0xd5d(%rip),%rsi
movzbl %cl,%edx
movzbl %al,%ecx
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x13c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x13c(%rbp)
jmp    125b <write_ppm_ascii+0xeb>
mov    -0x138(%rbp),%rdi
call   1050 <fclose@plt>
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_ppm_ascii>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x110,%rsp
mov    %ecx,%r15d
mov    %edx,%r14d
mov    %rsi,%rbx
test   %rdi,%rdi
je     119c <write_ppm_ascii+0x2c>
mov    %rdi,%rsi
lea    0x10(%rsp),%rdi
call   1040 <strcpy@plt>
jmp    11c9 <write_ppm_ascii+0x59>
xor    %edi,%edi
call   1070 <time@plt>
mov    %eax,0xc(%rsp)
lea    0xc(%rsp),%rdi
call   1030 <localtime@plt>
lea    0xe48(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x100,%esi
mov    %eax,%ecx
call   1080 <strftime@plt>
lea    0xe4b(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     123d <write_ppm_ascii+0xcd>
mov    %rax,%r12
movzbl %r14b,%edx
movzbl %r15b,%ecx
lea    0xe2c(%rip),%rsi
mov    %rax,%rdi
xor    %eax,%eax
call   1060 <fprintf@plt>
imul   %r14d,%r15d
test   %r15d,%r15d
jle    1235 <write_ppm_ascii+0xc5>
mov    %r15d,%r15d
lea    0xe1d(%rip),%r14
xor    %ebp,%ebp
movzbl (%rbx,%rbp,4),%r8d
movzbl 0x1(%rbx,%rbp,4),%ecx
movzbl 0x2(%rbx,%rbp,4),%edx
mov    %r12,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
add    $0x1,%rbp
cmp    %ebp,%r15d
jne    1210 <write_ppm_ascii+0xa0>
mov    %r12,%rdi
call   1050 <fclose@plt>
add    $0x110,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [27 x i8] c"SCRN_%Y_%m_%d_%H_%M_%S.ppm\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [14 x i8] c"P3\0A%d %d\0A255\0A\00"
@.str.3 = [10 x i8] c"\0A%d\0A%d\0A%d\00"
define void @write_ppm_ascii(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca [3 x i8]
%10 = alloca i8*
%11 = alloca [256 x i8]
%12 = alloca i32*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%16 = load i8*, i8** %5
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %22
18: ; preds = %4
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%20 = load i8*, i8** %5
%21 = call i32 @strcpy(i8* %19, i8* %20)
br label %27
22: ; preds = %4
%23 = call i32 @time(i32* null)
store i32 %23, i32* %15
%24 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%25 = call i32 @localtime(i32* %15)
%26 = call i32 @strftime(i8* %24, i32 256, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %25)
br label %27
27: ; preds = %22, %18
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%29 = call i32* @fopen(i8* %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %29, i32** %12
%30 = load i32*, i32** %12
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %33
32: ; preds = %27
br label %78
33: ; preds = %27
%34 = load i32*, i32** %12
%35 = load i32, i32* %7
%36 = trunc i32 %35 to i8
%37 = load i32, i32* %8
%38 = trunc i32 %37 to i8
%39 = call i32 (i32*, i8*, i8, i8, ...) @fprintf(i32* %34, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %36, i8 zeroext %38)
%40 = load i8*, i8** %6
store i8* %40, i8** %10
%41 = load i32, i32* %7
%42 = load i32, i32* %8
%43 = mul nsw i32 %41, %42
store i32 %43, i32* %14
store i32 0, i32* %13
br label %44
44: ; preds = %72, %33
%45 = load i32, i32* %13
%46 = load i32, i32* %14
%47 = icmp slt i32 %45, %46
br i1 %47, label %48, label %75
48: ; preds = %44
%49 = load i8*, i8** %10
%50 = getelementptr inbounds i8, i8* %49, i32 1
store i8* %50, i8** %10
%51 = load i8, i8* %49
%52 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 2
store i8 %51, i8* %52
%53 = load i8*, i8** %10
%54 = getelementptr inbounds i8, i8* %53, i32 1
store i8* %54, i8** %10
%55 = load i8, i8* %53
%56 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 1
store i8 %55, i8* %56
%57 = load i8*, i8** %10
%58 = getelementptr inbounds i8, i8* %57, i32 1
store i8* %58, i8** %10
%59 = load i8, i8* %57
%60 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 0
store i8 %59, i8* %60
%61 = load i8*, i8** %10
%62 = getelementptr inbounds i8, i8* %61, i32 1
store i8* %62, i8** %10
%63 = load i32*, i32** %12
%64 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 0
%65 = load i8, i8* %64
%66 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 1
%67 = load i8, i8* %66
%68 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 2
%69 = load i8, i8* %68
%70 = zext i8 %69 to i32
%71 = call i32 (i32*, i8*, i8, i8, ...) @fprintf(i32* %63, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8 zeroext %65, i8 zeroext %67, i32 %70)
br label %72
72: ; preds = %48
%73 = load i32, i32* %13
%74 = add nsw i32 %73, 1
store i32 %74, i32* %13
br label %44
75: ; preds = %44
%76 = load i32*, i32** %12
%77 = call i32 @fclose(i32* %76)
br label %78
78: ; preds = %75, %32
ret void
}

LLVM IR (-O2):
@.str = [27 x i8] c"SCRN_%Y_%m_%d_%H_%M_%S.ppm\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [14 x i8] c"P3\0A%d %d\0A255\0A\00"
@.str.3 = [10 x i8] c"\0A%d\0A%d\0A%d\00"
define void @write_ppm_ascii(i8* %0, i8* nocapture %1, i32 %2, i32 %3) {
%5 = alloca [256 x i8]
%6 = alloca i32
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%8 = bitcast i32* %6 to i8*
%9 = icmp eq i8* %0, null
br i1 %9, label %12, label %10
10: ; preds = %4
%11 = call i32 @strcpy(i8* %7, i8* %0)
br label %16
12: ; preds = %4
%13 = tail call i32 @time(i32* null)
store i32 %13, i32* %6
%14 = call i32 @localtime(i32* %6)
%15 = call i32 @strftime(i8* %7, i32 256, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %14)
br label %16
16: ; preds = %12, %10
%17 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %40, label %19
19: ; preds = %16
%20 = trunc i32 %2 to i8
%21 = trunc i32 %3 to i8
%22 = call i32 (i32*, i8*, i8, i8, ...) @fprintf(i32* %17, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %20, i8 zeroext %21)
%23 = mul i32 %3, %2
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %38
25: ; preds = %19, %25
%26 = phi i8* [ %33, %25 ], [ %1, %19 ]
%27 = phi i32 [ %36, %25 ], [ 0, %19 ]
%28 = getelementptr inbounds i8, i8* %26, i64 1
%29 = load i8, i8* %26
%30 = getelementptr inbounds i8, i8* %26, i64 2
%31 = load i8, i8* %28
%32 = load i8, i8* %30
%33 = getelementptr inbounds i8, i8* %26, i64 4
%34 = zext i8 %29 to i32
%35 = call i32 (i32*, i8*, i8, i8, ...) @fprintf(i32* %17, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8 zeroext %32, i8 zeroext %31, i32 %34)
%36 = add nuw nsw i32 %27, 1
%37 = icmp eq i32 %36, %23
br i1 %37, label %38, label %25
38: ; preds = %25, %19
%39 = call i32 @fclose(i32* %17)
br label %40
40: ; preds = %16, %38
ret void
}