#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*/ *);
scalar_t__ strlen(char *);
char *_q_fgets(char *str, size_t size, FILE *fp) {
  int c;
  char *ptr;
  for (ptr = str; size > 1; size--) {
    c = fgetc(fp);
    if (c == EOF)
      break;
    *ptr++ = (char)c;
    if (c == '\n')
      break;
  }
  *ptr = '\0';
  if (strlen(str) == 0)
    return NULL;
  return str;
}


x86 assembly (clang, -O0):
<_q_fgets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x1,-0x18(%rbp)
jbe    11a5 <_q_fgets+0x85>
mov    -0x20(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    0x2e8b(%rip),%rcx
cmp    (%rcx),%eax
jne    116a <_q_fgets+0x4a>
jmp    11a5 <_q_fgets+0x85>
mov    -0x24(%rbp),%eax
mov    %al,%cl
mov    -0x30(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x30(%rbp)
mov    %cl,(%rax)
cmpl   $0xa,-0x24(%rbp)
jne    118f <_q_fgets+0x6f>
jmp    11a5 <_q_fgets+0x85>
jmp    1194 <_q_fgets+0x74>
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
jmp    113c <_q_fgets+0x1c>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x0,%rax
jne    11cc <_q_fgets+0xac>
movq   $0x0,-0x8(%rbp)
jmp    11d4 <_q_fgets+0xb4>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_q_fgets>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r14
mov    %rdi,%rbx
cmp    $0x2,%rsi
jb     1167 <_q_fgets+0x57>
mov    %rdx,%r15
mov    %rsi,%r12
add    $0xffffffffffffffff,%r12
mov    0x2eb2(%rip),%r13
mov    %r14,%rbx
nopl   0x0(%rax)
mov    %r15,%rdi
call   1030 <fgetc@plt>
cmp    0x0(%r13),%eax
je     1167 <_q_fgets+0x57>
mov    %al,(%rbx)
add    $0x1,%rbx
cmp    $0xa,%eax
je     1167 <_q_fgets+0x57>
lea    -0x1(%r12),%rax
cmp    $0x1,%r12
mov    %rax,%r12
ja     1140 <_q_fgets+0x30>
movb   $0x0,(%rbx)
xor    %eax,%eax
cmpb   $0x0,(%r14)
cmovne %r14,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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