#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 ring {
  int dummy;
};

/* Variables and functions */
scalar_t__ ring_getch(struct ring *, int *);
int ring_gets(struct ring *rb, char *rbuf, int max) {
  int i;
  int ch;
  i = 0;
  while (ring_getch(rb, &ch) > 0 && i < max - 1)
    rbuf[i++] = ch;
  rbuf[i] = 0;
  return i;
}


x86 assembly (clang, -O0):
<ring_gets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
call   1030 <ring_getch@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x1d(%rbp)
jle    115a <ring_gets+0x4a>
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
setl   %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    116a <ring_gets+0x5a>
jmp    1189 <ring_gets+0x79>
mov    -0x1c(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x18(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
jmp    112a <ring_gets+0x1a>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ring_gets>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%r12
lea    0xc(%rsp),%rsi
call   1030 <ring_getch@plt>
xor    %ebx,%ebx
test   %rax,%rax
jle    1172 <ring_gets+0x62>
cmp    $0x2,%r15d
jl     1172 <ring_gets+0x62>
add    $0xffffffff,%r15d
xor    %ebx,%ebx
lea    0xc(%rsp),%r13
nopl   0x0(%rax,%rax,1)
movzbl 0xc(%rsp),%eax
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
mov    %r12,%rdi
mov    %r13,%rsi
call   1030 <ring_getch@plt>
test   %rax,%rax
jle    1172 <ring_gets+0x62>
cmp    %r15,%rbx
jb     1150 <ring_gets+0x40>
mov    %ebx,%eax
movb   $0x0,(%r14,%rax,1)
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.ring = type { i32 }
define i32 @ring_gets(%struct.ring* %0, i8* %1, i32 %2) {
%4 = alloca %struct.ring*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store %struct.ring* %0, %struct.ring** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %7
br label %9
9: ; preds = %20, %3
%10 = load %struct.ring*, %struct.ring** %4
%11 = call i64 @ring_getch(%struct.ring* %10, i32* %8)
%12 = icmp sgt i64 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %9
%14 = load i32, i32* %7
%15 = load i32, i32* %6
%16 = sub nsw i32 %15, 1
%17 = icmp slt i32 %14, %16
br label %18
18: ; preds = %13, %9
%19 = phi i1 [ false, %9 ], [ %17, %13 ]
br i1 %19, label %20, label %28
20: ; preds = %18
%21 = load i32, i32* %8
%22 = trunc i32 %21 to i8
%23 = load i8*, i8** %5
%24 = load i32, i32* %7
%25 = add nsw i32 %24, 1
store i32 %25, i32* %7
%26 = sext i32 %24 to i64
%27 = getelementptr inbounds i8, i8* %23, i64 %26
store i8 %22, i8* %27
br label %9
28: ; preds = %18
%29 = load i8*, i8** %5
%30 = load i32, i32* %7
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
store i8 0, i8* %32
%33 = load i32, i32* %7
ret i32 %33
}

LLVM IR (-O2):
%struct.ring = type { i32 }
define i32 @ring_gets(%struct.ring* %0, i8* nocapture writeonly %1, i32 %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = call i64 @ring_getch(%struct.ring* %0, i32* %4)
%7 = icmp sgt i64 %6, 0
%8 = icmp sgt i32 %2, 1
%9 = and i1 %7, %8
br i1 %9, label %10, label %25
10: ; preds = %3
%11 = add nsw i32 %2, -1
%12 = zext i32 %11 to i64
br label %13
13: ; preds = %10, %13
%14 = phi i64 [ 0, %10 ], [ %17, %13 ]
%15 = load i32, i32* %4
%16 = trunc i32 %15 to i8
%17 = add nuw nsw i64 %14, 1
%18 = getelementptr inbounds i8, i8* %1, i64 %14
store i8 %16, i8* %18
%19 = call i64 @ring_getch(%struct.ring* %0, i32* %4)
%20 = icmp sgt i64 %19, 0
%21 = icmp ult i64 %17, %12
%22 = select i1 %20, i1 %21, i1 false
br i1 %22, label %13, label %23
23: ; preds = %13
%24 = trunc i64 %17 to i32
br label %25
25: ; preds = %23, %3
%26 = phi i32 [ 0, %3 ], [ %24, %23 ]
%27 = zext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %1, i64 %27
store i8 0, i8* %28
ret i32 %26
}