#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*/ consputs(char *, int);
int getline(char *, int, int);
int /*<<< orphan*/ print(char *);
int /*<<< orphan*/ sprint(char *, char *, char *, ...);
int /*<<< orphan*/ strcpy(char *, char *);
int getstr(char *prompt, char *buf, int size, char *def, int timeout) {
  int len, isdefault;
  char pbuf[256];
  buf[0] = 0;
  isdefault = (def && *def);
  if (isdefault == 0) {
    timeout = 0;
    sprint(pbuf, "%s: ", prompt);
  } else if (timeout)
    sprint(pbuf, "%s[default==%s (%ds timeout)]: ", prompt, def, timeout);
  else
    sprint(pbuf, "%s[default==%s]: ", prompt, def);
  for (;;) {
    print(pbuf);
    len = getline(buf, size, timeout);
    switch (len) {
    case 0:
      if (isdefault)
        break;
      continue;
    case -1:
      continue;
    case -2:
      consputs("\n", 1);
      len = 0;
      break;
    default:
      break;
    }
    if (len >= size) {
      print("line too long\n");
      continue;
    }
    break;
  }
  if (len == 0 && isdefault)
    strcpy(buf, def);
  return 0;
}


x86 assembly (clang, -O0):
<getstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
xor    %eax,%eax
cmpq   $0x0,-0x20(%rbp)
mov    %al,-0x131(%rbp)
je     119b <getstr+0x4b>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x131(%rbp)
mov    -0x131(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    11d8 <getstr+0x88>
movl   $0x0,-0x24(%rbp)
lea    -0x130(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe34(%rip),%rsi
mov    $0x0,%al
call   1070 <sprint@plt>
jmp    122a <getstr+0xda>
cmpl   $0x0,-0x24(%rbp)
je     1208 <getstr+0xb8>
lea    -0x130(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x24(%rbp),%r8d
lea    0xe09(%rip),%rsi
mov    $0x0,%al
call   1070 <sprint@plt>
jmp    1225 <getstr+0xd5>
lea    -0x130(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    0xe07(%rip),%rsi
mov    $0x0,%al
call   1070 <sprint@plt>
jmp    122a <getstr+0xda>
jmp    122f <getstr+0xdf>
lea    -0x130(%rbp),%rdi
call   1040 <print@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x24(%rbp),%edx
call   1060 <getline@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x138(%rbp)
sub    $0xfffffffe,%eax
je     12a4 <getstr+0x154>
jmp    1264 <getstr+0x114>
mov    -0x138(%rbp),%eax
sub    $0xffffffff,%eax
je     129f <getstr+0x14f>
jmp    1278 <getstr+0x128>
mov    -0x138(%rbp),%eax
test   %eax,%eax
jne    12c1 <getstr+0x171>
jmp    128b <getstr+0x13b>
cmpl   $0x0,-0x2c(%rbp)
je     129a <getstr+0x14a>
jmp    12c6 <getstr+0x176>
jmp    122f <getstr+0xdf>
jmp    122f <getstr+0xdf>
lea    0xd99(%rip),%rdi
mov    $0x1,%esi
call   1050 <consputs@plt>
movl   $0x0,-0x28(%rbp)
jmp    12c6 <getstr+0x176>
jmp    12c6 <getstr+0x176>
mov    -0x28(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     12e3 <getstr+0x193>
lea    0xd5e(%rip),%rdi
call   1040 <print@plt>
jmp    122f <getstr+0xdf>
jmp    12e8 <getstr+0x198>
cmpl   $0x0,-0x28(%rbp)
jne    1309 <getstr+0x1b9>
cmpl   $0x0,-0x2c(%rbp)
je     1309 <getstr+0x1b9>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1030 <strcpy@plt>
xor    %eax,%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getstr>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %edx,%ebp
mov    %rsi,%r15
mov    %rdi,%rdx
movb   $0x0,(%rsi)
test   %rcx,%rcx
mov    %rcx,0x8(%rsp)
je     119b <getstr+0x4b>
cmpb   $0x0,(%rcx)
je     119b <getstr+0x4b>
mov    %r8d,%ebx
test   %r8d,%r8d
je     11b5 <getstr+0x65>
lea    0xe7b(%rip),%rsi
lea    0x10(%rsp),%rdi
mov    %ebx,%r8d
xor    %eax,%eax
call   1070 <sprint@plt>
jmp    11ca <getstr+0x7a>
lea    0xe5e(%rip),%rsi
xor    %r13d,%r13d
lea    0x10(%rsp),%rdi
xor    %eax,%eax
call   1070 <sprint@plt>
xor    %ebx,%ebx
jmp    11cd <getstr+0x7d>
lea    0xe69(%rip),%rsi
xor    %ebx,%ebx
lea    0x10(%rsp),%rdi
xor    %eax,%eax
call   1070 <sprint@plt>
mov    $0x1,%r13b
lea    0x10(%rsp),%r12
lea    0xe5e(%rip),%r14
nopl   0x0(%rax)
mov    %r12,%rdi
call   1040 <print@plt>
mov    %r15,%rdi
mov    %ebp,%esi
mov    %ebx,%edx
call   1060 <getline@plt>
cmp    $0xfffffffe,%eax
je     1210 <getstr+0xc0>
cmp    $0xffffffff,%eax
je     11e0 <getstr+0x90>
test   %eax,%eax
jne    1223 <getstr+0xd3>
xor    %eax,%eax
test   %r13b,%r13b
je     11e0 <getstr+0x90>
jmp    1223 <getstr+0xd3>
nopl   0x0(%rax,%rax,1)
lea    0xe2d(%rip),%rdi
mov    $0x1,%esi
call   1050 <consputs@plt>
xor    %eax,%eax
cmp    %ebp,%eax
jl     1231 <getstr+0xe1>
mov    %r14,%rdi
call   1040 <print@plt>
jmp    11e0 <getstr+0x90>
test   %eax,%eax
setne  %al
xor    $0x1,%r13b
or     %al,%r13b
jne    124c <getstr+0xfc>
mov    %r15,%rdi
mov    0x8(%rsp),%rsi
call   1030 <strcpy@plt>
xor    %eax,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [5 x i8] c"%s: \00"
@.str.1 = [32 x i8] c"%s[default==%s (%ds timeout)]: \00"
@.str.2 = [18 x i8] c"%s[default==%s]: \00"
@.str.3 = [2 x i8] c"\0A\00"
@.str.4 = [15 x i8] c"line too long\0A\00"
define i32 @getstr(i8* %0, i8* %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca [256 x i8]
%7 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
store i8 0, i8* %1
%8 = icmp eq i8* %3, null
br i1 %8, label %12, label %9
9: ; preds = %5
%10 = load i8, i8* %3
%11 = icmp eq i8 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %5, %9
%13 = call i32 (i8*, i8*, i8*, ...) @sprint(i8* %7, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %20
14: ; preds = %9
%15 = icmp eq i32 %4, 0
br i1 %15, label %18, label %16
16: ; preds = %14
%17 = call i32 (i8*, i8*, i8*, ...) @sprint(i8* %7, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %3, i32 %4)
br label %20
18: ; preds = %14
%19 = call i32 (i8*, i8*, i8*, ...) @sprint(i8* %7, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %3)
br label %20
20: ; preds = %16, %18, %12
%21 = phi i1 [ false, %12 ], [ true, %16 ], [ true, %18 ]
%22 = phi i32 [ 0, %12 ], [ %4, %16 ], [ 0, %18 ]
br label %23
23: ; preds = %34, %20
%24 = call i32 @print(i8* %7)
%25 = call i32 @getline(i8* %1, i32 %2, i32 %22)
switch i32 %25, label %29 [
i32 0, label %26
i32 -1, label %34
i32 -2, label %27
]
26: ; preds = %23
br i1 %21, label %29, label %34
27: ; preds = %23
%28 = call i32 @consputs(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
br label %29
29: ; preds = %23, %26, %27
%30 = phi i32 [ %25, %23 ], [ 0, %27 ], [ 0, %26 ]
%31 = icmp slt i32 %30, %2
br i1 %31, label %35, label %32
32: ; preds = %29
%33 = call i32 @print(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
br label %34
34: ; preds = %32, %26, %23
br label %23
35: ; preds = %29
%36 = icmp eq i32 %30, 0
%37 = and i1 %21, %36
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = call i32 @strcpy(i8* %1, i8* %3)
br label %40
40: ; preds = %38, %35
ret i32 0
}