#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*/ free(char *);
char *malloc(scalar_t__);
char *open_file_with_extension(char *, char *, char *,
                               int /*<<< orphan*/ **const);
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ strlen(char const *);
char *open_file_from_args(const int argument_count, char **const argument_value,
                          FILE **const fp) {
  char *filename;
  const char default_filename[] = "test";
  if (argument_count < 2) {
    filename = malloc(strlen(default_filename) + 1);
    strcpy(filename, default_filename);
  } else {
    filename = malloc(strlen(argument_value[1]) + 1);
    strcpy(filename, argument_value[1]);
  }
  char *output = open_file_with_extension(filename, ".vm", "r", fp);
  free(filename);
  return output;
}


x86 assembly (clang, -O0):
<open_file_from_args>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0xe97(%rip),%eax
mov    %eax,-0x25(%rbp)
mov    0xe92(%rip),%al
mov    %al,-0x21(%rbp)
cmpl   $0x2,-0x4(%rbp)
jge    11aa <open_file_from_args+0x5a>
lea    -0x25(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x25(%rbp),%rsi
call   1050 <strcpy@plt>
jmp    11d8 <open_file_from_args+0x88>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <strlen@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1050 <strcpy@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rcx
lea    0xe1e(%rip),%rsi
lea    0xe1b(%rip),%rdx
call   1030 <open_file_with_extension@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
mov    -0x30(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_file_from_args>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
movb   $0x0,0xc(%rsp)
movl   $0x74736574,0x8(%rsp)
74
cmp    $0x1,%edi
jg     1175 <open_file_from_args+0x25>
lea    0x8(%rsp),%r15
jmp    1179 <open_file_from_args+0x29>
mov    0x8(%rsi),%r15
mov    %r15,%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%rdi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
call   1050 <strcpy@plt>
lea    0xe66(%rip),%rsi
lea    0xe63(%rip),%rdx
mov    %rbx,%rdi
mov    %r14,%rcx
call   1030 <open_file_with_extension@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1040 <free@plt>
mov    %r14,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@__const.open_file_from_args.default_filename = [5 x i8] c"test\00"
@.str = [4 x i8] c".vm\00"
@.str.1 = [2 x i8] c"r\00"
define i8* @open_file_from_args(i32 %0, i8** %1, i32** %2) {
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32**
%7 = alloca i8*
%8 = alloca [5 x i8]
%9 = alloca i8*
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32** %2, i32*** %6
%10 = bitcast [5 x i8]* %8 to i8*
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %13, label %21
13: ; preds = %3
%14 = getelementptr inbounds [5 x i8], [5 x i8]* %8, i64 0, i64 0
%15 = call i64 @strlen(i8* %14)
%16 = add nsw i64 %15, 1
%17 = call i8* @malloc(i64 %16)
store i8* %17, i8** %7
%18 = load i8*, i8** %7
%19 = getelementptr inbounds [5 x i8], [5 x i8]* %8, i64 0, i64 0
%20 = call i32 @strcpy(i8* %18, i8* %19)
br label %33
21: ; preds = %3
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i64 @strlen(i8* %24)
%26 = add nsw i64 %25, 1
%27 = call i8* @malloc(i64 %26)
store i8* %27, i8** %7
%28 = load i8*, i8** %7
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call i32 @strcpy(i8* %28, i8* %31)
br label %33
33: ; preds = %21, %13
%34 = load i8*, i8** %7
%35 = load i32**, i32*** %6
%36 = call i8* @open_file_with_extension(i8* %34, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32** %35)
store i8* %36, i8** %9
%37 = load i8*, i8** %7
%38 = call i32 @free(i8* %37)
%39 = load i8*, i8** %9
ret i8* %39
}

LLVM IR (-O2):
@__const.open_file_from_args.default_filename = [5 x i8] c"test\00"
@.str = [4 x i8] c".vm\00"
@.str.1 = [2 x i8] c"r\00"
define i8* @open_file_from_args(i32 %0, i8** nocapture %1, i32** %2) {
%4 = alloca [5 x i8]
%5 = getelementptr inbounds [5 x i8], [5 x i8]* %4, i64 0, i64 0
%6 = icmp slt i32 %0, 2
br i1 %6, label %7, label %12
7: ; preds = %3
%8 = call i64 @strlen(i8* %5)
%9 = add nsw i64 %8, 1
%10 = tail call i8* @malloc(i64 %9)
%11 = call i32 @strcpy(i8* %10, i8* %5)
br label %19
12: ; preds = %3
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = tail call i64 @strlen(i8* dereferenceable(1) %14)
%16 = add nsw i64 %15, 1
%17 = tail call i8* @malloc(i64 %16)
%18 = tail call i32 @strcpy(i8* %17, i8* %14)
br label %19
19: ; preds = %12, %7
%20 = phi i8* [ %10, %7 ], [ %17, %12 ]
%21 = call i8* @open_file_with_extension(i8* %20, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32** %2)
%22 = call i32 @free(i8* %20)
ret i8* %21
}