#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 EOF;
int fgetc(int /*<<< orphan*/);
char *malloc(int);
int /*<<< orphan*/ printf(char *, char *);
char *realloc(char *, int);
int /*<<< orphan*/ stdin;
void d_s(char *s) {
  int c;
  int sz = 0;
  s = malloc(sizeof(char));
  while ((c = fgetc(stdin)) != '\n' && c != EOF) {
    s = realloc(s, sizeof(char) * ++sz);
    *(s + (sz - 1)) = c;
  }
  *(s + sz) = '\0';
  printf("d_s: s = [%s]\n", s);
}


x86 assembly (clang, -O0):
<d_s>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x10(%rbp)
mov    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,-0x8(%rbp)
mov    0x2e80(%rip),%rax
mov    (%rax),%edi
call   1040 <fgetc@plt>
mov    %eax,%ecx
mov    %ecx,-0xc(%rbp)
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x11(%rbp)
je     1194 <d_s+0x54>
mov    -0xc(%rbp),%eax
mov    0x2e54(%rip),%rcx
cmp    (%rcx),%eax
setne  %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    11a4 <d_s+0x64>
jmp    11dc <d_s+0x9c>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
cltq
shl    $0x0,%rax
mov    %eax,%esi
call   1060 <realloc@plt>
mov    %rax,-0x8(%rbp)
mov    -0xc(%rbp),%eax
mov    %al,%dl
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1161 <d_s+0x21>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rsi
lea    0xe0d(%rip),%rdi
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<d_s>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
mov    0x2e89(%rip),%r15
mov    (%r15),%edi
call   1040 <fgetc@plt>
mov    %eax,%ebp
xor    %eax,%eax
cmp    $0xa,%ebp
je     11b1 <d_s+0x71>
mov    0x2e69(%rip),%r12
cmp    (%r12),%ebp
je     11b1 <d_s+0x71>
xor    %r13d,%r13d
mov    %r13,%rbx
add    $0x1,%r13
mov    %r14,%rdi
mov    %r13d,%esi
call   1060 <realloc@plt>
mov    %rax,%r14
mov    %bpl,(%rax,%rbx,1)
mov    (%r15),%edi
call   1040 <fgetc@plt>
cmp    $0xa,%eax
je     11ae <d_s+0x6e>
mov    %eax,%ebp
cmp    (%r12),%eax
jne    1180 <d_s+0x40>
mov    %r13d,%eax
movb   $0x0,(%r14,%rax,1)
lea    0xe43(%rip),%rdi
mov    %r14,%rsi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <printf@plt>

LLVM IR (-O0):
@stdin = global i32 0
@EOF = global i32 0
@.str = [15 x i8] c"d_s: s = [%s]\0A\00"
define void @d_s(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %4
%5 = call i8* @malloc(i32 1)
store i8* %5, i8** %2
br label %6
6: ; preds = %16, %1
%7 = load i32, i32* @stdin
%8 = call i32 @fgetc(i32 %7)
store i32 %8, i32* %3
%9 = icmp ne i32 %8, 10
br i1 %9, label %10, label %14
10: ; preds = %6
%11 = load i32, i32* %3
%12 = load i32, i32* @EOF
%13 = icmp ne i32 %11, %12
br label %14
14: ; preds = %10, %6
%15 = phi i1 [ false, %6 ], [ %13, %10 ]
br i1 %15, label %16, label %31
16: ; preds = %14
%17 = load i8*, i8** %2
%18 = load i32, i32* %4
%19 = add nsw i32 %18, 1
store i32 %19, i32* %4
%20 = sext i32 %19 to i64
%21 = mul i64 1, %20
%22 = trunc i64 %21 to i32
%23 = call i8* @realloc(i8* %17, i32 %22)
store i8* %23, i8** %2
%24 = load i32, i32* %3
%25 = trunc i32 %24 to i8
%26 = load i8*, i8** %2
%27 = load i32, i32* %4
%28 = sub nsw i32 %27, 1
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %26, i64 %29
store i8 %25, i8* %30
br label %6
31: ; preds = %14
%32 = load i8*, i8** %2
%33 = load i32, i32* %4
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
store i8 0, i8* %35
%36 = load i8*, i8** %2
%37 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %36)
ret void
}

LLVM IR (-O2):
@stdin = global i32 0
@EOF = global i32 0
@.str = [15 x i8] c"d_s: s = [%s]\0A\00"
define void @d_s(i8* nocapture readnone %0) {
%2 = tail call dereferenceable_or_null(1) i8* @malloc(i32 1)
%3 = load i32, i32* @stdin
%4 = tail call i32 @fgetc(i32 %3)
%5 = icmp ne i32 %4, 10
%6 = load i32, i32* @EOF
%7 = icmp ne i32 %4, %6
%8 = select i1 %5, i1 %7, i1 false
br i1 %8, label %9, label %26
9: ; preds = %1, %9
%10 = phi i64 [ %13, %9 ], [ 0, %1 ]
%11 = phi i32 [ %19, %9 ], [ %4, %1 ]
%12 = phi i8* [ %15, %9 ], [ %2, %1 ]
%13 = add nuw i64 %10, 1
%14 = trunc i64 %13 to i32
%15 = tail call i8* @realloc(i8* %12, i32 %14)
%16 = trunc i32 %11 to i8
%17 = getelementptr inbounds i8, i8* %15, i64 %10
store i8 %16, i8* %17
%18 = load i32, i32* @stdin
%19 = tail call i32 @fgetc(i32 %18)
%20 = icmp ne i32 %19, 10
%21 = load i32, i32* @EOF
%22 = icmp ne i32 %19, %21
%23 = select i1 %20, i1 %22, i1 false
br i1 %23, label %9, label %24
24: ; preds = %9
%25 = and i64 %13, 4294967295
br label %26
26: ; preds = %24, %1
%27 = phi i8* [ %2, %1 ], [ %15, %24 ]
%28 = phi i64 [ 0, %1 ], [ %25, %24 ]
%29 = getelementptr inbounds i8, i8* %27, i64 %28
store i8 0, i8* %29
%30 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %27)
ret void
}