#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 EOF;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ *fgets(char *, size_t, int /*<<< orphan*/);
int getchar();
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int strlen(char *);
int getLine(char *prompt, char *buffer, size_t size) {
  if (prompt != NULL) {
    if (strlen(prompt) != 0)
      printf("%s\n", prompt);
    fflush(stdout);
  }
  if (fgets(buffer, size, stdin) == NULL)
    return 1;
  if (buffer[strlen(buffer) - 1] != '\n') {
    int character, extra = 0;
    while (((character = getchar()) != '\n') && (character != EOF))
      extra = 1;
    return (extra == 1) ? 2 : 0;
  }
  buffer[strlen(buffer) - 1] = '\0';
  return 0;
}


x86 assembly (clang, -O0):
<getLine>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     119f <getLine+0x4f>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x0,%eax
je     1191 <getLine+0x41>
mov    -0x10(%rbp),%rsi
lea    0xe74(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    0x2e3a(%rip),%rax
mov    (%rax),%edx
call   1050 <fgets@plt>
cmp    $0x0,%rax
jne    11cb <getLine+0x7b>
movl   $0x1,-0x4(%rbp)
jmp    1284 <getLine+0x134>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
je     125c <getLine+0x10c>
movl   $0x0,-0x28(%rbp)
mov    $0x0,%al
call   1060 <getchar@plt>
mov    %eax,%ecx
mov    %ecx,-0x24(%rbp)
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x31(%rbp)
je     1228 <getLine+0xd8>
mov    -0x24(%rbp),%eax
mov    0x2dc0(%rip),%rcx
cmp    (%rcx),%eax
setne  %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    1238 <getLine+0xe8>
jmp    1244 <getLine+0xf4>
movl   $0x1,-0x28(%rbp)
jmp    11fc <getLine+0xac>
mov    -0x28(%rbp),%edx
xor    %eax,%eax
mov    $0x2,%ecx
cmp    $0x1,%edx
cmove  %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    1284 <getLine+0x134>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getLine>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
test   %rdi,%rdi
je     1189 <getLine+0x39>
mov    %rdi,%rbx
call   1030 <strlen@plt>
test   %eax,%eax
je     117b <getLine+0x2b>
lea    0xe8d(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    0x2e4e(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    0x2e58(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
mov    %r14,%rsi
call   1050 <fgets@plt>
test   %rax,%rax
je     11c8 <getLine+0x78>
mov    %r15,%rdi
call   1030 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%r15,1)
jne    11cf <getLine+0x7f>
mov    %r15,%rdi
call   1030 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%r15,1)
xor    %ebx,%ebx
jmp    1201 <getLine+0xb1>
mov    $0x1,%ebx
jmp    1201 <getLine+0xb1>
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <getchar@plt>
cmp    $0xa,%eax
je     1201 <getLine+0xb1>
mov    0x2dfc(%rip),%r14
cmp    (%r14),%eax
je     1201 <getLine+0xb1>
mov    $0x2,%ebx
xchg   %ax,%ax
xor    %eax,%eax
call   1060 <getchar@plt>
cmp    $0xa,%eax
je     1201 <getLine+0xb1>
cmp    (%r14),%eax
jne    11f0 <getLine+0xa0>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%s\0A\00"
@stdout = global i32 0
@stdin = global i32 0
@EOF = global i32 0
define i32 @getLine(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i8*, i8** %5
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %22
12: ; preds = %3
%13 = load i8*, i8** %5
%14 = call i32 @strlen(i8* %13)
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = load i8*, i8** %5
%18 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %17)
br label %19
19: ; preds = %16, %12
%20 = load i32, i32* @stdout
%21 = call i32 @fflush(i32 %20)
br label %22
22: ; preds = %19, %3
%23 = load i8*, i8** %6
%24 = load i64, i64* %7
%25 = load i32, i32* @stdin
%26 = call i32* @fgets(i8* %23, i64 %24, i32 %25)
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %29
28: ; preds = %22
store i32 1, i32* %4
br label %62
29: ; preds = %22
%30 = load i8*, i8** %6
%31 = load i8*, i8** %6
%32 = call i32 @strlen(i8* %31)
%33 = sub nsw i32 %32, 1
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %30, i64 %34
%36 = load i8, i8* %35
%37 = sext i8 %36 to i32
%38 = icmp ne i32 %37, 10
br i1 %38, label %39, label %55
39: ; preds = %29
store i32 0, i32* %9
br label %40
40: ; preds = %49, %39
%41 = call i32 (...) @getchar()
store i32 %41, i32* %8
%42 = icmp ne i32 %41, 10
br i1 %42, label %43, label %47
43: ; preds = %40
%44 = load i32, i32* %8
%45 = load i32, i32* @EOF
%46 = icmp ne i32 %44, %45
br label %47
47: ; preds = %43, %40
%48 = phi i1 [ false, %40 ], [ %46, %43 ]
br i1 %48, label %49, label %50
49: ; preds = %47
store i32 1, i32* %9
br label %40
50: ; preds = %47
%51 = load i32, i32* %9
%52 = icmp eq i32 %51, 1
%53 = zext i1 %52 to i64
%54 = select i1 %52, i32 2, i32 0
store i32 %54, i32* %4
br label %62
55: ; preds = %29
%56 = load i8*, i8** %6
%57 = load i8*, i8** %6
%58 = call i32 @strlen(i8* %57)
%59 = sub nsw i32 %58, 1
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %56, i64 %60
store i8 0, i8* %61
store i32 0, i32* %4
br label %62
62: ; preds = %55, %50, %28
%63 = load i32, i32* %4
ret i32 %63
}

LLVM IR (-O2):
@.str = [4 x i8] c"%s\0A\00"
@stdout = global i32 0
@stdin = global i32 0
@EOF = global i32 0
define i32 @getLine(i8* %0, i8* %1, i64 %2) {
%4 = icmp eq i8* %0, null
br i1 %4, label %13, label %5
5: ; preds = %3
%6 = tail call i32 @strlen(i8* %0)
%7 = icmp eq i32 %6, 0
br i1 %7, label %10, label %8
8: ; preds = %5
%9 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %10
10: ; preds = %8, %5
%11 = load i32, i32* @stdout
%12 = tail call i32 @fflush(i32 %11)
br label %13
13: ; preds = %10, %3
%14 = load i32, i32* @stdin
%15 = tail call i32* @fgets(i8* %1, i64 %2, i32 %14)
%16 = icmp eq i32* %15, null
br i1 %16, label %41, label %17
17: ; preds = %13
%18 = tail call i32 @strlen(i8* %1)
%19 = add nsw i32 %18, -1
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %1, i64 %20
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 10
br i1 %23, label %36, label %24
24: ; preds = %17
%25 = tail call i32 (...) @getchar()
%26 = icmp ne i32 %25, 10
%27 = load i32, i32* @EOF
%28 = icmp ne i32 %25, %27
%29 = select i1 %26, i1 %28, i1 false
br i1 %29, label %30, label %41
30: ; preds = %24, %30
%31 = tail call i32 (...) @getchar()
%32 = icmp ne i32 %31, 10
%33 = load i32, i32* @EOF
%34 = icmp ne i32 %31, %33
%35 = select i1 %32, i1 %34, i1 false
br i1 %35, label %30, label %41
36: ; preds = %17
%37 = tail call i32 @strlen(i8* %1)
%38 = add nsw i32 %37, -1
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %1, i64 %39
store i8 0, i8* %40
br label %41
41: ; preds = %30, %24, %13, %36
%42 = phi i32 [ 0, %36 ], [ 1, %13 ], [ 0, %24 ], [ 2, %30 ]
ret i32 %42
}