#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 EOVERFLOW;
int errno;
int /*<<< orphan*/ *fgets(char *, size_t, int /*<<< orphan*/ *);
int ow__gets(FILE *file, char *buf, size_t len) {
  int i;
  if (fgets(buf, len, file) == NULL) {
    return errno;
  }
  for (i = 0;; i++) {
    if (i >= len - 1) {
      return EOVERFLOW;
    }
    if (buf[i] == '\n') {
      break;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<ow__gets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1030 <fgets@plt>
cmp    $0x0,%rax
jne    1150 <ow__gets+0x40>
mov    0x2e92(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11b1 <ow__gets+0xa1>
movl   $0x0,-0x24(%rbp)
movslq -0x24(%rbp),%rax
mov    -0x20(%rbp),%rcx
sub    $0x1,%rcx
cmp    %rcx,%rax
jb     117d <ow__gets+0x6d>
mov    0x2e7d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    11b1 <ow__gets+0xa1>
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1197 <ow__gets+0x87>
jmp    11aa <ow__gets+0x9a>
jmp    119c <ow__gets+0x8c>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1157 <ow__gets+0x47>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ow__gets>:
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%rdx
mov    %rsi,%rdi
mov    %rbx,%rsi
call   1030 <fgets@plt>
test   %rax,%rax
je     1159 <ow__gets+0x49>
add    $0xffffffffffffffff,%rbx
je     1150 <ow__gets+0x40>
xor    %eax,%eax
xor    %ecx,%ecx
nopw   0x0(%rax,%rax,1)
cmpb   $0xa,(%r14,%rcx,1)
je     1162 <ow__gets+0x52>
add    $0x1,%rcx
cmp    %rcx,%rbx
jne    1140 <ow__gets+0x30>
mov    0x2e99(%rip),%rax
jmp    1160 <ow__gets+0x50>
mov    0x2e78(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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