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

/* Variables and functions */
int /*<<< orphan*/ *__simplePNG__sfopen(char const *, char *);
int /*<<< orphan*/ __simplePNG_write_IDAT(int /*<<< orphan*/ *,
                                          int /*<<< orphan*/,
                                          int /*<<< orphan*/, int const *);
int /*<<< orphan*/ __simplePNG_write_IEND(int /*<<< orphan*/ *);
int /*<<< orphan*/ __simplePNG_write_IHDR(int /*<<< orphan*/ *,
                                          int /*<<< orphan*/,
                                          int /*<<< orphan*/, int const *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(int *, int, int, int /*<<< orphan*/ *);
int simplePNG_write(char const *filename, uint32_t width, uint32_t height,
                    uint8_t const *rgb_image) {
  FILE *f = __simplePNG__sfopen(filename, "wb");
  uint8_t png_header[8] = {0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a};
  fwrite(png_header, 1, 8, f);
  __simplePNG_write_IHDR(f, width, height, rgb_image);
  __simplePNG_write_IDAT(f, width, height, rgb_image);
  __simplePNG_write_IEND(f);
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<simplePNG_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe7f(%rip),%rsi
call   1040 <__simplePNG__sfopen@plt>
mov    %rax,-0x20(%rbp)
mov    0xe7f(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xe7c(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0xe79(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe76(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x8,%edx
call   1080 <fwrite@plt>
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x18(%rbp),%rcx
call   1030 <__simplePNG_write_IHDR@plt>
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
mov    -0x18(%rbp),%rcx
call   1070 <__simplePNG_write_IDAT@plt>
mov    -0x20(%rbp),%rdi
call   1060 <__simplePNG_write_IEND@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<simplePNG_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rcx,%r14
mov    %edx,%r15d
mov    %esi,%ebp
lea    0xe87(%rip),%rsi
call   1040 <__simplePNG__sfopen@plt>
mov    %rax,%rbx
movaps 0xe98(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movaps 0xe7c(%rip),%xmm0
movaps %xmm0,(%rsp)
mov    %rsp,%rdi
mov    $0x1,%esi
mov    $0x8,%edx
mov    %rax,%rcx
call   1080 <fwrite@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r15d,%edx
mov    %r14,%rcx
call   1030 <__simplePNG_write_IHDR@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r15d,%edx
mov    %r14,%rcx
call   1070 <__simplePNG_write_IDAT@plt>
mov    %rbx,%rdi
call   1060 <__simplePNG_write_IEND@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@__const.simplePNG_write.png_header = [8 x i32] [i32 137, i32 80, i32 78, i32 71, i32 13, i32 10, i32 26, i32 10]
define i32 @simplePNG_write(i8* %0, i32 %1, i32 %2, i32* %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca [8 x i32]
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32* %3, i32** %8
%11 = load i8*, i8** %5
%12 = call i32* @__simplePNG__sfopen(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %9
%13 = bitcast [8 x i32]* %10 to i8*
%14 = getelementptr inbounds [8 x i32], [8 x i32]* %10, i64 0, i64 0
%15 = load i32*, i32** %9
%16 = call i32 @fwrite(i32* %14, i32 1, i32 8, i32* %15)
%17 = load i32*, i32** %9
%18 = load i32, i32* %6
%19 = load i32, i32* %7
%20 = load i32*, i32** %8
%21 = call i32 @__simplePNG_write_IHDR(i32* %17, i32 %18, i32 %19, i32* %20)
%22 = load i32*, i32** %9
%23 = load i32, i32* %6
%24 = load i32, i32* %7
%25 = load i32*, i32** %8
%26 = call i32 @__simplePNG_write_IDAT(i32* %22, i32 %23, i32 %24, i32* %25)
%27 = load i32*, i32** %9
%28 = call i32 @__simplePNG_write_IEND(i32* %27)
%29 = load i32*, i32** %9
%30 = call i32 @fclose(i32* %29)
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@__const.simplePNG_write.png_header = [8 x i32] [i32 137, i32 80, i32 78, i32 71, i32 13, i32 10, i32 26, i32 10]
define i32 @simplePNG_write(i8* %0, i32 %1, i32 %2, i32* %3) {
%5 = alloca [8 x i32]
%6 = tail call i32* @__simplePNG__sfopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%7 = bitcast [8 x i32]* %5 to i8*
%8 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 0
%9 = call i32 @fwrite(i32* %8, i32 1, i32 8, i32* %6)
%10 = tail call i32 @__simplePNG_write_IHDR(i32* %6, i32 %1, i32 %2, i32* %3)
%11 = tail call i32 @__simplePNG_write_IDAT(i32* %6, i32 %1, i32 %2, i32* %3)
%12 = tail call i32 @__simplePNG_write_IEND(i32* %6)
%13 = tail call i32 @fclose(i32* %6)
ret i32 0
}