#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 *, char *);
int strlen(char *);
int fgets_with_openclose(char *fname, char *buf, size_t maxlen) {
  FILE *fp;
  char *s;
  if ((fp = fopen(fname, "r")) != NULL) {
    s = fgets(buf, maxlen, fp);
    fclose(fp);
    if (s == NULL)
      return -1;
    return strlen(buf);
  } else {
    return -1;
  }
}


x86 assembly (clang, -O0):
<fgets_with_openclose>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea1(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     11b8 <fgets_with_openclose+0x78>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1050 <fgets@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
cmpq   $0x0,-0x30(%rbp)
jne    11a7 <fgets_with_openclose+0x67>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bf <fgets_with_openclose+0x7f>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x4(%rbp)
jmp    11bf <fgets_with_openclose+0x7f>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fgets_with_openclose>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r14
lea    0xeae(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     118a <fgets_with_openclose+0x4a>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rax,%rdx
call   1050 <fgets@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1030 <fclose@plt>
test   %r15,%r15
je     118a <fgets_with_openclose+0x4a>
mov    %r14,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <strlen@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
define i32 @fgets_with_openclose(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i8*, i8** %5
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %8
%12 = icmp ne i32* %11, null
br i1 %12, label %13, label %26
13: ; preds = %3
%14 = load i8*, i8** %6
%15 = load i64, i64* %7
%16 = load i32*, i32** %8
%17 = call i8* @fgets(i8* %14, i64 %15, i32* %16)
store i8* %17, i8** %9
%18 = load i32*, i32** %8
%19 = call i32 @fclose(i32* %18)
%20 = load i8*, i8** %9
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %23
22: ; preds = %13
store i32 -1, i32* %4
br label %27
23: ; preds = %13
%24 = load i8*, i8** %6
%25 = call i32 @strlen(i8* %24)
store i32 %25, i32* %4
br label %27
26: ; preds = %3
store i32 -1, i32* %4
br label %27
27: ; preds = %26, %23, %22
%28 = load i32, i32* %4
ret i32 %28
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define i32 @fgets_with_openclose(i8* nocapture %0, i8* %1, i64 %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %12, label %6
6: ; preds = %3
%7 = tail call i8* @fgets(i8* %1, i64 %2, i32* %4)
%8 = tail call i32 @fclose(i32* %4)
%9 = icmp eq i8* %7, null
br i1 %9, label %12, label %10
10: ; preds = %6
%11 = tail call i32 @strlen(i8* %1)
br label %12
12: ; preds = %3, %6, %10
%13 = phi i32 [ %11, %10 ], [ -1, %6 ], [ -1, %3 ]
ret i32 %13
}