#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 read_length(int);
int read_string(int file, char *s, int s_size) {
  int len;
  len = read_length(file);
  if (len < 0 || len > s_size - 1 || read(file, s, len) < len)
    return -1;
  s[len] = 0;
  return len;
}


x86 assembly (clang, -O0):
<read_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <read_length@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     1170 <read_string+0x50>
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jg     1170 <read_string+0x50>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1040 <read@plt>
cmp    -0x18(%rbp),%eax
jge    117c <read_string+0x5c>
movl   $0xffffffff,-0x4(%rbp)
jmp    118e <read_string+0x6e>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_string>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r12d
mov    %rsi,%r14
mov    %edi,%ebx
call   1030 <read_length@plt>
mov    $0xffffffff,%r15d
test   %eax,%eax
js     1160 <read_string+0x40>
mov    %eax,%ebp
cmp    %r12d,%eax
jge    1160 <read_string+0x40>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %ebp,%edx
call   1040 <read@plt>
cmp    %ebp,%eax
jl     1160 <read_string+0x40>
mov    %ebp,%eax
movb   $0x0,(%r14,%rax,1)
mov    %ebp,%r15d
mov    %r15d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @read_string(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 = call i32 @read_length(i32 %9)
store i32 %10, i32* %8
%11 = load i32, i32* %8
%12 = icmp slt i32 %11, 0
br i1 %12, label %25, label %13
13: ; preds = %3
%14 = load i32, i32* %8
%15 = load i32, i32* %7
%16 = sub nsw i32 %15, 1
%17 = icmp sgt i32 %14, %16
br i1 %17, label %25, label %18
18: ; preds = %13
%19 = load i32, i32* %5
%20 = load i8*, i8** %6
%21 = load i32, i32* %8
%22 = call i32 @read(i32 %19, i8* %20, i32 %21)
%23 = load i32, i32* %8
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %26
25: ; preds = %18, %13, %3
store i32 -1, i32* %4
br label %32
26: ; preds = %18
%27 = load i8*, i8** %6
%28 = load i32, i32* %8
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
store i8 0, i8* %30
%31 = load i32, i32* %8
store i32 %31, i32* %4
br label %32
32: ; preds = %26, %25
%33 = load i32, i32* %4
ret i32 %33
}

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