#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 fgetc(int /*<<< orphan*/ *);
char *fgets(char *str, int num, FILE *stream) {
  char *buf = str;
  if (num <= 1) {
    return NULL;
  }
  int ch = '\0';
  while (--num > 0) {
    ch = fgetc(stream);
    if (ch == EOF) {
      return NULL;
    }
    *str++ = ch;
    if (ch == '\n') {
      break;
    }
  }
  *str = '\0';
  return buf;
}


x86 assembly (clang, -O0):
<fgets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x1,-0x14(%rbp)
jg     1142 <fgets+0x32>
movq   $0x0,-0x8(%rbp)
jmp    11bf <fgets+0xaf>
movl   $0x0,-0x2c(%rbp)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jle    11b0 <fgets+0xa0>
mov    -0x20(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    0x2e77(%rip),%rcx
cmp    (%rcx),%eax
jne    1186 <fgets+0x76>
movq   $0x0,-0x8(%rbp)
jmp    11bf <fgets+0xaf>
mov    -0x2c(%rbp),%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
cmpl   $0xa,-0x2c(%rbp)
jne    11ab <fgets+0x9b>
jmp    11b0 <fgets+0xa0>
jmp    1149 <fgets+0x39>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fgets>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
cmp    $0x2,%esi
jge    1122 <fgets+0x12>
xor    %r14d,%r14d
jmp    1164 <fgets+0x54>
mov    %rdx,%r15
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2eb7(%rip),%r12
mov    %rdi,%rbx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
cmp    $0x2,%ebp
jl     1161 <fgets+0x51>
mov    %r15,%rdi
call   1030 <fgetc@plt>
cmp    (%r12),%eax
je     111d <fgets+0xd>
mov    %al,(%rbx)
add    $0x1,%rbx
add    $0xffffffff,%ebp
cmp    $0xa,%eax
jne    1140 <fgets+0x30>
movb   $0x0,(%rbx)
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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