#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 */
struct string {
  int dummy;
};

/* Variables and functions */
struct string *create_string_copy(char *);
struct string *create_string_len(int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
size_t getline(char **, size_t *, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
struct string *_prim_read_string() {
  char *buffer = NULL;
  size_t len = 0;
  size_t read;
  read = getline(&buffer, &len, stdin);
  if (read == -1) {
    return create_string_len(0);
  } else {
    struct string *r = create_string_copy(buffer);
    free(buffer);
    return r;
  }
}


x86 assembly (clang, -O0):
<_prim_read_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    0x2e89(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <getline@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    118d <_prim_read_string+0x4d>
xor    %edi,%edi
call   1060 <create_string_len@plt>
mov    %rax,-0x8(%rbp)
jmp    11ab <_prim_read_string+0x6b>
mov    -0x10(%rbp),%rdi
call   1040 <create_string_copy@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_prim_read_string>:
push   %rbx
sub    $0x10,%rsp
movq   $0x0,(%rsp)
movq   $0x0,0x8(%rsp)
mov    0x2e8b(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rdi
lea    0x8(%rsp),%rsi
call   1050 <getline@plt>
cmp    $0xffffffffffffffff,%rax
je     1189 <_prim_read_string+0x49>
mov    (%rsp),%rdi
call   1040 <create_string_copy@plt>
mov    %rax,%rbx
mov    (%rsp),%rdi
call   1030 <free@plt>
jmp    1193 <_prim_read_string+0x53>
xor    %edi,%edi
call   1060 <create_string_len@plt>
mov    %rax,%rbx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.string = type { i32 }
@stdin = global i32 0
define %struct.string* @_prim_read_string() {
%1 = alloca %struct.string*
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i64
%5 = alloca %struct.string*
store i8* null, i8** %2
store i64 0, i64* %3
%6 = load i32, i32* @stdin
%7 = call i64 @getline(i8** %2, i64* %3, i32 %6)
store i64 %7, i64* %4
%8 = load i64, i64* %4
%9 = icmp eq i64 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call %struct.string* @create_string_len(i32 0)
store %struct.string* %11, %struct.string** %1
br label %18
12: ; preds = %0
%13 = load i8*, i8** %2
%14 = call %struct.string* @create_string_copy(i8* %13)
store %struct.string* %14, %struct.string** %5
%15 = load i8*, i8** %2
%16 = call i32 @free(i8* %15)
%17 = load %struct.string*, %struct.string** %5
store %struct.string* %17, %struct.string** %1
br label %18
18: ; preds = %12, %10
%19 = load %struct.string*, %struct.string** %1
ret %struct.string* %19
}

LLVM IR (-O2):
%struct.string = type { i32 }
@stdin = global i32 0
define %struct.string* @_prim_read_string() {
%1 = alloca i8*
%2 = alloca i64
%3 = bitcast i8** %1 to i8*
store i8* null, i8** %1
%4 = bitcast i64* %2 to i8*
store i64 0, i64* %2
%5 = load i32, i32* @stdin
%6 = call i64 @getline(i8** %1, i64* %2, i32 %5)
%7 = icmp eq i64 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call %struct.string* @create_string_len(i32 0)
br label %15
10: ; preds = %0
%11 = load i8*, i8** %1
%12 = call %struct.string* @create_string_copy(i8* %11)
%13 = load i8*, i8** %1
%14 = call i32 @free(i8* %13)
br label %15
15: ; preds = %10, %8
%16 = phi %struct.string* [ %9, %8 ], [ %12, %10 ]
ret %struct.string* %16
}