#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 /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, ...);
int /*<<< orphan*/ stdin;
char *strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char *);
int text_input(char *prompt, char *buf, int len, char *def) {
  char *p;
  if (def)
    printf("%s [%s]: ", prompt, def);
  else
    printf("%s: ", prompt);
  fgets(buf, len, stdin);
  if ((p = strchr(buf, '\n')) != NULL)
    *p = 0;
  if ((def != NULL) && (buf[0] == 0)) {
    strcpy(buf, def);
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<text_input>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     117d <text_input+0x3d>
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xe8f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    118f <text_input+0x4f>
mov    -0x10(%rbp),%rsi
lea    0xe82(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    0x2e4b(%rip),%rax
mov    (%rax),%edx
call   1060 <fgets@plt>
mov    -0x18(%rbp),%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     11c7 <text_input+0x87>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
cmpq   $0x0,-0x28(%rbp)
je     11fb <text_input+0xbb>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11fb <text_input+0xbb>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1030 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
jmp    1202 <text_input+0xc2>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<text_input>:
push   %rbp
push   %r14
push   %rbx
mov    %rcx,%r14
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %rdi,%rsi
test   %rcx,%rcx
je     1167 <text_input+0x27>
lea    0xea5(%rip),%rdi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1175 <text_input+0x35>
lea    0xe9c(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2e6c(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %ebp,%esi
call   1060 <fgets@plt>
mov    %rbx,%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     119d <text_input+0x5d>
movb   $0x0,(%rax)
mov    $0x1,%eax
test   %r14,%r14
je     11b9 <text_input+0x79>
cmpb   $0x0,(%rbx)
jne    11b9 <text_input+0x79>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"%s [%s]: \00"
@.str.1 = [5 x i8] c"%s: \00"
@stdin = global i32 0
define i32 @text_input(i8* %0, i8* %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i8* %3, i8** %9
%11 = load i8*, i8** %9
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %17
13: ; preds = %4
%14 = load i8*, i8** %6
%15 = load i8*, i8** %9
%16 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %14, i8* %15)
br label %20
17: ; preds = %4
%18 = load i8*, i8** %6
%19 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %18)
br label %20
20: ; preds = %17, %13
%21 = load i8*, i8** %7
%22 = load i32, i32* %8
%23 = load i32, i32* @stdin
%24 = call i32 @fgets(i8* %21, i32 %22, i32 %23)
%25 = load i8*, i8** %7
%26 = call i8* @strchr(i8* %25, i8 signext 10)
store i8* %26, i8** %10
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %30
28: ; preds = %20
%29 = load i8*, i8** %10
store i8 0, i8* %29
br label %30
30: ; preds = %28, %20
%31 = load i8*, i8** %9
%32 = icmp ne i8* %31, null
br i1 %32, label %33, label %43
33: ; preds = %30
%34 = load i8*, i8** %7
%35 = getelementptr inbounds i8, i8* %34, i64 0
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %43
39: ; preds = %33
%40 = load i8*, i8** %7
%41 = load i8*, i8** %9
%42 = call i32 @strcpy(i8* %40, i8* %41)
store i32 0, i32* %5
br label %44
43: ; preds = %33, %30
store i32 1, i32* %5
br label %44
44: ; preds = %43, %39
%45 = load i32, i32* %5
ret i32 %45
}

LLVM IR (-O2):
@.str = [10 x i8] c"%s [%s]: \00"
@.str.1 = [5 x i8] c"%s: \00"
@stdin = global i32 0
define i32 @text_input(i8* %0, i8* %1, i32 %2, i8* %3) {
%5 = icmp eq i8* %3, null
br i1 %5, label %8, label %6
6: ; preds = %4
%7 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %3)
br label %10
8: ; preds = %4
%9 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %10
10: ; preds = %8, %6
%11 = load i32, i32* @stdin
%12 = tail call i32 @fgets(i8* %1, i32 %2, i32 %11)
%13 = tail call i8* @strchr(i8* dereferenceable(1) %1, i8 signext 10)
%14 = icmp eq i8* %13, null
br i1 %14, label %16, label %15
15: ; preds = %10
store i8 0, i8* %13
br label %16
16: ; preds = %15, %10
br i1 %5, label %22, label %17
17: ; preds = %16
%18 = load i8, i8* %1
%19 = icmp eq i8 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = tail call i32 @strcpy(i8* %1, i8* %3)
br label %22
22: ; preds = %16, %17, %20
%23 = phi i32 [ 0, %20 ], [ 1, %17 ], [ 1, %16 ]
ret i32 %23
}