#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int EOF;
int getc(int /*<<< orphan*/ *);
int fgetline(char *s, int n, FILE *stream) {
  int c;
  do {
    if ((c = getc(stream)) == EOF)
      return -1;
    if (n > 1) {
      --n;
      *s++ = (char)c;
    }
  } while (c != '\n');
  s[-1] = 0;
  return 0;
}


x86 assembly (clang, -O0):
<fgetline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <getc@plt>
mov    %eax,-0x24(%rbp)
mov    0x2eb2(%rip),%rcx
cmp    (%rcx),%eax
jne    114a <fgetline+0x3a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1191 <fgetline+0x81>
cmpl   $0x1,-0x14(%rbp)
jle    1173 <fgetline+0x63>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x24(%rbp),%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
jmp    1178 <fgetline+0x68>
cmpl   $0xa,-0x24(%rbp)
jne    1123 <fgetline+0x13>
mov    -0x10(%rbp),%rax
movb   $0x0,-0x1(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fgetline>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%rbx
mov    0x2ec2(%rip),%r15
jmp    1135 <fgetline+0x25>
nopl   0x0(%rax,%rax,1)
cmp    $0xa,%eax
je     1152 <fgetline+0x42>
mov    %r14,%rdi
call   1030 <getc@plt>
cmp    (%r15),%eax
je     115a <fgetline+0x4a>
cmp    $0x2,%ebp
jl     1130 <fgetline+0x20>
add    $0xffffffff,%ebp
mov    %al,(%rbx)
add    $0x1,%rbx
jmp    1130 <fgetline+0x20>
movb   $0x0,-0x1(%rbx)
xor    %eax,%eax
jmp    115f <fgetline+0x4f>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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