#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*/ rgb;
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 *, int, int);
int /*<<< orphan*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
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_raw(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), "IMG_%Y_%m_%d_%H_%M_%S.ppm", localtime(&tm));
  }
  fp = fopen(fn, "wb");
  if (fp == NULL)
    return;
  fprintf(fp, "P6\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++;
    fwrite(rgb, sizeof(unsigned char), sizeof(rgb), fp);
  }
  fclose(fp);
}


x86 assembly (clang, -O0):
<write_ppm_raw>:
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     11b9 <write_ppm_raw+0x39>
lea    -0x130(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcpy@plt>
jmp    11fc <write_ppm_raw+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    0xe09(%rip),%rdx
call   1080 <strftime@plt>
lea    -0x130(%rbp),%rdi
lea    0xe10(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x138(%rbp)
cmpq   $0x0,-0x138(%rbp)
jne    1229 <write_ppm_raw+0xa9>
jmp    12f5 <write_ppm_raw+0x175>
mov    -0x138(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xde0(%rip),%rsi
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    12e9 <write_ppm_raw+0x169>
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)
lea    -0x1b(%rbp),%rdi
mov    -0x138(%rbp),%rcx
mov    $0x1,%esi
mov    $0x3,%edx
call   10a0 <fwrite@plt>
mov    -0x13c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x13c(%rbp)
jmp    1261 <write_ppm_raw+0xe1>
mov    -0x138(%rbp),%rdi
call   1050 <fclose@plt>
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_ppm_raw>:
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     11ac <write_ppm_raw+0x2c>
mov    %rdi,%rsi
lea    0x10(%rsp),%rdi
call   1040 <strcpy@plt>
jmp    11d9 <write_ppm_raw+0x59>
xor    %edi,%edi
call   1070 <time@plt>
mov    %eax,0xc(%rsp)
lea    0xc(%rsp),%rdi
call   1030 <localtime@plt>
lea    0xe38(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0x100,%esi
mov    %eax,%ecx
call   1080 <strftime@plt>
lea    0xe3a(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1260 <write_ppm_raw+0xe0>
mov    %rax,%r12
lea    0xe24(%rip),%rsi
mov    %rax,%rdi
mov    %r14d,%edx
mov    %r15d,%ecx
call   1060 <fprintf@plt>
imul   %r14d,%r15d
test   %r15d,%r15d
jle    1258 <write_ppm_raw+0xd8>
mov    %r15d,%r15d
xor    %ebp,%ebp
lea    0x9(%rsp),%r14
nopw   0x0(%rax,%rax,1)
movzbl (%rbx,%rbp,4),%eax
mov    %al,0xb(%rsp)
movzbl 0x1(%rbx,%rbp,4),%eax
mov    %al,0xa(%rsp)
movzbl 0x2(%rbx,%rbp,4),%eax
mov    %al,0x9(%rsp)
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x3,%edx
mov    %r12,%rcx
call   10a0 <fwrite@plt>
add    $0x1,%rbp
cmp    %ebp,%r15d
jne    1220 <write_ppm_raw+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 = [26 x i8] c"IMG_%Y_%m_%d_%H_%M_%S.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 @write_ppm_raw(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 ([26 x i8], [26 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 ([3 x i8], [3 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 %70
33: ; preds = %27
%34 = load i32*, i32** %12
%35 = load i32, i32* %7
%36 = load i32, i32* %8
%37 = call i32 @fprintf(i32* %34, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %35, i32 %36)
%38 = load i8*, i8** %6
store i8* %38, i8** %10
%39 = load i32, i32* %7
%40 = load i32, i32* %8
%41 = mul nsw i32 %39, %40
store i32 %41, i32* %14
store i32 0, i32* %13
br label %42
42: ; preds = %64, %33
%43 = load i32, i32* %13
%44 = load i32, i32* %14
%45 = icmp slt i32 %43, %44
br i1 %45, label %46, label %67
46: ; preds = %42
%47 = load i8*, i8** %10
%48 = getelementptr inbounds i8, i8* %47, i32 1
store i8* %48, i8** %10
%49 = load i8, i8* %47
%50 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 2
store i8 %49, i8* %50
%51 = load i8*, i8** %10
%52 = getelementptr inbounds i8, i8* %51, i32 1
store i8* %52, i8** %10
%53 = load i8, i8* %51
%54 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 1
store i8 %53, i8* %54
%55 = load i8*, i8** %10
%56 = getelementptr inbounds i8, i8* %55, i32 1
store i8* %56, i8** %10
%57 = load i8, i8* %55
%58 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 0
store i8 %57, i8* %58
%59 = load i8*, i8** %10
%60 = getelementptr inbounds i8, i8* %59, i32 1
store i8* %60, i8** %10
%61 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 0
%62 = load i32*, i32** %12
%63 = call i32 @fwrite(i8* %61, i32 1, i32 3, i32* %62)
br label %64
64: ; preds = %46
%65 = load i32, i32* %13
%66 = add nsw i32 %65, 1
store i32 %66, i32* %13
br label %42
67: ; preds = %42
%68 = load i32*, i32** %12
%69 = call i32 @fclose(i32* %68)
br label %70
70: ; preds = %67, %32
ret void
}

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