#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 */
int get_schar(char *, int);
int my_fgets(char *buf, int maxsize, int fd) {
  maxsize--;
  char c;
  char *p;
  p = buf;
  while (get_schar(&c, fd) == 1) {
    *p++ = c;
    maxsize--;
    if ((c == '\n') || (maxsize == 0)) {
      *p = 0;
      return (1);
    }
  }
  *p = 0;
  return (0);
}


x86 assembly (clang, -O0):
<my_fgets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%esi
lea    -0x19(%rbp),%rdi
call   1030 <get_schar@plt>
cmp    $0x1,%eax
jne    1194 <my_fgets+0x84>
mov    -0x19(%rbp),%cl
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    %cl,(%rax)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
movsbl -0x19(%rbp),%eax
cmp    $0xa,%eax
je     117c <my_fgets+0x6c>
cmpl   $0x0,-0x14(%rbp)
jne    118f <my_fgets+0x7f>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    11a2 <my_fgets+0x92>
jmp    1133 <my_fgets+0x23>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_fgets>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %esi,%ebp
mov    %rdi,%rbx
add    $0xfffffffe,%ebp
lea    0x7(%rsp),%r15
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %r14d,%esi
call   1030 <get_schar@plt>
cmp    $0x1,%eax
jne    1161 <my_fgets+0x51>
movzbl 0x7(%rsp),%edx
mov    %dl,(%rbx)
add    $0x1,%rbx
sub    $0x1,%ebp
setb   %cl
mov    $0x1,%eax
cmp    $0xa,%dl
je     1163 <my_fgets+0x53>
test   %cl,%cl
je     1130 <my_fgets+0x20>
jmp    1163 <my_fgets+0x53>
xor    %eax,%eax
movb   $0x0,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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