#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 strlen(char *);
void write_escape_str(FILE *fh, char *string) {
  int loop, len = strlen(string);
  fprintf(fh, "\"");
  for (loop = 0; loop < len; loop++) {
    if (string[loop] == '\"')
      fprintf(fh, "\\");
    fprintf(fh, "%c", string[loop]);
  }
  fprintf(fh, "\"");
}


x86 assembly (clang, -O0):
<write_escape_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xeb9(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    11b4 <write_escape_str+0x94>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x22,%eax
jne    1188 <write_escape_str+0x68>
mov    -0x8(%rbp),%rdi
lea    0xe81(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%edx
lea    0xe65(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1155 <write_escape_str+0x35>
mov    -0x8(%rbp),%rdi
lea    0xe41(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_escape_str>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebp
mov    $0x22,%edi
mov    %rbx,%rsi
call   1040 <fputc@plt>
test   %ebp,%ebp
jle    1181 <write_escape_str+0x61>
mov    %ebp,%r15d
xor    %ebp,%ebp
jmp    1164 <write_escape_str+0x44>
nop
movsbl %al,%edi
mov    %rbx,%rsi
call   1040 <fputc@plt>
add    $0x1,%rbp
cmp    %rbp,%r15
je     1181 <write_escape_str+0x61>
movzbl (%r14,%rbp,1),%eax
cmp    $0x22,%al
jne    1150 <write_escape_str+0x30>
mov    $0x5c,%edi
mov    %rbx,%rsi
call   1040 <fputc@plt>
movzbl (%r14,%rbp,1),%eax
jmp    1150 <write_escape_str+0x30>
mov    $0x22,%edi
mov    %rbx,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fputc@plt>

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

LLVM IR (-O2):
define void @write_escape_str(i32* nocapture %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
%4 = tail call i32 @fputc(i32 34, i32* %0)
%5 = icmp sgt i32 %3, 0
br i1 %5, label %6, label %22
6: ; preds = %2
%7 = zext i32 %3 to i64
br label %8
8: ; preds = %6, %16
%9 = phi i64 [ 0, %6 ], [ %20, %16 ]
%10 = getelementptr inbounds i8, i8* %1, i64 %9
%11 = load i8, i8* %10
%12 = icmp eq i8 %11, 34
br i1 %12, label %13, label %16
13: ; preds = %8
%14 = tail call i32 @fputc(i32 92, i32* %0)
%15 = load i8, i8* %10
br label %16
16: ; preds = %13, %8
%17 = phi i8 [ %15, %13 ], [ %11, %8 ]
%18 = sext i8 %17 to i32
%19 = tail call i32 @fputc(i32 %18, i32* %0)
%20 = add nuw nsw i64 %9, 1
%21 = icmp eq i64 %20, %7
br i1 %21, label %22, label %8
22: ; preds = %16, %2
%23 = tail call i32 @fputc(i32 34, i32* %0)
ret void
}