#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 stat {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ system(char *);
int check_and_set_lock_file(void) {
  char wanpipe_dir[] = "/usr/local/wanrouter";
  char lock_file[] = "/usr/local/wanrouter/wpkbd_lock";
  struct stat file_stat;
  if (stat(wanpipe_dir, &file_stat) < 0) {
    printf("wpkbdmon: Error, WANPIPE utilites not installed\n");
    printf("wpkbdmon: Directory not found: %s\n", wanpipe_dir);
    return 1;
  }
  if (stat(lock_file, &file_stat) < 0) {
    system("touch /usr/local/wanrouter/wpkbd_lock");
  } else {
    printf(
        "\nwpkbdmon: Error, WANPIPE Keyboard Led Monitor already running!\n");
    printf("                 The /usr/local/wanrouter/wpkbd_lock exits.\n\n");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<check_and_set_lock_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    0xec1(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xebe(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0xebc(%rip),%eax
mov    %eax,-0x10(%rbp)
mov    0xeb7(%rip),%al
mov    %al,-0xc(%rbp)
mov    0xeb9(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xeb6(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0xeb3(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xeb0(%rip),%rax
mov    %rax,-0x28(%rbp)
lea    -0x20(%rbp),%rdi
lea    -0x48(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
jge    11cf <check_and_set_lock_file+0x9f>
lea    0xe96(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x20(%rbp),%rsi
lea    0xeb5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1226 <check_and_set_lock_file+0xf6>
lea    -0x40(%rbp),%rdi
lea    -0x48(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
jge    11f7 <check_and_set_lock_file+0xc7>
lea    0xea7(%rip),%rdi
call   1030 <system@plt>
jmp    121f <check_and_set_lock_file+0xef>
lea    0xebc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xeef(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1226 <check_and_set_lock_file+0xf6>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_and_set_lock_file>:
sub    $0x58,%rsp
movaps 0xeb5(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movabs $0x726574756f726e,%rax
mov    %rax,0x1d(%rsp)
movaps 0xeca(%rip),%xmm0
movaps %xmm0,0x40(%rsp)
movaps 0xeae(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
test   %rax,%rax
js     11b9 <check_and_set_lock_file+0x79>
lea    0x30(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1060 <stat@plt>
test   %rax,%rax
js     11e2 <check_and_set_lock_file+0xa2>
lea    0xee3(%rip),%rdi
call   1030 <puts@plt>
lea    0xf17(%rip),%rdi
call   1030 <puts@plt>
jmp    11d8 <check_and_set_lock_file+0x98>
lea    0xf46(%rip),%rdi
call   1030 <puts@plt>
lea    0xe74(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%eax
add    $0x58,%rsp
ret
lea    0xe7a(%rip),%rdi
call   1040 <system@plt>
xor    %eax,%eax
add    $0x58,%rsp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@__const.check_and_set_lock_file.wanpipe_dir = [21 x i8] c"/usr/local/wanrouter\00"
@__const.check_and_set_lock_file.lock_file = [32 x i8] c"/usr/local/wanrouter/wpkbd_lock\00"
@.str = [49 x i8] c"wpkbdmon: Error, WANPIPE utilites not installed\0A\00"
@.str.1 = [35 x i8] c"wpkbdmon: Directory not found: %s\0A\00"
@.str.2 = [38 x i8] c"touch /usr/local/wanrouter/wpkbd_lock\00"
@.str.3 = [65 x i8] c"\0Awpkbdmon: Error, WANPIPE Keyboard Led Monitor already running!\0A\00"
@.str.4 = [62 x i8] c" The /usr/local/wanrouter/wpkbd_lock exits.\0A\0A\00"
define i32 @check_and_set_lock_file() {
%1 = alloca i32
%2 = alloca [21 x i8]
%3 = alloca [32 x i8]
%4 = alloca %struct.stat
%5 = bitcast [21 x i8]* %2 to i8*
%6 = bitcast [32 x i8]* %3 to i8*
%7 = getelementptr inbounds [21 x i8], [21 x i8]* %2, i64 0, i64 0
%8 = call i64 @stat(i8* %7, %struct.stat* %4)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %0
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str, i64 0, i64 0))
%12 = getelementptr inbounds [21 x i8], [21 x i8]* %2, i64 0, i64 0
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %12)
store i32 1, i32* %1
br label %24
14: ; preds = %0
%15 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%16 = call i64 @stat(i8* %15, %struct.stat* %4)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 @system(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
br label %23
20: ; preds = %14
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([65 x i8], [65 x i8]* @.str.3, i64 0, i64 0))
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %24
23: ; preds = %18
store i32 0, i32* %1
br label %24
24: ; preds = %23, %20, %10
%25 = load i32, i32* %1
ret i32 %25
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@__const.check_and_set_lock_file.wanpipe_dir = [21 x i8] c"/usr/local/wanrouter\00"
@__const.check_and_set_lock_file.lock_file = [32 x i8] c"/usr/local/wanrouter/wpkbd_lock\00"
@.str.1 = [35 x i8] c"wpkbdmon: Directory not found: %s\0A\00"
@.str.2 = [38 x i8] c"touch /usr/local/wanrouter/wpkbd_lock\00"
@str = [64 x i8] c"\0Awpkbdmon: Error, WANPIPE Keyboard Led Monitor already running!\00"
@str.5 = [61 x i8] c" The /usr/local/wanrouter/wpkbd_lock exits.\0A\00"
@str.6 = [48 x i8] c"wpkbdmon: Error, WANPIPE utilites not installed\00"
define i32 @check_and_set_lock_file() {
%1 = alloca [21 x i8]
%2 = alloca [32 x i8]
%3 = alloca %struct.stat
%4 = getelementptr inbounds [21 x i8], [21 x i8]* %1, i64 0, i64 0
%5 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%6 = bitcast %struct.stat* %3 to i8*
%7 = call i64 @stat(i8* %4, %struct.stat* %3)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str.6, i64 0, i64 0))
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %4)
br label %20
12: ; preds = %0
%13 = call i64 @stat(i8* %5, %struct.stat* %3)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @system(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
br label %20
17: ; preds = %12
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([64 x i8], [64 x i8]* @str, i64 0, i64 0))
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([61 x i8], [61 x i8]* @str.5, i64 0, i64 0))
br label %20
20: ; preds = %15, %17, %9
%21 = phi i32 [ 1, %9 ], [ 0, %15 ], [ 1, %17 ]
ret i32 %21
}