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

/* Variables and functions */
int O_CREAT;
int O_WRONLY;
int PATH_MAX;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int open(char *, int, int);
int /*<<< orphan*/ sprintf(char *, char *, char const *, unsigned int);
int write(int, void *, int);
int dump_to_file(const char *pathprefix, void *start, int len) {
  static unsigned int counter = 0;
  char fname[PATH_MAX];
  int outfd;
  sprintf(fname, "%s.%u", pathprefix, counter);
  counter++;
  outfd = open(fname, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
  if (!outfd)
    return -1;
  if (write(outfd, start, len) != len) {
    close(outfd);
    return -1;
  }
  close(outfd);
  return 1;
}


x86 assembly (clang, -O0):
<dump_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    0x2e8e(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x40(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdx
mov    0x2ebb(%rip),%ecx
lea    0xe70(%rip),%rsi
call   1060 <sprintf@plt>
mov    -0x40(%rbp),%rdi
mov    0x2ea5(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2e9c(%rip)
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
mov    0x2e40(%rip),%rax
or     (%rax),%esi
mov    0x2e07(%rip),%rax
mov    (%rax),%edx
mov    0x2e0e(%rip),%rax
or     (%rax),%edx
call   1050 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    11f1 <dump_to_file+0xb1>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    123a <dump_to_file+0xfa>
mov    -0x34(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
cmp    -0x1c(%rbp),%eax
je     1224 <dump_to_file+0xe4>
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    123a <dump_to_file+0xfa>
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dump_to_file>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%rdx
mov    0x2e8e(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x2ed1(%rip),%ecx
lea    0xe86(%rip),%rsi
mov    %rbx,%rdi
call   1060 <sprintf@plt>
addl   $0x1,0x2ebb(%rip)
mov    0x2e28(%rip),%rax
mov    0x2e61(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e26(%rip),%rax
mov    0x2e2f(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %rbx,%rdi
call   1050 <open@plt>
test   %eax,%eax
je     11e2 <dump_to_file+0xa2>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r14,%rsi
mov    %r15d,%edx
call   1030 <write@plt>
xor    %r14d,%r14d
cmp    %r15d,%eax
sete   %r14b
mov    %ebx,%edi
call   1040 <close@plt>
lea    (%r14,%r14,1),%eax
add    $0xffffffff,%eax
jmp    11e7 <dump_to_file+0xa7>
mov    $0xffffffff,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@dump_to_file.counter = internal global i32 0
@PATH_MAX = global i32 0
@.str = [6 x i8] c"%s.%u\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
define i32 @dump_to_file(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%12 = load i32, i32* @PATH_MAX
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %8
%15 = alloca i8, i64 %13
store i64 %13, i64* %9
%16 = load i8*, i8** %5
%17 = load i32, i32* @dump_to_file.counter
%18 = call i32 @sprintf(i8* %15, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %16, i32 %17)
%19 = load i32, i32* @dump_to_file.counter
%20 = add i32 %19, 1
store i32 %20, i32* @dump_to_file.counter
%21 = load i32, i32* @O_WRONLY
%22 = load i32, i32* @O_CREAT
%23 = or i32 %21, %22
%24 = load i32, i32* @S_IRUSR
%25 = load i32, i32* @S_IWUSR
%26 = or i32 %24, %25
%27 = call i32 @open(i8* %15, i32 %23, i32 %26)
store i32 %27, i32* %10
%28 = load i32, i32* %10
%29 = icmp ne i32 %28, 0
br i1 %29, label %31, label %30
30: ; preds = %3
store i32 -1, i32* %4
store i32 1, i32* %11
br label %44
31: ; preds = %3
%32 = load i32, i32* %10
%33 = load i8*, i8** %6
%34 = load i32, i32* %7
%35 = call i32 @write(i32 %32, i8* %33, i32 %34)
%36 = load i32, i32* %7
%37 = icmp ne i32 %35, %36
br i1 %37, label %38, label %41
38: ; preds = %31
%39 = load i32, i32* %10
%40 = call i32 @close(i32 %39)
store i32 -1, i32* %4
store i32 1, i32* %11
br label %44
41: ; preds = %31
%42 = load i32, i32* %10
%43 = call i32 @close(i32 %42)
store i32 1, i32* %4
store i32 1, i32* %11
br label %44
44: ; preds = %41, %38, %30
%45 = load i8*, i8** %8
%46 = load i32, i32* %4
ret i32 %46
}

LLVM IR (-O2):
@dump_to_file.counter = internal unnamed_addr global i32 0
@PATH_MAX = global i32 0
@.str = [6 x i8] c"%s.%u\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
define i32 @dump_to_file(i8* %0, i8* nocapture %1, i32 %2) {
%4 = load i32, i32* @PATH_MAX
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = load i32, i32* @dump_to_file.counter
%8 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %7)
%9 = load i32, i32* @dump_to_file.counter
%10 = add i32 %9, 1
store i32 %10, i32* @dump_to_file.counter
%11 = load i32, i32* @O_WRONLY
%12 = load i32, i32* @O_CREAT
%13 = or i32 %12, %11
%14 = load i32, i32* @S_IRUSR
%15 = load i32, i32* @S_IWUSR
%16 = or i32 %15, %14
%17 = call i32 @open(i8* %6, i32 %13, i32 %16)
%18 = icmp eq i32 %17, 0
br i1 %18, label %24, label %19
19: ; preds = %3
%20 = tail call i32 @write(i32 %17, i8* %1, i32 %2)
%21 = icmp eq i32 %20, %2
%22 = tail call i32 @close(i32 %17)
%23 = select i1 %21, i32 1, i32 -1
br label %24
24: ; preds = %19, %3
%25 = phi i32 [ -1, %3 ], [ %23, %19 ]
ret i32 %25
}