#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*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, size_t, int /*<<< orphan*/ *);
int strlen(char *);
char *xmalloc(size_t);
char *xrealloc(char *, size_t);
char *b_gets(FILE *FH, size_t blocksize) {
  char *tmp, *s = xmalloc(blocksize);
  size_t b = blocksize;
  unsigned int c = 1;
  fgets(s, blocksize, FH);
  while (*(s + strlen(s) - 1) != '\n' && !feof(FH)) {
    tmp = xrealloc(s, b + blocksize);
    s = tmp;
    fgets(s + b - c++, blocksize, FH);
    b += blocksize;
  }
  return s;
}


x86 assembly (clang, -O0):
<b_gets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <xmalloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x8(%rbp),%rdx
call   1060 <fgets@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
movsbl -0x1(%rax,%rcx,1),%ecx
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x2d(%rbp)
je     11ce <b_gets+0x7e>
mov    -0x8(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x2d(%rbp)
mov    -0x2d(%rbp),%al
test   $0x1,%al
jne    11de <b_gets+0x8e>
jmp    1238 <b_gets+0xe8>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
add    -0x10(%rbp),%rsi
call   1040 <xrealloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
add    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x2c(%rbp)
mov    %eax,%eax
mov    %eax,%ecx
xor    %eax,%eax
sub    %rcx,%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x8(%rbp),%rdx
call   1060 <fgets@plt>
mov    -0x10(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    118d <b_gets+0x3d>
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<b_gets>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r15
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1050 <xmalloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1060 <fgets@plt>
mov    %r12,%rdi
call   1030 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%r12,%rax,1)
je     1203 <b_gets+0xb3>
lea    (%r15,%r15,1),%r13
mov    $0x1,%ebp
mov    %r15,0x8(%rsp)
mov    %rbx,0x10(%rsp)
mov    0x8(%rsp),%r14
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1070 <feof@plt>
test   %eax,%eax
jne    1203 <b_gets+0xb3>
mov    %ebp,%eax
mov    %r15,%rbx
sub    %rax,%rbx
add    %r14,%r15
mov    %r12,%rdi
mov    %r13,%rsi
call   1040 <xrealloc@plt>
mov    %rax,%r12
add    $0x1,%ebp
add    %rax,%rbx
mov    %rbx,%rdi
mov    0x10(%rsp),%rbx
mov    %r14,%rsi
mov    %rbx,%rdx
call   1060 <fgets@plt>
mov    %r12,%rdi
call   1030 <strlen@plt>
cltq
add    %r14,%r13
cmpb   $0xa,-0x1(%r12,%rax,1)
jne    11b0 <b_gets+0x60>
mov    %r12,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @b_gets(i32* %0, i64 %1) {
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
store i32* %0, i32** %3
store i64 %1, i64* %4
%9 = load i64, i64* %4
%10 = call i8* @xmalloc(i64 %9)
store i8* %10, i8** %6
%11 = load i64, i64* %4
store i64 %11, i64* %7
store i32 1, i32* %8
%12 = load i8*, i8** %6
%13 = load i64, i64* %4
%14 = load i32*, i32** %3
%15 = call i32 @fgets(i8* %12, i64 %13, i32* %14)
br label %16
16: ; preds = %33, %2
%17 = load i8*, i8** %6
%18 = load i8*, i8** %6
%19 = call i32 @strlen(i8* %18)
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %17, i64 %20
%22 = getelementptr inbounds i8, i8* %21, i64 -1
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp ne i32 %24, 10
br i1 %25, label %26, label %31
26: ; preds = %16
%27 = load i32*, i32** %3
%28 = call i32 @feof(i32* %27)
%29 = icmp ne i32 %28, 0
%30 = xor i1 %29, true
br label %31
31: ; preds = %26, %16
%32 = phi i1 [ false, %16 ], [ %30, %26 ]
br i1 %32, label %33, label %54
33: ; preds = %31
%34 = load i8*, i8** %6
%35 = load i64, i64* %7
%36 = load i64, i64* %4
%37 = add i64 %35, %36
%38 = call i8* @xrealloc(i8* %34, i64 %37)
store i8* %38, i8** %5
%39 = load i8*, i8** %5
store i8* %39, i8** %6
%40 = load i8*, i8** %6
%41 = load i64, i64* %7
%42 = getelementptr inbounds i8, i8* %40, i64 %41
%43 = load i32, i32* %8
%44 = add i32 %43, 1
store i32 %44, i32* %8
%45 = zext i32 %43 to i64
%46 = sub i64 0, %45
%47 = getelementptr inbounds i8, i8* %42, i64 %46
%48 = load i64, i64* %4
%49 = load i32*, i32** %3
%50 = call i32 @fgets(i8* %47, i64 %48, i32* %49)
%51 = load i64, i64* %4
%52 = load i64, i64* %7
%53 = add i64 %52, %51
store i64 %53, i64* %7
br label %16
54: ; preds = %31
%55 = load i8*, i8** %6
ret i8* %55
}

LLVM IR (-O2):
define i8* @b_gets(i32* nocapture %0, i64 %1) {
%3 = tail call i8* @xmalloc(i64 %1)
%4 = tail call i32 @fgets(i8* %3, i64 %1, i32* %0)
%5 = tail call i32 @strlen(i8* %3)
%6 = sext i32 %5 to i64
%7 = getelementptr inbounds i8, i8* %3, i64 %6
%8 = getelementptr inbounds i8, i8* %7, i64 -1
%9 = load i8, i8* %8
%10 = icmp eq i8 %9, 10
br i1 %10, label %32, label %11
11: ; preds = %2, %17
%12 = phi i32 [ %21, %17 ], [ 1, %2 ]
%13 = phi i64 [ %18, %17 ], [ %1, %2 ]
%14 = phi i8* [ %19, %17 ], [ %3, %2 ]
%15 = tail call i32 @feof(i32* %0)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %32
17: ; preds = %11
%18 = add i64 %13, %1
%19 = tail call i8* @xrealloc(i8* %14, i64 %18)
%20 = getelementptr inbounds i8, i8* %19, i64 %13
%21 = add i32 %12, 1
%22 = zext i32 %12 to i64
%23 = sub nsw i64 0, %22
%24 = getelementptr inbounds i8, i8* %20, i64 %23
%25 = tail call i32 @fgets(i8* %24, i64 %1, i32* %0)
%26 = tail call i32 @strlen(i8* %19)
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %19, i64 -1
%29 = getelementptr inbounds i8, i8* %28, i64 %27
%30 = load i8, i8* %29
%31 = icmp eq i8 %30, 10
br i1 %31, label %32, label %11
32: ; preds = %11, %17, %2
%33 = phi i8* [ %3, %2 ], [ %19, %17 ], [ %14, %11 ]
ret i8* %33
}