#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[32], *bp;
  int status;
  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("read(%2d, %2d): %02x %02x,", len, status, buf[0], buf[1]);
  status -= 2;
  bp = buf + 2;
  while (status-- > 0)
    printf(" %02x", *bp++);
  printf("\n");
}


x86 assembly (clang, -O0):
<do_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1174 <do_read+0x24>
movl   $0x2,-0x8(%rbp)
jmp    118e <do_read+0x3e>
movslq -0x8(%rbp),%rax
cmp    $0x20,%rax
jbe    1189 <do_read+0x39>
movl   $0x20,-0x8(%rbp)
jmp    118e <do_read+0x3e>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1040 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    -0x8(%rbp),%edx
call   1050 <read@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    11cb <do_read+0x7b>
lea    0xe3f(%rip),%rdi
call   1070 <perror@plt>
jmp    126a <do_read+0x11a>
mov    -0x3c(%rbp),%eax
cmp    -0x8(%rbp),%eax
je     11f1 <do_read+0xa1>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
lea    0xe1e(%rip),%rsi
call   1060 <fprintf@plt>
jmp    126a <do_read+0x11a>
mov    -0x8(%rbp),%esi
mov    -0x3c(%rbp),%edx
movzbl -0x30(%rbp),%ecx
movzbl -0x2f(%rbp),%r8d
lea    0xe0a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x3c(%rbp),%eax
sub    $0x2,%eax
mov    %eax,-0x3c(%rbp)
lea    -0x30(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x38(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x3c(%rbp)
cmp    $0x0,%eax
jle    125c <do_read+0x10c>
mov    -0x38(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x38(%rbp)
movzbl (%rax),%esi
lea    0xddc(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1223 <do_read+0xd3>
lea    0xdac(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_read>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebp
cmp    $0x20,%esi
mov    $0x20,%eax
cmovb  %esi,%eax
cmp    $0x2,%esi
mov    $0x2,%ebx
cmovge %eax,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x20,%edx
call   1050 <memset@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1060 <read@plt>
test   %eax,%eax
js     1222 <do_read+0xc2>
cmp    %ebx,%eax
jne    1204 <do_read+0xa4>
movzbl (%rsp),%ecx
movzbl 0x1(%rsp),%r8d
lea    0xe56(%rip),%rdi
mov    %ebx,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <printf@plt>
cmp    $0x3,%ebx
jb     11f8 <do_read+0x98>
add    $0xfffffffd,%ebx
add    $0x1,%rbx
lea    0xe53(%rip),%r14
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
movzbl 0x2(%rsp,%rbp,1),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    11e0 <do_read+0x80>
mov    $0xa,%edi
call   1030 <putchar@plt>
jmp    1219 <do_read+0xb9>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
lea    0xdf1(%rip),%rsi
call   1070 <fprintf@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdd7(%rip),%rdi
call   1080 <perror@plt>
jmp    1219 <do_read+0xb9>

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