#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*/ *);
int /*<<< orphan*/ strcat(char *, char *);
void ipr_fgets(char *buf, int size, FILE *stream) {
  int i, ch = 0;
  for (i = 0; ch != EOF && i < (size - 1); i++) {
    ch = fgetc(stream);
    if (ch == '\n')
      break;
    buf[i] = ch;
  }
  buf[i] = '\0';
  strcat(buf, "\n");
}


x86 assembly (clang, -O0):
<ipr_fgets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x20(%rbp),%ecx
xor    %eax,%eax
mov    0x2e9b(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x21(%rbp)
je     1169 <ipr_fgets+0x49>
mov    -0x1c(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
setl   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1179 <ipr_fgets+0x59>
jmp    11b2 <ipr_fgets+0x92>
mov    -0x18(%rbp),%rdi
call   1030 <fgetc@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xa,-0x20(%rbp)
jne    1194 <ipr_fgets+0x74>
jmp    11b2 <ipr_fgets+0x92>
mov    -0x20(%rbp),%eax
mov    %al,%dl
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1141 <ipr_fgets+0x21>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rdi
lea    0xe37(%rip),%rsi
call   1040 <strcat@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipr_fgets>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r14
mov    0x2eb5(%rip),%r13
xor    %ebx,%ebx
cmpl   $0x0,0x0(%r13)
je     1170 <ipr_fgets+0x50>
mov    %esi,%r12d
cmp    $0x2,%esi
jl     1170 <ipr_fgets+0x50>
mov    %rdx,%r15
add    $0xffffffff,%r12d
xor    %ebx,%ebx
nopl   (%rax)
mov    %r15,%rdi
call   1030 <fgetc@plt>
cmp    $0xa,%eax
je     1170 <ipr_fgets+0x50>
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    0x0(%r13),%eax
je     1170 <ipr_fgets+0x50>
cmp    %r12,%rbx
jb     1150 <ipr_fgets+0x30>
mov    %ebx,%eax
movb   $0x0,(%r14,%rax,1)
lea    0xe82(%rip),%rsi
mov    %r14,%rdi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1040 <strcat@plt>

LLVM IR (-O0):
@EOF = global i32 0
@.str = [2 x i8] c"\0A\00"
define void @ipr_fgets(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 0, i32* %8
store i32 0, i32* %7
br label %9
9: ; preds = %33, %3
%10 = load i32, i32* %8
%11 = load i32, i32* @EOF
%12 = icmp ne i32 %10, %11
br i1 %12, label %13, label %18
13: ; preds = %9
%14 = load i32, i32* %7
%15 = load i32, i32* %5
%16 = sub nsw i32 %15, 1
%17 = icmp slt i32 %14, %16
br label %18
18: ; preds = %13, %9
%19 = phi i1 [ false, %9 ], [ %17, %13 ]
br i1 %19, label %20, label %36
20: ; preds = %18
%21 = load i32*, i32** %6
%22 = call i32 @fgetc(i32* %21)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp eq i32 %23, 10
br i1 %24, label %25, label %26
25: ; preds = %20
br label %36
26: ; preds = %20
%27 = load i32, i32* %8
%28 = trunc i32 %27 to i8
%29 = load i8*, i8** %4
%30 = load i32, i32* %7
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
store i8 %28, i8* %32
br label %33
33: ; preds = %26
%34 = load i32, i32* %7
%35 = add nsw i32 %34, 1
store i32 %35, i32* %7
br label %9
36: ; preds = %25, %18
%37 = load i8*, i8** %4
%38 = load i32, i32* %7
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
store i8 0, i8* %40
%41 = load i8*, i8** %4
%42 = call i32 @strcat(i8* %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@EOF = global i32 0
@.str = [2 x i8] c"\0A\00"
define void @ipr_fgets(i8* %0, i32 %1, i32* nocapture %2) {
%4 = load i32, i32* @EOF
%5 = icmp ne i32 %4, 0
%6 = icmp sgt i32 %1, 1
%7 = and i1 %5, %6
br i1 %7, label %8, label %23
8: ; preds = %3
%9 = add nsw i32 %1, -1
%10 = zext i32 %9 to i64
br label %11
11: ; preds = %8, %15
%12 = phi i64 [ 0, %8 ], [ %18, %15 ]
%13 = tail call i32 @fgetc(i32* %2)
%14 = icmp eq i32 %13, 10
br i1 %14, label %23, label %15
15: ; preds = %11
%16 = trunc i32 %13 to i8
%17 = getelementptr inbounds i8, i8* %0, i64 %12
store i8 %16, i8* %17
%18 = add nuw nsw i64 %12, 1
%19 = load i32, i32* @EOF
%20 = icmp ne i32 %13, %19
%21 = icmp ult i64 %18, %10
%22 = select i1 %20, i1 %21, i1 false
br i1 %22, label %11, label %23
23: ; preds = %15, %11, %3
%24 = phi i64 [ 0, %3 ], [ %12, %11 ], [ %18, %15 ]
%25 = and i64 %24, 4294967295
%26 = getelementptr inbounds i8, i8* %0, i64 %25
store i8 0, i8* %26
%27 = tail call i32 @strcat(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
ret void
}