#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int rand();
int main() {
  const char *fnames[] = {
      "C:\\a.txt", "C:\\b.txt", "C:\\c.txt", "C:\\d.txt", "C:\\e.txt",
  };
  FILE *fp[3];
  for (int i = 0; i < 5; i++) {
    fp[i] = fopen(fnames[i], "w");
  }
  for (int i = 0; i < 20; i++) {
    fprintf(fp[rand() % 5], "Hello %d\n", i);
  }
  for (int i = 0; i < 20; i++) {
    int idx = rand() % 5;
    if (fp[idx] != NULL) {
      fclose(fp[idx]);
      fp[idx] = NULL;
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
lea    0xe8a(%rip),%rax
mov    %rax,-0x30(%rbp)
lea    0xe88(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    0xe86(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    0xe84(%rip),%rax
mov    %rax,-0x18(%rbp)
lea    0xe82(%rip),%rax
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x54(%rbp)
cmpl   $0x5,-0x54(%rbp)
jge    11e6 <main+0x96>
movslq -0x54(%rbp),%rax
mov    -0x30(%rbp,%rax,8),%rdi
lea    0xe66(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rcx
movslq -0x54(%rbp),%rax
mov    %rcx,-0x50(%rbp,%rax,8)
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    11ad <main+0x5d>
movl   $0x0,-0x58(%rbp)
cmpl   $0x14,-0x58(%rbp)
jge    122b <main+0xdb>
mov    $0x0,%al
call   1070 <rand@plt>
mov    $0x5,%ecx
cltd
idiv   %ecx
movslq %edx,%rax
mov    -0x50(%rbp,%rax,8),%rdi
mov    -0x58(%rbp),%edx
lea    0xe17(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x58(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x58(%rbp)
jmp    11ed <main+0x9d>
movl   $0x0,-0x5c(%rbp)
cmpl   $0x14,-0x5c(%rbp)
jge    128c <main+0x13c>
mov    $0x0,%al
call   1070 <rand@plt>
mov    $0x5,%ecx
cltd
idiv   %ecx
mov    %edx,-0x60(%rbp)
movslq -0x60(%rbp),%rax
cmpq   $0x0,-0x50(%rbp,%rax,8)
je     1279 <main+0x129>
movslq -0x60(%rbp),%rax
mov    -0x50(%rbp,%rax,8),%rdi
call   1030 <fclose@plt>
movslq -0x60(%rbp),%rax
movq   $0x0,-0x50(%rbp,%rax,8)
jmp    127e <main+0x12e>
mov    -0x5c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x5c(%rbp)
jmp    1232 <main+0xe2>
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
lea    0xeb3(%rip),%rdi
lea    0xed9(%rip),%rbx
mov    %rbx,%rsi
call   1050 <fopen@plt>
mov    %rax,(%rsp)
lea    0xea2(%rip),%rdi
mov    %rbx,%rsi
call   1050 <fopen@plt>
mov    %rax,0x8(%rsp)
lea    0xe97(%rip),%rdi
mov    %rbx,%rsi
call   1050 <fopen@plt>
mov    %rax,0x10(%rsp)
lea    0xe8c(%rip),%rdi
mov    %rbx,%rsi
call   1050 <fopen@plt>
mov    %rax,0x18(%rsp)
lea    0xe81(%rip),%rdi
mov    %rbx,%rsi
call   1050 <fopen@plt>
mov    %rax,0x20(%rsp)
lea    0xe78(%rip),%rbx
xor    %ebp,%ebp
nopl   0x0(%rax)
xor    %eax,%eax
call   1060 <rand@plt>
cltq
imul   $0x66666667,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x21,%rcx
add    %edx,%ecx
lea    (%rcx,%rcx,4),%ecx
sub    %ecx,%eax
cltq
mov    (%rsp,%rax,8),%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <fprintf@plt>
add    $0x1,%ebp
cmp    $0x14,%ebp
jne    11c0 <main+0x80>
mov    $0x14,%ebx
jmp    1215 <main+0xd5>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0xffffffff,%ebx
je     1252 <main+0x112>
xor    %eax,%eax
call   1060 <rand@plt>
cltq
imul   $0x66666667,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x21,%rcx
add    %edx,%ecx
lea    (%rcx,%rcx,4),%ecx
sub    %ecx,%eax
movslq %eax,%rbp
mov    (%rsp,%rbp,8),%rdi
test   %rdi,%rdi
je     1210 <main+0xd0>
call   1030 <fclose@plt>
movq   $0x0,(%rsp,%rbp,8)
jmp    1210 <main+0xd0>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"C:\\a.txt\00"
@.str.1 = [9 x i8] c"C:\\b.txt\00"
@.str.2 = [9 x i8] c"C:\\c.txt\00"
@.str.3 = [9 x i8] c"C:\\d.txt\00"
@.str.4 = [9 x i8] c"C:\\e.txt\00"
@.str.5 = [2 x i8] c"w\00"
@.str.6 = [10 x i8] c"Hello %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [5 x i8*]
%3 = alloca [3 x i32*]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
%8 = bitcast [5 x i8*]* %2 to i8*
%9 = bitcast i8* %8 to [5 x i8*]*
%10 = getelementptr inbounds [5 x i8*], [5 x i8*]* %9, i32 0, i32 0
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i32 0, i32 0), i8** %10
%11 = getelementptr inbounds [5 x i8*], [5 x i8*]* %9, i32 0, i32 1
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i32 0, i32 0), i8** %11
%12 = getelementptr inbounds [5 x i8*], [5 x i8*]* %9, i32 0, i32 2
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i32 0, i32 0), i8** %12
%13 = getelementptr inbounds [5 x i8*], [5 x i8*]* %9, i32 0, i32 3
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i32 0, i32 0), i8** %13
%14 = getelementptr inbounds [5 x i8*], [5 x i8*]* %9, i32 0, i32 4
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i32 0, i32 0), i8** %14
store i32 0, i32* %4
br label %15
15: ; preds = %27, %0
%16 = load i32, i32* %4
%17 = icmp slt i32 %16, 5
br i1 %17, label %18, label %30
18: ; preds = %15
%19 = load i32, i32* %4
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [5 x i8*], [5 x i8*]* %2, i64 0, i64 %20
%22 = load i8*, i8** %21
%23 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%24 = load i32, i32* %4
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [3 x i32*], [3 x i32*]* %3, i64 0, i64 %25
store i32* %23, i32** %26
br label %27
27: ; preds = %18
%28 = load i32, i32* %4
%29 = add nsw i32 %28, 1
store i32 %29, i32* %4
br label %15
30: ; preds = %15
store i32 0, i32* %5
br label %31
31: ; preds = %42, %30
%32 = load i32, i32* %5
%33 = icmp slt i32 %32, 20
br i1 %33, label %34, label %45
34: ; preds = %31
%35 = call i32 (...) @rand()
%36 = srem i32 %35, 5
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds [3 x i32*], [3 x i32*]* %3, i64 0, i64 %37
%39 = load i32*, i32** %38
%40 = load i32, i32* %5
%41 = call i32 @fprintf(i32* %39, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i32 %40)
br label %42
42: ; preds = %34
%43 = load i32, i32* %5
%44 = add nsw i32 %43, 1
store i32 %44, i32* %5
br label %31
45: ; preds = %31
store i32 0, i32* %6
br label %46
46: ; preds = %67, %45
%47 = load i32, i32* %6
%48 = icmp slt i32 %47, 20
br i1 %48, label %49, label %70
49: ; preds = %46
%50 = call i32 (...) @rand()
%51 = srem i32 %50, 5
store i32 %51, i32* %7
%52 = load i32, i32* %7
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds [3 x i32*], [3 x i32*]* %3, i64 0, i64 %53
%55 = load i32*, i32** %54
%56 = icmp ne i32* %55, null
br i1 %56, label %57, label %66
57: ; preds = %49
%58 = load i32, i32* %7
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds [3 x i32*], [3 x i32*]* %3, i64 0, i64 %59
%61 = load i32*, i32** %60
%62 = call i32 @fclose(i32* %61)
%63 = load i32, i32* %7
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds [3 x i32*], [3 x i32*]* %3, i64 0, i64 %64
store i32* null, i32** %65
br label %66
66: ; preds = %57, %49
br label %67
67: ; preds = %66
%68 = load i32, i32* %6
%69 = add nsw i32 %68, 1
store i32 %69, i32* %6
br label %46
70: ; preds = %46
%71 = load i32, i32* %1
ret i32 %71
}

LLVM IR (-O2):
@.str = [9 x i8] c"C:\\a.txt\00"
@.str.1 = [9 x i8] c"C:\\b.txt\00"
@.str.2 = [9 x i8] c"C:\\c.txt\00"
@.str.3 = [9 x i8] c"C:\\d.txt\00"
@.str.4 = [9 x i8] c"C:\\e.txt\00"
@.str.5 = [2 x i8] c"w\00"
@.str.6 = [10 x i8] c"Hello %d\0A\00"
define i32 @main() {
%1 = alloca [3 x i32*]
%2 = bitcast [3 x i32*]* %1 to i8*
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%4 = getelementptr inbounds [3 x i32*], [3 x i32*]* %1, i64 0, i64 0
store i32* %3, i32** %4
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%6 = getelementptr inbounds [3 x i32*], [3 x i32*]* %1, i64 0, i64 1
store i32* %5, i32** %6
%7 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%8 = getelementptr inbounds [3 x i32*], [3 x i32*]* %1, i64 0, i64 2
store i32* %7, i32** %8
%9 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%10 = getelementptr inbounds [3 x i32*], [3 x i32*]* %1, i64 0, i64 3
store i32* %9, i32** %10
%11 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%12 = getelementptr inbounds [3 x i32*], [3 x i32*]* %1, i64 0, i64 4
store i32* %11, i32** %12
br label %13
13: ; preds = %0, %13
%14 = phi i32 [ %21, %13 ], [ 0, %0 ]
%15 = tail call i32 (...) @rand()
%16 = srem i32 %15, 5
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds [3 x i32*], [3 x i32*]* %1, i64 0, i64 %17
%19 = load i32*, i32** %18
%20 = tail call i32 @fprintf(i32* %19, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.6, i64 0, i64 0), i32 %14)
%21 = add nuw nsw i32 %14, 1
%22 = icmp eq i32 %21, 20
br i1 %22, label %24, label %13
23: ; preds = %34
ret i32 0
24: ; preds = %13, %34
%25 = phi i32 [ %35, %34 ], [ 0, %13 ]
%26 = tail call i32 (...) @rand()
%27 = srem i32 %26, 5
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [3 x i32*], [3 x i32*]* %1, i64 0, i64 %28
%30 = load i32*, i32** %29
%31 = icmp eq i32* %30, null
br i1 %31, label %34, label %32
32: ; preds = %24
%33 = tail call i32 @fclose(i32* %30)
store i32* null, i32** %29
br label %34
34: ; preds = %32, %24
%35 = add nuw nsw i32 %25, 1
%36 = icmp eq i32 %35, 20
br i1 %36, label %23, label %24
}