#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 */
size_t effectiveLen(char const *);
int /*<<< orphan*/ snprintf(char *, int, char *, ...);
size_t strlen(char const *);
int write(int, char const *, size_t);
void refreshLine(int fd, const char *prompt, char *buf, size_t len, size_t pos,
                 size_t cols) {
  char seq[64];
  size_t plen = effectiveLen(prompt);
  while ((plen + pos) >= cols) {
    buf++;
    len--;
    pos--;
  }
  while (plen + len > cols) {
    len--;
  }
  snprintf(seq, 64, "\x1b[0G");
  if (write(fd, seq, strlen(seq)) == -1)
    return;
  if (write(fd, prompt, strlen(prompt)) == -1)
    return;
  if (write(fd, buf, len) == -1)
    return;
  snprintf(seq, 64, "\x1b[0K");
  if (write(fd, seq, strlen(seq)) == -1)
    return;
  snprintf(seq, 64, "\x1b[0G\x1b[%dC", (int)(pos + plen));
  if (write(fd, seq, strlen(seq)) == -1)
    return;
}


x86 assembly (clang, -O0):
<refreshLine>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <effectiveLen@plt>
mov    %rax,-0x78(%rbp)
mov    -0x78(%rbp),%rax
add    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
jb     11aa <refreshLine+0x6a>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
jmp    116f <refreshLine+0x2f>
jmp    11af <refreshLine+0x6f>
mov    -0x78(%rbp),%rax
add    -0x20(%rbp),%rax
cmp    -0x30(%rbp),%rax
jbe    11d2 <refreshLine+0x92>
mov    -0x20(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x20(%rbp)
jmp    11af <refreshLine+0x6f>
lea    -0x70(%rbp),%rdi
mov    $0x40,%esi
lea    0xe1e(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x84(%rbp)
lea    -0x70(%rbp),%rax
mov    %rax,-0x80(%rbp)
lea    -0x70(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x84(%rbp),%edi
mov    -0x80(%rbp),%rsi
mov    %rax,%rdx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
jne    1223 <refreshLine+0xe3>
jmp    1339 <refreshLine+0x1f9>
mov    -0x4(%rbp),%eax
mov    %eax,-0x94(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x90(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x94(%rbp),%edi
mov    -0x90(%rbp),%rsi
mov    %rax,%rdx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
jne    1263 <refreshLine+0x123>
jmp    1339 <refreshLine+0x1f9>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
jne    1281 <refreshLine+0x141>
jmp    1339 <refreshLine+0x1f9>
lea    -0x70(%rbp),%rdi
mov    $0x40,%esi
lea    0xd74(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0xa4(%rbp)
lea    -0x70(%rbp),%rax
mov    %rax,-0xa0(%rbp)
lea    -0x70(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0xa4(%rbp),%edi
mov    -0xa0(%rbp),%rsi
mov    %rax,%rdx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
jne    12d8 <refreshLine+0x198>
jmp    1339 <refreshLine+0x1f9>
lea    -0x70(%rbp),%rdi
mov    -0x28(%rbp),%rax
add    -0x78(%rbp),%rax
mov    %eax,%ecx
mov    $0x40,%esi
lea    0xd18(%rip),%rdx
mov    $0x0,%al
call   1060 <snprintf@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0xb4(%rbp)
lea    -0x70(%rbp),%rax
mov    %rax,-0xb0(%rbp)
lea    -0x70(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0xb4(%rbp),%edi
mov    -0xb0(%rbp),%rsi
mov    %rax,%rdx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
jne    1339 <refreshLine+0x1f9>
jmp    1339 <refreshLine+0x1f9>
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<refreshLine>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %r9,%r14
mov    %r8,%r15
mov    %rcx,%rbp
mov    %rdx,%rbx
mov    %rsi,%r13
mov    %edi,0x4(%rsp)
mov    %rsi,%rdi
call   1030 <effectiveLen@plt>
mov    %rax,%r12
add    %rax,%r15
cmp    %r14,%r15
jae    117b <refreshLine+0x3b>
mov    %rbx,0x8(%rsp)
jmp    119e <refreshLine+0x5e>
lea    -0x1(%r15),%rax
lea    -0x1(%r14),%rcx
cmp    %rcx,%rax
cmovb  %rax,%rcx
add    %rcx,%rbp
sub    %r15,%rbp
sub    %rcx,%r15
add    %r15,%rbx
mov    %rbx,0x8(%rsp)
mov    %rcx,%r15
add    %r12,%rbp
cmp    %rbp,%r14
cmovb  %r14,%rbp
lea    0xe51(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
xor    %eax,%eax
call   1060 <snprintf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    0x4(%rsp),%r14d
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     1288 <refreshLine+0x148>
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    %r14d,%edi
mov    %r13,%rsi
mov    %rax,%rdx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     1288 <refreshLine+0x148>
sub    %r12,%rbp
mov    %r14d,%edi
mov    0x8(%rsp),%rsi
mov    %rbp,%rdx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     1288 <refreshLine+0x148>
lea    0xde0(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
xor    %eax,%eax
call   1060 <snprintf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     1288 <refreshLine+0x148>
lea    0xdaf(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x40,%esi
mov    %r15d,%ecx
xor    %eax,%eax
call   1060 <snprintf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1040 <write@plt>
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"\1B[0G\00"
@.str.1 = [5 x i8] c"\1B[0K\00"
@.str.2 = [10 x i8] c"\1B[0G\1B[%dC\00"
define void @refreshLine(i32 %0, i8* %1, i8* %2, i64 %3, i64 %4, i64 %5) {
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
%13 = alloca [64 x i8]
%14 = alloca i64
store i32 %0, i32* %7
store i8* %1, i8** %8
store i8* %2, i8** %9
store i64 %3, i64* %10
store i64 %4, i64* %11
store i64 %5, i64* %12
%15 = load i8*, i8** %8
%16 = call i64 @effectiveLen(i8* %15)
store i64 %16, i64* %14
br label %17
17: ; preds = %23, %6
%18 = load i64, i64* %14
%19 = load i64, i64* %11
%20 = add i64 %18, %19
%21 = load i64, i64* %12
%22 = icmp uge i64 %20, %21
br i1 %22, label %23, label %30
23: ; preds = %17
%24 = load i8*, i8** %9
%25 = getelementptr inbounds i8, i8* %24, i32 1
store i8* %25, i8** %9
%26 = load i64, i64* %10
%27 = add i64 %26, -1
store i64 %27, i64* %10
%28 = load i64, i64* %11
%29 = add i64 %28, -1
store i64 %29, i64* %11
br label %17
30: ; preds = %17
br label %31
31: ; preds = %37, %30
%32 = load i64, i64* %14
%33 = load i64, i64* %10
%34 = add i64 %32, %33
%35 = load i64, i64* %12
%36 = icmp ugt i64 %34, %35
br i1 %36, label %37, label %40
37: ; preds = %31
%38 = load i64, i64* %10
%39 = add i64 %38, -1
store i64 %39, i64* %10
br label %31
40: ; preds = %31
%41 = getelementptr inbounds [64 x i8], [64 x i8]* %13, i64 0, i64 0
%42 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %41, i32 64, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%43 = load i32, i32* %7
%44 = getelementptr inbounds [64 x i8], [64 x i8]* %13, i64 0, i64 0
%45 = getelementptr inbounds [64 x i8], [64 x i8]* %13, i64 0, i64 0
%46 = call i64 @strlen(i8* %45)
%47 = call i32 @write(i32 %43, i8* %44, i64 %46)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %50
49: ; preds = %40
br label %89
50: ; preds = %40
%51 = load i32, i32* %7
%52 = load i8*, i8** %8
%53 = load i8*, i8** %8
%54 = call i64 @strlen(i8* %53)
%55 = call i32 @write(i32 %51, i8* %52, i64 %54)
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %58
57: ; preds = %50
br label %89
58: ; preds = %50
%59 = load i32, i32* %7
%60 = load i8*, i8** %9
%61 = load i64, i64* %10
%62 = call i32 @write(i32 %59, i8* %60, i64 %61)
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %65
64: ; preds = %58
br label %89
65: ; preds = %58
%66 = getelementptr inbounds [64 x i8], [64 x i8]* %13, i64 0, i64 0
%67 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %66, i32 64, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%68 = load i32, i32* %7
%69 = getelementptr inbounds [64 x i8], [64 x i8]* %13, i64 0, i64 0
%70 = getelementptr inbounds [64 x i8], [64 x i8]* %13, i64 0, i64 0
%71 = call i64 @strlen(i8* %70)
%72 = call i32 @write(i32 %68, i8* %69, i64 %71)
%73 = icmp eq i32 %72, -1
br i1 %73, label %74, label %75
74: ; preds = %65
br label %89
75: ; preds = %65
%76 = getelementptr inbounds [64 x i8], [64 x i8]* %13, i64 0, i64 0
%77 = load i64, i64* %11
%78 = load i64, i64* %14
%79 = add i64 %77, %78
%80 = trunc i64 %79 to i32
%81 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %76, i32 64, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %80)
%82 = load i32, i32* %7
%83 = getelementptr inbounds [64 x i8], [64 x i8]* %13, i64 0, i64 0
%84 = getelementptr inbounds [64 x i8], [64 x i8]* %13, i64 0, i64 0
%85 = call i64 @strlen(i8* %84)
%86 = call i32 @write(i32 %82, i8* %83, i64 %85)
%87 = icmp eq i32 %86, -1
br i1 %87, label %88, label %89
88: ; preds = %75
br label %89
89: ; preds = %49, %57, %64, %74, %88, %75
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"\1B[0G\00"
@.str.1 = [5 x i8] c"\1B[0K\00"
@.str.2 = [10 x i8] c"\1B[0G\1B[%dC\00"
define void @refreshLine(i32 %0, i8* %1, i8* nocapture %2, i64 %3, i64 %4, i64 %5) {
%7 = alloca [64 x i8]
%8 = getelementptr inbounds [64 x i8], [64 x i8]* %7, i64 0, i64 0
%9 = tail call i64 @effectiveLen(i8* %1)
%10 = add i64 %9, %4
%11 = icmp ult i64 %10, %5
br i1 %11, label %21, label %12
12: ; preds = %6
%13 = add i64 %10, -1
%14 = add i64 %5, -1
%15 = call i64 @llvm.umin.i64(i64 %13, i64 %14)
%16 = sub i64 %10, %15
%17 = add i64 %15, %3
%18 = getelementptr i8, i8* %2, i64 %16
%19 = add i64 %9, %4
%20 = sub i64 %17, %19
br label %21
21: ; preds = %12, %6
%22 = phi i64 [ %3, %6 ], [ %20, %12 ]
%23 = phi i8* [ %2, %6 ], [ %18, %12 ]
%24 = phi i64 [ %10, %6 ], [ %15, %12 ]
%25 = add i64 %9, %22
%26 = call i64 @llvm.umin.i64(i64 %5, i64 %25)
%27 = sub i64 %26, %9
%28 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %8, i32 64, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%29 = call i64 @strlen(i8* %8)
%30 = call i32 @write(i32 %0, i8* %8, i64 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %49, label %32
32: ; preds = %21
%33 = call i64 @strlen(i8* dereferenceable(1) %1)
%34 = call i32 @write(i32 %0, i8* %1, i64 %33)
%35 = icmp eq i32 %34, -1
br i1 %35, label %49, label %36
36: ; preds = %32
%37 = call i32 @write(i32 %0, i8* %23, i64 %27)
%38 = icmp eq i32 %37, -1
br i1 %38, label %49, label %39
39: ; preds = %36
%40 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %8, i32 64, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%41 = call i64 @strlen(i8* %8)
%42 = call i32 @write(i32 %0, i8* %8, i64 %41)
%43 = icmp eq i32 %42, -1
br i1 %43, label %49, label %44
44: ; preds = %39
%45 = trunc i64 %24 to i32
%46 = call i32 (i8*, i32, i8*, ...) @snprintf(i8* %8, i32 64, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %45)
%47 = call i64 @strlen(i8* %8)
%48 = call i32 @write(i32 %0, i8* %8, i64 %47)
br label %49
49: ; preds = %44, %39, %36, %32, %21
ret void
}