#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*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, unsigned char *, int);
int /*<<< orphan*/ stderr;
void do_read(int fd, int len) {
  unsigned char buf[4096], *bp;
  int status, i;
  if (len < 2)
    len = 2;
  else if (len > sizeof(buf))
    len = sizeof(buf);
  memset(buf, 0, sizeof buf);
  status = read(fd, buf, len);
  if (status < 0) {
    perror("read");
    return;
  }
  if (status != len) {
    fprintf(stderr, "short read\n");
    return;
  }
  printf("\nread(%2d, %2d):", len, status);
  bp = buf;
  for (i = 0; i < status; i++) {
    if (i % 10 == 0)
      printf("\n");
    printf(" %03d", *bp++);
  }
  printf("\n");
}


x86 assembly (clang, -O0):
<do_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1177 <do_read+0x27>
movl   $0x2,-0x8(%rbp)
jmp    1193 <do_read+0x43>
movslq -0x8(%rbp),%rax
cmp    $0x1000,%rax
jbe    118e <do_read+0x3e>
movl   $0x1000,-0x8(%rbp)
jmp    1193 <do_read+0x43>
lea    -0x1010(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1040 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    -0x8(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x101c(%rbp)
cmpl   $0x0,-0x101c(%rbp)
jge    11dc <do_read+0x8c>
lea    0xe2e(%rip),%rdi
call   1070 <perror@plt>
jmp    12b3 <do_read+0x163>
mov    -0x101c(%rbp),%eax
cmp    -0x8(%rbp),%eax
je     1205 <do_read+0xb5>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
lea    0xe0a(%rip),%rsi
call   1060 <fprintf@plt>
jmp    12b3 <do_read+0x163>
mov    -0x8(%rbp),%esi
mov    -0x101c(%rbp),%edx
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x1010(%rbp),%rax
mov    %rax,-0x1018(%rbp)
movl   $0x0,-0x1020(%rbp)
mov    -0x1020(%rbp),%eax
cmp    -0x101c(%rbp),%eax
jge    12a5 <do_read+0x155>
mov    -0x1020(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    126b <do_read+0x11b>
lea    0xdab(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1018(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x1018(%rbp)
movzbl (%rax),%esi
lea    0xd98(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1020(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1020(%rbp)
jmp    1234 <do_read+0xe4>
lea    0xd63(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1008,%rsp
mov    %edi,%ebp
cmp    $0x1000,%esi
mov    $0x1000,%eax
cmovb  %esi,%eax
cmp    $0x2,%esi
mov    $0x2,%ebx
cmovge %eax,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1060 <read@plt>
test   %eax,%eax
js     1244 <do_read+0xe4>
cmp    %ebx,%eax
jne    1215 <do_read+0xb5>
lea    0xe58(%rip),%rdi
mov    %ebx,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
test   %ebx,%ebx
je     122c <do_read+0xcc>
mov    %ebx,%ebp
mov    $0xcccccccd,%r14d
lea    0xe4b(%rip),%r15
xor    %ebx,%ebx
jmp    11f6 <do_read+0x96>
nopl   0x0(%rax,%rax,1)
movzbl (%rsp,%rbx,1),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%rbx
cmp    %ebx,%ebp
je     122c <do_read+0xcc>
mov    %ebx,%eax
imul   %r14,%rax
shr    $0x23,%rax
add    %eax,%eax
lea    (%rax,%rax,4),%eax
cmp    %eax,%ebx
jne    11e0 <do_read+0x80>
mov    $0xa,%edi
call   1030 <putchar@plt>
jmp    11e0 <do_read+0x80>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
lea    0xde0(%rip),%rsi
call   1070 <fprintf@plt>
jmp    1236 <do_read+0xd6>
mov    $0xa,%edi
call   1030 <putchar@plt>
add    $0x1008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdb5(%rip),%rdi
call   1080 <perror@plt>
jmp    1236 <do_read+0xd6>

LLVM IR (-O0):
@.str = [5 x i8] c"read\00"
@stderr = global i32 0
@.str.1 = [12 x i8] c"short read\0A\00"
@.str.2 = [17 x i8] c"\0Aread(%2d, %2d):\00"
@.str.3 = [2 x i8] c"\0A\00"
@.str.4 = [6 x i8] c" %03d\00"
define void @do_read(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [4096 x i8]
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 2
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 2, i32* %4
br label %18
12: ; preds = %2
%13 = load i32, i32* %4
%14 = sext i32 %13 to i64
%15 = icmp ugt i64 %14, 4096
br i1 %15, label %16, label %17
16: ; preds = %12
store i32 4096, i32* %4
br label %17
17: ; preds = %16, %12
br label %18
18: ; preds = %17, %11
%19 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%20 = call i32 @memset(i8* %19, i32 0, i32 4096)
%21 = load i32, i32* %3
%22 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%23 = load i32, i32* %4
%24 = call i32 @read(i32 %21, i8* %22, i32 %23)
store i32 %24, i32* %7
%25 = load i32, i32* %7
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %18
%28 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %62
29: ; preds = %18
%30 = load i32, i32* %7
%31 = load i32, i32* %4
%32 = icmp ne i32 %30, %31
br i1 %32, label %33, label %36
33: ; preds = %29
%34 = load i32, i32* @stderr
%35 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %62
36: ; preds = %29
%37 = load i32, i32* %4
%38 = load i32, i32* %7
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %37, i32 %38)
%40 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
store i8* %40, i8** %6
store i32 0, i32* %8
br label %41
41: ; preds = %57, %36
%42 = load i32, i32* %8
%43 = load i32, i32* %7
%44 = icmp slt i32 %42, %43
br i1 %44, label %45, label %60
45: ; preds = %41
%46 = load i32, i32* %8
%47 = srem i32 %46, 10
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %45
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %51
51: ; preds = %49, %45
%52 = load i8*, i8** %6
%53 = getelementptr inbounds i8, i8* %52, i32 1
store i8* %53, i8** %6
%54 = load i8, i8* %52
%55 = zext i8 %54 to i32
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %55)
br label %57
57: ; preds = %51
%58 = load i32, i32* %8
%59 = add nsw i32 %58, 1
store i32 %59, i32* %8
br label %41
60: ; preds = %41
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %62
62: ; preds = %60, %33, %27
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"read\00"
@stderr = global i32 0
@.str.1 = [12 x i8] c"short read\0A\00"
@.str.2 = [17 x i8] c"\0Aread(%2d, %2d):\00"
@.str.4 = [6 x i8] c" %03d\00"
define void @do_read(i32 %0, i32 %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = icmp slt i32 %1, 2
%6 = icmp ult i32 %1, 4096
%7 = select i1 %6, i32 %1, i32 4096
%8 = select i1 %5, i32 2, i32 %7
%9 = call i32 @memset(i8* %4, i32 0, i32 4096)
%10 = call i32 @read(i32 %0, i8* %4, i32 %8)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %38
14: ; preds = %2
%15 = icmp eq i32 %10, %8
br i1 %15, label %19, label %16
16: ; preds = %14
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %38
19: ; preds = %14
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %8, i32 %8)
%21 = icmp eq i32 %8, 0
br i1 %21, label %36, label %22
22: ; preds = %19, %29
%23 = phi i32 [ %34, %29 ], [ 0, %19 ]
%24 = phi i8* [ %30, %29 ], [ %4, %19 ]
%25 = urem i32 %23, 10
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = call i32 @putchar(i32 10)
br label %29
29: ; preds = %27, %22
%30 = getelementptr inbounds i8, i8* %24, i64 1
%31 = load i8, i8* %24
%32 = zext i8 %31 to i32
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %32)
%34 = add nuw nsw i32 %23, 1
%35 = icmp eq i32 %34, %8
br i1 %35, label %36, label %22
36: ; preds = %29, %19
%37 = call i32 @putchar(i32 10)
br label %38
38: ; preds = %36, %16, %12
ret void
}