#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 scalar_t__ ssize_t;
typedef int /*<<< orphan*/ off_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ read(int, void *, int);
char *strerror(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  if (argc < 2) {
    printf("usage: ov-to-unix infile [outfile]\n");
    return 0;
  }
  int fin;
  fin = open(argv[1], O_RDONLY);
  if (fin == -1) {
    printf("failed to open %s: %s\n", argv[1], strerror(errno));
    return 1;
  }
  int lineno = 1;
  do {
    int linelen;
    ssize_t status;
    off_t pos;
    char line[256 + 1];
    unsigned char ll0, ll1;
    status = read(fin, (void *)&ll0, 1);
    if (status == 0)
      break;
    if (ll0 == 0)
      continue;
    status = read(fin, (void *)&ll1, 1);
    if (status == 0)
      break;
    linelen = (unsigned int)ll1 * 256 + ll0;
    if (linelen == 0)
      continue;
    if (linelen > 256) {
      pos = lseek(fin, 0, SEEK_CUR);
      printf(
          "error at line %d, current file offset %ld, line length %04xh > %d\n",
          lineno, pos, linelen, 256);
      continue;
    }
    status = read(fin, (void *)line, linelen);
    if (status == 0)
      break;
    line[linelen] = 0;
    printf("%s\n", line);
    lineno++;
  } while (1);
  close(fin);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    119d <main+0x3d>
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1345 <main+0x1e5>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1201 <main+0xa1>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x140(%rbp)
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x140(%rbp),%rsi
mov    %rax,%rdx
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1345 <main+0x1e5>
movl   $0x1,-0x18(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x131(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1230 <main+0xd0>
jmp    1336 <main+0x1d6>
movzbl -0x131(%rbp),%eax
cmp    $0x0,%eax
jne    1245 <main+0xe5>
jmp    1327 <main+0x1c7>
mov    -0x14(%rbp),%edi
lea    -0x132(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    126d <main+0x10d>
jmp    1336 <main+0x1d6>
movzbl -0x132(%rbp),%eax
shl    $0x8,%eax
movzbl -0x131(%rbp),%ecx
add    %ecx,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1292 <main+0x132>
jmp    1327 <main+0x1c7>
cmpl   $0x100,-0x1c(%rbp)
jle    12d7 <main+0x177>
mov    -0x14(%rbp),%edi
mov    0x2d3f(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%esi
mov    -0x2c(%rbp),%edx
mov    -0x1c(%rbp),%ecx
lea    0xd76(%rip),%rdi
mov    $0x100,%r8d
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1327 <main+0x1c7>
mov    -0x14(%rbp),%edi
lea    -0x130(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1060 <read@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12fd <main+0x19d>
jmp    1336 <main+0x1d6>
movslq -0x1c(%rbp),%rax
movb   $0x0,-0x130(%rbp,%rax,1)
lea    -0x130(%rbp),%rsi
lea    0xd20(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    $0x1,%al
test   $0x1,%al
jne    1208 <main+0xa8>
jmp    1336 <main+0x1d6>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
cmp    $0x1,%edi
jg     1197 <main+0x27>
lea    0xecd(%rip),%rdi
call   1030 <puts@plt>
jmp    1294 <main+0x124>
mov    %rsi,%rbp
mov    0x8(%rsi),%rdi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1298 <main+0x128>
mov    %eax,%ebx
lea    0xe(%rsp),%rsi
mov    $0x1,%ebp
mov    %eax,%edi
mov    $0x1,%edx
call   1070 <read@plt>
test   %rax,%rax
je     128d <main+0x11d>
lea    0xe(%rsp),%r14
lea    0x10(%rsp),%r13
jmp    1231 <main+0xc1>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x2df1(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
lea    0xe0e(%rip),%rdi
mov    %ebp,%esi
mov    %eax,%edx
mov    %r12d,%ecx
mov    $0x100,%r8d
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
test   %rax,%rax
je     128d <main+0x11d>
movzbl 0xe(%rsp),%r15d
test   %r15,%r15
je     121d <main+0xad>
mov    %ebx,%edi
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
test   %rax,%rax
je     128d <main+0x11d>
movzbl 0xf(%rsp),%r12d
shl    $0x8,%r12
or     %r15,%r12
cmp    $0x101,%r12d
jae    11f0 <main+0x80>
mov    %ebx,%edi
mov    %r13,%rsi
mov    %r12d,%edx
call   1070 <read@plt>
test   %rax,%rax
je     128d <main+0x11d>
movb   $0x0,0x10(%rsp,%r12,1)
mov    %r13,%rdi
call   1030 <puts@plt>
add    $0x1,%ebp
jmp    121d <main+0xad>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
jmp    12c3 <main+0x153>
mov    0x8(%rbp),%rbx
mov    0x2d2d(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd4f(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [36 x i8] c"usage: ov-to-unix infile [outfile]\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [23 x i8] c"failed to open %s: %s\0A\00"
@errno = global i32 0
@SEEK_CUR = global i32 0
@.str.2 = [67 x i8] c"error at line %d, current file offset %ld, line length %04xh > %d\0A\00"
@.str.3 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i64
%10 = alloca i32
%11 = alloca [257 x i8]
%12 = alloca i8
%13 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp slt i32 %14, 2
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %92
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = load i32, i32* @O_RDONLY
%23 = call i32 @open(i8* %21, i32 %22)
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %33
26: ; preds = %18
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = load i32, i32* @errno
%31 = call i8* @strerror(i32 %30)
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %29, i8* %31)
store i32 1, i32* %3
br label %92
33: ; preds = %18
store i32 1, i32* %7
br label %34
34: ; preds = %88, %33
%35 = load i32, i32* %6
%36 = call i64 @read(i32 %35, i8* %12, i32 1)
store i64 %36, i64* %9
%37 = load i64, i64* %9
%38 = icmp eq i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %34
br label %89
40: ; preds = %34
%41 = load i8, i8* %12
%42 = zext i8 %41 to i32
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %40
br label %88
45: ; preds = %40
%46 = load i32, i32* %6
%47 = call i64 @read(i32 %46, i8* %13, i32 1)
store i64 %47, i64* %9
%48 = load i64, i64* %9
%49 = icmp eq i64 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %45
br label %89
51: ; preds = %45
%52 = load i8, i8* %13
%53 = zext i8 %52 to i32
%54 = mul i32 %53, 256
%55 = load i8, i8* %12
%56 = zext i8 %55 to i32
%57 = add i32 %54, %56
store i32 %57, i32* %8
%58 = load i32, i32* %8
%59 = icmp eq i32 %58, 0
br i1 %59, label %60, label %61
60: ; preds = %51
br label %88
61: ; preds = %51
%62 = load i32, i32* %8
%63 = icmp sgt i32 %62, 256
br i1 %63, label %64, label %72
64: ; preds = %61
%65 = load i32, i32* %6
%66 = load i32, i32* @SEEK_CUR
%67 = call i32 @lseek(i32 %65, i32 0, i32 %66)
store i32 %67, i32* %10
%68 = load i32, i32* %7
%69 = load i32, i32* %10
%70 = load i32, i32* %8
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.2, i64 0, i64 0), i32 %68, i32 %69, i32 %70, i32 256)
br label %88
72: ; preds = %61
%73 = load i32, i32* %6
%74 = getelementptr inbounds [257 x i8], [257 x i8]* %11, i64 0, i64 0
%75 = load i32, i32* %8
%76 = call i64 @read(i32 %73, i8* %74, i32 %75)
store i64 %76, i64* %9
%77 = load i64, i64* %9
%78 = icmp eq i64 %77, 0
br i1 %78, label %79, label %80
79: ; preds = %72
br label %89
80: ; preds = %72
%81 = load i32, i32* %8
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds [257 x i8], [257 x i8]* %11, i64 0, i64 %82
store i8 0, i8* %83
%84 = getelementptr inbounds [257 x i8], [257 x i8]* %11, i64 0, i64 0
%85 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %84)
%86 = load i32, i32* %7
%87 = add nsw i32 %86, 1
store i32 %87, i32* %7
br label %88
88: ; preds = %80, %64, %60, %44
br i1 true, label %34, label %89
89: ; preds = %88, %79, %50, %39
%90 = load i32, i32* %6
%91 = call i32 @close(i32 %90)
store i32 0, i32* %3
br label %92
92: ; preds = %89, %26, %16
%93 = load i32, i32* %3
ret i32 %93
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str.1 = [23 x i8] c"failed to open %s: %s\0A\00"
@errno = global i32 0
@SEEK_CUR = global i32 0
@.str.2 = [67 x i8] c"error at line %d, current file offset %ld, line length %04xh > %d\0A\00"
@str = [35 x i8] c"usage: ov-to-unix infile [outfile]\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [257 x i8]
%4 = alloca i8
%5 = alloca i8
%6 = icmp slt i32 %0, 2
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
br label %56
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = load i32, i32* @O_RDONLY
%13 = tail call i32 @open(i8* %11, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %19, label %15
15: ; preds = %9
%16 = getelementptr inbounds [257 x i8], [257 x i8]* %3, i64 0, i64 0
%17 = call i64 @read(i32 %13, i8* %4, i32 1)
%18 = icmp eq i64 %17, 0
br i1 %18, label %54, label %24
19: ; preds = %9
%20 = load i8*, i8** %10
%21 = load i32, i32* @errno
%22 = tail call i8* @strerror(i32 %21)
%23 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %20, i8* %22)
br label %56
24: ; preds = %15, %50
%25 = phi i32 [ %51, %50 ], [ 1, %15 ]
%26 = load i8, i8* %4
%27 = icmp eq i8 %26, 0
br i1 %27, label %50, label %28
28: ; preds = %24
%29 = call i64 @read(i32 %13, i8* %5, i32 1)
%30 = icmp eq i64 %29, 0
br i1 %30, label %54, label %31
31: ; preds = %28
%32 = load i8, i8* %5
%33 = zext i8 %32 to i32
%34 = shl nuw nsw i32 %33, 8
%35 = zext i8 %26 to i32
%36 = or i32 %34, %35
%37 = icmp ugt i32 %36, 256
br i1 %37, label %38, label %42
38: ; preds = %31
%39 = load i32, i32* @SEEK_CUR
%40 = tail call i32 @lseek(i32 %13, i32 0, i32 %39)
%41 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([67 x i8], [67 x i8]* @.str.2, i64 0, i64 0), i32 %25, i32 %40, i32 %36, i32 256)
br label %50
42: ; preds = %31
%43 = call i64 @read(i32 %13, i8* %16, i32 %36)
%44 = icmp eq i64 %43, 0
br i1 %44, label %54, label %45
45: ; preds = %42
%46 = zext i32 %36 to i64
%47 = getelementptr inbounds [257 x i8], [257 x i8]* %3, i64 0, i64 %46
store i8 0, i8* %47
%48 = call i32 @puts(i8* %16)
%49 = add nsw i32 %25, 1
br label %50
50: ; preds = %24, %45, %38
%51 = phi i32 [ %25, %38 ], [ %49, %45 ], [ %25, %24 ]
%52 = call i64 @read(i32 %13, i8* %4, i32 1)
%53 = icmp eq i64 %52, 0
br i1 %53, label %54, label %24
54: ; preds = %42, %28, %50, %15
%55 = tail call i32 @close(i32 %13)
br label %56
56: ; preds = %19, %54, %7
%57 = phi i32 [ 0, %7 ], [ 1, %19 ], [ 0, %54 ]
ret i32 %57
}