#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*/ *);
scalar_t__ malloc(int);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *freadln(FILE *ifile) {
  char line[1024];
  char *r = NULL;
  int len;
  char *s = fgets((char *)line, 1024, ifile);
  if (!s)
    return s;
  len = strlen(s) + 1;
  r = (char *)malloc(len);
  strncpy(r, s, len);
  return r;
}


x86 assembly (clang, -O0):
<freadln>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x418(%rbp)
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x400,%esi
call   1050 <fgets@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jne    1194 <freadln+0x54>
mov    -0x428(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11df <freadln+0x9f>
mov    -0x428(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
mov    %eax,-0x41c(%rbp)
mov    -0x41c(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x418(%rbp)
mov    -0x418(%rbp),%rdi
mov    -0x428(%rbp),%rsi
mov    -0x41c(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x418(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<freadln>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    %rdi,%rdx
mov    %rsp,%rdi
mov    $0x400,%esi
call   1050 <fgets@plt>
test   %rax,%rax
je     1187 <freadln+0x47>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <strncpy@plt>
jmp    118a <freadln+0x4a>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i8* @freadln(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca [1024 x i8]
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
store i32* %0, i32** %3
store i8* null, i8** %5
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%9 = load i32*, i32** %3
%10 = call i8* @fgets(i8* %8, i32 1024, i32* %9)
store i8* %10, i8** %7
%11 = load i8*, i8** %7
%12 = icmp ne i8* %11, null
br i1 %12, label %15, label %13
13: ; preds = %1
%14 = load i8*, i8** %7
store i8* %14, i8** %2
br label %27
15: ; preds = %1
%16 = load i8*, i8** %7
%17 = call i32 @strlen(i8* %16)
%18 = add nsw i32 %17, 1
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = call i64 @malloc(i32 %19)
%21 = inttoptr i64 %20 to i8*
store i8* %21, i8** %5
%22 = load i8*, i8** %5
%23 = load i8*, i8** %7
%24 = load i32, i32* %6
%25 = call i32 @strncpy(i8* %22, i8* %23, i32 %24)
%26 = load i8*, i8** %5
store i8* %26, i8** %2
br label %27
27: ; preds = %15, %13
%28 = load i8*, i8** %2
ret i8* %28
}

LLVM IR (-O2):
define i8* @freadln(i32* nocapture %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = call i8* @fgets(i8* %3, i32 1024, i32* %0)
%5 = icmp eq i8* %4, null
br i1 %5, label %12, label %6
6: ; preds = %1
%7 = call i32 @strlen(i8* %4)
%8 = add nsw i32 %7, 1
%9 = call i64 @malloc(i32 %8)
%10 = inttoptr i64 %9 to i8*
%11 = call i32 @strncpy(i8* %10, i8* %4, i32 %8)
br label %12
12: ; preds = %1, %6
%13 = phi i8* [ %10, %6 ], [ null, %1 ]
ret i8* %13
}