#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__ calloc(int, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ perror(char *);
int read_single(int, char *);
char *read_str(int fd) {
  char *s_buf = (char *)calloc(1, sizeof(char));
  char *t_buf = (char *)calloc(513, sizeof(char));
  int index = 0;
  if (s_buf == NULL || t_buf == NULL) {
    exit(1);
  }
  int r = read_single(fd, s_buf);
  while (r > 0) {
    memcpy(t_buf + index++, s_buf, 1);
    if (s_buf[0] == '\n') {
      free(s_buf);
      return t_buf;
    }
    r = read_single(fd, s_buf);
  }
  if (r <= 0) {
    perror("Failed to read");
    free(s_buf);
    free(t_buf);
    exit(1);
  }
  free(s_buf);
  return t_buf;
}


x86 assembly (clang, -O0):
<read_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    $0x1,%esi
mov    %esi,%edi
call   1040 <calloc@plt>
mov    %rax,-0x18(%rbp)
mov    $0x201,%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11ab <read_str+0x4b>
cmpq   $0x0,-0x20(%rbp)
jne    11b5 <read_str+0x55>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1060 <read_single@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jle    122a <read_str+0xca>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x24(%rbp)
cltq
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
mov    $0x1,%edx
call   1050 <memcpy@plt>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
jne    1216 <read_str+0xb6>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    126d <read_str+0x10d>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1060 <read_single@plt>
mov    %eax,-0x28(%rbp)
jmp    11c4 <read_str+0x64>
cmpl   $0x0,-0x28(%rbp)
jg     125c <read_str+0xfc>
lea    0xdc5(%rip),%rdi
call   1070 <perror@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_str>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r15d
mov    $0x1,%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,%rbx
mov    $0x201,%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
test   %rbx,%rbx
je     11f4 <read_str+0x94>
mov    %rax,%r14
test   %rax,%rax
je     11f4 <read_str+0x94>
mov    %r15d,%edi
mov    %rbx,%rsi
call   1060 <read_single@plt>
test   %eax,%eax
jle    11d8 <read_str+0x78>
mov    %r14,%rbp
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1050 <memcpy@plt>
cmpb   $0xa,(%rbx)
je     11fe <read_str+0x9e>
mov    %r15d,%edi
mov    %rbx,%rsi
call   1060 <read_single@plt>
add    $0x1,%rbp
test   %eax,%eax
jg     11b0 <read_str+0x50>
lea    0xe21(%rip),%rdi
call   1070 <perror@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"Failed to read\00"
define i8* @read_str(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
%8 = call i64 @calloc(i32 1, i32 1)
%9 = inttoptr i64 %8 to i8*
store i8* %9, i8** %4
%10 = call i64 @calloc(i32 513, i32 1)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %5
store i32 0, i32* %6
%12 = load i8*, i8** %4
%13 = icmp eq i8* %12, null
br i1 %13, label %17, label %14
14: ; preds = %1
%15 = load i8*, i8** %5
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %14, %1
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %14
%20 = load i32, i32* %3
%21 = load i8*, i8** %4
%22 = call i32 @read_single(i32 %20, i8* %21)
store i32 %22, i32* %7
br label %23
23: ; preds = %43, %19
%24 = load i32, i32* %7
%25 = icmp sgt i32 %24, 0
br i1 %25, label %26, label %47
26: ; preds = %23
%27 = load i8*, i8** %5
%28 = load i32, i32* %6
%29 = add nsw i32 %28, 1
store i32 %29, i32* %6
%30 = sext i32 %28 to i64
%31 = getelementptr inbounds i8, i8* %27, i64 %30
%32 = load i8*, i8** %4
%33 = call i32 @memcpy(i8* %31, i8* %32, i32 1)
%34 = load i8*, i8** %4
%35 = getelementptr inbounds i8, i8* %34, i64 0
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %37, 10
br i1 %38, label %39, label %43
39: ; preds = %26
%40 = load i8*, i8** %4
%41 = call i32 @free(i8* %40)
%42 = load i8*, i8** %5
store i8* %42, i8** %2
br label %61
43: ; preds = %26
%44 = load i32, i32* %3
%45 = load i8*, i8** %4
%46 = call i32 @read_single(i32 %44, i8* %45)
store i32 %46, i32* %7
br label %23
47: ; preds = %23
%48 = load i32, i32* %7
%49 = icmp sle i32 %48, 0
br i1 %49, label %50, label %57
50: ; preds = %47
%51 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%52 = load i8*, i8** %4
%53 = call i32 @free(i8* %52)
%54 = load i8*, i8** %5
%55 = call i32 @free(i8* %54)
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %47
%58 = load i8*, i8** %4
%59 = call i32 @free(i8* %58)
%60 = load i8*, i8** %5
store i8* %60, i8** %2
br label %61
61: ; preds = %57, %39
%62 = load i8*, i8** %2
ret i8* %62
}

LLVM IR (-O2):
@.str = [15 x i8] c"Failed to read\00"
define i8* @read_str(i32 %0) {
%2 = tail call i64 @calloc(i32 1, i32 1)
%3 = inttoptr i64 %2 to i8*
%4 = tail call i64 @calloc(i32 513, i32 1)
%5 = inttoptr i64 %4 to i8*
%6 = icmp eq i64 %2, 0
%7 = icmp eq i64 %4, 0
%8 = select i1 %6, i1 true, i1 %7
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %1
%12 = tail call i32 @read_single(i32 %0, i8* %3)
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %26
14: ; preds = %11, %22
%15 = phi i64 [ %23, %22 ], [ 0, %11 ]
%16 = getelementptr inbounds i8, i8* %5, i64 %15
%17 = tail call i32 @memcpy(i8* %16, i8* %3, i32 1)
%18 = load i8, i8* %3
%19 = icmp eq i8 %18, 10
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = tail call i32 @free(i8* %3)
ret i8* %5
22: ; preds = %14
%23 = add nuw i64 %15, 1
%24 = tail call i32 @read_single(i32 %0, i8* %3)
%25 = icmp sgt i32 %24, 0
br i1 %25, label %14, label %26
26: ; preds = %22, %11
%27 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%28 = tail call i32 @free(i8* %3)
%29 = tail call i32 @free(i8* %5)
%30 = tail call i32 @exit(i32 1)
unreachable
}