#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 */
scalar_t__ isprint(unsigned char);
scalar_t__ snprintf(char *, int, char *, unsigned char);
int /*<<< orphan*/ strncat(char *, char *, int);
int trace_write_end(char *out, int size, unsigned char *start, int col) {
  int pos = 0, w;
  strncat(out, " |", size - pos);
  pos += 2;
  for (w = 0; w < col; w++) {
    pos += snprintf(out + pos, size - pos, "%c",
                    isprint(start[w]) ? start[w] : '.');
  }
  strncat(out + pos, "|\n", size - pos);
  pos += 2;
  return pos;
}


x86 assembly (clang, -O0):
<trace_write_end>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%edx
sub    -0x20(%rbp),%edx
lea    0xea2(%rip),%rsi
call   1040 <strncat@plt>
mov    -0x20(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1207 <trace_write_end+0xd7>
mov    -0x8(%rbp),%rax
movslq -0x20(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0xc(%rbp),%eax
sub    -0x20(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
movzbl (%rax,%rcx,1),%edi
call   1050 <isprint@plt>
cmp    $0x0,%rax
je     11c6 <trace_write_end+0x96>
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
mov    %eax,-0x34(%rbp)
jmp    11d3 <trace_write_end+0xa3>
mov    $0x2e,%eax
mov    %eax,-0x34(%rbp)
jmp    11d3 <trace_write_end+0xa3>
mov    -0x28(%rbp),%esi
mov    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%eax
lea    0xe1f(%rip),%rdx
movzbl %al,%ecx
call   1030 <snprintf@plt>
mov    %rax,%rcx
movslq -0x20(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1173 <trace_write_end+0x43>
mov    -0x8(%rbp),%rdi
movslq -0x20(%rbp),%rax
add    %rax,%rdi
mov    -0xc(%rbp),%edx
sub    -0x20(%rbp),%edx
lea    0xde7(%rip),%rsi
call   1040 <strncat@plt>
mov    -0x20(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<trace_write_end>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%ebx
mov    %rdx,%r13
mov    %esi,%edx
lea    0xeb4(%rip),%rsi
mov    %rdi,0x10(%rsp)
mov    %edx,0xc(%rsp)
call   1040 <strncat@plt>
test   %ebx,%ebx
jle    11bc <trace_write_end+0x8c>
mov    %ebx,%r14d
mov    $0x2,%r15d
xor    %r12d,%r12d
jmp    1196 <trace_write_end+0x66>
nopl   0x0(%rax)
movzbl 0x0(%r13,%r12,1),%eax
movzbl %al,%ecx
mov    %rbx,%rdi
mov    %ebp,%esi
lea    0xe7e(%rip),%rdx
call   1030 <snprintf@plt>
add    %eax,%r15d
add    $0x1,%r12
cmp    %r12,%r14
je     11c2 <trace_write_end+0x92>
movslq %r15d,%rax
mov    0x10(%rsp),%rcx
lea    (%rcx,%rax,1),%rbx
mov    0xc(%rsp),%ebp
sub    %eax,%ebp
movzbl 0x0(%r13,%r12,1),%edi
call   1050 <isprint@plt>
test   %rax,%rax
jne    1170 <trace_write_end+0x40>
mov    $0x2e,%al
jmp    1176 <trace_write_end+0x46>
mov    $0x2,%r15d
movslq %r15d,%rbx
mov    0x10(%rsp),%rdi
add    %rbx,%rdi
mov    0xc(%rsp),%edx
sub    %ebx,%edx
lea    0xe2c(%rip),%rsi
call   1040 <strncat@plt>
add    $0x2,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c" |\00"
@.str.1 = [3 x i8] c"%c\00"
@.str.2 = [3 x i8] c"|\0A\00"
define i32 @trace_write_end(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
store i32 0, i32* %9
%11 = load i8*, i8** %5
%12 = load i32, i32* %6
%13 = load i32, i32* %9
%14 = sub nsw i32 %12, %13
%15 = call i32 @strncat(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = load i32, i32* %9
%17 = add nsw i32 %16, 2
store i32 %17, i32* %9
store i32 0, i32* %10
br label %18
18: ; preds = %53, %4
%19 = load i32, i32* %10
%20 = load i32, i32* %8
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %56
22: ; preds = %18
%23 = load i8*, i8** %5
%24 = load i32, i32* %9
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = load i32, i32* %6
%28 = load i32, i32* %9
%29 = sub nsw i32 %27, %28
%30 = load i8*, i8** %7
%31 = load i32, i32* %10
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %30, i64 %32
%34 = load i8, i8* %33
%35 = call i64 @isprint(i8 zeroext %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %44
37: ; preds = %22
%38 = load i8*, i8** %7
%39 = load i32, i32* %10
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %38, i64 %40
%42 = load i8, i8* %41
%43 = zext i8 %42 to i32
br label %45
44: ; preds = %22
br label %45
45: ; preds = %44, %37
%46 = phi i32 [ %43, %37 ], [ 46, %44 ]
%47 = trunc i32 %46 to i8
%48 = call i64 @snprintf(i8* %26, i32 %29, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %47)
%49 = load i32, i32* %9
%50 = sext i32 %49 to i64
%51 = add nsw i64 %50, %48
%52 = trunc i64 %51 to i32
store i32 %52, i32* %9
br label %53
53: ; preds = %45
%54 = load i32, i32* %10
%55 = add nsw i32 %54, 1
store i32 %55, i32* %10
br label %18
56: ; preds = %18
%57 = load i8*, i8** %5
%58 = load i32, i32* %9
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %57, i64 %59
%61 = load i32, i32* %6
%62 = load i32, i32* %9
%63 = sub nsw i32 %61, %62
%64 = call i32 @strncat(i8* %60, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %63)
%65 = load i32, i32* %9
%66 = add nsw i32 %65, 2
store i32 %66, i32* %9
%67 = load i32, i32* %9
ret i32 %67
}

LLVM IR (-O2):
@.str = [3 x i8] c" |\00"
@.str.1 = [3 x i8] c"%c\00"
@.str.2 = [3 x i8] c"|\0A\00"
define i32 @trace_write_end(i8* %0, i32 %1, i8* nocapture %2, i32 %3) {
%5 = tail call i32 @strncat(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %1)
%6 = icmp sgt i32 %3, 0
br i1 %6, label %7, label %28
7: ; preds = %4
%8 = zext i32 %3 to i64
br label %9
9: ; preds = %7, %21
%10 = phi i64 [ 0, %7 ], [ %26, %21 ]
%11 = phi i32 [ 2, %7 ], [ %25, %21 ]
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %0, i64 %12
%14 = sub nsw i32 %1, %11
%15 = getelementptr inbounds i8, i8* %2, i64 %10
%16 = load i8, i8* %15
%17 = tail call i64 @isprint(i8 zeroext %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %9
%20 = load i8, i8* %15
br label %21
21: ; preds = %9, %19
%22 = phi i8 [ %20, %19 ], [ 46, %9 ]
%23 = tail call i64 @snprintf(i8* %13, i32 %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8 zeroext %22)
%24 = trunc i64 %23 to i32
%25 = add i32 %11, %24
%26 = add nuw nsw i64 %10, 1
%27 = icmp eq i64 %26, %8
br i1 %27, label %28, label %9
28: ; preds = %21, %4
%29 = phi i32 [ 2, %4 ], [ %25, %21 ]
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %0, i64 %30
%32 = sub nsw i32 %1, %29
%33 = tail call i32 @strncat(i8* %31, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %32)
%34 = add nsw i32 %29, 2
ret i32 %34
}