#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
unsigned char *stbi_write_png_to_mem(unsigned char *, int, int, int, int,
                                     int *);
int stbi_write_png(char const *filename, int x, int y, int comp,
                   const void *data, int stride_bytes) {
  FILE *f;
  int len;
  unsigned char *png = stbi_write_png_to_mem((unsigned char *)data,
                                             stride_bytes, x, y, comp, &len);
  if (!png)
    return 0;
  f = fopen(filename, "wb");
  if (!f) {
    free(png);
    return 0;
  }
  fwrite(png, 1, len, f);
  fclose(f);
  free(png);
  return 1;
}


x86 assembly (clang, -O0):
<stbi_write_png>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9d,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
mov    -0x1c(%rbp),%r8d
lea    -0x3c(%rbp),%r9
call   1050 <stbi_write_png_to_mem@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    11a2 <stbi_write_png+0x52>
movl   $0x0,-0x4(%rbp)
jmp    1204 <stbi_write_png+0xb4>
mov    -0x10(%rbp),%rdi
lea    0xe53(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11d6 <stbi_write_png+0x86>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    1204 <stbi_write_png+0xb4>
mov    -0x48(%rbp),%rdi
mov    -0x3c(%rbp),%edx
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
call   1070 <fwrite@plt>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stbi_write_png>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %r9d,%ebx
mov    %ecx,%eax
mov    %edx,%ecx
mov    %esi,%edx
mov    %rdi,%rbp
lea    0xc(%rsp),%r9
mov    %r8,%rdi
mov    %ebx,%esi
mov    %eax,%r8d
call   1050 <stbi_write_png_to_mem@plt>
test   %rax,%rax
je     11b9 <stbi_write_png+0x69>
mov    %rax,%rbx
lea    0xe7b(%rip),%rsi
mov    %rbp,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11be <stbi_write_png+0x6e>
mov    %rax,%rbp
mov    0xc(%rsp),%edx
mov    $0x1,%r14d
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %rax,%rcx
call   1070 <fwrite@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
jmp    11c1 <stbi_write_png+0x71>
xor    %r14d,%r14d
jmp    11c9 <stbi_write_png+0x79>
xor    %r14d,%r14d
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
define i32 @stbi_write_png(i8* %0, i32 %1, i32 %2, i32 %3, i8* %4, i32 %5) {
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32*
%15 = alloca i32
%16 = alloca i8*
store i8* %0, i8** %8
store i32 %1, i32* %9
store i32 %2, i32* %10
store i32 %3, i32* %11
store i8* %4, i8** %12
store i32 %5, i32* %13
%17 = load i8*, i8** %12
%18 = load i32, i32* %13
%19 = load i32, i32* %9
%20 = load i32, i32* %10
%21 = load i32, i32* %11
%22 = call i8* @stbi_write_png_to_mem(i8* %17, i32 %18, i32 %19, i32 %20, i32 %21, i32* %15)
store i8* %22, i8** %16
%23 = load i8*, i8** %16
%24 = icmp ne i8* %23, null
br i1 %24, label %26, label %25
25: ; preds = %6
store i32 0, i32* %7
br label %43
26: ; preds = %6
%27 = load i8*, i8** %8
%28 = call i32* @fopen(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %28, i32** %14
%29 = load i32*, i32** %14
%30 = icmp ne i32* %29, null
br i1 %30, label %34, label %31
31: ; preds = %26
%32 = load i8*, i8** %16
%33 = call i32 @free(i8* %32)
store i32 0, i32* %7
br label %43
34: ; preds = %26
%35 = load i8*, i8** %16
%36 = load i32, i32* %15
%37 = load i32*, i32** %14
%38 = call i32 @fwrite(i8* %35, i32 1, i32 %36, i32* %37)
%39 = load i32*, i32** %14
%40 = call i32 @fclose(i32* %39)
%41 = load i8*, i8** %16
%42 = call i32 @free(i8* %41)
store i32 1, i32* %7
br label %43
43: ; preds = %34, %31, %25
%44 = load i32, i32* %7
ret i32 %44
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
define i32 @stbi_write_png(i8* nocapture %0, i32 %1, i32 %2, i32 %3, i8* %4, i32 %5) {
%7 = alloca i32
%8 = bitcast i32* %7 to i8*
%9 = call i8* @stbi_write_png_to_mem(i8* %4, i32 %5, i32 %1, i32 %2, i32 %3, i32* %7)
%10 = icmp eq i8* %9, null
br i1 %10, label %21, label %11
11: ; preds = %6
%12 = call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %18, label %14
14: ; preds = %11
%15 = load i32, i32* %7
%16 = call i32 @fwrite(i8* %9, i32 1, i32 %15, i32* %12)
%17 = call i32 @fclose(i32* %12)
br label %18
18: ; preds = %11, %14
%19 = phi i32 [ 1, %14 ], [ 0, %11 ]
%20 = call i32 @free(i8* %9)
br label %21
21: ; preds = %18, %6
%22 = phi i32 [ 0, %6 ], [ %19, %18 ]
ret i32 %22
}