#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 ssize_t;

/* Variables and functions */
int read(int, char *, int);
int read_data(int fd, char *buffer, ssize_t size) {
  int i;
  char ch[1];
  ssize_t num_bytes;
  i = 0;
  while (1) {
    if (i >= size)
      break;
    num_bytes = read(fd, ch, sizeof(char));
    if (num_bytes == -1)
      return -1;
    buffer[i++] = ch[0];
  }
  return i;
}


x86 assembly (clang, -O0):
<read_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     113a <read_data+0x2a>
jmp    1181 <read_data+0x71>
mov    -0x8(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1164 <read_data+0x54>
movl   $0xffffffff,-0x4(%rbp)
jmp    1187 <read_data+0x77>
mov    -0x19(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x18(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1129 <read_data+0x19>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_data>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
jle    1168 <read_data+0x58>
mov    %edx,%r14d
mov    %rsi,%r12
mov    %edi,%ebp
mov    %edx,%r13d
xor    %ebx,%ebx
lea    0x7(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0xffffffff,%eax
je     116d <read_data+0x5d>
movzbl 0x7(%rsp),%eax
mov    %al,(%r12,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r13
jne    1140 <read_data+0x30>
jmp    1173 <read_data+0x63>
xor    %r14d,%r14d
jmp    1173 <read_data+0x63>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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