#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 /*<<< orphan*/ bzero(char *, size_t);
int /*<<< orphan*/ *fgets(char *, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, int, char *);
int strlen(char *);
int /*<<< orphan*/ trimstr(char *);
int readline(FILE *fin, char *buf, size_t size) {
  int len;
  if (fgets(buf, size, fin) == NULL)
    return -1;
  trimstr(buf);
  len = strlen(buf);
  bzero(buf + len, size - len);
  printf("read line (%d): %s\n", len, buf);
  return len;
}


x86 assembly (clang, -O0):
<readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1050 <fgets@plt>
cmp    $0x0,%rax
jne    118b <readline+0x3b>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d4 <readline+0x84>
mov    -0x18(%rbp),%rdi
call   1060 <trimstr@plt>
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
mov    -0x20(%rbp),%rsi
movslq -0x24(%rbp),%rax
sub    %rax,%rsi
call   1070 <bzero@plt>
mov    -0x24(%rbp),%esi
mov    -0x18(%rbp),%rdx
lea    0xe37(%rip),%rdi
call   1040 <printf@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readline>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%rdx
mov    %rsi,%rdi
mov    %r14,%rsi
call   1050 <fgets@plt>
test   %rax,%rax
je     11a8 <readline+0x58>
mov    %rbx,%rdi
call   1060 <trimstr@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
mov    %eax,%r15d
movslq %eax,%rbp
lea    (%rbx,%rbp,1),%rdi
sub    %rbp,%r14
mov    %r14,%rsi
call   1070 <bzero@plt>
lea    0xe64(%rip),%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
call   1040 <printf@plt>
jmp    11ae <readline+0x5e>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [20 x i8] c"read line (%d): %s\0A\00"
define i32 @readline(i32* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store i32* %0, i32** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%9 = load i8*, i8** %6
%10 = load i64, i64* %7
%11 = load i32*, i32** %5
%12 = call i32* @fgets(i8* %9, i64 %10, i32* %11)
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 -1, i32* %4
br label %33
15: ; preds = %3
%16 = load i8*, i8** %6
%17 = call i32 @trimstr(i8* %16)
%18 = load i8*, i8** %6
%19 = call i32 @strlen(i8* %18)
store i32 %19, i32* %8
%20 = load i8*, i8** %6
%21 = load i32, i32* %8
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
%24 = load i64, i64* %7
%25 = load i32, i32* %8
%26 = sext i32 %25 to i64
%27 = sub i64 %24, %26
%28 = call i32 @bzero(i8* %23, i64 %27)
%29 = load i32, i32* %8
%30 = load i8*, i8** %6
%31 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %29, i8* %30)
%32 = load i32, i32* %8
store i32 %32, i32* %4
br label %33
33: ; preds = %15, %14
%34 = load i32, i32* %4
ret i32 %34
}

LLVM IR (-O2):
@.str = [20 x i8] c"read line (%d): %s\0A\00"
define i32 @readline(i32* nocapture %0, i8* %1, i64 %2) {
%4 = tail call i32* @fgets(i8* %1, i64 %2, i32* %0)
%5 = icmp eq i32* %4, null
br i1 %5, label %14, label %6
6: ; preds = %3
%7 = tail call i32 @trimstr(i8* %1)
%8 = tail call i32 @strlen(i8* %1)
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %1, i64 %9
%11 = sub i64 %2, %9
%12 = tail call i32 @bzero(i8* %10, i64 %11)
%13 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %8, i8* %1)
br label %14
14: ; preds = %3, %6
%15 = phi i32 [ %8, %6 ], [ -1, %3 ]
ret i32 %15
}