#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 read(int, unsigned char *, int);
int fdgets(unsigned char *buffer, int bufferSize, int fd) {
  int total = 0, got = 1;
  while (got == 1 && total < bufferSize && *(buffer + total - 1) != '\n') {
    got = read(fd, buffer + total, 1);
    total++;
  }
  return got;
}


x86 assembly (clang, -O0):
<fdgets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x1,-0x18(%rbp)
xor    %eax,%eax
cmpl   $0x1,-0x18(%rbp)
mov    %al,-0x19(%rbp)
jne    1166 <fdgets+0x56>
mov    -0x14(%rbp),%ecx
xor    %eax,%eax
cmp    -0xc(%rbp),%ecx
mov    %al,-0x19(%rbp)
jge    1166 <fdgets+0x56>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movzbl -0x1(%rax,%rcx,1),%eax
cmp    $0xa,%eax
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    1176 <fdgets+0x66>
jmp    119f <fdgets+0x8f>
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%rsi
movslq -0x14(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1130 <fdgets+0x20>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fdgets>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %esi,%esi
jle    115d <fdgets+0x4d>
mov    %edx,%r14d
mov    %rdi,%rbx
mov    %esi,%r15d
mov    $0x1,%ebp
nopl   0x0(%rax)
cmpb   $0xa,-0x1(%rbx)
je     115d <fdgets+0x4d>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    1162 <fdgets+0x52>
add    $0x1,%rbx
lea    0x1(%rbp),%rcx
cmp    %r15,%rbp
mov    %rcx,%rbp
jb     1130 <fdgets+0x20>
jmp    1162 <fdgets+0x52>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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