#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*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ mkdir(char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char const *, ...);
int /*<<< orphan*/ stderr;
void printToFile(char const *argv[], int *list, int people, int numLines,
                 char rules[][450]) {
  FILE *filePtr;
  int i;
  char tempName[100];
  sprintf(tempName, "Results/%s", argv[1]);
  mkdir(tempName, 0777);
  for (i = 0; i < people; ++i) {
    char fileName[100];
    sprintf(fileName, "%s/%s.txt", tempName, argv[i + 2]);
    filePtr = fopen(fileName, "w");
    if (filePtr == NULL) {
      fprintf(stderr, "Fatal: Failed to open file\n");
      fprintf(stderr, "Invalid filename or filepath\n");
      exit(0);
    }
    fprintf(filePtr, "You are the secret santa for %s!\n\n", argv[list[i] + 2]);
    for (int j = 0; j < numLines; ++j) {
      fprintf(filePtr, "%s", rules[j]);
    }
    fclose(filePtr);
  }
}


x86 assembly (clang, -O0):
<printToFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x110,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe6d(%rip),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
lea    -0x90(%rbp),%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    12da <printToFile+0x17a>
lea    -0x100(%rbp),%rdi
lea    -0x90(%rbp),%rdx
mov    -0x8(%rbp),%rax
mov    -0x2c(%rbp),%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rcx
lea    0xe27(%rip),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
lea    -0x100(%rbp),%rdi
lea    0xe1c(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1244 <printToFile+0xe4>
mov    0x2dcc(%rip),%rax
movslq (%rax),%rdi
lea    0xdf9(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2db4(%rip),%rax
movslq (%rax),%rdi
lea    0xdfd(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
movslq -0x2c(%rbp),%rdx
mov    (%rcx,%rdx,4),%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xde9(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x104(%rbp)
mov    -0x104(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    12c3 <printToFile+0x163>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
movslq -0x104(%rbp),%rax
imul   $0x1c2,%rax,%rax
add    %rax,%rdx
lea    0xd60(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x104(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x104(%rbp)
jmp    1279 <printToFile+0x119>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11b2 <printToFile+0x52>
add    $0x110,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<printToFile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    %r8,0x28(%rsp)
mov    %ecx,0xc(%rsp)
mov    %edx,%ebp
mov    %rsi,0x20(%rsp)
mov    %rdi,%rbx
mov    0x8(%rdi),%rdx
lea    0xe51(%rip),%rsi
lea    0x30(%rsp),%r14
mov    %r14,%rdi
xor    %eax,%eax
call   1080 <sprintf@plt>
mov    %r14,%rdi
mov    $0x1ff,%esi
call   1030 <mkdir@plt>
test   %ebp,%ebp
jle    128d <printToFile+0x10d>
mov    %ebp,%eax
mov    %rax,0x18(%rsp)
mov    0xc(%rsp),%eax
mov    %rax,0x10(%rsp)
lea    0xa0(%rsp),%rbp
xor    %r15d,%r15d
jmp    1207 <printToFile+0x87>
mov    %r12,%rdi
call   1040 <fclose@plt>
add    $0x1,%r15
cmp    0x18(%rsp),%r15
je     128d <printToFile+0x10d>
mov    0x10(%rbx,%r15,8),%rcx
mov    %rbp,%rdi
lea    0xdf5(%rip),%rsi
lea    0x30(%rsp),%rdx
xor    %eax,%eax
call   1080 <sprintf@plt>
mov    %rbp,%rdi
lea    0xde9(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     129f <printToFile+0x11f>
mov    %rax,%r12
mov    0x20(%rsp),%rax
movslq (%rax,%r15,4),%rax
mov    0x10(%rbx,%rax,8),%rdx
mov    %r12,%rdi
lea    0xe00(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x10(%rsp),%r13
mov    0x28(%rsp),%r14
cmpl   $0x0,0xc(%rsp)
jle    11f0 <printToFile+0x70>
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %r12,%rsi
call   1050 <fputs@plt>
add    $0x1c2,%r14
add    $0xffffffffffffffff,%r13
jne    1270 <printToFile+0xf0>
jmp    11f0 <printToFile+0x70>
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d3a(%rip),%rbx
movslq (%rbx),%rcx
lea    0xd67(%rip),%rdi
mov    $0x1b,%esi
mov    $0x1,%edx
call   10a0 <fwrite@plt>
movslq (%rbx),%rcx
lea    0xd6a(%rip),%rdi
mov    $0x1d,%esi
mov    $0x1,%edx
call   10a0 <fwrite@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"Results/%s\00"
@.str.1 = [10 x i8] c"%s/%s.txt\00"
@.str.2 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.3 = [28 x i8] c"Fatal: Failed to open file\0A\00"
@.str.4 = [30 x i8] c"Invalid filename or filepath\0A\00"
@.str.5 = [35 x i8] c"You are the secret santa for %s!\0A\0A\00"
@.str.6 = [3 x i8] c"%s\00"
define void @printToFile(i8** %0, i32* %1, i32 %2, i32 %3, [450 x i8]* %4) {
%6 = alloca i8**
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca [450 x i8]*
%11 = alloca i32*
%12 = alloca i32
%13 = alloca [100 x i8]
%14 = alloca [100 x i8]
%15 = alloca i32
store i8** %0, i8*** %6
store i32* %1, i32** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store [450 x i8]* %4, [450 x i8]** %10
%16 = getelementptr inbounds [100 x i8], [100 x i8]* %13, i64 0, i64 0
%17 = load i8**, i8*** %6
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %16, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = getelementptr inbounds [100 x i8], [100 x i8]* %13, i64 0, i64 0
%22 = call i32 @mkdir(i8* %21, i32 511)
store i32 0, i32* %12
br label %23
23: ; preds = %82, %5
%24 = load i32, i32* %12
%25 = load i32, i32* %8
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %85
27: ; preds = %23
%28 = getelementptr inbounds [100 x i8], [100 x i8]* %14, i64 0, i64 0
%29 = getelementptr inbounds [100 x i8], [100 x i8]* %13, i64 0, i64 0
%30 = load i8**, i8*** %6
%31 = load i32, i32* %12
%32 = add nsw i32 %31, 2
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8*, i8** %30, i64 %33
%35 = load i8*, i8** %34
%36 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %28, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %29, i8* %35)
%37 = getelementptr inbounds [100 x i8], [100 x i8]* %14, i64 0, i64 0
%38 = call i32* @fopen(i8* %37, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %38, i32** %11
%39 = load i32*, i32** %11
%40 = icmp eq i32* %39, null
br i1 %40, label %41, label %51
41: ; preds = %27
%42 = load i32, i32* @stderr
%43 = sext i32 %42 to i64
%44 = inttoptr i64 %43 to i32*
%45 = call i32 (i32*, i8*, ...) @fprintf(i32* %44, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%46 = load i32, i32* @stderr
%47 = sext i32 %46 to i64
%48 = inttoptr i64 %47 to i32*
%49 = call i32 (i32*, i8*, ...) @fprintf(i32* %48, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0))
%50 = call i32 @exit(i32 0)
unreachable
51: ; preds = %27
%52 = load i32*, i32** %11
%53 = load i8**, i8*** %6
%54 = load i32*, i32** %7
%55 = load i32, i32* %12
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i32, i32* %54, i64 %56
%58 = load i32, i32* %57
%59 = add nsw i32 %58, 2
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8*, i8** %53, i64 %60
%62 = load i8*, i8** %61
%63 = call i32 (i32*, i8*, ...) @fprintf(i32* %52, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0), i8* %62)
store i32 0, i32* %15
br label %64
64: ; preds = %76, %51
%65 = load i32, i32* %15
%66 = load i32, i32* %9
%67 = icmp slt i32 %65, %66
br i1 %67, label %68, label %79
68: ; preds = %64
%69 = load i32*, i32** %11
%70 = load [450 x i8]*, [450 x i8]** %10
%71 = load i32, i32* %15
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds [450 x i8], [450 x i8]* %70, i64 %72
%74 = getelementptr inbounds [450 x i8], [450 x i8]* %73, i64 0, i64 0
%75 = call i32 (i32*, i8*, ...) @fprintf(i32* %69, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %74)
br label %76
76: ; preds = %68
%77 = load i32, i32* %15
%78 = add nsw i32 %77, 1
store i32 %78, i32* %15
br label %64
79: ; preds = %64
%80 = load i32*, i32** %11
%81 = call i32 @fclose(i32* %80)
br label %82
82: ; preds = %79
%83 = load i32, i32* %12
%84 = add nsw i32 %83, 1
store i32 %84, i32* %12
br label %23
85: ; preds = %23
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"Results/%s\00"
@.str.1 = [10 x i8] c"%s/%s.txt\00"
@.str.2 = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.3 = [28 x i8] c"Fatal: Failed to open file\0A\00"
@.str.4 = [30 x i8] c"Invalid filename or filepath\0A\00"
@.str.5 = [35 x i8] c"You are the secret santa for %s!\0A\0A\00"
define void @printToFile(i8** nocapture %0, i32* nocapture %1, i32 %2, i32 %3, [450 x i8]* nocapture %4) {
%6 = alloca [100 x i8]
%7 = alloca [100 x i8]
%8 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%9 = getelementptr inbounds i8*, i8** %0, i64 1
%10 = load i8*, i8** %9
%11 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %8, i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = call i32 @mkdir(i8* %8, i32 511)
%13 = icmp sgt i32 %2, 0
br i1 %13, label %14, label %55
14: ; preds = %5
%15 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%16 = icmp sgt i32 %3, 0
%17 = zext i32 %2 to i64
%18 = zext i32 %3 to i64
br label %19
19: ; preds = %14, %45
%20 = phi i64 [ 0, %14 ], [ %47, %45 ]
%21 = add nuw nsw i64 %20, 2
%22 = getelementptr inbounds i8*, i8** %0, i64 %21
%23 = load i8*, i8** %22
%24 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %15, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %8, i8* %23)
%25 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %37
27: ; preds = %19
%28 = load i32, i32* @stderr
%29 = sext i32 %28 to i64
%30 = inttoptr i64 %29 to i32*
%31 = call i64 @fwrite(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i64 27, i64 1, i32* %30)
%32 = load i32, i32* @stderr
%33 = sext i32 %32 to i64
%34 = inttoptr i64 %33 to i32*
%35 = call i64 @fwrite(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i64 29, i64 1, i32* %34)
%36 = call i32 @exit(i32 0)
unreachable
37: ; preds = %19
%38 = getelementptr inbounds i32, i32* %1, i64 %20
%39 = load i32, i32* %38
%40 = add nsw i32 %39, 2
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8*, i8** %0, i64 %41
%43 = load i8*, i8** %42
%44 = call i32 (i32*, i8*, ...) @fprintf(i32* %25, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0), i8* %43)
br i1 %16, label %49, label %45
45: ; preds = %49, %37
%46 = call i32 @fclose(i32* %25)
%47 = add nuw nsw i64 %20, 1
%48 = icmp eq i64 %47, %17
br i1 %48, label %55, label %19
49: ; preds = %37, %49
%50 = phi i64 [ %53, %49 ], [ 0, %37 ]
%51 = getelementptr inbounds [450 x i8], [450 x i8]* %4, i64 %50, i64 0
%52 = call i32 @fputs(i8* %51, i32* %25)
%53 = add nuw nsw i64 %50, 1
%54 = icmp eq i64 %53, %18
br i1 %54, label %45, label %49
55: ; preds = %45, %5
ret void
}