#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 /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
char *fgets(char *, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
char *memchr(char *, char, size_t);
scalar_t__ strlen(char *);
int file_gets(char *str, size_t size, const char *path) {
  char *p, *end;
  FILE *fp;
  int rc;
  str[0] = 0;
  fp = fopen(path, "r");
  if (!fp)
    return -1;
  p = fgets(str, size, fp);
  if (p) {
    end = memchr(str, '\n', size);
    if (end)
      *end = 0;
    else
      str[size - 1] = 0;
  }
  if (strlen(str) == 0) {
    rc = -1;
    goto out_fclose;
  }
  rc = 0;
out_fclose:
  fclose(fp);
  return rc;
}


x86 assembly (clang, -O0):
<file_gets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
lea    0xe8a(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1196 <file_gets+0x46>
movl   $0xffffffff,-0x4(%rbp)
jmp    122d <file_gets+0xdd>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1060 <fgets@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11f8 <file_gets+0xa8>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0xa,%esi
call   1050 <memchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11e3 <file_gets+0x93>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
jmp    11f3 <file_gets+0xa3>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11f8 <file_gets+0xa8>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%rax
jne    1217 <file_gets+0xc7>
movl   $0xffffffff,-0x3c(%rbp)
jmp    121e <file_gets+0xce>
movl   $0x0,-0x3c(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_gets>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
movb   $0x0,(%rdi)
lea    0xeab(%rip),%rsi
mov    %rdx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11a6 <file_gets+0x66>
mov    %rax,%r15
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1197 <file_gets+0x57>
mov    %rbx,%rdi
mov    $0xa,%esi
mov    %r14,%rdx
call   1040 <memchr@plt>
test   %rax,%rax
lea    -0x1(%r14,%rbx,1),%rcx
cmovne %rax,%rcx
movb   $0x0,(%rcx)
cmpb   $0x1,(%rbx)
sbb    %ebx,%ebx
mov    %r15,%rdi
call   1030 <fclose@plt>
jmp    11ab <file_gets+0x6b>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i32 @file_gets(i8* %0, i64 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i32*
%11 = alloca i32
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
%12 = load i8*, i8** %5
%13 = getelementptr inbounds i8, i8* %12, i64 0
store i8 0, i8* %13
%14 = load i8*, i8** %7
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %10
%16 = load i32*, i32** %10
%17 = icmp ne i32* %16, null
br i1 %17, label %19, label %18
18: ; preds = %3
store i32 -1, i32* %4
br label %50
19: ; preds = %3
%20 = load i8*, i8** %5
%21 = load i64, i64* %6
%22 = load i32*, i32** %10
%23 = call i8* @fgets(i8* %20, i64 %21, i32* %22)
store i8* %23, i8** %8
%24 = load i8*, i8** %8
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %40
26: ; preds = %19
%27 = load i8*, i8** %5
%28 = load i64, i64* %6
%29 = call i8* @memchr(i8* %27, i8 signext 10, i64 %28)
store i8* %29, i8** %9
%30 = load i8*, i8** %9
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %34
32: ; preds = %26
%33 = load i8*, i8** %9
store i8 0, i8* %33
br label %39
34: ; preds = %26
%35 = load i8*, i8** %5
%36 = load i64, i64* %6
%37 = sub i64 %36, 1
%38 = getelementptr inbounds i8, i8* %35, i64 %37
store i8 0, i8* %38
br label %39
39: ; preds = %34, %32
br label %40
40: ; preds = %39, %19
%41 = load i8*, i8** %5
%42 = call i64 @strlen(i8* %41)
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %40
store i32 -1, i32* %11
br label %46
45: ; preds = %40
store i32 0, i32* %11
br label %46
46: ; preds = %45, %44
%47 = load i32*, i32** %10
%48 = call i32 @fclose(i32* %47)
%49 = load i32, i32* %11
store i32 %49, i32* %4
br label %50
50: ; preds = %46, %18
%51 = load i32, i32* %4
ret i32 %51
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i32 @file_gets(i8* %0, i64 %1, i8* nocapture %2) {
store i8 0, i8* %0
%4 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %20, label %6
6: ; preds = %3
%7 = tail call i8* @fgets(i8* %0, i64 %1, i32* %4)
%8 = icmp eq i8* %7, null
br i1 %8, label %15, label %9
9: ; preds = %6
%10 = tail call i8* @memchr(i8* %0, i8 signext 10, i64 %1)
%11 = icmp eq i8* %10, null
%12 = add i64 %1, -1
%13 = getelementptr inbounds i8, i8* %0, i64 %12
%14 = select i1 %11, i8* %13, i8* %10
store i8 0, i8* %14
br label %15
15: ; preds = %9, %6
%16 = load i8, i8* %0
%17 = icmp eq i8 %16, 0
%18 = sext i1 %17 to i32
%19 = tail call i32 @fclose(i32* %4)
br label %20
20: ; preds = %3, %15
%21 = phi i32 [ %18, %15 ], [ -1, %3 ]
ret i32 %21
}