#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*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fscanf(int /*<<< orphan*/ *, char *, int *);
scalar_t__ kill(int, int /*<<< orphan*/);
int tty_already_locked(char *nam) {
  int i = 0, pid = 0;
  FILE *fd = 0;
  if ((fd = fopen(nam, "r")) == NULL)
    return 0;
  i = fscanf(fd, "%d", &pid);
  (void)fclose(fd);
  if (i != 1)
    return 0;
  if (kill(pid, 0) == 0)
    return 1;
  return 0;
}


x86 assembly (clang, -O0):
<tty_already_locked>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe93(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    118c <tty_already_locked+0x4c>
movl   $0x0,-0x4(%rbp)
jmp    11e9 <tty_already_locked+0xa9>
mov    -0x20(%rbp),%rdi
lea    0xe6b(%rip),%rsi
lea    -0x18(%rbp),%rdx
call   1040 <fscanf@plt>
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
cmpl   $0x1,-0x14(%rbp)
je     11c2 <tty_already_locked+0x82>
movl   $0x0,-0x4(%rbp)
jmp    11e9 <tty_already_locked+0xa9>
mov    -0x18(%rbp),%edi
xor    %esi,%esi
call   1050 <kill@plt>
cmp    $0x0,%rax
jne    11e2 <tty_already_locked+0xa2>
movl   $0x1,-0x4(%rbp)
jmp    11e9 <tty_already_locked+0xa9>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tty_already_locked>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
movl   $0x0,0xc(%rsp)
lea    0xea9(%rip),%rsi
call   1060 <fopen@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     119f <tty_already_locked+0x5f>
mov    %rax,%rbx
lea    0xe95(%rip),%rsi
lea    0xc(%rsp),%rdx
mov    %rax,%rdi
call   1040 <fscanf@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1030 <fclose@plt>
cmp    $0x1,%r14d
jne    119f <tty_already_locked+0x5f>
mov    0xc(%rsp),%edi
xor    %esi,%esi
call   1050 <kill@plt>
xor    %ebp,%ebp
test   %rax,%rax
sete   %bpl
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [3 x i8] c"%d\00"
define i32 @tty_already_locked(i8* nocapture %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
store i32 0, i32* %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 %15, label %6
6: ; preds = %1
%7 = call i32 @fscanf(i32* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %2)
%8 = call i32 @fclose(i32* %4)
%9 = icmp eq i32 %7, 1
br i1 %9, label %10, label %15
10: ; preds = %6
%11 = load i32, i32* %2
%12 = call i64 @kill(i32 %11, i32 0)
%13 = icmp eq i64 %12, 0
%14 = zext i1 %13 to i32
br label %15
15: ; preds = %10, %6, %1
%16 = phi i32 [ 0, %1 ], [ 0, %6 ], [ %14, %10 ]
ret i32 %16
}