#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 read(int, char *, int);
int readString(int fd, char *str, int len) {
  int count = read(fd, str, len - 1);
  if (count < 0) {
    return 0;
  }
  str[count] = 0;
  while (count > 0 && (str[count - 1] == '\n' || str[count - 1] == 0)) {
    count--;
    str[count] = 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<readString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    114d <readString+0x3d>
movl   $0x0,-0x4(%rbp)
jmp    11d8 <readString+0xc8>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
xor    %eax,%eax
cmpl   $0x0,-0x18(%rbp)
mov    %al,-0x19(%rbp)
jle    11a7 <readString+0x97>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%ecx
mov    $0x1,%al
cmp    $0xa,%ecx
mov    %al,-0x1a(%rbp)
je     11a1 <readString+0x91>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
sete   %al
mov    %al,-0x1a(%rbp)
mov    -0x1a(%rbp),%al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    11b7 <readString+0xa7>
jmp    11d1 <readString+0xc1>
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1159 <readString+0x49>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readString>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
add    $0xffffffff,%edx
call   1030 <read@plt>
test   %eax,%eax
js     1162 <readString+0x52>
mov    %eax,%ecx
movb   $0x0,(%r14,%rcx,1)
mov    $0x1,%eax
je     1164 <readString+0x54>
add    $0x1,%rcx
jmp    114f <readString+0x3f>
nopw   0x0(%rax,%rax,1)
movb   $0x0,(%r14,%rdx,1)
add    $0xffffffffffffffff,%rcx
cmp    $0x1,%rcx
jbe    1164 <readString+0x54>
lea    -0x2(%rcx),%edx
movzbl (%r14,%rdx,1),%ebx
cmp    $0xa,%bl
je     1140 <readString+0x30>
test   %bl,%bl
je     1140 <readString+0x30>
jmp    1164 <readString+0x54>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @readString(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%9 = load i32, i32* %5
%10 = load i8*, i8** %6
%11 = load i32, i32* %7
%12 = sub nsw i32 %11, 1
%13 = call i32 @read(i32 %9, i8* %10, i32 %12)
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %3
store i32 0, i32* %4
br label %55
17: ; preds = %3
%18 = load i8*, i8** %6
%19 = load i32, i32* %8
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
store i8 0, i8* %21
br label %22
22: ; preds = %47, %17
%23 = load i32, i32* %8
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %45
25: ; preds = %22
%26 = load i8*, i8** %6
%27 = load i32, i32* %8
%28 = sub nsw i32 %27, 1
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %26, i64 %29
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = icmp eq i32 %32, 10
br i1 %33, label %43, label %34
34: ; preds = %25
%35 = load i8*, i8** %6
%36 = load i32, i32* %8
%37 = sub nsw i32 %36, 1
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %35, i64 %38
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = icmp eq i32 %41, 0
br label %43
43: ; preds = %34, %25
%44 = phi i1 [ true, %25 ], [ %42, %34 ]
br label %45
45: ; preds = %43, %22
%46 = phi i1 [ false, %22 ], [ %44, %43 ]
br i1 %46, label %47, label %54
47: ; preds = %45
%48 = load i32, i32* %8
%49 = add nsw i32 %48, -1
store i32 %49, i32* %8
%50 = load i8*, i8** %6
%51 = load i32, i32* %8
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds i8, i8* %50, i64 %52
store i8 0, i8* %53
br label %22
54: ; preds = %45
store i32 1, i32* %4
br label %55
55: ; preds = %54, %16
%56 = load i32, i32* %4
ret i32 %56
}

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