#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 fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
int genC(int, int, int, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, int, int, int);
int create_source(int max_so, int max_chain, int max_depth) {
  int i, j, k, rval;
  FILE *fp, *fp_header;
  char filename[100];
  fp_header = fopen("FOO.h", "w+");
  fprintf(fp_header, "typedef long (*FOO_pt) (long);\n");
  fprintf(fp_header, "FOO_pt *FOO_array;\n");
  for (i = 0; i < max_so; i++) {
    for (j = 0; j < max_chain; j++) {
      for (k = 0; k < max_depth; k++) {
        sprintf(filename, "FOO_%03d_%03d_%03d.c", i, j, k);
        fp = fopen(filename, "w+");
        rval = genC(i, j, k, fp, fp_header);
        rval = fclose(fp);
      }
    }
  }
  rval = fclose(fp_header);
  return 0;
}


x86 assembly (clang, -O0):
<create_source>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
lea    0xe95(%rip),%rdi
lea    0xe94(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xe83(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x30(%rbp),%rdi
lea    0xe93(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1261 <create_source+0x111>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    124e <create_source+0xfe>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    123b <create_source+0xeb>
lea    -0xa0(%rbp),%rdi
mov    -0x10(%rbp),%edx
mov    -0x14(%rbp),%ecx
mov    -0x18(%rbp),%r8d
lea    0xe51(%rip),%rsi
call   1070 <sprintf@plt>
lea    -0xa0(%rbp),%rdi
lea    0xe07(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%edi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    -0x30(%rbp),%r8
call   1040 <genC@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11c8 <create_source+0x78>
jmp    1240 <create_source+0xf0>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11b5 <create_source+0x65>
jmp    1253 <create_source+0x103>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11a2 <create_source+0x52>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
mov    %eax,-0x1c(%rbp)
xor    %eax,%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_source>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    %edx,0x10(%rsp)
mov    %esi,0x8(%rsp)
mov    %edi,%r14d
lea    0xe8d(%rip),%rdi
lea    0xe8c(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%rbx
lea    0xe80(%rip),%rdi
mov    $0x1f,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1070 <fwrite@plt>
lea    0xe87(%rip),%rdi
mov    $0x13,%esi
mov    $0x1,%edx
mov    %rbx,0x18(%rsp)
mov    %rbx,%rcx
call   1070 <fwrite@plt>
mov    %r14d,0x14(%rsp)
test   %r14d,%r14d
jle    1286 <create_source+0x136>
xor    %eax,%eax
lea    0xe6d(%rip),%r14
lea    0x20(%rsp),%rbx
lea    0xe2a(%rip),%r12
jmp    11f1 <create_source+0xa1>
xchg   %ax,%ax
mov    0xc(%rsp),%eax
add    $0x1,%eax
cmp    0x14(%rsp),%eax
je     1286 <create_source+0x136>
mov    %eax,0xc(%rsp)
cmpl   $0x0,0x8(%rsp)
jle    11e0 <create_source+0x90>
xor    %r13d,%r13d
jmp    121b <create_source+0xcb>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%r13d
cmp    0x8(%rsp),%r13d
je     11e0 <create_source+0x90>
cmpl   $0x0,0x10(%rsp)
mov    0xc(%rsp),%ebp
jle    1210 <create_source+0xc0>
xor    %r15d,%r15d
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
mov    %r13d,%ecx
mov    %r15d,%r8d
call   1060 <sprintf@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1050 <fopen@plt>
mov    %r12,%rbx
mov    %rax,%r12
mov    %ebp,%edi
mov    %r13d,%esi
mov    %r15d,%edx
mov    %rax,%rcx
mov    0x18(%rsp),%r8
call   1040 <genC@plt>
mov    %r12,%rdi
mov    %rbx,%r12
lea    0x20(%rsp),%rbx
call   1030 <fclose@plt>
add    $0x1,%r15d
cmp    %r15d,0x10(%rsp)
jne    1230 <create_source+0xe0>
jmp    1210 <create_source+0xc0>
mov    0x18(%rsp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"FOO.h\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [32 x i8] c"typedef long (*FOO_pt) (long);\0A\00"
@.str.3 = [20 x i8] c"FOO_pt *FOO_array;\0A\00"
@.str.4 = [21 x i8] c"FOO_%03d_%03d_%03d.c\00"
define i32 @create_source(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i32*
%13 = alloca [100 x i8]
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%14 = call i32* @fopen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %14, i32** %12
%15 = load i32*, i32** %12
%16 = call i32 @fprintf(i32* %15, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0))
%17 = load i32*, i32** %12
%18 = call i32 @fprintf(i32* %17, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %7
br label %19
19: ; preds = %57, %3
%20 = load i32, i32* %7
%21 = load i32, i32* %4
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %60
23: ; preds = %19
store i32 0, i32* %8
br label %24
24: ; preds = %53, %23
%25 = load i32, i32* %8
%26 = load i32, i32* %5
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %56
28: ; preds = %24
store i32 0, i32* %9
br label %29
29: ; preds = %49, %28
%30 = load i32, i32* %9
%31 = load i32, i32* %6
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %52
33: ; preds = %29
%34 = getelementptr inbounds [100 x i8], [100 x i8]* %13, i64 0, i64 0
%35 = load i32, i32* %7
%36 = load i32, i32* %8
%37 = load i32, i32* %9
%38 = call i32 @sprintf(i8* %34, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32 %35, i32 %36, i32 %37)
%39 = getelementptr inbounds [100 x i8], [100 x i8]* %13, i64 0, i64 0
%40 = call i32* @fopen(i8* %39, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %40, i32** %11
%41 = load i32, i32* %7
%42 = load i32, i32* %8
%43 = load i32, i32* %9
%44 = load i32*, i32** %11
%45 = load i32*, i32** %12
%46 = call i32 @genC(i32 %41, i32 %42, i32 %43, i32* %44, i32* %45)
store i32 %46, i32* %10
%47 = load i32*, i32** %11
%48 = call i32 @fclose(i32* %47)
store i32 %48, i32* %10
br label %49
49: ; preds = %33
%50 = load i32, i32* %9
%51 = add nsw i32 %50, 1
store i32 %51, i32* %9
br label %29
52: ; preds = %29
br label %53
53: ; preds = %52
%54 = load i32, i32* %8
%55 = add nsw i32 %54, 1
store i32 %55, i32* %8
br label %24
56: ; preds = %24
br label %57
57: ; preds = %56
%58 = load i32, i32* %7
%59 = add nsw i32 %58, 1
store i32 %59, i32* %7
br label %19
60: ; preds = %19
%61 = load i32*, i32** %12
%62 = call i32 @fclose(i32* %61)
store i32 %62, i32* %10
ret i32 0
}

LLVM IR (-O2):
@.str = [6 x i8] c"FOO.h\00"
@.str.1 = [3 x i8] c"w+\00"
@.str.2 = [32 x i8] c"typedef long (*FOO_pt) (long);\0A\00"
@.str.3 = [20 x i8] c"FOO_pt *FOO_array;\0A\00"
@.str.4 = [21 x i8] c"FOO_%03d_%03d_%03d.c\00"
define i32 @create_source(i32 %0, i32 %1, i32 %2) {
%4 = alloca [100 x i8]
%5 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%6 = tail call i32* @fopen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%7 = tail call i64 @fwrite(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i64 31, i64 1, i32* %6)
%8 = tail call i64 @fwrite(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i64 19, i64 1, i32* %6)
%9 = icmp sgt i32 %0, 0
br i1 %9, label %10, label %31
10: ; preds = %3
%11 = icmp sgt i32 %1, 0
%12 = icmp sgt i32 %2, 0
br label %13
13: ; preds = %10, %28
%14 = phi i32 [ 0, %10 ], [ %29, %28 ]
br i1 %11, label %15, label %28
15: ; preds = %13, %25
%16 = phi i32 [ %26, %25 ], [ 0, %13 ]
br i1 %12, label %17, label %25
17: ; preds = %15, %17
%18 = phi i32 [ %23, %17 ], [ 0, %15 ]
%19 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i32 %14, i32 %16, i32 %18)
%20 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%21 = tail call i32 @genC(i32 %14, i32 %16, i32 %18, i32* %20, i32* %6)
%22 = tail call i32 @fclose(i32* %20)
%23 = add nuw nsw i32 %18, 1
%24 = icmp eq i32 %23, %2
br i1 %24, label %25, label %17
25: ; preds = %17, %15
%26 = add nuw nsw i32 %16, 1
%27 = icmp eq i32 %26, %1
br i1 %27, label %28, label %15
28: ; preds = %25, %13
%29 = add nuw nsw i32 %14, 1
%30 = icmp eq i32 %29, %0
br i1 %30, label %31, label %13
31: ; preds = %28, %3
%32 = tail call i32 @fclose(i32* %6)
ret i32 0
}