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


x86 assembly (clang, -O0):
<fdgets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
xor    %eax,%eax
cmpl   $0x1,-0x14(%rbp)
mov    %al,-0x19(%rbp)
jne    1166 <fdgets+0x56>
mov    -0x18(%rbp),%ecx
xor    %eax,%eax
cmp    -0xc(%rbp),%ecx
mov    %al,-0x19(%rbp)
jge    1166 <fdgets+0x56>
mov    -0x8(%rbp),%rax
movslq -0x18(%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 -0x18(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1130 <fdgets+0x20>
cmpl   $0x0,-0x14(%rbp)
jne    11b4 <fdgets+0xa4>
xor    %eax,%eax
mov    %rax,-0x28(%rbp)
jmp    11bc <fdgets+0xac>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fdgets>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r14
test   %esi,%esi
jle    1163 <fdgets+0x53>
mov    %edx,%r15d
mov    %esi,%r12d
mov    $0x1,%ebp
mov    %r14,%rbx
nopl   (%rax)
cmpb   $0xa,-0x1(%rbx)
je     1163 <fdgets+0x53>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    115b <fdgets+0x4b>
add    $0x1,%rbx
lea    0x1(%rbp),%rcx
cmp    %r12,%rbp
mov    %rcx,%rbp
jb     1130 <fdgets+0x20>
xor    %ecx,%ecx
test   %eax,%eax
cmove  %rcx,%r14
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @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 1, i32* %7
store i32 0, i32* %8
br label %9
9: ; preds = %27, %3
%10 = load i32, i32* %7
%11 = icmp eq i32 %10, 1
br i1 %11, label %12, label %25
12: ; preds = %9
%13 = load i32, i32* %8
%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* %8
%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* %8
%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* %7
%34 = load i32, i32* %8
%35 = add nsw i32 %34, 1
store i32 %35, i32* %8
br label %9
36: ; preds = %25
%37 = load i32, i32* %7
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %36
br label %42
40: ; preds = %36
%41 = load i8*, i8** %4
br label %42
42: ; preds = %40, %39
%43 = phi i8* [ null, %39 ], [ %41, %40 ]
ret i8* %43
}

LLVM IR (-O2):
define i8* @fdgets(i8* %0, i32 %1, i32 %2) {
%4 = icmp sgt i32 %1, 0
br i1 %4, label %5, label %22
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 %22, 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 = %13
%20 = icmp eq i32 %14, 0
%21 = select i1 %20, i8* null, i8* %0
br label %22
22: ; preds = %7, %19, %3
%23 = phi i8* [ %0, %3 ], [ %21, %19 ], [ %0, %7 ]
ret i8* %23
}