#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 */
#define EOF 128
char getchar();
int mgets(char *z, int i) {
  for (int j = 0; j < (i - 1); z++, j++) {
    *z = getchar();
    switch (*z) {
    case '\n':
      *z = '\0';
      return j;
    case EOF:
      *z = '\0';
      return -1;
    }
  }
  z[i - 1] = '\0';
  return i - 1;
}


x86 assembly (clang, -O0):
<mgets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    11b5 <mgets+0xa5>
xor    %eax,%eax
call   1030 <getchar@plt>
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,-0x1c(%rbp)
sub    $0xa,%eax
je     1171 <mgets+0x61>
jmp    115e <mgets+0x4e>
mov    -0x1c(%rbp),%eax
sub    $0x80,%eax
je     1183 <mgets+0x73>
jmp    1196 <mgets+0x86>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11cf <mgets+0xbf>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11cf <mgets+0xbf>
jmp    119b <mgets+0x8b>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1126 <mgets+0x16>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mgets>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r15
lea    -0x1(%rsi),%r14d
cmp    $0x2,%esi
jl     114a <mgets+0x3a>
mov    %r14d,%ebx
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <getchar@plt>
mov    %al,(%r15,%rbp,1)
cmp    $0xa,%al
je     1152 <mgets+0x42>
add    $0x1,%rbp
cmp    %ebp,%ebx
jne    1130 <mgets+0x20>
add    %rbp,%r15
movslq %r14d,%rax
add    %r15,%rax
jmp    115b <mgets+0x4b>
add    %rbp,%r15
mov    %r15,%rax
mov    %ebp,%r14d
movb   $0x0,(%rax)
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @mgets(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 0, i32* %6
br label %7
7: ; preds = %24, %2
%8 = load i32, i32* %6
%9 = load i32, i32* %5
%10 = sub nsw i32 %9, 1
%11 = icmp slt i32 %8, %10
br i1 %11, label %12, label %29
12: ; preds = %7
%13 = call signext i8 (...) @getchar()
%14 = load i8*, i8** %4
store i8 %13, i8* %14
%15 = load i8*, i8** %4
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
switch i32 %17, label %23 [
i32 10, label %18
i32 128, label %21
]
18: ; preds = %12
%19 = load i8*, i8** %4
store i8 0, i8* %19
%20 = load i32, i32* %6
store i32 %20, i32* %3
br label %37
21: ; preds = %12
%22 = load i8*, i8** %4
store i8 0, i8* %22
store i32 -1, i32* %3
br label %37
23: ; preds = %12
br label %24
24: ; preds = %23
%25 = load i8*, i8** %4
%26 = getelementptr inbounds i8, i8* %25, i32 1
store i8* %26, i8** %4
%27 = load i32, i32* %6
%28 = add nsw i32 %27, 1
store i32 %28, i32* %6
br label %7
29: ; preds = %7
%30 = load i8*, i8** %4
%31 = load i32, i32* %5
%32 = sub nsw i32 %31, 1
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %30, i64 %33
store i8 0, i8* %34
%35 = load i32, i32* %5
%36 = sub nsw i32 %35, 1
store i32 %36, i32* %3
br label %37
37: ; preds = %29, %21, %18
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
define i32 @mgets(i8* nocapture writeonly %0, i32 %1) {
%3 = add i32 %1, -1
%4 = icmp sgt i32 %1, 1
br i1 %4, label %5, label %14
5: ; preds = %2, %10
%6 = phi i32 [ %12, %10 ], [ 0, %2 ]
%7 = phi i8* [ %11, %10 ], [ %0, %2 ]
%8 = tail call signext i8 (...) @getchar()
store i8 %8, i8* %7
%9 = icmp eq i8 %8, 10
br i1 %9, label %18, label %10
10: ; preds = %5
%11 = getelementptr inbounds i8, i8* %7, i64 1
%12 = add nuw nsw i32 %6, 1
%13 = icmp eq i32 %12, %3
br i1 %13, label %14, label %5
14: ; preds = %10, %2
%15 = phi i8* [ %0, %2 ], [ %11, %10 ]
%16 = sext i32 %3 to i64
%17 = getelementptr inbounds i8, i8* %15, i64 %16
br label %18
18: ; preds = %5, %14
%19 = phi i8* [ %17, %14 ], [ %7, %5 ]
%20 = phi i32 [ %3, %14 ], [ %6, %5 ]
store i8 0, i8* %19
ret i32 %20
}