#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 */
char *escape(char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ str_printf(char *, int *, char *, char *, char *);
int strlen(char *);
void write_string(char *out, int *written, void *p_str) {
  int elide;
  char *str, *escaped;
  str = p_str;
  escaped = escape(str);
  elide = (str[32 - 1] && strlen(str) == 32);
  str_printf(out, written, "\"%s\"%s", escaped, elide ? "..." : "");
  free(escaped);
  free(str);
}


x86 assembly (clang, -O0):
<write_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <escape@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rax
movsbl 0x1f(%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x31(%rbp)
je     1191 <write_string+0x51>
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x20,%eax
sete   %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rcx
mov    -0x1c(%rbp),%edx
lea    0xe58(%rip),%r8
lea    0xe4e(%rip),%rax
cmp    $0x0,%edx
cmovne %rax,%r8
lea    0xe39(%rip),%rdx
call   1060 <str_printf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_string>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r12
mov    %rdx,%rdi
call   1050 <escape@plt>
mov    %rax,%r14
cmpb   $0x0,0x1f(%rbx)
je     1181 <write_string+0x41>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cmp    $0x20,%eax
lea    0xe93(%rip),%rax
lea    0xe8f(%rip),%r8
cmove  %rax,%r8
jmp    1188 <write_string+0x48>
lea    0xe82(%rip),%r8
lea    0xe71(%rip),%rdx
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14,%rcx
call   1060 <str_printf@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <free@plt>

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

LLVM IR (-O2):
@.str = [7 x i8] c"\22%s\22%s\00"
@.str.1 = [4 x i8] c"...\00"
@.str.2 = [1 x i8] zeroinitializer
define void @write_string(i8* %0, i32* %1, i8* %2) {
%4 = tail call i8* @escape(i8* %2)
%5 = getelementptr inbounds i8, i8* %2, i64 31
%6 = load i8, i8* %5
%7 = icmp eq i8 %6, 0
br i1 %7, label %12, label %8
8: ; preds = %3
%9 = tail call i32 @strlen(i8* %2)
%10 = icmp eq i32 %9, 32
%11 = select i1 %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0)
br label %12
12: ; preds = %8, %3
%13 = phi i8* [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.2, i64 0, i64 0), %3 ], [ %11, %8 ]
%14 = tail call i32 @str_printf(i8* %0, i32* %1, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i8* %4, i8* %13)
%15 = tail call i32 @free(i8* %4)
%16 = tail call i32 @free(i8* %2)
ret void
}