#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 */
char getchar();
int /*<<< orphan*/ printf(char *);
int getline2(char line[], int nmax) {
  int len;
  char c;
  len = 0;
  printf("Enter a string [CR to exit]: ");
  while (((c = getchar()) != '\n') && len < nmax - 1)
    line[len++] = c;
  line[len] = '\0';
  return len;
}


x86 assembly (clang, -O0):
<getline2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
lea    0xec3(%rip),%rdi
call   1030 <printf@plt>
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %al,-0x11(%rbp)
movsbl %al,%ecx
xor    %eax,%eax
cmp    $0xa,%ecx
mov    %al,-0x12(%rbp)
je     116e <getline2+0x4e>
mov    -0x10(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
setl   %al
mov    %al,-0x12(%rbp)
mov    -0x12(%rbp),%al
test   $0x1,%al
jne    117e <getline2+0x5e>
jmp    119b <getline2+0x7b>
mov    -0x11(%rbp),%dl
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x10(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    1142 <getline2+0x22>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getline2>:
push   %r15
push   %r14
push   %rbx
mov    %esi,%r15d
mov    %rdi,%r14
lea    0xece(%rip),%rdi
call   1030 <printf@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <getchar@plt>
cmp    $0xa,%al
je     1168 <getline2+0x48>
cmp    $0x2,%r15d
jl     1168 <getline2+0x48>
add    $0xffffffff,%r15d
xor    %ebx,%ebx
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
xor    %eax,%eax
call   1040 <getchar@plt>
cmp    $0xa,%al
je     1168 <getline2+0x48>
cmp    %r15,%rbx
jb     1150 <getline2+0x30>
mov    %ebx,%eax
movb   $0x0,(%r14,%rax,1)
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [30 x i8] c"Enter a string [CR to exit]: \00"
define i32 @getline2(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8
store i8* %0, i8** %3
store i32 %1, i32* %4
store i32 0, i32* %5
%7 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %19, %2
%9 = call signext i8 (...) @getchar()
store i8 %9, i8* %6
%10 = sext i8 %9 to i32
%11 = icmp ne i32 %10, 10
br i1 %11, label %12, label %17
12: ; preds = %8
%13 = load i32, i32* %5
%14 = load i32, i32* %4
%15 = sub nsw i32 %14, 1
%16 = icmp slt i32 %13, %15
br label %17
17: ; preds = %12, %8
%18 = phi i1 [ false, %8 ], [ %16, %12 ]
br i1 %18, label %19, label %26
19: ; preds = %17
%20 = load i8, i8* %6
%21 = load i8*, i8** %3
%22 = load i32, i32* %5
%23 = add nsw i32 %22, 1
store i32 %23, i32* %5
%24 = sext i32 %22 to i64
%25 = getelementptr inbounds i8, i8* %21, i64 %24
store i8 %20, i8* %25
br label %8
26: ; preds = %17
%27 = load i8*, i8** %3
%28 = load i32, i32* %5
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
store i8 0, i8* %30
%31 = load i32, i32* %5
ret i32 %31
}

LLVM IR (-O2):
@.str = [30 x i8] c"Enter a string [CR to exit]: \00"
define i32 @getline2(i8* nocapture writeonly %0, i32 %1) {
%3 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%4 = tail call signext i8 (...) @getchar()
%5 = icmp ne i8 %4, 10
%6 = icmp sgt i32 %1, 1
%7 = and i1 %5, %6
br i1 %7, label %8, label %22
8: ; preds = %2
%9 = add nsw i32 %1, -1
%10 = zext i32 %9 to i64
br label %11
11: ; preds = %8, %11
%12 = phi i64 [ 0, %8 ], [ %14, %11 ]
%13 = phi i8 [ %4, %8 ], [ %16, %11 ]
%14 = add nuw nsw i64 %12, 1
%15 = getelementptr inbounds i8, i8* %0, i64 %12
store i8 %13, i8* %15
%16 = tail call signext i8 (...) @getchar()
%17 = icmp ne i8 %16, 10
%18 = icmp ult i64 %14, %10
%19 = select i1 %17, i1 %18, i1 false
br i1 %19, label %11, label %20
20: ; preds = %11
%21 = trunc i64 %14 to i32
br label %22
22: ; preds = %20, %2
%23 = phi i32 [ 0, %2 ], [ %21, %20 ]
%24 = zext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %0, i64 %24
store i8 0, i8* %25
ret i32 %23
}