#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 pid_t;

/* Variables and functions */
int /*<<< orphan*/ WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ execl(char *, char *, char const *, int /*<<< orphan*/ *);
int fork();
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
bool is_open(const char *file) {
  int status;
  pid_t pid;
  pid = fork();
  switch (pid) {
  case -1:
    return false;
  case 0:
    execl("/usr/bin/lsof", "lsof", file, NULL);
    _exit(1);
  default:
    waitpid(pid, &status, 0);
    if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
      return true;
    else
      return false;
  }
}


x86 assembly (clang, -O0):
<is_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
xor    %eax,%eax
call   1080 <fork@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
sub    $0xffffffff,%eax
je     119a <is_open+0x3a>
jmp    118a <is_open+0x2a>
mov    -0x1c(%rbp),%eax
test   %eax,%eax
je     11a6 <is_open+0x46>
jmp    11cb <is_open+0x6b>
movl   $0x0,-0x4(%rbp)
jmp    120e <is_open+0xae>
mov    -0x10(%rbp),%rdx
lea    0xe4f(%rip),%rdi
lea    0xe51(%rip),%rsi
xor    %eax,%eax
mov    %eax,%ecx
call   1070 <execl@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>
mov    -0x18(%rbp),%edi
lea    -0x14(%rbp),%rsi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    -0x14(%rbp),%edi
call   1060 <WIFEXITED@plt>
cmp    $0x0,%eax
je     1207 <is_open+0xa7>
mov    -0x14(%rbp),%edi
call   1040 <WEXITSTATUS@plt>
cmp    $0x0,%eax
jne    1207 <is_open+0xa7>
movl   $0x1,-0x4(%rbp)
jmp    120e <is_open+0xae>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_open>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %ebx,%ebx
xor    %eax,%eax
call   1080 <fork@plt>
cmp    $0xffffffff,%eax
je     11a8 <is_open+0x48>
test   %eax,%eax
je     11b2 <is_open+0x52>
lea    0x4(%rsp),%rsi
xor    %ebx,%ebx
mov    %eax,%edi
xor    %edx,%edx
call   1050 <waitpid@plt>
mov    0x4(%rsp),%edi
call   1060 <WIFEXITED@plt>
test   %eax,%eax
je     11a8 <is_open+0x48>
mov    0x4(%rsp),%edi
call   1040 <WEXITSTATUS@plt>
test   %eax,%eax
jne    11a8 <is_open+0x48>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe47(%rip),%rdi
lea    0xe49(%rip),%rsi
mov    %r14,%rdx
xor    %ecx,%ecx
call   1070 <execl@plt>
mov    $0x1,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"/usr/bin/lsof\00"
@.str.1 = [5 x i8] c"lsof\00"
define i32 @is_open(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %3
%6 = call i32 (...) @fork()
store i32 %6, i32* %5
%7 = load i32, i32* %5
switch i32 %7, label %13 [
i32 -1, label %8
i32 0, label %9
]
8: ; preds = %1
store i32 0, i32* %2
br label %25
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = call i32 @execl(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %10, i32* null)
%12 = call i32 @_exit(i32 1)
unreachable
13: ; preds = %1
%14 = load i32, i32* %5
%15 = call i32 @waitpid(i32 %14, i32* %4, i32 0)
%16 = load i32, i32* %4
%17 = call i32 @WIFEXITED(i32 %16)
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %13
%20 = load i32, i32* %4
%21 = call i32 @WEXITSTATUS(i32 %20)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %19
store i32 1, i32* %2
br label %25
24: ; preds = %19, %13
store i32 0, i32* %2
br label %25
25: ; preds = %24, %23, %8
%26 = load i32, i32* %2
ret i32 %26
}

LLVM IR (-O2):
@.str = [14 x i8] c"/usr/bin/lsof\00"
@.str.1 = [5 x i8] c"lsof\00"
define i32 @is_open(i8* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = tail call i32 (...) @fork()
switch i32 %4, label %8 [
i32 -1, label %18
i32 0, label %5
]
5: ; preds = %1
%6 = tail call i32 @execl(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32* null)
%7 = tail call i32 @_exit(i32 1)
unreachable
8: ; preds = %1
%9 = call i32 @waitpid(i32 %4, i32* %2, i32 0)
%10 = load i32, i32* %2
%11 = call i32 @WIFEXITED(i32 %10)
%12 = icmp eq i32 %11, 0
br i1 %12, label %17, label %13
13: ; preds = %8
%14 = load i32, i32* %2
%15 = call i32 @WEXITSTATUS(i32 %14)
%16 = icmp eq i32 %15, 0
br i1 %16, label %18, label %17
17: ; preds = %13, %8
br label %18
18: ; preds = %13, %1, %17
%19 = phi i32 [ 0, %17 ], [ 0, %1 ], [ 1, %13 ]
ret i32 %19
}