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

/* Variables and functions */
int /*<<< orphan*/ dup(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int /*<<< orphan*/, char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char const *);
scalar_t__ isalnum(char);
scalar_t__ isfd_read(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
size_t isfd_prompt(int sd, const char *prompt, char *buffer, size_t length) {
  size_t c;
  FILE *sh = fdopen(dup(sd), "w");
  if (!sh)
    return -1;
  fprintf(sh, "%s ", prompt);
  fflush(sh);
  memset(buffer, 0, length);
  for (c = 0; c < length;) {
    if ((buffer[c] = (char)isfd_read(sd)) == -1)
      return -1;
    if (buffer[c] == '\n') {
      buffer[c] = 0;
      break;
    }
    if (isalnum(buffer[c]) || buffer[c] == '.' || buffer[c] == '-')
      c++;
  }
  fclose(sh);
  return c;
}


x86 assembly (clang, -O0):
<isfd_prompt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <dup@plt>
mov    %eax,%edi
lea    0xe58(%rip),%rsi
call   10a0 <fdopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11c9 <isfd_prompt+0x49>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    12c4 <isfd_prompt+0x144>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe2a(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1090 <fflush@plt>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rdx
xor    %esi,%esi
call   1060 <memset@plt>
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    12b3 <isfd_prompt+0x133>
mov    -0xc(%rbp),%edi
call   1080 <isfd_read@plt>
mov    -0x20(%rbp),%rcx
mov    -0x30(%rbp),%rdx
mov    %al,(%rcx,%rdx,1)
movsbl %al,%eax
cmp    $0xffffffff,%eax
jne    1237 <isfd_prompt+0xb7>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    12c4 <isfd_prompt+0x144>
mov    -0x20(%rbp),%rax
mov    -0x30(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    125d <isfd_prompt+0xdd>
mov    -0x20(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    12b3 <isfd_prompt+0x133>
mov    -0x20(%rbp),%rax
mov    -0x30(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <isalnum@plt>
cmp    $0x0,%rax
jne    12a2 <isfd_prompt+0x122>
mov    -0x20(%rbp),%rax
mov    -0x30(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2e,%eax
je     12a2 <isfd_prompt+0x122>
mov    -0x20(%rbp),%rax
mov    -0x30(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2d,%eax
jne    12ae <isfd_prompt+0x12e>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    11fd <isfd_prompt+0x7d>
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<isfd_prompt>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %rsi,%r13
mov    %edi,%r12d
call   1050 <dup@plt>
lea    0xe5d(%rip),%rsi
mov    %eax,%edi
call   10a0 <fdopen@plt>
mov    %rax,%r15
mov    $0xffffffffffffffff,%rax
test   %r15,%r15
je     123d <isfd_prompt+0xbd>
lea    0xe3e(%rip),%rsi
mov    %r15,%rdi
mov    %r13,%rdx
call   1070 <fprintf@plt>
mov    %r15,%rdi
call   1090 <fflush@plt>
xor    %ebp,%ebp
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r14,%rdx
call   1060 <memset@plt>
test   %r14,%r14
je     1232 <isfd_prompt+0xb2>
xor    %ebp,%ebp
jmp    11f9 <isfd_prompt+0x79>
nop
add    $0x1,%rbp
cmp    %r14,%rbp
jae    1232 <isfd_prompt+0xb2>
mov    %r12d,%edi
call   1080 <isfd_read@plt>
mov    %al,(%rbx,%rbp,1)
cmp    $0xff,%al
je     1225 <isfd_prompt+0xa5>
cmp    $0xa,%al
je     122e <isfd_prompt+0xae>
movsbl %al,%edi
call   1040 <isalnum@plt>
test   %rax,%rax
jne    11f0 <isfd_prompt+0x70>
movzbl (%rbx,%rbp,1),%eax
add    $0xd3,%al
cmp    $0x1,%al
jbe    11f0 <isfd_prompt+0x70>
jmp    11f4 <isfd_prompt+0x74>
mov    $0xffffffffffffffff,%rax
jmp    123d <isfd_prompt+0xbd>
movb   $0x0,(%rbx,%rbp,1)
mov    %r15,%rdi
call   1030 <fclose@plt>
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [4 x i8] c"%s \00"
define i64 @isfd_prompt(i32 %0, i8* %1, i8* %2, i64 %3) {
%5 = alloca i64
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32*
store i32 %0, i32* %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i64 %3, i64* %9
%12 = load i32, i32* %6
%13 = call i32 @dup(i32 %12)
%14 = call i32* @fdopen(i32 %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %11
%15 = load i32*, i32** %11
%16 = icmp ne i32* %15, null
br i1 %16, label %18, label %17
17: ; preds = %4
store i64 -1, i64* %5
br label %81
18: ; preds = %4
%19 = load i32*, i32** %11
%20 = load i8*, i8** %7
%21 = call i32 @fprintf(i32* %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%22 = load i32*, i32** %11
%23 = call i32 @fflush(i32* %22)
%24 = load i8*, i8** %8
%25 = load i64, i64* %9
%26 = call i32 @memset(i8* %24, i32 0, i64 %25)
store i64 0, i64* %10
br label %27
27: ; preds = %76, %18
%28 = load i64, i64* %10
%29 = load i64, i64* %9
%30 = icmp ult i64 %28, %29
br i1 %30, label %31, label %77
31: ; preds = %27
%32 = load i32, i32* %6
%33 = call i64 @isfd_read(i32 %32)
%34 = trunc i64 %33 to i8
%35 = load i8*, i8** %8
%36 = load i64, i64* %10
%37 = getelementptr inbounds i8, i8* %35, i64 %36
store i8 %34, i8* %37
%38 = sext i8 %34 to i32
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %41
40: ; preds = %31
store i64 -1, i64* %5
br label %81
41: ; preds = %31
%42 = load i8*, i8** %8
%43 = load i64, i64* %10
%44 = getelementptr inbounds i8, i8* %42, i64 %43
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = icmp eq i32 %46, 10
br i1 %47, label %48, label %52
48: ; preds = %41
%49 = load i8*, i8** %8
%50 = load i64, i64* %10
%51 = getelementptr inbounds i8, i8* %49, i64 %50
store i8 0, i8* %51
br label %77
52: ; preds = %41
%53 = load i8*, i8** %8
%54 = load i64, i64* %10
%55 = getelementptr inbounds i8, i8* %53, i64 %54
%56 = load i8, i8* %55
%57 = call i64 @isalnum(i8 signext %56)
%58 = icmp ne i64 %57, 0
br i1 %58, label %73, label %59
59: ; preds = %52
%60 = load i8*, i8** %8
%61 = load i64, i64* %10
%62 = getelementptr inbounds i8, i8* %60, i64 %61
%63 = load i8, i8* %62
%64 = sext i8 %63 to i32
%65 = icmp eq i32 %64, 46
br i1 %65, label %73, label %66
66: ; preds = %59
%67 = load i8*, i8** %8
%68 = load i64, i64* %10
%69 = getelementptr inbounds i8, i8* %67, i64 %68
%70 = load i8, i8* %69
%71 = sext i8 %70 to i32
%72 = icmp eq i32 %71, 45
br i1 %72, label %73, label %76
73: ; preds = %66, %59, %52
%74 = load i64, i64* %10
%75 = add i64 %74, 1
store i64 %75, i64* %10
br label %76
76: ; preds = %73, %66
br label %27
77: ; preds = %48, %27
%78 = load i32*, i32** %11
%79 = call i32 @fclose(i32* %78)
%80 = load i64, i64* %10
store i64 %80, i64* %5
br label %81
81: ; preds = %77, %40, %17
%82 = load i64, i64* %5
ret i64 %82
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [4 x i8] c"%s \00"
define i64 @isfd_prompt(i32 %0, i8* %1, i8* %2, i64 %3) {
%5 = tail call i32 @dup(i32 %0)
%6 = tail call i32* @fdopen(i32 %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %34, label %8
8: ; preds = %4
%9 = tail call i32 @fprintf(i32* %6, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%10 = tail call i32 @fflush(i32* %6)
%11 = tail call i32 @memset(i8* %2, i32 0, i64 %3)
%12 = icmp eq i64 %3, 0
br i1 %12, label %31, label %13
13: ; preds = %8, %28
%14 = phi i64 [ %29, %28 ], [ 0, %8 ]
%15 = tail call i64 @isfd_read(i32 %0)
%16 = trunc i64 %15 to i8
%17 = getelementptr inbounds i8, i8* %2, i64 %14
store i8 %16, i8* %17
switch i8 %16, label %19 [
i8 -1, label %34
i8 10, label %18
]
18: ; preds = %13
store i8 0, i8* %17
br label %31
19: ; preds = %13
%20 = tail call i64 @isalnum(i8 signext %16)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %19
%23 = load i8, i8* %17
%24 = add i8 %23, -45
%25 = icmp ult i8 %24, 2
br i1 %25, label %26, label %28
26: ; preds = %22, %19
%27 = add i64 %14, 1
br label %28
28: ; preds = %22, %26
%29 = phi i64 [ %27, %26 ], [ %14, %22 ]
%30 = icmp ult i64 %29, %3
br i1 %30, label %13, label %31
31: ; preds = %28, %8, %18
%32 = phi i64 [ %14, %18 ], [ 0, %8 ], [ %29, %28 ]
%33 = tail call i32 @fclose(i32* %6)
br label %34
34: ; preds = %13, %4, %31
%35 = phi i64 [ %32, %31 ], [ -1, %4 ], [ -1, %13 ]
ret i64 %35
}