#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 */
scalar_t__ EEXIST;
int /*<<< orphan*/ _IOFBF;
int asprintf(char **, char *, char *, int);
int /*<<< orphan*/ **calloc(int, int);
scalar_t__ errno;
int /*<<< orphan*/ fatal(char *, ...);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
char *malloc(int);
scalar_t__ mkdir(char *, int);
scalar_t__ setvbuf(int /*<<< orphan*/ *, char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ strerror(scalar_t__);
FILE **output_open(char *basename, int ct) {
  FILE **out = calloc(ct, sizeof(FILE *));
  char *filename;
  char *buf;
  if (mkdir(basename, 0777)) {
    if (errno != EEXIST)
      fatal("can't mkdir %s: %s", basename, strerror(errno));
  }
  for (int i = 0; i < ct; i++) {
    if (asprintf(&filename, "%s/%d", basename, i) == -1)
      fatal("asprintf() failed");
    out[i] = fopen(filename, "wb");
    if (!out[i])
      fatal("can't open %s: %s", filename, strerror(errno));
    buf = malloc(4194304);
    if (buf == NULL)
      fatal("malloc() failed");
    if (setvbuf(out[i], buf, _IOFBF, 4194304))
      fatal("setvbuf() failed: %s", strerror(errno));
    free(filename);
  }
  return out;
}


x86 assembly (clang, -O0):
<output_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
mov    $0x8,%esi
call   1050 <calloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
cmp    $0x0,%rax
je     1212 <output_open+0x82>
mov    0x2e09(%rip),%rax
mov    (%rax),%rax
mov    0x2e07(%rip),%rcx
cmp    (%rcx),%rax
je     120d <output_open+0x7d>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2de7(%rip),%rax
mov    (%rax),%rdi
call   10a0 <strerror@plt>
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
lea    0xdfa(%rip),%rdi
mov    $0x0,%al
call   10b0 <fatal@plt>
jmp    1212 <output_open+0x82>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    133a <output_open+0x1aa>
mov    -0x8(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
lea    -0x20(%rbp),%rdi
lea    0xddc(%rip),%rsi
call   1070 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    1253 <output_open+0xc3>
lea    0xdcd(%rip),%rdi
mov    $0x0,%al
call   10b0 <fatal@plt>
mov    -0x20(%rbp),%rdi
lea    0xdcd(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%rdx
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
jne    12b0 <output_open+0x120>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d44(%rip),%rax
mov    (%rax),%rdi
call   10a0 <strerror@plt>
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
lea    0xd85(%rip),%rdi
mov    $0x0,%al
call   10b0 <fatal@plt>
mov    $0x400000,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12d7 <output_open+0x147>
lea    0xd70(%rip),%rdi
mov    $0x0,%al
call   10b0 <fatal@plt>
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x28(%rbp),%rsi
mov    0x2ce2(%rip),%rax
mov    (%rax),%edx
mov    $0x400000,%ecx
call   1080 <setvbuf@plt>
cmp    $0x0,%rax
je     1323 <output_open+0x193>
mov    0x2ccd(%rip),%rax
mov    (%rax),%rdi
call   10a0 <strerror@plt>
mov    %eax,%esi
lea    0xd34(%rip),%rdi
mov    $0x0,%al
call   10b0 <fatal@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1219 <output_open+0x89>
mov    -0x18(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<output_open>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %esi,%r14d
mov    %rdi,%rbp
mov    %esi,%edi
mov    $0x8,%esi
call   1050 <calloc@plt>
mov    %rax,%rbx
mov    %rbp,0x10(%rsp)
mov    %rbp,%rdi
mov    $0x1ff,%esi
call   1040 <mkdir@plt>
test   %rax,%rax
je     11fa <output_open+0x6a>
mov    0x2e07(%rip),%rax
mov    (%rax),%rdi
mov    0x2e05(%rip),%rax
cmp    (%rax),%rdi
je     11fa <output_open+0x6a>
call   10a0 <strerror@plt>
lea    0xe14(%rip),%rdi
mov    0x10(%rsp),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   10b0 <fatal@plt>
test   %r14d,%r14d
jle    1315 <output_open+0x185>
mov    %r14d,%r14d
lea    0xe06(%rip),%rbp
xor    %r13d,%r13d
jmp    1237 <output_open+0xa7>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
add    $0x1,%r13
cmp    %r13,%r14
je     1315 <output_open+0x185>
lea    0x8(%rsp),%rdi
mov    %rbp,%rsi
mov    0x10(%rsp),%rdx
mov    %r13d,%ecx
call   1070 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    125f <output_open+0xcf>
lea    0xdc1(%rip),%rdi
xor    %eax,%eax
call   10b0 <fatal@plt>
mov    0x8(%rsp),%rdi
lea    0xdc0(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%r15
mov    %rax,(%rbx,%r13,8)
test   %rax,%rax
jne    12af <output_open+0x11f>
mov    %rbx,%r12
mov    %rbp,%rbx
mov    0x8(%rsp),%rbp
mov    0x2d4a(%rip),%rax
mov    (%rax),%rdi
call   10a0 <strerror@plt>
lea    0xd91(%rip),%rdi
mov    %rbp,%rsi
mov    %rbx,%rbp
mov    %r12,%rbx
mov    %eax,%edx
xor    %eax,%eax
call   10b0 <fatal@plt>
mov    $0x400000,%edi
call   1060 <malloc@plt>
mov    %rax,%r12
test   %rax,%rax
jne    12cf <output_open+0x13f>
lea    0xd78(%rip),%rdi
xor    %eax,%eax
call   10b0 <fatal@plt>
mov    0x2cfa(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
mov    %r12,%rsi
mov    $0x400000,%ecx
call   1080 <setvbuf@plt>
test   %rax,%rax
je     1220 <output_open+0x90>
mov    0x2ce0(%rip),%rax
mov    (%rax),%rdi
call   10a0 <strerror@plt>
lea    0xd49(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   10b0 <fatal@plt>
jmp    1220 <output_open+0x90>
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EEXIST = global i64 0
@.str = [19 x i8] c"can't mkdir %s: %s\00"
@.str.1 = [6 x i8] c"%s/%d\00"
@.str.2 = [18 x i8] c"asprintf() failed\00"
@.str.3 = [3 x i8] c"wb\00"
@.str.4 = [18 x i8] c"can't open %s: %s\00"
@.str.5 = [16 x i8] c"malloc() failed\00"
@_IOFBF = global i32 0
@.str.6 = [21 x i8] c"setvbuf() failed: %s\00"
define i32** @output_open(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%9 = load i32, i32* %4
%10 = call i32** @calloc(i32 %9, i32 8)
store i32** %10, i32*** %5
%11 = load i8*, i8** %3
%12 = call i64 @mkdir(i8* %11, i32 511)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %24
14: ; preds = %2
%15 = load i64, i64* @errno
%16 = load i64, i64* @EEXIST
%17 = icmp ne i64 %15, %16
br i1 %17, label %18, label %23
18: ; preds = %14
%19 = load i8*, i8** %3
%20 = load i64, i64* @errno
%21 = call i32 @strerror(i64 %20)
%22 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %19, i32 %21)
br label %23
23: ; preds = %18, %14
br label %24
24: ; preds = %23, %2
store i32 0, i32* %8
br label %25
25: ; preds = %77, %24
%26 = load i32, i32* %8
%27 = load i32, i32* %4
%28 = icmp slt i32 %26, %27
br i1 %28, label %29, label %80
29: ; preds = %25
%30 = load i8*, i8** %3
%31 = load i32, i32* %8
%32 = call i32 @asprintf(i8** %6, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %30, i32 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %36
34: ; preds = %29
%35 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %36
36: ; preds = %34, %29
%37 = load i8*, i8** %6
%38 = call i32* @fopen(i8* %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%39 = load i32**, i32*** %5
%40 = load i32, i32* %8
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i32*, i32** %39, i64 %41
store i32* %38, i32** %42
%43 = load i32**, i32*** %5
%44 = load i32, i32* %8
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i32*, i32** %43, i64 %45
%47 = load i32*, i32** %46
%48 = icmp ne i32* %47, null
br i1 %48, label %54, label %49
49: ; preds = %36
%50 = load i8*, i8** %6
%51 = load i64, i64* @errno
%52 = call i32 @strerror(i64 %51)
%53 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %50, i32 %52)
br label %54
54: ; preds = %49, %36
%55 = call i8* @malloc(i32 4194304)
store i8* %55, i8** %7
%56 = load i8*, i8** %7
%57 = icmp eq i8* %56, null
br i1 %57, label %58, label %60
58: ; preds = %54
%59 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
br label %60
60: ; preds = %58, %54
%61 = load i32**, i32*** %5
%62 = load i32, i32* %8
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i32*, i32** %61, i64 %63
%65 = load i32*, i32** %64
%66 = load i8*, i8** %7
%67 = load i32, i32* @_IOFBF
%68 = call i64 @setvbuf(i32* %65, i8* %66, i32 %67, i32 4194304)
%69 = icmp ne i64 %68, 0
br i1 %69, label %70, label %74
70: ; preds = %60
%71 = load i64, i64* @errno
%72 = call i32 @strerror(i64 %71)
%73 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i32 %72)
br label %74
74: ; preds = %70, %60
%75 = load i8*, i8** %6
%76 = call i32 @free(i8* %75)
br label %77
77: ; preds = %74
%78 = load i32, i32* %8
%79 = add nsw i32 %78, 1
store i32 %79, i32* %8
br label %25
80: ; preds = %25
%81 = load i32**, i32*** %5
ret i32** %81
}

LLVM IR (-O2):
@errno = global i64 0
@EEXIST = global i64 0
@.str = [19 x i8] c"can't mkdir %s: %s\00"
@.str.1 = [6 x i8] c"%s/%d\00"
@.str.2 = [18 x i8] c"asprintf() failed\00"
@.str.3 = [3 x i8] c"wb\00"
@.str.4 = [18 x i8] c"can't open %s: %s\00"
@.str.5 = [16 x i8] c"malloc() failed\00"
@_IOFBF = global i32 0
@.str.6 = [21 x i8] c"setvbuf() failed: %s\00"
define noalias i32** @output_open(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = tail call i32** @calloc(i32 %1, i32 8)
%5 = bitcast i8** %3 to i8*
%6 = tail call i64 @mkdir(i8* %0, i32 511)
%7 = icmp eq i64 %6, 0
br i1 %7, label %15, label %8
8: ; preds = %2
%9 = load i64, i64* @errno
%10 = load i64, i64* @EEXIST
%11 = icmp eq i64 %9, %10
br i1 %11, label %15, label %12
12: ; preds = %8
%13 = tail call i32 @strerror(i64 %9)
%14 = tail call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %13)
br label %15
15: ; preds = %8, %12, %2
%16 = icmp sgt i32 %1, 0
br i1 %16, label %17, label %19
17: ; preds = %15
%18 = zext i32 %1 to i64
br label %20
19: ; preds = %50, %15
ret i32** %4
20: ; preds = %17, %50
%21 = phi i64 [ 0, %17 ], [ %53, %50 ]
%22 = trunc i64 %21 to i32
%23 = call i32 @asprintf(i8** %3, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %27
27: ; preds = %25, %20
%28 = load i8*, i8** %3
%29 = call i32* @fopen(i8* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%30 = getelementptr inbounds i32*, i32** %4, i64 %21
store i32* %29, i32** %30
%31 = icmp eq i32* %29, null
br i1 %31, label %32, label %37
32: ; preds = %27
%33 = load i8*, i8** %3
%34 = load i64, i64* @errno
%35 = call i32 @strerror(i64 %34)
%36 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %33, i32 %35)
br label %37
37: ; preds = %32, %27
%38 = call dereferenceable_or_null(4194304) i8* @malloc(i32 4194304)
%39 = icmp eq i8* %38, null
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
br label %42
42: ; preds = %40, %37
%43 = load i32, i32* @_IOFBF
%44 = call i64 @setvbuf(i32* %29, i8* %38, i32 %43, i32 4194304)
%45 = icmp eq i64 %44, 0
br i1 %45, label %50, label %46
46: ; preds = %42
%47 = load i64, i64* @errno
%48 = call i32 @strerror(i64 %47)
%49 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i32 %48)
br label %50
50: ; preds = %46, %42
%51 = load i8*, i8** %3
%52 = call i32 @free(i8* %51)
%53 = add nuw nsw i64 %21, 1
%54 = icmp eq i64 %53, %18
br i1 %54, label %19, label %20
}