#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 */
scalar_t__ read(int const, char *, int);
char *my_fgets(char *buf, size_t len, const int fp) {
  char *cp = buf;
  char c;
  while (len > 1 && (read(fp, &c, 1) > 0)) {
    len--;
    *cp++ = c;
    if (c == '\n')
      break;
  }
  if (cp > buf) {
    *cp = 0;
    return (buf);
  } else {
    return (0);
  }
}


x86 assembly (clang, -O0):
<my_fgets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmpq   $0x1,-0x18(%rbp)
mov    %al,-0x2a(%rbp)
jbe    1156 <my_fgets+0x46>
mov    -0x1c(%rbp),%edi
lea    -0x29(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x0,%rax
setg   %al
mov    %al,-0x2a(%rbp)
mov    -0x2a(%rbp),%al
test   $0x1,%al
jne    1166 <my_fgets+0x56>
jmp    119d <my_fgets+0x8d>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
mov    -0x29(%rbp),%cl
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    %cl,(%rax)
movsbl -0x29(%rbp),%eax
cmp    $0xa,%eax
jne    1198 <my_fgets+0x88>
jmp    119d <my_fgets+0x8d>
jmp    112b <my_fgets+0x1b>
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
jbe    11bf <my_fgets+0xaf>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11c7 <my_fgets+0xb7>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_fgets>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
cmp    $0x2,%rsi
jb     117d <my_fgets+0x6d>
mov    %edx,%r12d
mov    %rsi,%rbx
mov    %rdi,%r14
add    $0xffffffffffffffff,%rbx
lea    0xf(%rsp),%r15
mov    %rdi,%rbp
nopw   0x0(%rax,%rax,1)
mov    %r12d,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
test   %rax,%rax
jle    1172 <my_fgets+0x62>
movzbl 0xf(%rsp),%eax
mov    %al,0x0(%rbp)
add    $0x1,%rbp
cmp    $0xa,%al
je     1172 <my_fgets+0x62>
lea    -0x1(%rbx),%rax
cmp    $0x1,%rbx
mov    %rax,%rbx
ja     1140 <my_fgets+0x30>
cmp    %r14,%rbp
jbe    117d <my_fgets+0x6d>
movb   $0x0,0x0(%rbp)
jmp    1180 <my_fgets+0x70>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @my_fgets(i8* %0, i64 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8
store i8* %0, i8** %5
store i64 %1, i64* %6
store i32 %2, i32* %7
%10 = load i8*, i8** %5
store i8* %10, i8** %8
br label %11
11: ; preds = %30, %3
%12 = load i64, i64* %6
%13 = icmp ugt i64 %12, 1
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = load i32, i32* %7
%16 = call i64 @read(i32 %15, i8* %9, i32 1)
%17 = icmp sgt i64 %16, 0
br label %18
18: ; preds = %14, %11
%19 = phi i1 [ false, %11 ], [ %17, %14 ]
br i1 %19, label %20, label %31
20: ; preds = %18
%21 = load i64, i64* %6
%22 = add i64 %21, -1
store i64 %22, i64* %6
%23 = load i8, i8* %9
%24 = load i8*, i8** %8
%25 = getelementptr inbounds i8, i8* %24, i32 1
store i8* %25, i8** %8
store i8 %23, i8* %24
%26 = load i8, i8* %9
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 10
br i1 %28, label %29, label %30
29: ; preds = %20
br label %31
30: ; preds = %20
br label %11
31: ; preds = %29, %18
%32 = load i8*, i8** %8
%33 = load i8*, i8** %5
%34 = icmp ugt i8* %32, %33
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = load i8*, i8** %8
store i8 0, i8* %36
%37 = load i8*, i8** %5
store i8* %37, i8** %4
br label %39
38: ; preds = %31
store i8* null, i8** %4
br label %39
39: ; preds = %38, %35
%40 = load i8*, i8** %4
ret i8* %40
}

LLVM IR (-O2):
define i8* @my_fgets(i8* writeonly %0, i64 %1, i32 %2) {
%4 = alloca i8
%5 = icmp ugt i64 %1, 1
br i1 %5, label %6, label %22
6: ; preds = %3, %11
%7 = phi i64 [ %12, %11 ], [ %1, %3 ]
%8 = phi i8* [ %14, %11 ], [ %0, %3 ]
%9 = call i64 @read(i32 %2, i8* %4, i32 1)
%10 = icmp sgt i64 %9, 0
br i1 %10, label %11, label %18
11: ; preds = %6
%12 = add i64 %7, -1
%13 = load i8, i8* %4
%14 = getelementptr inbounds i8, i8* %8, i64 1
store i8 %13, i8* %8
%15 = icmp ne i8 %13, 10
%16 = icmp ugt i64 %12, 1
%17 = select i1 %15, i1 %16, i1 false
br i1 %17, label %6, label %18
18: ; preds = %11, %6
%19 = phi i8* [ %14, %11 ], [ %8, %6 ]
%20 = icmp ugt i8* %19, %0
br i1 %20, label %21, label %22
21: ; preds = %18
store i8 0, i8* %19
br label %22
22: ; preds = %3, %18, %21
%23 = phi i8* [ %0, %21 ], [ null, %18 ], [ null, %3 ]
ret i8* %23
}