#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 /*<<< orphan*/ free(char *);
char *px_malloc0(int);
char *px_strdup(char *);
int read(int, char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
char *px_readline(int fd, char *buffer, size_t bufsize) {
  char c = '\0';
  if (fd < 0)
    return NULL;
  if (read(fd, &c, 1) != 1)
    return buffer;
  if (c == '\n')
    return buffer ? buffer : px_strdup("");
  if (!buffer) {
    buffer = px_strdup("");
    bufsize = 0;
  }
  if (bufsize <= strlen(buffer)) {
    char *tmp = px_malloc0(1024 + strlen(buffer) + 1);
    strcpy(tmp, buffer);
    free(buffer);
    buffer = tmp;
    bufsize = strlen(buffer) + 1024;
  }
  strncat(buffer, &c, 1);
  return px_readline(fd, buffer, bufsize);
}


x86 assembly (clang, -O0):
<px_readline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movb   $0x0,-0x21(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11ae <px_readline+0x2e>
movq   $0x0,-0x8(%rbp)
jmp    12d2 <px_readline+0x152>
mov    -0xc(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
cmp    $0x1,%eax
je     11d5 <px_readline+0x55>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12d2 <px_readline+0x152>
movsbl -0x21(%rbp),%eax
cmp    $0xa,%eax
jne    1217 <px_readline+0x97>
cmpq   $0x0,-0x18(%rbp)
je     11fa <px_readline+0x7a>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    120a <px_readline+0x8a>
lea    0xdff(%rip),%rdi
call   1080 <px_strdup@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12d2 <px_readline+0x152>
cmpq   $0x0,-0x18(%rbp)
jne    123a <px_readline+0xba>
lea    0xdd7(%rip),%rdi
call   1080 <px_strdup@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
movslq %ecx,%rcx
cmp    %rcx,%rax
ja     12ac <px_readline+0x12c>
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%edi
add    $0x400,%edi
add    $0x1,%edi
call   1090 <px_malloc0@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x400,%eax
cltq
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1060 <strncat@plt>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   10a0 <px_readline@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<px_readline>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
movb   $0x0,0x7(%rsp)
test   %edi,%edi
js     11d6 <px_readline+0x56>
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
cmp    $0x1,%eax
jne    1268 <px_readline+0xe8>
cmpb   $0xa,0x7(%rsp)
jne    11dd <px_readline+0x5d>
test   %rbx,%rbx
jne    1268 <px_readline+0xe8>
lea    0xe39(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1080 <px_strdup@plt>
xor    %ebx,%ebx
jmp    1268 <px_readline+0xe8>
test   %rbx,%rbx
je     11f3 <px_readline+0x73>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cltq
cmp    %r14,%rax
jae    120a <px_readline+0x8a>
jmp    1246 <px_readline+0xc6>
lea    0xe06(%rip),%rdi
call   1080 <px_strdup@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <strlen@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    0x401(%rax),%edi
call   1090 <px_malloc0@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1050 <strlen@plt>
add    $0x400,%eax
movslq %eax,%r14
mov    %r15,%rbx
lea    0x7(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x1,%edx
call   1060 <strncat@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   10a0 <px_readline@plt>
mov    %rax,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [1 x i8] zeroinitializer
define i8* @px_readline(i32 %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8
%9 = alloca i8*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i8 0, i8* %8
%10 = load i32, i32* %5
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %3
store i8* null, i8** %4
br label %66
13: ; preds = %3
%14 = load i32, i32* %5
%15 = call i32 @read(i32 %14, i8* %8, i32 1)
%16 = icmp ne i32 %15, 1
br i1 %16, label %17, label %19
17: ; preds = %13
%18 = load i8*, i8** %6
store i8* %18, i8** %4
br label %66
19: ; preds = %13
%20 = load i8, i8* %8
%21 = sext i8 %20 to i32
%22 = icmp eq i32 %21, 10
br i1 %22, label %23, label %32
23: ; preds = %19
%24 = load i8*, i8** %6
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %28
26: ; preds = %23
%27 = load i8*, i8** %6
br label %30
28: ; preds = %23
%29 = call i8* @px_strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %30
30: ; preds = %28, %26
%31 = phi i8* [ %27, %26 ], [ %29, %28 ]
store i8* %31, i8** %4
br label %66
32: ; preds = %19
%33 = load i8*, i8** %6
%34 = icmp ne i8* %33, null
br i1 %34, label %37, label %35
35: ; preds = %32
%36 = call i8* @px_strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
store i8* %36, i8** %6
store i64 0, i64* %7
br label %37
37: ; preds = %35, %32
%38 = load i64, i64* %7
%39 = load i8*, i8** %6
%40 = call i32 @strlen(i8* %39)
%41 = sext i32 %40 to i64
%42 = icmp ule i64 %38, %41
br i1 %42, label %43, label %59
43: ; preds = %37
%44 = load i8*, i8** %6
%45 = call i32 @strlen(i8* %44)
%46 = add nsw i32 1024, %45
%47 = add nsw i32 %46, 1
%48 = call i8* @px_malloc0(i32 %47)
store i8* %48, i8** %9
%49 = load i8*, i8** %9
%50 = load i8*, i8** %6
%51 = call i32 @strcpy(i8* %49, i8* %50)
%52 = load i8*, i8** %6
%53 = call i32 @free(i8* %52)
%54 = load i8*, i8** %9
store i8* %54, i8** %6
%55 = load i8*, i8** %6
%56 = call i32 @strlen(i8* %55)
%57 = add nsw i32 %56, 1024
%58 = sext i32 %57 to i64
store i64 %58, i64* %7
br label %59
59: ; preds = %43, %37
%60 = load i8*, i8** %6
%61 = call i32 @strncat(i8* %60, i8* %8, i32 1)
%62 = load i32, i32* %5
%63 = load i8*, i8** %6
%64 = load i64, i64* %7
%65 = call i8* @px_readline(i32 %62, i8* %63, i64 %64)
store i8* %65, i8** %4
br label %66
66: ; preds = %59, %30, %17, %12
%67 = load i8*, i8** %4
ret i8* %67
}

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
define i8* @px_readline(i32 %0, i8* %1, i64 %2) {
%4 = alloca i8
store i8 0, i8* %4
%5 = icmp slt i32 %0, 0
br i1 %5, label %39, label %6
6: ; preds = %3
%7 = call i32 @read(i32 %0, i8* %4, i32 1)
%8 = icmp eq i32 %7, 1
br i1 %8, label %9, label %39
9: ; preds = %6
%10 = load i8, i8* %4
%11 = icmp eq i8 %10, 10
%12 = icmp eq i8* %1, null
br i1 %11, label %13, label %16
13: ; preds = %9
br i1 %12, label %14, label %39
14: ; preds = %13
%15 = tail call i8* @px_strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
br label %39
16: ; preds = %9
br i1 %12, label %17, label %20
17: ; preds = %16
%18 = tail call i8* @px_strdup(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%19 = tail call i32 @strlen(i8* %18)
br label %24
20: ; preds = %16
%21 = tail call i32 @strlen(i8* %1)
%22 = sext i32 %21 to i64
%23 = icmp ult i64 %22, %2
br i1 %23, label %34, label %24
24: ; preds = %17, %20
%25 = phi i8* [ %18, %17 ], [ %1, %20 ]
%26 = tail call i32 @strlen(i8* %25)
%27 = add nsw i32 %26, 1025
%28 = tail call i8* @px_malloc0(i32 %27)
%29 = tail call i32 @strcpy(i8* %28, i8* %25)
%30 = tail call i32 @free(i8* %25)
%31 = tail call i32 @strlen(i8* %28)
%32 = add nsw i32 %31, 1024
%33 = sext i32 %32 to i64
br label %34
34: ; preds = %24, %20
%35 = phi i8* [ %28, %24 ], [ %1, %20 ]
%36 = phi i64 [ %33, %24 ], [ %2, %20 ]
%37 = call i32 @strncat(i8* %35, i8* %4, i32 1)
%38 = call i8* @px_readline(i32 %0, i8* %35, i64 %36)
br label %39
39: ; preds = %14, %13, %6, %3, %34
%40 = phi i8* [ %38, %34 ], [ null, %3 ], [ %1, %6 ], [ %15, %14 ], [ %1, %13 ]
ret i8* %40
}