#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);
    return (s != NULL) ? strlen(buf) : 0;
  } else {
    return -1;
  }
}


x86 assembly (clang, -O0):
<fgets_with_openclose>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%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     11c1 <fgets_with_openclose+0x81>
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)
je     11ac <fgets_with_openclose+0x6c>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x34(%rbp)
jmp    11b6 <fgets_with_openclose+0x76>
xor    %eax,%eax
mov    %eax,-0x34(%rbp)
jmp    11b6 <fgets_with_openclose+0x76>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11c8 <fgets_with_openclose+0x88>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%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     1191 <fgets_with_openclose+0x51>
mov    %r14,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <strlen@plt>
mov    $0xffffffff,%eax
jmp    1193 <fgets_with_openclose+0x53>
xor    %eax,%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 %28
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 ne i8* %20, null
br i1 %21, label %22, label %25
22: ; preds = %13
%23 = load i8*, i8** %6
%24 = call i32 @strlen(i8* %23)
br label %26
25: ; preds = %13
br label %26
26: ; preds = %25, %22
%27 = phi i32 [ %24, %22 ], [ 0, %25 ]
store i32 %27, i32* %4
br label %29
28: ; preds = %3
store i32 -1, i32* %4
br label %29
29: ; preds = %28, %26
%30 = load i32, i32* %4
ret i32 %30
}

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, %10, %6
%13 = phi i32 [ %11, %10 ], [ 0, %6 ], [ -1, %3 ]
ret i32 %13
}