#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*/ O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ setecho(int, int);
int /*<<< orphan*/ string_chomp(char *);
int do_getline(const char *prompt, char *buffer, int length, int echo) {
  int fd;
  char *result;
  FILE *stream;
  fd = open("/dev/tty", O_RDWR);
  if (fd < 0)
    return 0;
  if (!echo) {
    if (!setecho(fd, 0)) {
      close(fd);
      return 0;
    }
  }
  stream = fdopen(fd, "r+");
  if (!stream) {
    if (!echo) {
      setecho(fd, 1);
    }
    close(fd);
    return 0;
  }
  fprintf(stream, "%s", prompt);
  fflush(stream);
  result = fgets(buffer, length, stream);
  string_chomp(buffer);
  if (!echo) {
    fprintf(stream, "\n");
    fflush(stream);
    setecho(fd, 1);
  }
  fclose(stream);
  return result != 0;
}


x86 assembly (clang, -O0):
<do_getline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
lea    0xe4a(%rip),%rdi
call   10b0 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11d4 <do_getline+0x44>
movl   $0x0,-0x4(%rbp)
jmp    12da <do_getline+0x14a>
cmpl   $0x0,-0x20(%rbp)
jne    120a <do_getline+0x7a>
mov    -0x24(%rbp),%edi
xor    %esi,%esi
call   1040 <setecho@plt>
cmp    $0x0,%eax
jne    1205 <do_getline+0x75>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    12da <do_getline+0x14a>
jmp    120a <do_getline+0x7a>
mov    -0x24(%rbp),%edi
lea    0xdf5(%rip),%rsi
call   10a0 <fdopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1253 <do_getline+0xc3>
cmpl   $0x0,-0x20(%rbp)
jne    123f <do_getline+0xaf>
mov    -0x24(%rbp),%edi
mov    $0x1,%esi
call   1040 <setecho@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    12da <do_getline+0x14a>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xdaa(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1090 <fflush@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x38(%rbp),%rdx
call   1070 <fgets@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <string_chomp@plt>
cmpl   $0x0,-0x20(%rbp)
jne    12c1 <do_getline+0x131>
mov    -0x38(%rbp),%rdi
lea    0xd6b(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1090 <fflush@plt>
mov    -0x24(%rbp),%edi
mov    $0x1,%esi
call   1040 <setecho@plt>
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
cmpq   $0x0,-0x30(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_getline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r15d
mov    %edx,%r12d
mov    %rsi,%r14
mov    %rdi,%r13
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
lea    0xe39(%rip),%rdi
call   10c0 <open@plt>
xor    %ebp,%ebp
test   %eax,%eax
js     12ad <do_getline+0x10d>
mov    %eax,%ebx
test   %r15d,%r15d
je     11fd <do_getline+0x5d>
mov    %r12d,0x4(%rsp)
lea    0xe20(%rip),%rsi
mov    %ebx,%edi
call   10b0 <fdopen@plt>
mov    %rax,%r12
test   %rax,%rax
jne    1229 <do_getline+0x89>
jmp    12a6 <do_getline+0x106>
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <setecho@plt>
test   %eax,%eax
je     128f <do_getline+0xef>
mov    %r12d,0x4(%rsp)
lea    0xdef(%rip),%rsi
mov    %ebx,%edi
call   10b0 <fdopen@plt>
mov    %rax,%r12
test   %rax,%rax
je     129a <do_getline+0xfa>
mov    %r13,%rdi
mov    %r12,%rsi
call   1050 <fputs@plt>
mov    %r12,%rdi
call   10a0 <fflush@plt>
mov    %r14,%rdi
mov    0x4(%rsp),%esi
mov    %r12,%rdx
call   1090 <fgets@plt>
mov    %rax,%r13
mov    %r14,%rdi
call   1060 <string_chomp@plt>
test   %r15d,%r15d
jne    127c <do_getline+0xdc>
mov    $0xa,%edi
mov    %r12,%rsi
call   1080 <fputc@plt>
mov    %r12,%rdi
call   10a0 <fflush@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1040 <setecho@plt>
mov    %r12,%rdi
call   1030 <fclose@plt>
xor    %ebp,%ebp
test   %r13,%r13
setne  %bpl
jmp    12ad <do_getline+0x10d>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %ebp,%ebp
jmp    12ad <do_getline+0x10d>
mov    %ebx,%edi
mov    $0x1,%esi
call   1040 <setecho@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/tty\00"
@O_RDWR = global i32 0
@.str.1 = [3 x i8] c"r+\00"
@.str.2 = [3 x i8] c"%s\00"
@.str.3 = [2 x i8] c"\0A\00"
define i32 @do_getline(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%13 = load i32, i32* @O_RDWR
%14 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %13)
store i32 %14, i32* %10
%15 = load i32, i32* %10
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %4
store i32 0, i32* %5
br label %70
18: ; preds = %4
%19 = load i32, i32* %9
%20 = icmp ne i32 %19, 0
br i1 %20, label %29, label %21
21: ; preds = %18
%22 = load i32, i32* %10
%23 = call i32 @setecho(i32 %22, i32 0)
%24 = icmp ne i32 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %21
%26 = load i32, i32* %10
%27 = call i32 @close(i32 %26)
store i32 0, i32* %5
br label %70
28: ; preds = %21
br label %29
29: ; preds = %28, %18
%30 = load i32, i32* %10
%31 = call i32* @fdopen(i32 %30, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %31, i32** %12
%32 = load i32*, i32** %12
%33 = icmp ne i32* %32, null
br i1 %33, label %43, label %34
34: ; preds = %29
%35 = load i32, i32* %9
%36 = icmp ne i32 %35, 0
br i1 %36, label %40, label %37
37: ; preds = %34
%38 = load i32, i32* %10
%39 = call i32 @setecho(i32 %38, i32 1)
br label %40
40: ; preds = %37, %34
%41 = load i32, i32* %10
%42 = call i32 @close(i32 %41)
store i32 0, i32* %5
br label %70
43: ; preds = %29
%44 = load i32*, i32** %12
%45 = load i8*, i8** %6
%46 = call i32 (i32*, i8*, ...) @fprintf(i32* %44, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %45)
%47 = load i32*, i32** %12
%48 = call i32 @fflush(i32* %47)
%49 = load i8*, i8** %7
%50 = load i32, i32* %8
%51 = load i32*, i32** %12
%52 = call i8* @fgets(i8* %49, i32 %50, i32* %51)
store i8* %52, i8** %11
%53 = load i8*, i8** %7
%54 = call i32 @string_chomp(i8* %53)
%55 = load i32, i32* %9
%56 = icmp ne i32 %55, 0
br i1 %56, label %64, label %57
57: ; preds = %43
%58 = load i32*, i32** %12
%59 = call i32 (i32*, i8*, ...) @fprintf(i32* %58, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%60 = load i32*, i32** %12
%61 = call i32 @fflush(i32* %60)
%62 = load i32, i32* %10
%63 = call i32 @setecho(i32 %62, i32 1)
br label %64
64: ; preds = %57, %43
%65 = load i32*, i32** %12
%66 = call i32 @fclose(i32* %65)
%67 = load i8*, i8** %11
%68 = icmp ne i8* %67, null
%69 = zext i1 %68 to i32
store i32 %69, i32* %5
br label %70
70: ; preds = %64, %40, %25, %17
%71 = load i32, i32* %5
ret i32 %71
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/tty\00"
@O_RDWR = global i32 0
@.str.1 = [3 x i8] c"r+\00"
define i32 @do_getline(i8* nocapture %0, i8* %1, i32 %2, i32 %3) {
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %39, label %8
8: ; preds = %4
%9 = icmp eq i32 %3, 0
br i1 %9, label %10, label %18
10: ; preds = %8
%11 = tail call i32 @setecho(i32 %6, i32 0)
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @close(i32 %6)
br label %39
15: ; preds = %10
%16 = tail call i32* @fdopen(i32 %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %21, label %25
18: ; preds = %8
%19 = tail call i32* @fdopen(i32 %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%20 = icmp eq i32* %19, null
br i1 %20, label %23, label %25
21: ; preds = %15
%22 = tail call i32 @setecho(i32 %6, i32 1)
br label %23
23: ; preds = %18, %21
%24 = tail call i32 @close(i32 %6)
br label %39
25: ; preds = %18, %15
%26 = phi i32* [ %19, %18 ], [ %16, %15 ]
%27 = tail call i32 @fputs(i8* %0, i32* %26)
%28 = tail call i32 @fflush(i32* %26)
%29 = tail call i8* @fgets(i8* %1, i32 %2, i32* %26)
%30 = tail call i32 @string_chomp(i8* %1)
br i1 %9, label %31, label %35
31: ; preds = %25
%32 = tail call i32 @fputc(i32 10, i32* %26)
%33 = tail call i32 @fflush(i32* %26)
%34 = tail call i32 @setecho(i32 %6, i32 1)
br label %35
35: ; preds = %31, %25
%36 = tail call i32 @fclose(i32* %26)
%37 = icmp ne i8* %29, null
%38 = zext i1 %37 to i32
br label %39
39: ; preds = %4, %35, %23, %13
%40 = phi i32 [ %38, %35 ], [ 0, %23 ], [ 0, %13 ], [ 0, %4 ]
ret i32 %40
}