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

/* Variables and functions */
int /*<<< orphan*/ GL_RGB;
int /*<<< orphan*/ GL_UNSIGNED_BYTE;
int /*<<< orphan*/ GL_VIEWPORT;
int /*<<< orphan*/ assert(void *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int, int);
int /*<<< orphan*/ free(void *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ glGetIntegerv(int /*<<< orphan*/, int *);
int /*<<< orphan*/ glReadPixels(int, int, int, int, int /*<<< orphan*/,
                                int /*<<< orphan*/, void *);
void *malloc(int);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
void save_frame(void) {
  GLint viewport[4];
  int x, y, width, height;
  void *pixels;
  FILE *f;
  glGetIntegerv(GL_VIEWPORT, viewport);
  x = viewport[0];
  y = viewport[1];
  width = viewport[2];
  height = viewport[3];
  printf("saving %dx%d screenshot\n", width, height);
  pixels = malloc(width * height * 3);
  assert(pixels);
  memset(pixels, 0, width * height * 3);
  glReadPixels(x, y, width, height, GL_RGB, GL_UNSIGNED_BYTE, pixels);
  f = fopen("screenshot.ppm", "wb");
  fprintf(f, "P6\n%d %d\n255\n", width, height);
  for (y = height - 1; y >= 0; y--)
    fwrite((char *)pixels + y * width * 3, width, 3, f);
  fclose(f);
  free(pixels);
  printf("done\n");
}


x86 assembly (clang, -O0):
<save_frame>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   10d0 <glGetIntegerv@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
lea    0xe11(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x1c(%rbp),%eax
imul   -0x20(%rbp),%eax
imul   $0x3,%eax,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1070 <assert@plt>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%eax
imul   -0x20(%rbp),%eax
imul   $0x3,%eax,%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
mov    0x2db6(%rip),%rax
mov    (%rax),%r8d
mov    0x2d94(%rip),%rax
mov    (%rax),%r9d
mov    -0x28(%rbp),%rax
mov    %rax,(%rsp)
call   1090 <glReadPixels@plt>
lea    0xdbe(%rip),%rdi
lea    0xdc6(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
lea    0xdaf(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x20(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     12c6 <save_frame+0x116>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%eax
imul   -0x1c(%rbp),%eax
imul   $0x3,%eax,%eax
cltq
add    %rax,%rdi
mov    -0x1c(%rbp),%esi
mov    -0x30(%rbp),%rcx
mov    $0x3,%edx
call   10c0 <fwrite@plt>
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
jmp    128a <save_frame+0xda>
mov    -0x30(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
lea    0xd5a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_frame>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
lea    0x10(%rsp),%rsi
call   10e0 <glGetIntegerv@plt>
mov    0x10(%rsp),%r13d
mov    0x14(%rsp),%ebp
mov    0x18(%rsp),%r14d
mov    0x1c(%rsp),%r15d
lea    0xe05(%rip),%rdi
mov    %r14d,%esi
mov    %r15d,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14d,%eax
imul   %r15d,%eax
lea    (%rax,%rax,2),%ebx
mov    %ebx,%edi
call   10b0 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
call   1080 <assert@plt>
mov    %r12,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1070 <memset@plt>
mov    0x2db9(%rip),%rax
mov    (%rax),%r8d
mov    0x2d97(%rip),%rax
mov    (%rax),%r9d
mov    %r12,(%rsp)
mov    %r13d,%edi
mov    %ebp,%esi
mov    %r14d,%edx
mov    %r15d,%ecx
call   10a0 <glReadPixels@plt>
lea    0xdba(%rip),%rdi
lea    0xdc2(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,%r13
lea    0xdb6(%rip),%rsi
mov    %rax,%rdi
mov    %r14d,%edx
mov    %r15d,%ecx
call   1090 <fprintf@plt>
test   %r15d,%r15d
jle    12c4 <save_frame+0x104>
mov    %r15,%rbp
add    $0x1,%rbp
lea    -0x1(%r15),%eax
imul   %r14d,%eax
lea    (%rax,%rax,2),%ebx
lea    (%r14,%r14,2),%r15d
xchg   %ax,%ax
movslq %ebx,%rbx
lea    (%r12,%rbx,1),%rdi
mov    %r14d,%esi
mov    $0x3,%edx
mov    %r13,%rcx
call   10d0 <fwrite@plt>
add    $0xffffffffffffffff,%rbp
sub    %r15d,%ebx
cmp    $0x1,%rbp
ja     12a0 <save_frame+0xe0>
mov    %r13,%rdi
call   1050 <fclose@plt>
mov    %r12,%rdi
call   1030 <free@plt>
lea    0xd5e(%rip),%rdi
call   1040 <puts@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@GL_VIEWPORT = global i32 0
@.str = [25 x i8] c"saving %dx%d screenshot\0A\00"
@GL_RGB = global i32 0
@GL_UNSIGNED_BYTE = global i32 0
@.str.1 = [15 x i8] c"screenshot.ppm\00"
@.str.2 = [3 x i8] c"wb\00"
@.str.3 = [14 x i8] c"P6\0A%d %d\0A255\0A\00"
@.str.4 = [6 x i8] c"done\0A\00"
define void @save_frame() {
%1 = alloca [4 x i32]
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = load i32, i32* @GL_VIEWPORT
%9 = getelementptr inbounds [4 x i32], [4 x i32]* %1, i64 0, i64 0
%10 = call i32 @glGetIntegerv(i32 %8, i32* %9)
%11 = getelementptr inbounds [4 x i32], [4 x i32]* %1, i64 0, i64 0
%12 = load i32, i32* %11
store i32 %12, i32* %2
%13 = getelementptr inbounds [4 x i32], [4 x i32]* %1, i64 0, i64 1
%14 = load i32, i32* %13
store i32 %14, i32* %3
%15 = getelementptr inbounds [4 x i32], [4 x i32]* %1, i64 0, i64 2
%16 = load i32, i32* %15
store i32 %16, i32* %4
%17 = getelementptr inbounds [4 x i32], [4 x i32]* %1, i64 0, i64 3
%18 = load i32, i32* %17
store i32 %18, i32* %5
%19 = load i32, i32* %4
%20 = load i32, i32* %5
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %19, i32 %20)
%22 = load i32, i32* %4
%23 = load i32, i32* %5
%24 = mul nsw i32 %22, %23
%25 = mul nsw i32 %24, 3
%26 = call i8* @malloc(i32 %25)
store i8* %26, i8** %6
%27 = load i8*, i8** %6
%28 = call i32 @assert(i8* %27)
%29 = load i8*, i8** %6
%30 = load i32, i32* %4
%31 = load i32, i32* %5
%32 = mul nsw i32 %30, %31
%33 = mul nsw i32 %32, 3
%34 = call i32 @memset(i8* %29, i32 0, i32 %33)
%35 = load i32, i32* %2
%36 = load i32, i32* %3
%37 = load i32, i32* %4
%38 = load i32, i32* %5
%39 = load i32, i32* @GL_RGB
%40 = load i32, i32* @GL_UNSIGNED_BYTE
%41 = load i8*, i8** %6
%42 = call i32 @glReadPixels(i32 %35, i32 %36, i32 %37, i32 %38, i32 %39, i32 %40, i8* %41)
%43 = call i32* @fopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %43, i32** %7
%44 = load i32*, i32** %7
%45 = load i32, i32* %4
%46 = load i32, i32* %5
%47 = call i32 @fprintf(i32* %44, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %45, i32 %46)
%48 = load i32, i32* %5
%49 = sub nsw i32 %48, 1
store i32 %49, i32* %3
br label %50
50: ; preds = %64, %0
%51 = load i32, i32* %3
%52 = icmp sge i32 %51, 0
br i1 %52, label %53, label %67
53: ; preds = %50
%54 = load i8*, i8** %6
%55 = load i32, i32* %3
%56 = load i32, i32* %4
%57 = mul nsw i32 %55, %56
%58 = mul nsw i32 %57, 3
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %54, i64 %59
%61 = load i32, i32* %4
%62 = load i32*, i32** %7
%63 = call i32 @fwrite(i8* %60, i32 %61, i32 3, i32* %62)
br label %64
64: ; preds = %53
%65 = load i32, i32* %3
%66 = add nsw i32 %65, -1
store i32 %66, i32* %3
br label %50
67: ; preds = %50
%68 = load i32*, i32** %7
%69 = call i32 @fclose(i32* %68)
%70 = load i8*, i8** %6
%71 = call i32 @free(i8* %70)
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@GL_VIEWPORT = global i32 0
@.str = [25 x i8] c"saving %dx%d screenshot\0A\00"
@GL_RGB = global i32 0
@GL_UNSIGNED_BYTE = global i32 0
@.str.1 = [15 x i8] c"screenshot.ppm\00"
@.str.2 = [3 x i8] c"wb\00"
@.str.3 = [14 x i8] c"P6\0A%d %d\0A255\0A\00"
@str = [5 x i8] c"done\00"
define void @save_frame() {
%1 = alloca [4 x i32]
%2 = bitcast [4 x i32]* %1 to i8*
%3 = load i32, i32* @GL_VIEWPORT
%4 = getelementptr inbounds [4 x i32], [4 x i32]* %1, i64 0, i64 0
%5 = call i32 @glGetIntegerv(i32 %3, i32* %4)
%6 = load i32, i32* %4
%7 = getelementptr inbounds [4 x i32], [4 x i32]* %1, i64 0, i64 1
%8 = load i32, i32* %7
%9 = getelementptr inbounds [4 x i32], [4 x i32]* %1, i64 0, i64 2
%10 = load i32, i32* %9
%11 = getelementptr inbounds [4 x i32], [4 x i32]* %1, i64 0, i64 3
%12 = load i32, i32* %11
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %10, i32 %12)
%14 = mul i32 %10, 3
%15 = mul i32 %14, %12
%16 = call i8* @malloc(i32 %15)
%17 = call i32 @assert(i8* %16)
%18 = call i32 @memset(i8* %16, i32 0, i32 %15)
%19 = load i32, i32* @GL_RGB
%20 = load i32, i32* @GL_UNSIGNED_BYTE
%21 = call i32 @glReadPixels(i32 %6, i32 %8, i32 %10, i32 %12, i32 %19, i32 %20, i8* %16)
%22 = call i32* @fopen(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @fprintf(i32* %22, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %10, i32 %12)
%24 = icmp sgt i32 %12, 0
br i1 %24, label %25, label %36
25: ; preds = %0
%26 = zext i32 %12 to i64
br label %27
27: ; preds = %25, %27
%28 = phi i64 [ %26, %25 ], [ %29, %27 ]
%29 = add nsw i64 %28, -1
%30 = trunc i64 %29 to i32
%31 = mul i32 %14, %30
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %16, i64 %32
%34 = call i32 @fwrite(i8* %33, i32 %10, i32 3, i32* %22)
%35 = icmp ugt i64 %28, 1
br i1 %35, label %27, label %36
36: ; preds = %27, %0
%37 = call i32 @fclose(i32* %22)
%38 = call i32 @free(i8* %16)
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
ret void
}