#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 */
struct detector {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ detector_free(struct detector *);
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
struct detector *headerparser_parse(int /*<<< orphan*/ *);
struct detector *parse_detector(const char *path) {
  FILE *file;
  struct detector *dtc;
  if ((file = fopen(path, "r")) == NULL)
    return NULL;
  dtc = headerparser_parse(file);
  if (fclose(file) && dtc != NULL) {
    detector_free(dtc);
    dtc = NULL;
  }
  return dtc;
}


x86 assembly (clang, -O0):
<parse_detector>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea9(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1177 <parse_detector+0x37>
movq   $0x0,-0x8(%rbp)
jmp    11bb <parse_detector+0x7b>
mov    -0x18(%rbp),%rdi
call   1060 <headerparser_parse@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
cmp    $0x0,%rax
je     11b3 <parse_detector+0x73>
cmpq   $0x0,-0x20(%rbp)
je     11b3 <parse_detector+0x73>
mov    -0x20(%rbp),%rdi
call   1040 <detector_free@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_detector>:
push   %r14
push   %rbx
push   %rax
lea    0xeb5(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     117d <parse_detector+0x3d>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <headerparser_parse@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <fclose@plt>
test   %rax,%rax
je     1180 <parse_detector+0x40>
test   %r14,%r14
je     1180 <parse_detector+0x40>
mov    %r14,%rdi
call   1040 <detector_free@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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