#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*/ line;

/* Variables and functions */
char *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fputs(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
char *strchr(char *, char);
char *tdb_getline(const char *prompt) {
  static char line[1024];
  char *p;
  fputs(prompt, stdout);
  line[0] = 0;
  p = fgets(line, sizeof(line) - 1, stdin);
  if (p)
    p = strchr(p, '\n');
  if (p)
    *p = 0;
  return p ? line : NULL;
}


x86 assembly (clang, -O0):
<tdb_getline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
call   1040 <fputs@plt>
movb   $0x0,0x2efb(%rip)
mov    0x2e8c(%rip),%rax
mov    (%rax),%edx
lea    0x2eeb(%rip),%rdi
mov    $0x3ff,%esi
call   1050 <fgets@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1190 <tdb_getline+0x60>
mov    -0x10(%rbp),%rdi
mov    $0xa,%esi
call   1030 <strchr@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11a2 <tdb_getline+0x72>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdx
xor    %eax,%eax
lea    0x2ea1(%rip),%rcx
cmp    $0x0,%rdx
cmovne %rcx,%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tdb_getline>:
push   %rbx
mov    0x2ea0(%rip),%rax
mov    (%rax),%esi
call   1040 <fputs@plt>
movb   $0x0,0x2f0a(%rip)
lea    0x2f03(%rip),%rbx
mov    0x2e94(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0x3ff,%esi
call   1050 <fgets@plt>
test   %rax,%rax
je     117f <tdb_getline+0x4f>
mov    %rax,%rdi
mov    $0xa,%esi
call   1030 <strchr@plt>
test   %rax,%rax
je     117f <tdb_getline+0x4f>
movb   $0x0,(%rax)
jmp    1181 <tdb_getline+0x51>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@tdb_getline.line = internal global [1024 x i8] zeroinitializer
@stdout = global i32 0
@stdin = global i32 0
define i8* @tdb_getline(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
store i8* %0, i8** %2
%4 = load i8*, i8** %2
%5 = load i32, i32* @stdout
%6 = call i32 @fputs(i8* %4, i32 %5)
store i8 0, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @tdb_getline.line, i64 0, i64 0)
%7 = load i32, i32* @stdin
%8 = call i8* @fgets(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @tdb_getline.line, i64 0, i64 0), i32 1023, i32 %7)
store i8* %8, i8** %3
%9 = load i8*, i8** %3
%10 = icmp ne i8* %9, null
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i8*, i8** %3
%13 = call i8* @strchr(i8* %12, i8 signext 10)
store i8* %13, i8** %3
br label %14
14: ; preds = %11, %1
%15 = load i8*, i8** %3
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = load i8*, i8** %3
store i8 0, i8* %18
br label %19
19: ; preds = %17, %14
%20 = load i8*, i8** %3
%21 = icmp ne i8* %20, null
%22 = zext i1 %21 to i64
%23 = select i1 %21, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @tdb_getline.line, i64 0, i64 0), i8* null
ret i8* %23
}

LLVM IR (-O2):
@tdb_getline.line = internal global [1024 x i8] zeroinitializer
@stdout = global i32 0
@stdin = global i32 0
define i8* @tdb_getline(i8* %0) {
%2 = load i32, i32* @stdout
%3 = tail call i32 @fputs(i8* %0, i32 %2)
store i8 0, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @tdb_getline.line, i64 0, i64 0)
%4 = load i32, i32* @stdin
%5 = tail call i8* @fgets(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @tdb_getline.line, i64 0, i64 0), i32 1023, i32 %4)
%6 = icmp eq i8* %5, null
br i1 %6, label %11, label %7
7: ; preds = %1
%8 = tail call i8* @strchr(i8* %5, i8 signext 10)
%9 = icmp eq i8* %8, null
br i1 %9, label %11, label %10
10: ; preds = %7
store i8 0, i8* %8
br label %11
11: ; preds = %1, %10, %7
%12 = phi i8* [ getelementptr inbounds ([1024 x i8], [1024 x i8]* @tdb_getline.line, i64 0, i64 0), %10 ], [ null, %7 ], [ null, %1 ]
ret i8* %12
}