#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 */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int atoi(char *);
int /*<<< orphan*/ error(char *, char *);
scalar_t__ lseek(int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  if (argc != 4)
    error("Usage: %s <file> <chars> <pos>", argv[0]);
  char buf[1024];
  int fd, count;
  int n = atoi(argv[2]), t = atoi(argv[3]);
  if ((fd = open(argv[1], O_RDONLY)) == -1)
    error("cp: failed to open %s", argv[1]);
  if (lseek(fd, t, SEEK_SET) >= 0)
    if (n < 1024) {
      count = read(fd, buf, n);
      write(1, buf, count);
    } else
      while (n > 0) {
        count = read(fd, buf, 1024 - 1);
        write(1, buf, count);
        if ((n -= count) > 1024)
          continue;
        else {
          read(fd, buf, n);
          write(1, buf, n);
          break;
        }
      }
  else
    error("readnt: can't seek to position %s", argv[3]);
  write(1, "\n", 1);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     1196 <main+0x36>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe6f(%rip),%rdi
call   1060 <error@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x41c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x420(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x414(%rbp)
cmp    $0xffffffff,%eax
jne    11f5 <main+0x95>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe2f(%rip),%rdi
call   1060 <error@plt>
mov    -0x414(%rbp),%edi
mov    -0x420(%rbp),%esi
mov    0x2dd0(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
cmp    $0x0,%rax
jl     1306 <main+0x1a6>
cmpl   $0x400,-0x41c(%rbp)
jge    1263 <main+0x103>
mov    -0x414(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x41c(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x418(%rbp)
lea    -0x410(%rbp),%rsi
mov    -0x418(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
jmp    1301 <main+0x1a1>
jmp    1268 <main+0x108>
cmpl   $0x0,-0x41c(%rbp)
jle    12fc <main+0x19c>
mov    -0x414(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x3ff,%edx
call   1050 <read@plt>
mov    %eax,-0x418(%rbp)
lea    -0x410(%rbp),%rsi
mov    -0x418(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    -0x418(%rbp),%ecx
mov    -0x41c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x41c(%rbp)
cmp    $0x400,%eax
jle    12cd <main+0x16d>
jmp    1268 <main+0x108>
mov    -0x414(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    -0x41c(%rbp),%edx
call   1050 <read@plt>
lea    -0x410(%rbp),%rsi
mov    -0x41c(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
jmp    1301 <main+0x1a1>
jmp    131a <main+0x1ba>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rsi
lea    0xd20(%rip),%rdi
call   1060 <error@plt>
mov    $0x1,%edx
lea    0xd31(%rip),%rsi
mov    %edx,%edi
call   1030 <write@plt>
xor    %eax,%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %rsi,%r14
cmp    $0x4,%edi
je     1186 <main+0x26>
mov    (%r14),%rsi
lea    0xe7f(%rip),%rdi
call   1060 <error@plt>
mov    0x10(%r14),%rdi
call   1080 <atoi@plt>
mov    %eax,%ebx
mov    0x18(%r14),%rdi
call   1080 <atoi@plt>
mov    %eax,%r15d
mov    0x8(%r14),%rdi
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
jne    11c7 <main+0x67>
mov    0x8(%r14),%rsi
lea    0xe5d(%rip),%rdi
call   1060 <error@plt>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edx
mov    %r12d,%edi
mov    %r15d,%esi
call   1040 <lseek@plt>
test   %rax,%rax
js     1204 <main+0xa4>
cmp    $0x400,%ebx
jge    1216 <main+0xb6>
mov    %rsp,%rbp
mov    %r12d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1050 <read@plt>
mov    $0x1,%edi
mov    %rbp,%rsi
mov    %eax,%edx
jmp    1265 <main+0x105>
mov    0x18(%r14),%rsi
lea    0xe26(%rip),%rdi
call   1060 <error@plt>
jmp    126a <main+0x10a>
mov    %rsp,%r14
nopl   0x0(%rax)
mov    %r12d,%edi
mov    %r14,%rsi
mov    $0x3ff,%edx
call   1050 <read@plt>
mov    %eax,%ebp
mov    $0x1,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
sub    %ebp,%ebx
cmp    $0x400,%ebx
jg     1220 <main+0xc0>
mov    %rsp,%rbp
mov    %r12d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1050 <read@plt>
mov    $0x1,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
lea    0xde6(%rip),%rsi
mov    $0x1,%edi
mov    $0x1,%edx
call   1030 <write@plt>
xor    %eax,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [31 x i8] c"Usage: %s <file> <chars> <pos>\00"
@O_RDONLY = global i32 0
@.str.1 = [22 x i8] c"cp: failed to open %s\00"
@SEEK_SET = global i32 0
@.str.2 = [34 x i8] c"readnt: can't seek to position %s\00"
@.str.3 = [2 x i8] c"\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [1024 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 4
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 @error(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %16)
br label %18
18: ; preds = %13, %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 2
%21 = load i8*, i8** %20
%22 = call i32 @atoi(i8* %21)
store i32 %22, i32* %9
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 3
%25 = load i8*, i8** %24
%26 = call i32 @atoi(i8* %25)
store i32 %26, i32* %10
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 1
%29 = load i8*, i8** %28
%30 = load i32, i32* @O_RDONLY
%31 = call i32 @open(i8* %29, i32 %30)
store i32 %31, i32* %7
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %38
33: ; preds = %18
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 1
%36 = load i8*, i8** %35
%37 = call i32 @error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %36)
br label %38
38: ; preds = %33, %18
%39 = load i32, i32* %7
%40 = load i32, i32* %10
%41 = load i32, i32* @SEEK_SET
%42 = call i64 @lseek(i32 %39, i32 %40, i32 %41)
%43 = icmp sge i64 %42, 0
br i1 %43, label %44, label %81
44: ; preds = %38
%45 = load i32, i32* %9
%46 = icmp slt i32 %45, 1024
br i1 %46, label %47, label %55
47: ; preds = %44
%48 = load i32, i32* %7
%49 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%50 = load i32, i32* %9
%51 = call i32 @read(i32 %48, i8* %49, i32 %50)
store i32 %51, i32* %8
%52 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%53 = load i32, i32* %8
%54 = call i32 @write(i32 1, i8* %52, i32 %53)
br label %80
55: ; preds = %44
br label %56
56: ; preds = %70, %55
%57 = load i32, i32* %9
%58 = icmp sgt i32 %57, 0
br i1 %58, label %59, label %79
59: ; preds = %56
%60 = load i32, i32* %7
%61 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%62 = call i32 @read(i32 %60, i8* %61, i32 1023)
store i32 %62, i32* %8
%63 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%64 = load i32, i32* %8
%65 = call i32 @write(i32 1, i8* %63, i32 %64)
%66 = load i32, i32* %8
%67 = load i32, i32* %9
%68 = sub nsw i32 %67, %66
store i32 %68, i32* %9
%69 = icmp sgt i32 %68, 1024
br i1 %69, label %70, label %71
70: ; preds = %59
br label %56
71: ; preds = %59
%72 = load i32, i32* %7
%73 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%74 = load i32, i32* %9
%75 = call i32 @read(i32 %72, i8* %73, i32 %74)
%76 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%77 = load i32, i32* %9
%78 = call i32 @write(i32 1, i8* %76, i32 %77)
br label %79
79: ; preds = %71, %56
br label %80
80: ; preds = %79, %47
br label %86
81: ; preds = %38
%82 = load i8**, i8*** %5
%83 = getelementptr inbounds i8*, i8** %82, i64 3
%84 = load i8*, i8** %83
%85 = call i32 @error(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %84)
br label %86
86: ; preds = %81, %80
%87 = call i32 @write(i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
ret i32 0
}

LLVM IR (-O2):
@.str = [31 x i8] c"Usage: %s <file> <chars> <pos>\00"
@O_RDONLY = global i32 0
@.str.1 = [22 x i8] c"cp: failed to open %s\00"
@SEEK_SET = global i32 0
@.str.2 = [34 x i8] c"readnt: can't seek to position %s\00"
@.str.3 = [2 x i8] c"\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = icmp eq i32 %0, 4
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = load i8*, i8** %1
%7 = tail call i32 @error(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %8
8: ; preds = %5, %2
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%10 = getelementptr inbounds i8*, i8** %1, i64 2
%11 = load i8*, i8** %10
%12 = tail call i32 @atoi(i8* %11)
%13 = getelementptr inbounds i8*, i8** %1, i64 3
%14 = load i8*, i8** %13
%15 = tail call i32 @atoi(i8* %14)
%16 = getelementptr inbounds i8*, i8** %1, i64 1
%17 = load i8*, i8** %16
%18 = load i32, i32* @O_RDONLY
%19 = tail call i32 @open(i8* %17, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %8
%22 = load i8*, i8** %16
%23 = tail call i32 @error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %22)
br label %24
24: ; preds = %21, %8
%25 = load i32, i32* @SEEK_SET
%26 = tail call i64 @lseek(i32 %19, i32 %15, i32 %25)
%27 = icmp sgt i64 %26, -1
br i1 %27, label %28, label %42
28: ; preds = %24
%29 = icmp slt i32 %12, 1024
br i1 %29, label %30, label %33
30: ; preds = %28
%31 = call i32 @read(i32 %19, i8* %9, i32 %12)
%32 = call i32 @write(i32 1, i8* %9, i32 %31)
br label %45
33: ; preds = %28, %33
%34 = phi i32 [ %37, %33 ], [ %12, %28 ]
%35 = call i32 @read(i32 %19, i8* %9, i32 1023)
%36 = call i32 @write(i32 1, i8* %9, i32 %35)
%37 = sub nsw i32 %34, %35
%38 = icmp sgt i32 %37, 1024
br i1 %38, label %33, label %39
39: ; preds = %33
%40 = call i32 @read(i32 %19, i8* %9, i32 %37)
%41 = call i32 @write(i32 1, i8* %9, i32 %37)
br label %45
42: ; preds = %24
%43 = load i8*, i8** %13
%44 = tail call i32 @error(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0), i8* %43)
br label %45
45: ; preds = %30, %39, %42
%46 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
ret i32 0
}