#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 */
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, unsigned int);
int /*<<< orphan*/ read(int /*<<< orphan*/, char *, int);
char *line_read_stdin(unsigned int max_length) {
  char *line;
  unsigned int i;
  char c;
  line = (char *)malloc(sizeof(char) * max_length);
  memset(line, 0, max_length);
  for (i = 0; i < max_length - 1; i++) {
    read(0, &c, 1);
    if (c == '\n')
      break;
    line[i] = c;
  }
  return line;
}


x86 assembly (clang, -O0):
<line_read_stdin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%edx
xor    %esi,%esi
call   1030 <memset@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    -0x4(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jae    11b0 <line_read_stdin+0x80>
xor    %edi,%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
movsbl -0x15(%rbp),%eax
cmp    $0xa,%eax
jne    1195 <line_read_stdin+0x65>
jmp    11b0 <line_read_stdin+0x80>
mov    -0x15(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1162 <line_read_stdin+0x32>
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<line_read_stdin>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebp
call   1050 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1030 <memset@plt>
add    $0xffffffff,%ebp
je     1185 <line_read_stdin+0x55>
mov    %ebp,%ebp
xor    %ebx,%ebx
lea    0x7(%rsp),%r14
nopl   0x0(%rax,%rax,1)
xor    %edi,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
movzbl 0x7(%rsp),%eax
cmp    $0xa,%al
je     1185 <line_read_stdin+0x55>
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1160 <line_read_stdin+0x30>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @line_read_stdin(i32 %0) {
%2 = alloca i8
%3 = tail call i64 @malloc(i32 %0)
%4 = inttoptr i64 %3 to i8*
%5 = tail call i32 @memset(i8* %4, i32 0, i32 %0)
%6 = add i32 %0, -1
%7 = icmp eq i32 %6, 0
br i1 %7, label %19, label %8
8: ; preds = %1
%9 = zext i32 %6 to i64
br label %10
10: ; preds = %8, %15
%11 = phi i64 [ 0, %8 ], [ %17, %15 ]
%12 = call i32 @read(i32 0, i8* %2, i32 1)
%13 = load i8, i8* %2
%14 = icmp eq i8 %13, 10
br i1 %14, label %19, label %15
15: ; preds = %10
%16 = getelementptr inbounds i8, i8* %4, i64 %11
store i8 %13, i8* %16
%17 = add nuw nsw i64 %11, 1
%18 = icmp eq i64 %17, %9
br i1 %18, label %19, label %10
19: ; preds = %15, %10, %1
ret i8* %4
}