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

/* Variables and functions */
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int com_read(int fd, char *s, int size) {
  int i;
  int r;
  for (i = 0; i < size; i++) {
    r = read(fd, s, 1);
    if (r < 0) {
      perror("read() failed");
      return -1;
    }
    if (r != 1)
      return 32;
    s++;
  }
  return 0;
}


x86 assembly (clang, -O0):
<com_read>:
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
jge    11ab <com_read+0x8b>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    117b <com_read+0x5b>
lea    0xe96(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b2 <com_read+0x92>
cmpl   $0x1,-0x1c(%rbp)
je     1191 <com_read+0x71>
movl   $0x20,-0x4(%rbp)
jmp    11b2 <com_read+0x92>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1139 <com_read+0x19>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<com_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %r14d,%r14d
test   %edx,%edx
jle    116a <com_read+0x4a>
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r15d
cs nopw 0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %eax,%eax
js     1178 <com_read+0x58>
cmp    $0x1,%eax
jne    1164 <com_read+0x44>
add    $0x1,%rbx
add    $0xffffffff,%ebp
jne    1140 <com_read+0x20>
jmp    116a <com_read+0x4a>
mov    $0x20,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe81(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%r14d
jmp    116a <com_read+0x4a>

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

LLVM IR (-O2):
@.str = [14 x i8] c"read() failed\00"
define i32 @com_read(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %18
5: ; preds = %3, %14
%6 = phi i32 [ %16, %14 ], [ 0, %3 ]
%7 = phi i8* [ %15, %14 ], [ %1, %3 ]
%8 = tail call i32 @read(i32 %0, i8* %7, i32 1)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %5
%11 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %18
12: ; preds = %5
%13 = icmp eq i32 %8, 1
br i1 %13, label %14, label %18
14: ; preds = %12
%15 = getelementptr inbounds i8, i8* %7, i64 1
%16 = add nuw nsw i32 %6, 1
%17 = icmp eq i32 %16, %2
br i1 %17, label %18, label %5
18: ; preds = %12, %14, %3, %10
%19 = phi i32 [ -1, %10 ], [ 0, %3 ], [ 32, %12 ], [ 0, %14 ]
ret i32 %19
}