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

/* Variables and functions */
scalar_t__ read(int, char *, int);
int read_string(int sock, char *buf, ssize_t maxlen) {
  ssize_t len;
  ssize_t left = maxlen;
  while ((len = read(sock, buf, 1))) {
    if (*buf == '\0')
      return 1;
    left--;
    buf++;
  }
  if (read(sock, buf, 1) != 0)
    return 0;
  *buf = '\0';
  return 1;
}


x86 assembly (clang, -O0):
<read_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     1183 <read_string+0x73>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1166 <read_string+0x56>
movl   $0x1,-0x4(%rbp)
jmp    11b8 <read_string+0xa8>
mov    -0x28(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    112b <read_string+0x1b>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x0,%rax
je     11aa <read_string+0x9a>
movl   $0x0,-0x4(%rbp)
jmp    11b8 <read_string+0xa8>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_string>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %edi,%ebp
mov    $0x1,%edx
call   1030 <read@plt>
test   %rax,%rax
je     114d <read_string+0x3d>
mov    $0x1,%r14d
xchg   %ax,%ax
cmpb   $0x0,(%rbx)
je     116d <read_string+0x5d>
add    $0x1,%rbx
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %rax,%rax
jne    1130 <read_string+0x20>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
xor    %r14d,%r14d
test   %rax,%rax
jne    116d <read_string+0x5d>
movb   $0x0,(%rbx)
mov    $0x1,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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