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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fwrite(int /*<<< orphan*/ *, size_t, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
void dumpfile(int idx, uint8_t *from, size_t len) {
  char fname[32];
  sprintf(fname, "png%05d.png", idx);
  FILE *f = fopen(fname, "wb");
  if (f == NULL) {
    perror("fopen");
    return;
  }
  size_t wlen = fwrite(from, len, 1, f);
  if (wlen != 1) {
    perror("fwrite");
  }
  fclose(f);
}


x86 assembly (clang, -O0):
<dumpfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe8f(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x40(%rbp),%rdi
lea    0xe8b(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    11a6 <dumpfile+0x56>
lea    0xe73(%rip),%rdi
call   1050 <perror@plt>
jmp    11e0 <dumpfile+0x90>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x48(%rbp),%rcx
mov    $0x1,%edx
call   1070 <fwrite@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x1,-0x50(%rbp)
je     11d7 <dumpfile+0x87>
lea    0xe43(%rip),%rdi
call   1050 <perror@plt>
mov    -0x48(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dumpfile>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %edi,%edx
lea    0xe98(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1060 <sprintf@plt>
lea    0xe92(%rip),%rsi
mov    %rbx,%rdi
call   1040 <fopen@plt>
test   %rax,%rax
je     11b4 <dumpfile+0x64>
mov    %rax,%rbx
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0x1,%edx
mov    %rax,%rcx
call   1070 <fwrite@plt>
cmp    $0x1,%rax
jne    11c9 <dumpfile+0x79>
mov    %rbx,%rdi
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <fclose@plt>
lea    0xe54(%rip),%rdi
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
jmp    1050 <perror@plt>
lea    0xe45(%rip),%rdi
call   1050 <perror@plt>
jmp    11a3 <dumpfile+0x53>

LLVM IR (-O0):
@.str = [12 x i8] c"png%05d.png\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [6 x i8] c"fopen\00"
@.str.3 = [7 x i8] c"fwrite\00"
define void @dumpfile(i32 %0, i32* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i64
%7 = alloca [32 x i8]
%8 = alloca i32*
%9 = alloca i64
store i32 %0, i32* %4
store i32* %1, i32** %5
store i64 %2, i64* %6
%10 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%11 = load i32, i32* %4
%12 = call i32 @sprintf(i8* %10, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %14, i32** %8
%15 = load i32*, i32** %8
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %19
17: ; preds = %3
%18 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %31
19: ; preds = %3
%20 = load i32*, i32** %5
%21 = load i64, i64* %6
%22 = load i32*, i32** %8
%23 = call i64 @fwrite(i32* %20, i64 %21, i32 1, i32* %22)
store i64 %23, i64* %9
%24 = load i64, i64* %9
%25 = icmp ne i64 %24, 1
br i1 %25, label %26, label %28
26: ; preds = %19
%27 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %28
28: ; preds = %26, %19
%29 = load i32*, i32** %8
%30 = call i32 @fclose(i32* %29)
br label %31
31: ; preds = %28, %17
ret void
}

LLVM IR (-O2):
@.str = [12 x i8] c"png%05d.png\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [6 x i8] c"fopen\00"
@.str.3 = [7 x i8] c"fwrite\00"
define void @dumpfile(i32 %0, i32* nocapture %1, i64 %2) {
%4 = alloca [32 x i8]
%5 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%6 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %3
%10 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %18
11: ; preds = %3
%12 = tail call i64 @fwrite(i32* %1, i64 %2, i32 1, i32* %7)
%13 = icmp eq i64 %12, 1
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %16
16: ; preds = %14, %11
%17 = tail call i32 @fclose(i32* %7)
br label %18
18: ; preds = %16, %9
ret void
}