#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*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ *stdout;
int /*<<< orphan*/ testFputs(int /*<<< orphan*/ *);
int /*<<< orphan*/ testPrintf(int /*<<< orphan*/ *);
int main(int argc, char **argv) {
  char *path;
  FILE *f;
  if (argc > 1) {
    f = fopen(argv[1], "w");
    path = argv[1];
  } else {
    f = stdout;
    path = "stdout";
  }
  printf("A test of \"fprintf\" to file: \"%s\".\r\n\n", path);
  if (f != (FILE *)0) {
    testPrintf(f);
    testFputs(f);
  } else {
    printf("File could not be opened.\r\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    1189 <main+0x49>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe91(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
jmp    11a2 <main+0x62>
mov    0x2e50(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
lea    0xe64(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpq   $0x0,-0x20(%rbp)
je     11d6 <main+0x96>
mov    -0x20(%rbp),%rdi
call   1040 <testPrintf@plt>
mov    -0x20(%rbp),%rdi
call   1050 <testFputs@plt>
jmp    11e4 <main+0xa4>
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jl     1175 <main+0x25>
mov    %rsi,%r14
mov    0x8(%rsi),%rdi
lea    0xe99(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
mov    0x8(%r14),%rsi
jmp    1186 <main+0x36>
mov    0x2e64(%rip),%rax
mov    (%rax),%rbx
lea    0xe7c(%rip),%rsi
lea    0xe7c(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
test   %rbx,%rbx
je     11ab <main+0x5b>
mov    %rbx,%rdi
call   1050 <testPrintf@plt>
mov    %rbx,%rdi
call   1060 <testFputs@plt>
jmp    11b7 <main+0x67>
lea    0xe7d(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@stdout = global i32* null
@.str.1 = [7 x i8] c"stdout\00"
@.str.2 = [38 x i8] c"A test of \22fprintf\22 to file: \22%s\22.\0D\0A\0A\00"
@.str.3 = [28 x i8] c"File could not be opened.\0D\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp sgt i32 %8, 1
br i1 %9, label %10, label %18
10: ; preds = %2
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 1
%13 = load i8*, i8** %12
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %7
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
store i8* %17, i8** %6
br label %20
18: ; preds = %2
%19 = load i32*, i32** @stdout
store i32* %19, i32** %7
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8** %6
br label %20
20: ; preds = %18, %10
%21 = load i8*, i8** %6
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %21)
%23 = load i32*, i32** %7
%24 = icmp ne i32* %23, null
br i1 %24, label %25, label %30
25: ; preds = %20
%26 = load i32*, i32** %7
%27 = call i32 @testPrintf(i32* %26)
%28 = load i32*, i32** %7
%29 = call i32 @testFputs(i32* %28)
br label %32
30: ; preds = %20
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %32
32: ; preds = %30, %25
ret i32 0
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@stdout = global i32* null
@.str.1 = [7 x i8] c"stdout\00"
@.str.2 = [38 x i8] c"A test of \22fprintf\22 to file: \22%s\22.\0D\0A\0A\00"
@str = [27 x i8] c"File could not be opened.\0D\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 1
br i1 %3, label %4, label %9
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = load i8*, i8** %5
br label %11
9: ; preds = %2
%10 = load i32*, i32** @stdout
br label %11
11: ; preds = %9, %4
%12 = phi i8* [ %8, %4 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %9 ]
%13 = phi i32* [ %7, %4 ], [ %10, %9 ]
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %12)
%15 = icmp eq i32* %13, null
br i1 %15, label %19, label %16
16: ; preds = %11
%17 = tail call i32 @testPrintf(i32* %13)
%18 = tail call i32 @testFputs(i32* %13)
br label %21
19: ; preds = %11
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
br label %21
21: ; preds = %19, %16
ret i32 0
}