#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 */
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *strchr(char *, char);
char *xalloc(int, char *);
char *xrealloc(char *, int, char *);
char *xreadline(FILE *fin, char *myname) {
  int buflen = 32;
  char *buf = xalloc(buflen, myname);
  char *res, *nl;
  res = fgets(buf, 32, fin);
  if (res == NULL) {
    free(buf);
    return NULL;
  }
  nl = strchr(buf, '\n');
  while (nl == NULL) {
    int oldbuflen = buflen;
    buflen <<= 1;
    buf = xrealloc(buf, buflen, myname);
    res = fgets(buf + oldbuflen - 1, buflen - oldbuflen + 1, fin);
    if (res == NULL)
      return buf;
    nl = strchr(buf + oldbuflen - 1, '\n');
  }
  *nl = 0;
  return buf;
}


x86 assembly (clang, -O0):
<xreadline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x20,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1070 <xalloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x20,%esi
call   1060 <fgets@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11ae <xreadline+0x5e>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    125c <xreadline+0x10c>
mov    -0x28(%rbp),%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    124d <xreadline+0xfd>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x1c(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   1050 <xrealloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
movslq -0x3c(%rbp),%rax
add    %rax,%rdi
add    $0xffffffffffffffff,%rdi
mov    -0x1c(%rbp),%esi
sub    -0x3c(%rbp),%esi
add    $0x1,%esi
mov    -0x10(%rbp),%rdx
call   1060 <fgets@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    122b <xreadline+0xdb>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    125c <xreadline+0x10c>
mov    -0x28(%rbp),%rdi
movslq -0x3c(%rbp),%rax
add    %rax,%rdi
add    $0xffffffffffffffff,%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
mov    %rax,-0x38(%rbp)
jmp    11c0 <xreadline+0x70>
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xreadline>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    $0x20,%edi
call   1070 <xalloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    $0x20,%esi
mov    %r15,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     11ea <xreadline+0x9a>
mov    %r12,%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
test   %rax,%rax
jne    11e5 <xreadline+0x95>
mov    $0x20,%ebp
nopw   0x0(%rax,%rax,1)
mov    %ebp,%ebx
lea    (%rbx,%rbx,1),%ebp
mov    %r12,%rdi
mov    %ebp,%esi
mov    %r14,%rdx
call   1050 <xrealloc@plt>
mov    %rax,%r12
movslq %ebx,%rsi
lea    (%rax,%rsi,1),%rbx
add    $0xffffffffffffffff,%rbx
or     $0x1,%esi
mov    %rbx,%rdi
mov    %r15,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     11f5 <xreadline+0xa5>
mov    %rbx,%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11a0 <xreadline+0x50>
movb   $0x0,(%rax)
jmp    11f5 <xreadline+0xa5>
mov    %r12,%rdi
call   1030 <free@plt>
xor    %r12d,%r12d
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @xreadline(i32* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32
store i32* %0, i32** %4
store i8* %1, i8** %5
store i32 32, i32* %6
%11 = load i32, i32* %6
%12 = load i8*, i8** %5
%13 = call i8* @xalloc(i32 %11, i8* %12)
store i8* %13, i8** %7
%14 = load i8*, i8** %7
%15 = load i32*, i32** %4
%16 = call i8* @fgets(i8* %14, i32 32, i32* %15)
store i8* %16, i8** %8
%17 = load i8*, i8** %8
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %22
19: ; preds = %2
%20 = load i8*, i8** %7
%21 = call i32 @free(i8* %20)
store i8* null, i8** %3
br label %61
22: ; preds = %2
%23 = load i8*, i8** %7
%24 = call i8* @strchr(i8* %23, i8 signext 10)
store i8* %24, i8** %9
br label %25
25: ; preds = %51, %22
%26 = load i8*, i8** %9
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %58
28: ; preds = %25
%29 = load i32, i32* %6
store i32 %29, i32* %10
%30 = load i32, i32* %6
%31 = shl i32 %30, 1
store i32 %31, i32* %6
%32 = load i8*, i8** %7
%33 = load i32, i32* %6
%34 = load i8*, i8** %5
%35 = call i8* @xrealloc(i8* %32, i32 %33, i8* %34)
store i8* %35, i8** %7
%36 = load i8*, i8** %7
%37 = load i32, i32* %10
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
%40 = getelementptr inbounds i8, i8* %39, i64 -1
%41 = load i32, i32* %6
%42 = load i32, i32* %10
%43 = sub nsw i32 %41, %42
%44 = add nsw i32 %43, 1
%45 = load i32*, i32** %4
%46 = call i8* @fgets(i8* %40, i32 %44, i32* %45)
store i8* %46, i8** %8
%47 = load i8*, i8** %8
%48 = icmp eq i8* %47, null
br i1 %48, label %49, label %51
49: ; preds = %28
%50 = load i8*, i8** %7
store i8* %50, i8** %3
br label %61
51: ; preds = %28
%52 = load i8*, i8** %7
%53 = load i32, i32* %10
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %52, i64 %54
%56 = getelementptr inbounds i8, i8* %55, i64 -1
%57 = call i8* @strchr(i8* %56, i8 signext 10)
store i8* %57, i8** %9
br label %25
58: ; preds = %25
%59 = load i8*, i8** %9
store i8 0, i8* %59
%60 = load i8*, i8** %7
store i8* %60, i8** %3
br label %61
61: ; preds = %58, %49, %19
%62 = load i8*, i8** %3
ret i8* %62
}

LLVM IR (-O2):
define i8* @xreadline(i32* nocapture %0, i8* %1) {
%3 = tail call i8* @xalloc(i32 32, i8* %1)
%4 = tail call i8* @fgets(i8* %3, i32 32, i32* %0)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @free(i8* %3)
br label %28
8: ; preds = %2
%9 = tail call i8* @strchr(i8* dereferenceable(1) %3, i8 signext 10)
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %25
11: ; preds = %8, %22
%12 = phi i8* [ %15, %22 ], [ %3, %8 ]
%13 = phi i32 [ %14, %22 ], [ 32, %8 ]
%14 = shl i32 %13, 1
%15 = tail call i8* @xrealloc(i8* %12, i32 %14, i8* %1)
%16 = sext i32 %13 to i64
%17 = getelementptr inbounds i8, i8* %15, i64 -1
%18 = getelementptr inbounds i8, i8* %17, i64 %16
%19 = or i32 %13, 1
%20 = tail call i8* @fgets(i8* %18, i32 %19, i32* %0)
%21 = icmp eq i8* %20, null
br i1 %21, label %28, label %22
22: ; preds = %11
%23 = tail call i8* @strchr(i8* %18, i8 signext 10)
%24 = icmp eq i8* %23, null
br i1 %24, label %11, label %25
25: ; preds = %22, %8
%26 = phi i8* [ %3, %8 ], [ %15, %22 ]
%27 = phi i8* [ %9, %8 ], [ %23, %22 ]
store i8 0, i8* %27
br label %28
28: ; preds = %11, %25, %6
%29 = phi i8* [ null, %6 ], [ %26, %25 ], [ %15, %11 ]
ret i8* %29
}