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

/* Variables and functions */
int read(int, char *, int);
int lc_read_data(int sd, void *data, int max_bytes) {
  int bytes = 0;
  int rv;
  char c;
  char *buf = data;
  while ((rv = read(sd, &c, sizeof(c))) == sizeof(c)) {
    buf[bytes] = c;
    ++bytes;
    if (bytes == max_bytes)
      break;
  }
  return bytes;
}


x86 assembly (clang, -O0):
<lc_read_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x1d(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cltq
cmp    $0x1,%rax
jne    117e <lc_read_data+0x6e>
mov    -0x1d(%rbp),%dl
mov    -0x28(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    1179 <lc_read_data+0x69>
jmp    117e <lc_read_data+0x6e>
jmp    1131 <lc_read_data+0x21>
mov    -0x18(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lc_read_data>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
mov    %edx,%r12d
xor    %ebx,%ebx
lea    0xf(%rsp),%r15
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    1159 <lc_read_data+0x49>
movzbl 0xf(%rsp),%eax
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1130 <lc_read_data+0x20>
mov    %r12d,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @lc_read_data(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8
%10 = alloca i8*
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %7
%11 = load i8*, i8** %5
store i8* %11, i8** %10
br label %12
12: ; preds = %29, %3
%13 = load i32, i32* %4
%14 = call i32 @read(i32 %13, i8* %9, i32 1)
store i32 %14, i32* %8
%15 = sext i32 %14 to i64
%16 = icmp eq i64 %15, 1
br i1 %16, label %17, label %30
17: ; preds = %12
%18 = load i8, i8* %9
%19 = load i8*, i8** %10
%20 = load i32, i32* %7
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %19, i64 %21
store i8 %18, i8* %22
%23 = load i32, i32* %7
%24 = add nsw i32 %23, 1
store i32 %24, i32* %7
%25 = load i32, i32* %7
%26 = load i32, i32* %6
%27 = icmp eq i32 %25, %26
br i1 %27, label %28, label %29
28: ; preds = %17
br label %30
29: ; preds = %17
br label %12
30: ; preds = %28, %12
%31 = load i32, i32* %7
ret i32 %31
}

LLVM IR (-O2):
define i32 @lc_read_data(i32 %0, i8* nocapture writeonly %1, i32 %2) {
%4 = alloca i8
%5 = zext i32 %2 to i64
br label %6
6: ; preds = %10, %3
%7 = phi i64 [ %13, %10 ], [ 0, %3 ]
%8 = call i32 @read(i32 %0, i8* %4, i32 1)
%9 = icmp eq i32 %8, 1
br i1 %9, label %10, label %15
10: ; preds = %6
%11 = load i8, i8* %4
%12 = getelementptr inbounds i8, i8* %1, i64 %7
store i8 %11, i8* %12
%13 = add nuw nsw i64 %7, 1
%14 = icmp eq i64 %13, %5
br i1 %14, label %17, label %6
15: ; preds = %6
%16 = trunc i64 %7 to i32
br label %17
17: ; preds = %10, %15
%18 = phi i32 [ %16, %15 ], [ %2, %10 ]
ret i32 %18
}