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

/* Variables and functions */
int /*<<< orphan*/ assert(int);
scalar_t__ fgets(char *, int /*<<< orphan*/, int /*<<< orphan*/);
char *getenv(char const *);
int /*<<< orphan*/ printf(char *, char const *, char const *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strncpy(char *, char const *, int /*<<< orphan*/);
char *askforstring(const char *question, const char *envname, const char *deflt,
                   char *buffer, uint bufsize) {
  char *env;
  char *bufptr;
  assert(bufsize > 0);
  env = getenv(envname);
  if (env) {
    strncpy(buffer, env, bufsize);
    buffer[bufsize - 1] = '\0';
    return buffer;
  }
  (void)printf("%s (%s)\n", question, deflt);
  if (fgets(buffer, bufsize, stdin) && buffer[0] != '\n' && buffer[0] != '\0') {
    for (bufptr = buffer; *bufptr != '\n' && *bufptr != '\0'; bufptr++)
      ;
    *bufptr = '\0';
    return buffer;
  }
  strncpy(buffer, deflt, bufsize);
  buffer[bufsize - 1] = '\0';
  return buffer;
}


x86 assembly (clang, -O0):
<askforstring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x18(%rbp),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     11c3 <askforstring+0x73>
mov    -0x28(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1040 <strncpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x2c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12a6 <askforstring+0x156>
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xe2e(%rip),%rdi
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    0x2e03(%rip),%rax
mov    (%rax),%edx
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     127d <askforstring+0x12d>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
je     127d <askforstring+0x12d>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     127d <askforstring+0x12d>
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x41(%rbp)
je     1243 <askforstring+0xf3>
mov    -0x40(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%al
test   $0x1,%al
jne    1253 <askforstring+0x103>
jmp    1269 <askforstring+0x119>
jmp    1258 <askforstring+0x108>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
jmp    121e <askforstring+0xce>
mov    -0x40(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12a6 <askforstring+0x156>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1040 <strncpy@plt>
mov    -0x28(%rbp),%rax
mov    -0x2c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<askforstring>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %r8d,%ebp
mov    %rcx,%r12
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%r15
xor    %edi,%edi
test   %r8d,%r8d
setg   %dil
call   1070 <assert@plt>
mov    %rbx,%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     118a <askforstring+0x3a>
mov    %r12,%rdi
mov    %rax,%rsi
jmp    11c7 <askforstring+0x77>
lea    0xe6f(%rip),%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1050 <printf@plt>
mov    0x2e45(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rdi
mov    %ebp,%esi
call   1060 <fgets@plt>
test   %rax,%rax
je     11c1 <askforstring+0x71>
mov    (%r12),%cl
test   %cl,%cl
je     11c1 <askforstring+0x71>
cmp    $0xa,%cl
jne    11e7 <askforstring+0x97>
mov    %r12,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1040 <strncpy@plt>
movslq %ebp,%rax
add    %r12,%rax
add    $0xffffffffffffffff,%rax
movb   $0x0,(%rax)
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r12,%rax
test   %cl,%cl
je     11d8 <askforstring+0x88>
xchg   %ax,%ax
cmp    $0xa,%cl
je     11d8 <askforstring+0x88>
movzbl 0x1(%rax),%ecx
add    $0x1,%rax
test   %cl,%cl
jne    11f0 <askforstring+0xa0>
jmp    11d8 <askforstring+0x88>

LLVM IR (-O0):
@.str = [9 x i8] c"%s (%s)\0A\00"
@stdin = global i32 0
define i8* @askforstring(i8* %0, i8* %1, i8* %2, i8* %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i8*
store i8* %0, i8** %7
store i8* %1, i8** %8
store i8* %2, i8** %9
store i8* %3, i8** %10
store i32 %4, i32* %11
%14 = load i32, i32* %11
%15 = icmp sgt i32 %14, 0
%16 = zext i1 %15 to i32
%17 = call i32 @assert(i32 %16)
%18 = load i8*, i8** %8
%19 = call i8* @getenv(i8* %18)
store i8* %19, i8** %12
%20 = load i8*, i8** %12
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %33
22: ; preds = %5
%23 = load i8*, i8** %10
%24 = load i8*, i8** %12
%25 = load i32, i32* %11
%26 = call i32 @strncpy(i8* %23, i8* %24, i32 %25)
%27 = load i8*, i8** %10
%28 = load i32, i32* %11
%29 = sub nsw i32 %28, 1
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %27, i64 %30
store i8 0, i8* %31
%32 = load i8*, i8** %10
store i8* %32, i8** %6
br label %86
33: ; preds = %5
%34 = load i8*, i8** %7
%35 = load i8*, i8** %9
%36 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %34, i8* %35)
%37 = load i8*, i8** %10
%38 = load i32, i32* %11
%39 = load i32, i32* @stdin
%40 = call i64 @fgets(i8* %37, i32 %38, i32 %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %75
42: ; preds = %33
%43 = load i8*, i8** %10
%44 = getelementptr inbounds i8, i8* %43, i64 0
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = icmp ne i32 %46, 10
br i1 %47, label %48, label %75
48: ; preds = %42
%49 = load i8*, i8** %10
%50 = getelementptr inbounds i8, i8* %49, i64 0
%51 = load i8, i8* %50
%52 = sext i8 %51 to i32
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %75
54: ; preds = %48
%55 = load i8*, i8** %10
store i8* %55, i8** %13
br label %56
56: ; preds = %69, %54
%57 = load i8*, i8** %13
%58 = load i8, i8* %57
%59 = sext i8 %58 to i32
%60 = icmp ne i32 %59, 10
br i1 %60, label %61, label %66
61: ; preds = %56
%62 = load i8*, i8** %13
%63 = load i8, i8* %62
%64 = sext i8 %63 to i32
%65 = icmp ne i32 %64, 0
br label %66
66: ; preds = %61, %56
%67 = phi i1 [ false, %56 ], [ %65, %61 ]
br i1 %67, label %68, label %72
68: ; preds = %66
br label %69
69: ; preds = %68
%70 = load i8*, i8** %13
%71 = getelementptr inbounds i8, i8* %70, i32 1
store i8* %71, i8** %13
br label %56
72: ; preds = %66
%73 = load i8*, i8** %13
store i8 0, i8* %73
%74 = load i8*, i8** %10
store i8* %74, i8** %6
br label %86
75: ; preds = %48, %42, %33
%76 = load i8*, i8** %10
%77 = load i8*, i8** %9
%78 = load i32, i32* %11
%79 = call i32 @strncpy(i8* %76, i8* %77, i32 %78)
%80 = load i8*, i8** %10
%81 = load i32, i32* %11
%82 = sub nsw i32 %81, 1
%83 = sext i32 %82 to i64
%84 = getelementptr inbounds i8, i8* %80, i64 %83
store i8 0, i8* %84
%85 = load i8*, i8** %10
store i8* %85, i8** %6
br label %86
86: ; preds = %75, %72, %22
%87 = load i8*, i8** %6
ret i8* %87
}

LLVM IR (-O2):
@.str = [9 x i8] c"%s (%s)\0A\00"
@stdin = global i32 0
define i8* @askforstring(i8* %0, i8* nocapture %1, i8* %2, i8* returned %3, i32 %4) {
%6 = icmp sgt i32 %4, 0
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = tail call i8* @getenv(i8* %1)
%10 = icmp eq i8* %9, null
br i1 %10, label %16, label %11
11: ; preds = %5
%12 = tail call i32 @strncpy(i8* %3, i8* %9, i32 %4)
%13 = add nsw i32 %4, -1
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %3, i64 %14
br label %34
16: ; preds = %5
%17 = tail call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %2)
%18 = load i32, i32* @stdin
%19 = tail call i64 @fgets(i8* %3, i32 %4, i32 %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %29, label %21
21: ; preds = %16
%22 = load i8, i8* %3
switch i8 %22, label %23 [
i8 10, label %29
i8 0, label %29
]
23: ; preds = %21, %26
%24 = phi i8 [ %28, %26 ], [ %22, %21 ]
%25 = phi i8* [ %27, %26 ], [ %3, %21 ]
switch i8 %24, label %26 [
i8 10, label %34
i8 0, label %34
]
26: ; preds = %23
%27 = getelementptr inbounds i8, i8* %25, i64 1
%28 = load i8, i8* %27
br label %23
29: ; preds = %21, %21, %16
%30 = tail call i32 @strncpy(i8* %3, i8* %2, i32 %4)
%31 = add nsw i32 %4, -1
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %3, i64 %32
br label %34
34: ; preds = %23, %23, %29, %11
%35 = phi i8* [ %33, %29 ], [ %15, %11 ], [ %25, %23 ], [ %25, %23 ]
store i8 0, i8* %35
ret i8* %3
}