#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 */
struct cc_usb {
  int dummy;
};

/* Variables and functions */
int cc_usb_getchar(struct cc_usb *);
void cc_usb_getline(struct cc_usb *cc, char *line, int max) {
  int c;
  while ((c = cc_usb_getchar(cc)) != '\n') {
    switch (c) {
    case '\r':
      break;
    default:
      if (max > 1) {
        *line++ = c;
        max--;
      }
      break;
    }
  }
  *line++ = '\0';
}


x86 assembly (clang, -O0):
<cc_usb_getline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <cc_usb_getchar@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xa,%eax
je     1181 <cc_usb_getline+0x71>
mov    -0x18(%rbp),%eax
sub    $0xd,%eax
jne    114e <cc_usb_getline+0x3e>
jmp    1149 <cc_usb_getline+0x39>
jmp    117c <cc_usb_getline+0x6c>
cmpl   $0x1,-0x14(%rbp)
jle    1177 <cc_usb_getline+0x67>
mov    -0x18(%rbp),%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    117c <cc_usb_getline+0x6c>
jmp    1123 <cc_usb_getline+0x13>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movb   $0x0,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cc_usb_getline>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %rdi,%r14
jmp    1123 <cc_usb_getline+0x13>
xchg   %ax,%ax
mov    %r14,%rdi
call   1030 <cc_usb_getchar@plt>
cmp    $0xa,%eax
je     1142 <cc_usb_getline+0x32>
cmp    $0xd,%eax
je     1120 <cc_usb_getline+0x10>
cmp    $0x2,%ebp
jl     1120 <cc_usb_getline+0x10>
mov    %al,(%rbx)
add    $0x1,%rbx
add    $0xffffffff,%ebp
jmp    1120 <cc_usb_getline+0x10>
movb   $0x0,(%rbx)
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.cc_usb = type { i32 }
define void @cc_usb_getline(%struct.cc_usb* %0, i8* %1, i32 %2) {
%4 = alloca %struct.cc_usb*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store %struct.cc_usb* %0, %struct.cc_usb** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
br label %8
8: ; preds = %26, %3
%9 = load %struct.cc_usb*, %struct.cc_usb** %4
%10 = call i32 @cc_usb_getchar(%struct.cc_usb* %9)
store i32 %10, i32* %7
%11 = icmp ne i32 %10, 10
br i1 %11, label %12, label %27
12: ; preds = %8
%13 = load i32, i32* %7
switch i32 %13, label %15 [
i32 13, label %14
]
14: ; preds = %12
br label %26
15: ; preds = %12
%16 = load i32, i32* %6
%17 = icmp sgt i32 %16, 1
br i1 %17, label %18, label %25
18: ; preds = %15
%19 = load i32, i32* %7
%20 = trunc i32 %19 to i8
%21 = load i8*, i8** %5
%22 = getelementptr inbounds i8, i8* %21, i32 1
store i8* %22, i8** %5
store i8 %20, i8* %21
%23 = load i32, i32* %6
%24 = add nsw i32 %23, -1
store i32 %24, i32* %6
br label %25
25: ; preds = %18, %15
br label %26
26: ; preds = %25, %14
br label %8
27: ; preds = %8
%28 = load i8*, i8** %5
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %5
store i8 0, i8* %28
ret void
}

LLVM IR (-O2):
%struct.cc_usb = type { i32 }
define void @cc_usb_getline(%struct.cc_usb* %0, i8* nocapture writeonly %1, i32 %2) {
%4 = tail call i32 @cc_usb_getchar(%struct.cc_usb* %0)
%5 = icmp eq i32 %4, 10
br i1 %5, label %22, label %6
6: ; preds = %3, %17
%7 = phi i32 [ %20, %17 ], [ %4, %3 ]
%8 = phi i8* [ %19, %17 ], [ %1, %3 ]
%9 = phi i32 [ %18, %17 ], [ %2, %3 ]
%10 = icmp ne i32 %7, 13
%11 = icmp sgt i32 %9, 1
%12 = select i1 %10, i1 %11, i1 false
br i1 %12, label %13, label %17
13: ; preds = %6
%14 = trunc i32 %7 to i8
%15 = getelementptr inbounds i8, i8* %8, i64 1
store i8 %14, i8* %8
%16 = add nsw i32 %9, -1
br label %17
17: ; preds = %13, %6
%18 = phi i32 [ %16, %13 ], [ %9, %6 ]
%19 = phi i8* [ %15, %13 ], [ %8, %6 ]
%20 = tail call i32 @cc_usb_getchar(%struct.cc_usb* %0)
%21 = icmp eq i32 %20, 10
br i1 %21, label %22, label %6
22: ; preds = %17, %3
%23 = phi i8* [ %1, %3 ], [ %19, %17 ]
store i8 0, i8* %23
ret void
}