#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 */
unsigned int fgetc(unsigned int);
void fgets(unsigned string[], unsigned maxlength, unsigned handle) {
  unsigned c;
  unsigned i = 0;
  while (1) {
    c = fgetc(handle);
    string[i] = c;
    i++;
    if (c == '\n')
      break;
    if (i == maxlength - 1)
      break;
  }
  string[i] = 0;
}


x86 assembly (clang, -O0):
<fgets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%edi
call   1030 <fgetc@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edx
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    %edx,(%rax,%rcx,4)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0xa,-0x14(%rbp)
jne    1159 <fgets+0x49>
jmp    1174 <fgets+0x64>
mov    -0x18(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    116f <fgets+0x5f>
jmp    1174 <fgets+0x64>
jmp    1129 <fgets+0x19>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%ecx
movl   $0x0,(%rax,%rcx,4)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fgets>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %esi,%ebx
mov    %rdi,%r14
add    $0xfffffffe,%ebx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15d,%edi
call   1030 <fgetc@plt>
mov    %eax,(%r14,%rbp,4)
lea    0x1(%rbp),%rcx
cmp    $0xa,%eax
je     114c <fgets+0x3c>
cmp    %ebp,%ebx
mov    %rcx,%rbp
jne    1130 <fgets+0x20>
mov    %ecx,%eax
movl   $0x0,(%r14,%rax,4)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define void @fgets(i32* nocapture writeonly %0, i32 %1, i32 %2) {
%4 = add i32 %1, -1
br label %5
5: ; preds = %5, %3
%6 = phi i64 [ %9, %5 ], [ 0, %3 ]
%7 = tail call i32 @fgetc(i32 %2)
%8 = getelementptr inbounds i32, i32* %0, i64 %6
store i32 %7, i32* %8
%9 = add nuw nsw i64 %6, 1
%10 = trunc i64 %9 to i32
%11 = icmp eq i32 %7, 10
%12 = icmp eq i32 %4, %10
%13 = select i1 %11, i1 true, i1 %12
br i1 %13, label %14, label %5
14: ; preds = %5
%15 = and i64 %9, 4294967295
%16 = getelementptr inbounds i32, i32* %0, i64 %15
store i32 0, i32* %16
ret void
}