#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*/ errno;
int /*<<< orphan*/ fatal_error(int, char *, int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fwrite(char *, size_t, int, int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int /*<<< orphan*/ run_replaces(char *);
int /*<<< orphan*/ sprintf(char *, char *, int);
size_t strlen(char const *);
void writeout_test(int test, const char *test_data) {
  char filename[256];
  FILE *fd;
  size_t len;
  char *parse;
  sprintf(filename, "test-expandargv-%d.lst", test);
  fd = fopen(filename, "w");
  if (fd == NULL)
    fatal_error(167, "Failed to create test file.", errno);
  len = strlen(test_data);
  parse = malloc(sizeof(char) * (len + 1));
  if (parse == NULL)
    fatal_error(173, "Failed to malloc parse.", errno);
  memcpy(parse, test_data, sizeof(char) * len);
  run_replaces(parse);
  fwrite(parse, len, sizeof(char), fd);
  free(parse);
  fclose(fd);
}


x86 assembly (clang, -O0):
<writeout_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x110(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe3d(%rip),%rsi
call   10b0 <sprintf@plt>
lea    -0x110(%rbp),%rdi
lea    0xe41(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x118(%rbp)
cmpq   $0x0,-0x118(%rbp)
jne    120a <writeout_test+0x6a>
mov    0x2de9(%rip),%rax
mov    (%rax),%edx
mov    $0xa7,%edi
lea    0xe14(%rip),%rsi
call   1080 <fatal_error@plt>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x120(%rbp)
mov    -0x120(%rbp),%rax
add    $0x1,%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x128(%rbp)
cmpq   $0x0,-0x128(%rbp)
jne    125f <writeout_test+0xbf>
mov    0x2d94(%rip),%rax
mov    (%rax),%edx
mov    $0xad,%edi
lea    0xddb(%rip),%rsi
call   1080 <fatal_error@plt>
mov    -0x128(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x120(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edx
call   1070 <memcpy@plt>
mov    -0x128(%rbp),%rdi
call   1050 <run_replaces@plt>
mov    -0x128(%rbp),%rdi
mov    -0x120(%rbp),%rsi
mov    -0x118(%rbp),%rcx
mov    $0x1,%edx
call   10c0 <fwrite@plt>
mov    -0x128(%rbp),%rdi
call   1030 <free@plt>
mov    -0x118(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeout_test>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x108,%rsp
mov    %rsi,%r14
mov    %edi,%edx
lea    0xe46(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   10b0 <sprintf@plt>
lea    0xe4b(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11f6 <writeout_test+0x56>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edx
lea    0xe2d(%rip),%rsi
mov    $0xa7,%edi
call   1080 <fatal_error@plt>
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    %rax,%r12
lea    0x1(%rax),%edi
call   1090 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    122b <writeout_test+0x8b>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edx
lea    0xe14(%rip),%rsi
mov    $0xad,%edi
call   1080 <fatal_error@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r12d,%edx
call   1070 <memcpy@plt>
mov    %rbx,%rdi
call   1050 <run_replaces@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0x1,%edx
mov    %r15,%rcx
call   10c0 <fwrite@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
add    $0x108,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1040 <fclose@plt>

LLVM IR (-O0):
@.str = [23 x i8] c"test-expandargv-%d.lst\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [28 x i8] c"Failed to create test file.\00"
@errno = global i32 0
@.str.3 = [24 x i8] c"Failed to malloc parse.\00"
define void @writeout_test(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca [256 x i8]
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i8*
store i32 %0, i32* %3
store i8* %1, i8** %4
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%10 = load i32, i32* %3
%11 = call i32 @sprintf(i8* %9, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %13, i32** %6
%14 = load i32*, i32** %6
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = load i32, i32* @errno
%18 = call i32 @fatal_error(i32 167, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %17)
br label %19
19: ; preds = %16, %2
%20 = load i8*, i8** %4
%21 = call i64 @strlen(i8* %20)
store i64 %21, i64* %7
%22 = load i64, i64* %7
%23 = add i64 %22, 1
%24 = mul i64 1, %23
%25 = trunc i64 %24 to i32
%26 = call i8* @malloc(i32 %25)
store i8* %26, i8** %8
%27 = load i8*, i8** %8
%28 = icmp eq i8* %27, null
br i1 %28, label %29, label %32
29: ; preds = %19
%30 = load i32, i32* @errno
%31 = call i32 @fatal_error(i32 173, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %30)
br label %32
32: ; preds = %29, %19
%33 = load i8*, i8** %8
%34 = load i8*, i8** %4
%35 = load i64, i64* %7
%36 = mul i64 1, %35
%37 = trunc i64 %36 to i32
%38 = call i32 @memcpy(i8* %33, i8* %34, i32 %37)
%39 = load i8*, i8** %8
%40 = call i32 @run_replaces(i8* %39)
%41 = load i8*, i8** %8
%42 = load i64, i64* %7
%43 = load i32*, i32** %6
%44 = call i32 @fwrite(i8* %41, i64 %42, i32 1, i32* %43)
%45 = load i8*, i8** %8
%46 = call i32 @free(i8* %45)
%47 = load i32*, i32** %6
%48 = call i32 @fclose(i32* %47)
ret void
}

LLVM IR (-O2):
@.str = [23 x i8] c"test-expandargv-%d.lst\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [28 x i8] c"Failed to create test file.\00"
@errno = global i32 0
@.str.3 = [24 x i8] c"Failed to malloc parse.\00"
define void @writeout_test(i32 %0, i8* %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i32, i32* @errno
%10 = tail call i32 @fatal_error(i32 167, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %9)
br label %11
11: ; preds = %8, %2
%12 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%13 = trunc i64 %12 to i32
%14 = add i32 %13, 1
%15 = tail call i8* @malloc(i32 %14)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %11
%18 = load i32, i32* @errno
%19 = tail call i32 @fatal_error(i32 173, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %18)
br label %20
20: ; preds = %17, %11
%21 = tail call i32 @memcpy(i8* %15, i8* %1, i32 %13)
%22 = tail call i32 @run_replaces(i8* %15)
%23 = tail call i32 @fwrite(i8* %15, i64 %12, i32 1, i32* %6)
%24 = tail call i32 @free(i8* %15)
%25 = tail call i32 @fclose(i32* %6)
ret void
}