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

/* Variables and functions */
int fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int readstring(FILE *infile, unsigned char *buf) {
  int i;
  for (i = 0; fread(buf + i, 1, 1, infile) == 1 && buf[i]; i++)
    ;
  return i;
}


x86 assembly (clang, -O0):
<readstring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
movslq -0x14(%rbp),%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <fread@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x1,%ecx
mov    %al,-0x15(%rbp)
jne    1167 <readstring+0x57>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    1177 <readstring+0x67>
jmp    118a <readstring+0x7a>
jmp    117c <readstring+0x6c>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1127 <readstring+0x17>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readstring>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsi,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
xor    %ebx,%ebx
cmp    $0x1,%eax
jne    116a <readstring+0x5a>
xor    %ebx,%ebx
nopl   0x0(%rax)
cmpb   $0x0,(%r15,%rbx,1)
je     116a <readstring+0x5a>
lea    (%r15,%rbx,1),%rdi
add    $0x1,%rdi
add    $0x1,%rbx
mov    $0x1,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1030 <fread@plt>
cmp    $0x1,%eax
je     1140 <readstring+0x30>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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