#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*/ printf(char *, ...);
int read(int, char *, int);
char *strlen(char *);
int readFD(char *s, int fd) {
  int n, m = 0;
  do {
    n = read(fd, s, 1);
    m++;
    if (0) {
      printf("--r- read %d chars :%s: at addr %p\n", n, s, s);
      if (*s == 0) {
        printf("--r- s is zero\n");
      }
    }
  } while ((n > 0) && ((*s++) != 0));
  if (0) {
    printf("--r- m: %d strlen: %lu s: %s\n", m, strlen(s - m), s - m);
  }
  return m - 1;
}


x86 assembly (clang, -O0):
<readFD>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x10(%rbp)
mov    %al,-0x15(%rbp)
jle    116d <readFD+0x5d>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movsbl (%rax),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    1126 <readFD+0x16>
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readFD>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r15
xor    %eax,%eax
nop
mov    %rax,%rbp
lea    (%r15,%rax,1),%rbx
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %eax,%eax
jle    1144 <readFD+0x34>
lea    0x1(%rbp),%rax
cmpb   $0x0,(%rbx)
jne    1120 <readFD+0x10>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @readFD(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %6
br label %7
7: ; preds = %22, %2
%8 = load i32, i32* %4
%9 = load i8*, i8** %3
%10 = call i32 @read(i32 %8, i8* %9, i32 1)
store i32 %10, i32* %5
%11 = load i32, i32* %6
%12 = add nsw i32 %11, 1
store i32 %12, i32* %6
br label %13
13: ; preds = %7
%14 = load i32, i32* %5
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %13
%17 = load i8*, i8** %3
%18 = getelementptr inbounds i8, i8* %17, i32 1
store i8* %18, i8** %3
%19 = load i8, i8* %17
%20 = sext i8 %19 to i32
%21 = icmp ne i32 %20, 0
br label %22
22: ; preds = %16, %13
%23 = phi i1 [ false, %13 ], [ %21, %16 ]
br i1 %23, label %7, label %24
24: ; preds = %22
%25 = load i32, i32* %6
%26 = sub nsw i32 %25, 1
ret i32 %26
}

LLVM IR (-O2):
define i32 @readFD(i8* nocapture %0, i32 %1) {
br label %3
3: ; preds = %8, %2
%4 = phi i8* [ %0, %2 ], [ %10, %8 ]
%5 = phi i32 [ 0, %2 ], [ %9, %8 ]
%6 = tail call i32 @read(i32 %1, i8* %4, i32 1)
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %3
%9 = add nuw nsw i32 %5, 1
%10 = getelementptr inbounds i8, i8* %4, i64 1
%11 = load i8, i8* %4
%12 = icmp eq i8 %11, 0
br i1 %12, label %13, label %3
13: ; preds = %3, %8
ret i32 %5
}