#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 */
struct image {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ image_write_png(struct image *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ warn(char *, char *);
void write_sprite(struct image *image, char *outfile) {
  strcat(outfile, ".png");
  FILE *outfp = fopen(outfile, "wb");
  if (outfp != NULL) {
    if (image_write_png(image, outfp)) {
      warn("Error writing %s.", outfile);
    }
    fclose(outfp);
  } else {
    perror(outfile);
  }
}


x86 assembly (clang, -O0):
<write_sprite>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe85(%rip),%rsi
call   1080 <strcat@plt>
mov    -0x10(%rbp),%rdi
lea    0xe7a(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11d4 <write_sprite+0x74>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <image_write_png@plt>
cmp    $0x0,%rax
je     11c6 <write_sprite+0x66>
mov    -0x10(%rbp),%rsi
lea    0xe47(%rip),%rdi
call   1030 <warn@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
jmp    11dd <write_sprite+0x7d>
mov    -0x10(%rbp),%rdi
call   1070 <perror@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_sprite>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe8e(%rip),%rsi
mov    %r14,%rdi
call   1080 <strcat@plt>
lea    0xe84(%rip),%rsi
mov    %r14,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11bd <write_sprite+0x5d>
mov    %rax,%rbx
mov    %r15,%rdi
mov    %rax,%rsi
call   1040 <image_write_png@plt>
test   %rax,%rax
je     11b0 <write_sprite+0x50>
lea    0xe60(%rip),%rdi
mov    %r14,%rsi
call   1030 <warn@plt>
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1050 <fclose@plt>
mov    %r14,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1070 <perror@plt>

LLVM IR (-O0):
%struct.image = type { i32 }
@.str = [5 x i8] c".png\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [18 x i8] c"Error writing %s.\00"
define void @write_sprite(%struct.image* %0, i8* %1) {
%3 = alloca %struct.image*
%4 = alloca i8*
%5 = alloca i32*
store %struct.image* %0, %struct.image** %3
store i8* %1, i8** %4
%6 = load i8*, i8** %4
%7 = call i32 @strcat(i8* %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%8 = load i8*, i8** %4
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %5
%10 = load i32*, i32** %5
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %23
12: ; preds = %2
%13 = load %struct.image*, %struct.image** %3
%14 = load i32*, i32** %5
%15 = call i64 @image_write_png(%struct.image* %13, i32* %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = load i8*, i8** %4
%19 = call i32 @warn(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %18)
br label %20
20: ; preds = %17, %12
%21 = load i32*, i32** %5
%22 = call i32 @fclose(i32* %21)
br label %26
23: ; preds = %2
%24 = load i8*, i8** %4
%25 = call i32 @perror(i8* %24)
br label %26
26: ; preds = %23, %20
ret void
}

LLVM IR (-O2):
%struct.image = type { i32 }
@.str = [5 x i8] c".png\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [18 x i8] c"Error writing %s.\00"
define void @write_sprite(%struct.image* %0, i8* %1) {
%3 = tail call i32 @strcat(i8* %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%4 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %13, label %6
6: ; preds = %2
%7 = tail call i64 @image_write_png(%struct.image* %0, i32* %4)
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %6
%10 = tail call i32 @warn(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %1)
br label %11
11: ; preds = %9, %6
%12 = tail call i32 @fclose(i32* %4)
br label %15
13: ; preds = %2
%14 = tail call i32 @perror(i8* %1)
br label %15
15: ; preds = %13, %11
ret void
}