#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 /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int atoi(char *);
int lseek(int, int, int /*<<< orphan*/);
int read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
void cmd_read(int argc, char *argv[]) {
  int fd, size;
  int read_out, rsz;
  char buf[512];
  fd = atoi(argv[1]);
  {
    int cur, end;
    cur = lseek(fd, 0, SEEK_CUR);
    end = lseek(fd, 0, SEEK_END);
    size = end - cur;
    lseek(fd, cur, SEEK_SET);
  }
  if (argc > 2)
    size = atoi(argv[2]);
  for (rsz = 0; rsz < size;) {
    read_out = read(
        fd, buf, ((sizeof(buf)) < (size - rsz) ? (sizeof(buf)) : (size - rsz)));
    if (read_out == -1)
      return;
    write(1, buf, read_out);
    rsz += read_out;
  }
}


x86 assembly (clang, -O0):
<cmd_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e74(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %eax,-0x224(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e63(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %eax,-0x228(%rbp)
mov    -0x228(%rbp),%eax
sub    -0x224(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x224(%rbp),%esi
mov    0x2e1d(%rip),%rax
mov    (%rax),%edx
call   1040 <lseek@plt>
cmpl   $0x2,-0x4(%rbp)
jle    11d4 <cmd_read+0x94>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1284 <cmd_read+0x144>
mov    -0x14(%rbp),%eax
mov    %eax,-0x234(%rbp)
lea    -0x220(%rbp),%rax
mov    %rax,-0x230(%rbp)
mov    -0x18(%rbp),%eax
sub    -0x20(%rbp),%eax
movslq %eax,%rcx
mov    $0x200,%eax
cmp    %rcx,%rax
jae    1226 <cmd_read+0xe6>
mov    $0x200,%eax
mov    %rax,-0x240(%rbp)
jmp    1235 <cmd_read+0xf5>
mov    -0x18(%rbp),%eax
sub    -0x20(%rbp),%eax
cltq
mov    %rax,-0x240(%rbp)
mov    -0x230(%rbp),%rsi
mov    -0x234(%rbp),%edi
mov    -0x240(%rbp),%rax
mov    %eax,%edx
call   1050 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1262 <cmd_read+0x122>
jmp    1284 <cmd_read+0x144>
lea    -0x220(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    11db <cmd_read+0x9b>
add    $0x240,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %rsi,%r14
mov    %edi,%r15d
mov    0x8(%rsi),%rdi
call   1060 <atoi@plt>
mov    %eax,%r12d
mov    0x2e76(%rip),%rax
mov    (%rax),%edx
mov    %r12d,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %eax,%ebx
mov    0x2e69(%rip),%rax
mov    (%rax),%edx
mov    %r12d,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %eax,%r13d
mov    0x2e3b(%rip),%rax
mov    (%rax),%edx
mov    %r12d,%edi
mov    %ebx,%esi
call   1040 <lseek@plt>
cmp    $0x3,%r15d
jl     11ba <cmd_read+0x7a>
mov    0x10(%r14),%rdi
call   1060 <atoi@plt>
mov    %eax,%r13d
test   %r13d,%r13d
jg     11c2 <cmd_read+0x82>
jmp    1207 <cmd_read+0xc7>
sub    %ebx,%r13d
test   %r13d,%r13d
jle    1207 <cmd_read+0xc7>
xor    %ebp,%ebp
mov    $0x200,%r15d
mov    %rsp,%r14
nopl   (%rax)
mov    %r13d,%edx
sub    %ebp,%edx
cmp    $0x200,%edx
cmovae %r15d,%edx
mov    %r12d,%edi
mov    %r14,%rsi
call   1050 <read@plt>
cmp    $0xffffffff,%eax
je     1207 <cmd_read+0xc7>
mov    %eax,%ebx
mov    $0x1,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
add    %ebx,%ebp
cmp    %ebp,%r13d
jg     11d0 <cmd_read+0x90>
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SEEK_CUR = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define void @cmd_read(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [512 x i8]
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
%12 = load i8**, i8*** %4
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = call i32 @atoi(i8* %14)
store i32 %15, i32* %5
%16 = load i32, i32* %5
%17 = load i32, i32* @SEEK_CUR
%18 = call i32 @lseek(i32 %16, i32 0, i32 %17)
store i32 %18, i32* %10
%19 = load i32, i32* %5
%20 = load i32, i32* @SEEK_END
%21 = call i32 @lseek(i32 %19, i32 0, i32 %20)
store i32 %21, i32* %11
%22 = load i32, i32* %11
%23 = load i32, i32* %10
%24 = sub nsw i32 %22, %23
store i32 %24, i32* %6
%25 = load i32, i32* %5
%26 = load i32, i32* %10
%27 = load i32, i32* @SEEK_SET
%28 = call i32 @lseek(i32 %25, i32 %26, i32 %27)
%29 = load i32, i32* %3
%30 = icmp sgt i32 %29, 2
br i1 %30, label %31, label %36
31: ; preds = %2
%32 = load i8**, i8*** %4
%33 = getelementptr inbounds i8*, i8** %32, i64 2
%34 = load i8*, i8** %33
%35 = call i32 @atoi(i8* %34)
store i32 %35, i32* %6
br label %36
36: ; preds = %31, %2
store i32 0, i32* %8
br label %37
37: ; preds = %62, %36
%38 = load i32, i32* %8
%39 = load i32, i32* %6
%40 = icmp slt i32 %38, %39
br i1 %40, label %41, label %69
41: ; preds = %37
%42 = load i32, i32* %5
%43 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%44 = load i32, i32* %6
%45 = load i32, i32* %8
%46 = sub nsw i32 %44, %45
%47 = sext i32 %46 to i64
%48 = icmp ult i64 512, %47
br i1 %48, label %49, label %50
49: ; preds = %41
br label %55
50: ; preds = %41
%51 = load i32, i32* %6
%52 = load i32, i32* %8
%53 = sub nsw i32 %51, %52
%54 = sext i32 %53 to i64
br label %55
55: ; preds = %50, %49
%56 = phi i64 [ 512, %49 ], [ %54, %50 ]
%57 = trunc i64 %56 to i32
%58 = call i32 @read(i32 %42, i8* %43, i32 %57)
store i32 %58, i32* %7
%59 = load i32, i32* %7
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %62
61: ; preds = %55
br label %69
62: ; preds = %55
%63 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%64 = load i32, i32* %7
%65 = call i32 @write(i32 1, i8* %63, i32 %64)
%66 = load i32, i32* %7
%67 = load i32, i32* %8
%68 = add nsw i32 %67, %66
store i32 %68, i32* %8
br label %37
69: ; preds = %61, %37
ret void
}

LLVM IR (-O2):
@SEEK_CUR = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define void @cmd_read(i32 %0, i8** nocapture %1) {
%3 = alloca [512 x i8]
%4 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i32 @atoi(i8* %6)
%8 = load i32, i32* @SEEK_CUR
%9 = tail call i32 @lseek(i32 %7, i32 0, i32 %8)
%10 = load i32, i32* @SEEK_END
%11 = tail call i32 @lseek(i32 %7, i32 0, i32 %10)
%12 = sub nsw i32 %11, %9
%13 = load i32, i32* @SEEK_SET
%14 = tail call i32 @lseek(i32 %7, i32 %9, i32 %13)
%15 = icmp sgt i32 %0, 2
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = getelementptr inbounds i8*, i8** %1, i64 2
%18 = load i8*, i8** %17
%19 = tail call i32 @atoi(i8* %18)
br label %20
20: ; preds = %16, %2
%21 = phi i32 [ %19, %16 ], [ %12, %2 ]
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %34
23: ; preds = %20, %30
%24 = phi i32 [ %32, %30 ], [ 0, %20 ]
%25 = sub nsw i32 %21, %24
%26 = icmp ult i32 %25, 512
%27 = select i1 %26, i32 %25, i32 512
%28 = call i32 @read(i32 %7, i8* %4, i32 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %34, label %30
30: ; preds = %23
%31 = call i32 @write(i32 1, i8* %4, i32 %28)
%32 = add nsw i32 %28, %24
%33 = icmp sgt i32 %21, %32
br i1 %33, label %23, label %34
34: ; preds = %23, %30, %20
ret void
}