#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*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ *strchrs(char const *, char *, int);
size_t strlen(char const *);
void print_csv_field(const char *field, FILE *output) {
  if (NULL != strchrs(field, ",\n", 2)) {
    fprintf(output, "\"");
    for (size_t i = 0; i < strlen(field); i++) {
      if (field[i] == '"') {
        fprintf(output, "\"\"");
      } else {
        fprintf(output, "%c", field[i]);
      }
    }
    fprintf(output, "\"");
  } else {
    fprintf(output, "%s", field);
  }
}


x86 assembly (clang, -O0):
<print_csv_field>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xeb5(%rip),%rsi
mov    $0x2,%edx
call   1050 <strchrs@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     1215 <print_csv_field+0xe5>
mov    -0x10(%rbp),%rdi
lea    0xe96(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
cmp    %rcx,%rax
jae    11fe <print_csv_field+0xce>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x22,%eax
jne    11ca <print_csv_field+0x9a>
mov    -0x10(%rbp),%rdi
lea    0xe45(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    11e8 <print_csv_field+0xb8>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%edx
lea    0xe25(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    11ed <print_csv_field+0xbd>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    117d <print_csv_field+0x4d>
mov    -0x10(%rbp),%rdi
lea    0xdfb(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    122b <print_csv_field+0xfb>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xde5(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_csv_field>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
lea    0xe9b(%rip),%rsi
mov    $0x2,%edx
call   1060 <strchrs@plt>
test   %rax,%rax
je     11fe <print_csv_field+0xae>
mov    $0x22,%edi
mov    %r14,%rsi
call   1050 <fputc@plt>
mov    (%r12),%al
test   %al,%al
je     11e6 <print_csv_field+0x96>
mov    $0x1,%ebx
lea    0xe6a(%rip),%r15
cmp    $0x22,%al
jne    11c0 <print_csv_field+0x70>
nopl   (%rax)
mov    $0x2,%esi
mov    $0x1,%edx
mov    %r15,%rdi
mov    %r14,%rcx
call   1070 <fwrite@plt>
jmp    11cb <print_csv_field+0x7b>
nopw   0x0(%rax,%rax,1)
movsbl %al,%edi
mov    %r14,%rsi
call   1050 <fputc@plt>
mov    %r12,%rdi
call   1030 <strlen@plt>
cmp    %rax,%rbx
jae    11e6 <print_csv_field+0x96>
mov    (%r12,%rbx,1),%al
add    $0x1,%rbx
cmp    $0x22,%al
jne    11c0 <print_csv_field+0x70>
jmp    11a0 <print_csv_field+0x50>
mov    $0x22,%edi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1050 <fputc@plt>
mov    %r12,%rdi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1040 <fputs@plt>

LLVM IR (-O0):
@.str = [3 x i8] c",\0A\00"
@.str.1 = [2 x i8] c"\22\00"
@.str.2 = [3 x i8] c"\22\22\00"
@.str.3 = [3 x i8] c"%c\00"
@.str.4 = [3 x i8] c"%s\00"
define void @print_csv_field(i8* %0, i32* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64
store i8* %0, i8** %3
store i32* %1, i32** %4
%6 = load i8*, i8** %3
%7 = call i32* @strchrs(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%8 = icmp ne i32* null, %7
br i1 %8, label %9, label %42
9: ; preds = %2
%10 = load i32*, i32** %4
%11 = call i32 (i32*, i8*, ...) @fprintf(i32* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i64 0, i64* %5
br label %12
12: ; preds = %36, %9
%13 = load i64, i64* %5
%14 = load i8*, i8** %3
%15 = call i64 @strlen(i8* %14)
%16 = icmp ult i64 %13, %15
br i1 %16, label %17, label %39
17: ; preds = %12
%18 = load i8*, i8** %3
%19 = load i64, i64* %5
%20 = getelementptr inbounds i8, i8* %18, i64 %19
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 34
br i1 %23, label %24, label %27
24: ; preds = %17
%25 = load i32*, i32** %4
%26 = call i32 (i32*, i8*, ...) @fprintf(i32* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
br label %35
27: ; preds = %17
%28 = load i32*, i32** %4
%29 = load i8*, i8** %3
%30 = load i64, i64* %5
%31 = getelementptr inbounds i8, i8* %29, i64 %30
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = call i32 (i32*, i8*, ...) @fprintf(i32* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %33)
br label %35
35: ; preds = %27, %24
br label %36
36: ; preds = %35
%37 = load i64, i64* %5
%38 = add i64 %37, 1
store i64 %38, i64* %5
br label %12
39: ; preds = %12
%40 = load i32*, i32** %4
%41 = call i32 (i32*, i8*, ...) @fprintf(i32* %40, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %46
42: ; preds = %2
%43 = load i32*, i32** %4
%44 = load i8*, i8** %3
%45 = call i32 (i32*, i8*, ...) @fprintf(i32* %43, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %44)
br label %46
46: ; preds = %42, %39
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c",\0A\00"
@.str.2 = [3 x i8] c"\22\22\00"
define void @print_csv_field(i8* %0, i32* nocapture %1) {
%3 = tail call i32* @strchrs(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%4 = icmp eq i32* %3, null
br i1 %4, label %27, label %5
5: ; preds = %2
%6 = tail call i32 @fputc(i32 34, i32* %1)
%7 = load i8, i8* %0
%8 = icmp eq i8 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %20, %5
%10 = tail call i32 @fputc(i32 34, i32* %1)
br label %29
11: ; preds = %5, %24
%12 = phi i8 [ %26, %24 ], [ %7, %5 ]
%13 = phi i64 [ %21, %24 ], [ 0, %5 ]
%14 = icmp eq i8 %12, 34
br i1 %14, label %15, label %17
15: ; preds = %11
%16 = tail call i64 @fwrite(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i64 2, i64 1, i32* %1)
br label %20
17: ; preds = %11
%18 = sext i8 %12 to i32
%19 = tail call i32 @fputc(i32 %18, i32* %1)
br label %20
20: ; preds = %15, %17
%21 = add nuw i64 %13, 1
%22 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%23 = icmp ult i64 %21, %22
br i1 %23, label %24, label %9
24: ; preds = %20
%25 = getelementptr inbounds i8, i8* %0, i64 %21
%26 = load i8, i8* %25
br label %11
27: ; preds = %2
%28 = tail call i32 @fputs(i8* %0, i32* %1)
br label %29
29: ; preds = %27, %9
ret void
}