#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 *, char *);
int /*<<< orphan*/ free(char *);
char *open_file_from_args(int, char **, int /*<<< orphan*/ **);
int /*<<< orphan*/ printf(char *, char *);
char *replace_extension(char *, char *);
int main(int argc, char *argv[]) {
  FILE *file_pointer = NULL;
  char *input_filename = open_file_from_args(argc, argv, &file_pointer);
  if (file_pointer == NULL) {
    printf("%s does not exist.\n", input_filename);
    return 0;
  } else {
    printf("%s opened\n", input_filename);
  }
  fclose(file_pointer);
  char *output_filename = replace_extension(input_filename, ".asm");
  free(input_filename);
  file_pointer = fopen(output_filename, "w");
  if (file_pointer == NULL) {
    printf("%s could not be created\n", output_filename);
  } else {
    printf("%s created\n", output_filename);
  }
  fclose(file_pointer);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    -0x18(%rbp),%rdx
call   1040 <open_file_from_args@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b9 <main+0x59>
mov    -0x20(%rbp),%rsi
lea    0xe58(%rip),%rdi
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1243 <main+0xe3>
mov    -0x20(%rbp),%rsi
lea    0xe50(%rip),%rdi
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rdi
lea    0xe42(%rip),%rsi
call   1070 <replace_extension@plt>
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
lea    0xe2a(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1223 <main+0xc3>
mov    -0x28(%rbp),%rsi
lea    0xe0d(%rip),%rdi
call   1060 <printf@plt>
jmp    1233 <main+0xd3>
mov    -0x28(%rbp),%rsi
lea    0xe11(%rip),%rdi
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
movq   $0x0,(%rsp)
mov    %rsp,%rdx
call   1040 <open_file_from_args@plt>
mov    %rax,%rbx
cmpq   $0x0,(%rsp)
je     11eb <main+0x8b>
lea    0xe8f(%rip),%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
mov    (%rsp),%rdi
call   1050 <fclose@plt>
lea    0xe82(%rip),%rsi
mov    %rbx,%rdi
call   1070 <replace_extension@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xe6d(%rip),%rsi
mov    %r14,%rdi
call   1080 <fopen@plt>
mov    %rax,(%rsp)
test   %rax,%rax
lea    0xe59(%rip),%rax
lea    0xe6b(%rip),%rdi
cmove  %rax,%rdi
mov    %r14,%rsi
call   1060 <printf@plt>
mov    (%rsp),%rdi
call   1050 <fclose@plt>
jmp    11fa <main+0x9a>
lea    0xe0e(%rip),%rdi
mov    %rbx,%rsi
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [20 x i8] c"%s does not exist.\0A\00"
@.str.1 = [11 x i8] c"%s opened\0A\00"
@.str.2 = [5 x i8] c".asm\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [25 x i8] c"%s could not be created\0A\00"
@.str.5 = [12 x i8] c"%s created\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32* null, i32** %6
%9 = load i32, i32* %4
%10 = load i8**, i8*** %5
%11 = call i8* @open_file_from_args(i32 %9, i8** %10, i32** %6)
store i8* %11, i8** %7
%12 = load i32*, i32** %6
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i8*, i8** %7
%16 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 0, i32* %3
br label %40
17: ; preds = %2
%18 = load i8*, i8** %7
%19 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %18)
br label %20
20: ; preds = %17
%21 = load i32*, i32** %6
%22 = call i32 @fclose(i32* %21)
%23 = load i8*, i8** %7
%24 = call i8* @replace_extension(i8* %23, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i8* %24, i8** %8
%25 = load i8*, i8** %7
%26 = call i32 @free(i8* %25)
%27 = load i8*, i8** %8
%28 = call i32* @fopen(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %28, i32** %6
%29 = load i32*, i32** %6
%30 = icmp eq i32* %29, null
br i1 %30, label %31, label %34
31: ; preds = %20
%32 = load i8*, i8** %8
%33 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* %32)
br label %37
34: ; preds = %20
%35 = load i8*, i8** %8
%36 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i8* %35)
br label %37
37: ; preds = %34, %31
%38 = load i32*, i32** %6
%39 = call i32 @fclose(i32* %38)
store i32 0, i32* %3
br label %40
40: ; preds = %37, %14
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
@.str = [20 x i8] c"%s does not exist.\0A\00"
@.str.1 = [11 x i8] c"%s opened\0A\00"
@.str.2 = [5 x i8] c".asm\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [25 x i8] c"%s could not be created\0A\00"
@.str.5 = [12 x i8] c"%s created\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32*
%4 = bitcast i32** %3 to i8*
store i32* null, i32** %3
%5 = call i8* @open_file_from_args(i32 %0, i8** %1, i32** %3)
%6 = load i32*, i32** %3
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %5)
br label %22
10: ; preds = %2
%11 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %5)
%12 = load i32*, i32** %3
%13 = call i32 @fclose(i32* %12)
%14 = call i8* @replace_extension(i8* %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%15 = call i32 @free(i8* %5)
%16 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %16, i32** %3
%17 = icmp eq i32* %16, null
%18 = select i1 %17, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0)
%19 = call i32 @printf(i8* %18, i8* %14)
%20 = load i32*, i32** %3
%21 = call i32 @fclose(i32* %20)
br label %22
22: ; preds = %10, %8
ret i32 0
}