#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 ssize_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EOF;
int fgetc(int /*<<< orphan*/ *);
ssize_t slave_read_cstr(char *buf, size_t buf_len, FILE *f) {
  for (size_t i = 0; i < buf_len; ++i) {
    int c = fgetc(f);
    if (c == EOF) {
      buf[i] = 0;
      return EOF;
    }
    buf[i] = c;
    if (c == 0) {
      break;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<slave_read_cstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    11aa <slave_read_cstr+0x9a>
mov    -0x20(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    0x2e98(%rip),%rcx
cmp    (%rcx),%eax
jne    1175 <slave_read_cstr+0x65>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x2e7d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11b1 <slave_read_cstr+0xa1>
mov    -0x2c(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
cmpl   $0x0,-0x2c(%rbp)
jne    1194 <slave_read_cstr+0x84>
jmp    11aa <slave_read_cstr+0x9a>
jmp    1199 <slave_read_cstr+0x89>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    112c <slave_read_cstr+0x1c>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<slave_read_cstr>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
test   %rsi,%rsi
je     1168 <slave_read_cstr+0x58>
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r15
add    $0xffffffffffffffff,%r12
xor    %ebx,%ebx
mov    0x2eb4(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r14,%rdi
call   1030 <fgetc@plt>
cmp    0x0(%r13),%eax
je     116c <slave_read_cstr+0x5c>
mov    %eax,%ecx
mov    %cl,(%r15,%rbx,1)
xor    %eax,%eax
test   %ecx,%ecx
je     1175 <slave_read_cstr+0x65>
lea    0x1(%rbx),%rcx
cmp    %rbx,%r12
mov    %rcx,%rbx
jne    1140 <slave_read_cstr+0x30>
jmp    1175 <slave_read_cstr+0x65>
xor    %eax,%eax
jmp    1175 <slave_read_cstr+0x65>
movb   $0x0,(%r15,%rbx,1)
mov    0x0(%r13),%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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