#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*/ eprintf(char *, char *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fshut(int /*<<< orphan*/ *, char *);
scalar_t__ itostr(char *, int /*<<< orphan*/, int);
FILE *nextfile(FILE *f, char *buf, int plen, int slen) {
  static int filecount = 0;
  if (f)
    fshut(f, "<file>");
  if (itostr(buf + plen, filecount++, slen) < 0)
    return NULL;
  if (!(f = fopen(buf, "w")))
    eprintf("'%s':", buf);
  return f;
}


x86 assembly (clang, -O0):
<nextfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1171 <nextfile+0x31>
mov    -0x10(%rbp),%rdi
lea    0xe94(%rip),%rsi
call   1030 <fshut@plt>
mov    -0x18(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
mov    0x2ec2(%rip),%esi
mov    %esi,%eax
add    $0x1,%eax
mov    %eax,0x2eb7(%rip)
mov    -0x20(%rbp),%edx
call   1060 <itostr@plt>
cmp    $0x0,%rax
jge    11ac <nextfile+0x6c>
movq   $0x0,-0x8(%rbp)
jmp    11e2 <nextfile+0xa2>
mov    -0x18(%rbp),%rdi
lea    0xe50(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11da <nextfile+0x9a>
mov    -0x18(%rbp),%rsi
lea    0xe34(%rip),%rdi
call   1040 <eprintf@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nextfile>:
push   %rbp
push   %r14
push   %rbx
mov    %ecx,%r14d
mov    %edx,%ebp
mov    %rsi,%rbx
test   %rdi,%rdi
je     115d <nextfile+0x1d>
lea    0xea8(%rip),%rsi
call   1030 <fshut@plt>
movslq %ebp,%rdi
add    %rbx,%rdi
mov    0x2edb(%rip),%esi
lea    0x1(%rsi),%eax
mov    %eax,0x2ed2(%rip)
mov    %r14d,%edx
call   1060 <itostr@plt>
test   %rax,%rax
js     11a2 <nextfile+0x62>
lea    0xe81(%rip),%rsi
mov    %rbx,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
jne    11a4 <nextfile+0x64>
lea    0xe6f(%rip),%rdi
mov    %rbx,%rsi
call   1040 <eprintf@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@nextfile.filecount = internal global i32 0
@.str = [7 x i8] c"<file>\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [6 x i8] c"'%s':\00"
define i32* @nextfile(i32* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i32* %0, i32** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%10 = load i32*, i32** %6
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %15
12: ; preds = %4
%13 = load i32*, i32** %6
%14 = call i32 @fshut(i32* %13, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %12, %4
%16 = load i8*, i8** %7
%17 = load i32, i32* %8
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i32, i32* @nextfile.filecount
%21 = add nsw i32 %20, 1
store i32 %21, i32* @nextfile.filecount
%22 = load i32, i32* %9
%23 = call i64 @itostr(i8* %19, i32 %20, i32 %22)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %15
store i32* null, i32** %5
br label %35
26: ; preds = %15
%27 = load i8*, i8** %7
%28 = call i32* @fopen(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %28, i32** %6
%29 = icmp ne i32* %28, null
br i1 %29, label %33, label %30
30: ; preds = %26
%31 = load i8*, i8** %7
%32 = call i32 @eprintf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %31)
br label %33
33: ; preds = %30, %26
%34 = load i32*, i32** %6
store i32* %34, i32** %5
br label %35
35: ; preds = %33, %25
%36 = load i32*, i32** %5
ret i32* %36
}

LLVM IR (-O2):
@nextfile.filecount = internal unnamed_addr global i32 0
@.str = [7 x i8] c"<file>\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [6 x i8] c"'%s':\00"
define noalias i32* @nextfile(i32* %0, i8* %1, i32 %2, i32 %3) {
%5 = icmp eq i32* %0, null
br i1 %5, label %8, label %6
6: ; preds = %4
%7 = tail call i32 @fshut(i32* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %6, %4
%9 = sext i32 %2 to i64
%10 = getelementptr inbounds i8, i8* %1, i64 %9
%11 = load i32, i32* @nextfile.filecount
%12 = add nsw i32 %11, 1
store i32 %12, i32* @nextfile.filecount
%13 = tail call i64 @itostr(i8* %10, i32 %11, i32 %3)
%14 = icmp slt i64 %13, 0
br i1 %14, label %20, label %15
15: ; preds = %8
%16 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = tail call i32 @eprintf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %1)
br label %20
20: ; preds = %15, %18, %8
%21 = phi i32* [ null, %8 ], [ null, %18 ], [ %16, %15 ]
ret i32* %21
}