#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 */

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
size_t csv_write(char *, size_t, char *, size_t);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fail_writer(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *malloc(size_t);
scalar_t__ memcmp(char *, char *, size_t);
int /*<<< orphan*/ stderr;
void test_writer(char *test_name, char *input, size_t input_len, char *expected,
                 size_t expected_len) {
  size_t actual_len;
  char *temp = malloc(input_len * 2 + 2);
  if (!temp) {
    fprintf(stderr, "Failed to allocate memory in test_writer!\n");
    exit(EXIT_FAILURE);
  }
  actual_len = csv_write(temp, input_len * 2 + 2, input, input_len);
  if (actual_len != expected_len)
    fail_writer(test_name, "actual length doesn't match expected length");
  if (memcmp(temp, expected, actual_len) != 0)
    fail_writer(test_name, "actual data doesn't match expected data");
}


x86 assembly (clang, -O0):
<test_writer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
shl    $0x1,%rdi
add    $0x2,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11bf <test_writer+0x5f>
mov    0x2e3d(%rip),%rax
mov    (%rax),%edi
lea    0xe54(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
shl    $0x1,%rsi
add    $0x2,%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
call   1030 <csv_write@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x28(%rbp),%rax
je     11fe <test_writer+0x9e>
mov    -0x8(%rbp),%rdi
lea    0xe32(%rip),%rsi
call   1070 <fail_writer@plt>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1040 <memcmp@plt>
cmp    $0x0,%rax
je     1229 <test_writer+0xc9>
mov    -0x8(%rbp),%rdi
lea    0xe33(%rip),%rsi
call   1070 <fail_writer@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_writer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r12
mov    %rcx,(%rsp)
mov    %rdx,%r13
mov    %rsi,%rbp
mov    %rdi,%r15
lea    (%rdx,%rdx,1),%r14
add    $0x2,%r14
mov    %r14,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     11fb <test_writer+0x9b>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbp,%rdx
mov    %r13,%rcx
call   1030 <csv_write@plt>
mov    %rax,%r13
cmp    %r12,%rax
je     11bb <test_writer+0x5b>
lea    0xe78(%rip),%rsi
mov    %r15,%rdi
call   1070 <fail_writer@plt>
mov    %rbx,%rdi
mov    (%rsp),%rsi
mov    %r13,%rdx
call   1040 <memcmp@plt>
test   %rax,%rax
je     11ec <test_writer+0x8c>
lea    0xe81(%rip),%rsi
mov    %r15,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <fail_writer@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
lea    0xdf5(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [43 x i8] c"Failed to allocate memory in test_writer!\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [44 x i8] c"actual length doesn't match expected length\00"
@.str.2 = [40 x i8] c"actual data doesn't match expected data\00"
define void @test_writer(i8* %0, i8* %1, i64 %2, i8* %3, i64 %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i8*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i8* %3, i8** %9
store i64 %4, i64* %10
%13 = load i64, i64* %8
%14 = mul i64 %13, 2
%15 = add i64 %14, 2
%16 = call i8* @malloc(i64 %15)
store i8* %16, i8** %12
%17 = load i8*, i8** %12
%18 = icmp ne i8* %17, null
br i1 %18, label %24, label %19
19: ; preds = %5
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %5
%25 = load i8*, i8** %12
%26 = load i64, i64* %8
%27 = mul i64 %26, 2
%28 = add i64 %27, 2
%29 = load i8*, i8** %7
%30 = load i64, i64* %8
%31 = call i64 @csv_write(i8* %25, i64 %28, i8* %29, i64 %30)
store i64 %31, i64* %11
%32 = load i64, i64* %11
%33 = load i64, i64* %10
%34 = icmp ne i64 %32, %33
br i1 %34, label %35, label %38
35: ; preds = %24
%36 = load i8*, i8** %6
%37 = call i32 @fail_writer(i8* %36, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0))
br label %38
38: ; preds = %35, %24
%39 = load i8*, i8** %12
%40 = load i8*, i8** %9
%41 = load i64, i64* %11
%42 = call i64 @memcmp(i8* %39, i8* %40, i64 %41)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %38
%45 = load i8*, i8** %6
%46 = call i32 @fail_writer(i8* %45, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
br label %47
47: ; preds = %44, %38
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [43 x i8] c"Failed to allocate memory in test_writer!\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [44 x i8] c"actual length doesn't match expected length\00"
@.str.2 = [40 x i8] c"actual data doesn't match expected data\00"
define void @test_writer(i8* %0, i8* %1, i64 %2, i8* %3, i64 %4) {
%6 = shl i64 %2, 1
%7 = add i64 %6, 2
%8 = tail call i8* @malloc(i64 %7)
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %15
10: ; preds = %5
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @EXIT_FAILURE
%14 = tail call i32 @exit(i32 %13)
unreachable
15: ; preds = %5
%16 = tail call i64 @csv_write(i8* %8, i64 %7, i8* %1, i64 %2)
%17 = icmp eq i64 %16, %4
br i1 %17, label %20, label %18
18: ; preds = %15
%19 = tail call i32 @fail_writer(i8* %0, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %18, %15
%21 = tail call i64 @memcmp(i8* %8, i8* %3, i64 %16)
%22 = icmp eq i64 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %20
%24 = tail call i32 @fail_writer(i8* %0, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %23, %20
ret void
}