#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 ssize_t;
typedef int off_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char const *);
int read(int, char *, int);
int /*<<< orphan*/ replacechars(char *, char, char);
int serial_write(int, char *, int);
int /*<<< orphan*/ stderr;
void sendfiles(int filesc, const char *filesv[], int serialfd) {
  int filesfd;
  int i;
  ssize_t n_read, n_swritten;
  off_t n_current, n_total;
  unsigned int progress;
  char readbuf[200];
  for (i = 0; i < filesc; ++i) {
    filesfd = open(filesv[i], O_RDONLY);
    if (-1 == filesfd) {
      perror(filesv[i]);
      continue;
    }
    n_total = lseek(filesfd, 0, SEEK_END);
    progress = 0;
    fprintf(stderr, "%s, %zd bytes\n", filesv[i], n_total);
    lseek(filesfd, 0, SEEK_SET);
    while ((n_read = read(filesfd, readbuf, 200)) > 0) {
      replacechars(readbuf, '\n', '\r');
      replacechars(readbuf, '\t', ' ');
      n_swritten = serial_write(serialfd, readbuf, n_read);
      if ((progress += n_swritten) > 200) {
        n_current = lseek(filesfd, 0, SEEK_CUR);
        fprintf(stderr, "\b\b\b\b    \b\b\b\b%zd%%", 100 * n_current / n_total);
      }
    }
    fprintf(stderr, "\n");
  }
  return;
}


x86 assembly (clang, -O0):
<sendfiles>:
push   %rbp
mov    %rsp,%rbp
sub    $0x100,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1305 <sendfiles+0x195>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
mov    $0xffffffff,%eax
cmp    -0x18(%rbp),%eax
jne    11d9 <sendfiles+0x69>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1080 <perror@plt>
jmp    12f7 <sendfiles+0x187>
mov    -0x18(%rbp),%edi
mov    0x2e05(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
mov    -0x2c(%rbp),%ecx
lea    0xdeb(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%edi
mov    0x2d9a(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
mov    -0x18(%rbp),%edi
lea    -0x100(%rbp),%rsi
mov    $0xc8,%edx
call   1040 <read@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jle    12e0 <sendfiles+0x170>
lea    -0x100(%rbp),%rdi
mov    $0xa,%esi
mov    $0xd,%edx
call   1050 <replacechars@plt>
lea    -0x100(%rbp),%rdi
mov    $0x9,%esi
mov    $0x20,%edx
call   1050 <replacechars@plt>
mov    -0x14(%rbp),%edi
lea    -0x100(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1090 <serial_write@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
cmp    $0xc8,%eax
jbe    12db <sendfiles+0x16b>
mov    -0x18(%rbp),%edi
mov    0x2d32(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1030 <lseek@plt>
mov    %eax,-0x28(%rbp)
mov    0x2d07(%rip),%rax
mov    (%rax),%edi
imul   $0x64,-0x28(%rbp),%eax
cltd
idivl  -0x2c(%rbp)
mov    %eax,%edx
lea    0xd3b(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    122f <sendfiles+0xbf>
mov    0x2ce1(%rip),%rax
mov    (%rax),%edi
lea    0xd1d(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    118c <sendfiles+0x1c>
add    $0x100,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sendfiles>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xe8,%rsp
mov    %edx,(%rsp)
test   %edi,%edi
jle    131d <sendfiles+0x1ad>
mov    %rsi,%r14
mov    %edi,%eax
mov    %rax,0x10(%rsp)
lea    0x20(%rsp),%rbx
xor    %ebp,%ebp
mov    %rsi,0x8(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r14,%rbp,8),%rdi
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1312 <sendfiles+0x1a2>
mov    %eax,%r15d
mov    0x2e13(%rip),%rax
mov    (%rax),%edx
mov    %r15d,%edi
xor    %esi,%esi
call   1030 <lseek@plt>
mov    %eax,%ecx
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
mov    %rbp,0x18(%rsp)
mov    (%r14,%rbp,8),%rdx
lea    0xe04(%rip),%rsi
mov    %ecx,0x4(%rsp)
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%edx
mov    %r15d,%edi
xor    %esi,%esi
call   1030 <lseek@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0xc8,%edx
call   1040 <read@plt>
test   %eax,%eax
mov    (%rsp),%ebp
jle    12e0 <sendfiles+0x170>
mov    %eax,%r12d
xor    %r14d,%r14d
jmp    125b <sendfiles+0xeb>
nopl   (%rax)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0xc8,%edx
call   1040 <read@plt>
mov    %eax,%r12d
test   %eax,%eax
jle    12e0 <sendfiles+0x170>
mov    %r14d,%r13d
mov    %rbx,%rdi
mov    $0xa,%esi
mov    $0xd,%edx
call   1050 <replacechars@plt>
mov    %rbx,%rdi
mov    $0x9,%esi
mov    $0x20,%edx
call   1050 <replacechars@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1090 <serial_write@plt>
mov    %eax,%r14d
add    %r13d,%r14d
cmp    $0xc9,%r14d
jb     1240 <sendfiles+0xd0>
mov    0x2d3b(%rip),%rax
mov    (%rax),%edx
mov    %r15d,%edi
xor    %esi,%esi
call   1030 <lseek@plt>
mov    0x2d10(%rip),%rcx
mov    (%rcx),%edi
imul   $0x64,%eax,%eax
cltd
idivl  0x4(%rsp)
lea    0xd46(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1240 <sendfiles+0xd0>
nopw   0x0(%rax,%rax,1)
mov    0x2ce1(%rip),%rax
mov    (%rax),%edi
lea    0xd1d(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x8(%rsp),%r14
mov    0x18(%rsp),%rbp
add    $0x1,%rbp
cmp    0x10(%rsp),%rbp
jne    11b0 <sendfiles+0x40>
jmp    131d <sendfiles+0x1ad>
mov    (%r14,%rbp,8),%rdi
call   1080 <perror@plt>
jmp    1301 <sendfiles+0x191>
add    $0xe8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
@stderr = global i32 0
@.str = [15 x i8] c"%s, %zd bytes\0A\00"
@SEEK_SET = global i32 0
@SEEK_CUR = global i32 0
@.str.1 = [18 x i8] c"\08\08\08\08 \08\08\08\08%zd%%\00"
@.str.2 = [2 x i8] c"\0A\00"
define void @sendfiles(i32 %0, i8** %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca [200 x i8]
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 %2, i32* %6
store i32 0, i32* %8
br label %15
15: ; preds = %83, %3
%16 = load i32, i32* %8
%17 = load i32, i32* %4
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %86
19: ; preds = %15
%20 = load i8**, i8*** %5
%21 = load i32, i32* %8
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8*, i8** %20, i64 %22
%24 = load i8*, i8** %23
%25 = load i32, i32* @O_RDONLY
%26 = call i32 @open(i8* %24, i32 %25)
store i32 %26, i32* %7
%27 = load i32, i32* %7
%28 = icmp eq i32 -1, %27
br i1 %28, label %29, label %36
29: ; preds = %19
%30 = load i8**, i8*** %5
%31 = load i32, i32* %8
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8*, i8** %30, i64 %32
%34 = load i8*, i8** %33
%35 = call i32 @perror(i8* %34)
br label %83
36: ; preds = %19
%37 = load i32, i32* %7
%38 = load i32, i32* @SEEK_END
%39 = call i32 @lseek(i32 %37, i32 0, i32 %38)
store i32 %39, i32* %12
store i32 0, i32* %13
%40 = load i32, i32* @stderr
%41 = load i8**, i8*** %5
%42 = load i32, i32* %8
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8*, i8** %41, i64 %43
%45 = load i8*, i8** %44
%46 = load i32, i32* %12
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %45, i32 %46)
%48 = load i32, i32* %7
%49 = load i32, i32* @SEEK_SET
%50 = call i32 @lseek(i32 %48, i32 0, i32 %49)
br label %51
51: ; preds = %79, %36
%52 = load i32, i32* %7
%53 = getelementptr inbounds [200 x i8], [200 x i8]* %14, i64 0, i64 0
%54 = call i32 @read(i32 %52, i8* %53, i32 200)
store i32 %54, i32* %9
%55 = icmp sgt i32 %54, 0
br i1 %55, label %56, label %80
56: ; preds = %51
%57 = getelementptr inbounds [200 x i8], [200 x i8]* %14, i64 0, i64 0
%58 = call i32 @replacechars(i8* %57, i8 signext 10, i8 signext 13)
%59 = getelementptr inbounds [200 x i8], [200 x i8]* %14, i64 0, i64 0
%60 = call i32 @replacechars(i8* %59, i8 signext 9, i8 signext 32)
%61 = load i32, i32* %6
%62 = getelementptr inbounds [200 x i8], [200 x i8]* %14, i64 0, i64 0
%63 = load i32, i32* %9
%64 = call i32 @serial_write(i32 %61, i8* %62, i32 %63)
store i32 %64, i32* %10
%65 = load i32, i32* %10
%66 = load i32, i32* %13
%67 = add i32 %66, %65
store i32 %67, i32* %13
%68 = icmp ugt i32 %67, 200
br i1 %68, label %69, label %79
69: ; preds = %56
%70 = load i32, i32* %7
%71 = load i32, i32* @SEEK_CUR
%72 = call i32 @lseek(i32 %70, i32 0, i32 %71)
store i32 %72, i32* %11
%73 = load i32, i32* @stderr
%74 = load i32, i32* %11
%75 = mul nsw i32 100, %74
%76 = load i32, i32* %12
%77 = sdiv i32 %75, %76
%78 = call i32 (i32, i8*, ...) @fprintf(i32 %73, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %77)
br label %79
79: ; preds = %69, %56
br label %51
80: ; preds = %51
%81 = load i32, i32* @stderr
%82 = call i32 (i32, i8*, ...) @fprintf(i32 %81, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %83
83: ; preds = %80, %29
%84 = load i32, i32* %8
%85 = add nsw i32 %84, 1
store i32 %85, i32* %8
br label %15
86: ; preds = %15
ret void
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
@stderr = global i32 0
@.str = [15 x i8] c"%s, %zd bytes\0A\00"
@SEEK_SET = global i32 0
@SEEK_CUR = global i32 0
@.str.1 = [18 x i8] c"\08\08\08\08 \08\08\08\08%zd%%\00"
@.str.2 = [2 x i8] c"\0A\00"
define void @sendfiles(i32 %0, i8** nocapture %1, i32 %2) {
%4 = alloca [200 x i8]
%5 = getelementptr inbounds [200 x i8], [200 x i8]* %4, i64 0, i64 0
%6 = icmp sgt i32 %0, 0
br i1 %6, label %7, label %53
7: ; preds = %3
%8 = zext i32 %0 to i64
br label %9
9: ; preds = %7, %50
%10 = phi i64 [ 0, %7 ], [ %51, %50 ]
%11 = getelementptr inbounds i8*, i8** %1, i64 %10
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_RDONLY
%14 = call i32 @open(i8* %12, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %9
%17 = load i8*, i8** %11
%18 = call i32 @perror(i8* %17)
br label %50
19: ; preds = %9
%20 = load i32, i32* @SEEK_END
%21 = call i32 @lseek(i32 %14, i32 0, i32 %20)
%22 = load i32, i32* @stderr
%23 = load i8*, i8** %11
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %23, i32 %21)
%25 = load i32, i32* @SEEK_SET
%26 = call i32 @lseek(i32 %14, i32 0, i32 %25)
%27 = call i32 @read(i32 %14, i8* %5, i32 200)
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %47
29: ; preds = %19, %44
%30 = phi i32 [ %45, %44 ], [ %27, %19 ]
%31 = phi i32 [ %35, %44 ], [ 0, %19 ]
%32 = call i32 @replacechars(i8* %5, i8 signext 10, i8 signext 13)
%33 = call i32 @replacechars(i8* %5, i8 signext 9, i8 signext 32)
%34 = call i32 @serial_write(i32 %2, i8* %5, i32 %30)
%35 = add i32 %34, %31
%36 = icmp ugt i32 %35, 200
br i1 %36, label %37, label %44
37: ; preds = %29
%38 = load i32, i32* @SEEK_CUR
%39 = call i32 @lseek(i32 %14, i32 0, i32 %38)
%40 = load i32, i32* @stderr
%41 = mul nsw i32 %39, 100
%42 = sdiv i32 %41, %21
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %42)
br label %44
44: ; preds = %37, %29
%45 = call i32 @read(i32 %14, i8* %5, i32 200)
%46 = icmp sgt i32 %45, 0
br i1 %46, label %29, label %47
47: ; preds = %44, %19
%48 = load i32, i32* @stderr
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %50
50: ; preds = %47, %16
%51 = add nuw nsw i64 %10, 1
%52 = icmp eq i64 %51, %8
br i1 %52, label %53, label %9
53: ; preds = %50, %3
ret void
}