#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*/ LC_CTYPE;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int open(char *, int, int);
int pidfile_check(char *);
int pidfile_read(char *);
int pidfile_remove(char *);
int pidfile_write(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ setlocale(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
int main(void) {
  setlocale(LC_CTYPE, "");
  char *pidfile = "/tmp/avm-motion-trigger-test.pid";
  printf(" pidfile_write (1): %d\n", pidfile_write(pidfile));
  printf("  pidfile_read (1): %d\n", pidfile_read(pidfile));
  printf(" pidfile_check (1): %d\n", pidfile_check(pidfile));
  printf("pidfile_remove (1): %d\n", pidfile_remove(pidfile));
  printf("\n");
  FILE *file;
  int fd;
  pidfile = "/tmp/avm-motion-trigger-test.1.pid";
  if ((-1 == (fd = open(pidfile, O_RDWR | O_CREAT, 0644))) ||
      (NULL == (file = fdopen(fd, "r+")))) {
    fprintf(stderr, "Can't open or create %s.\n", pidfile);
    return 0;
  }
  if (!fprintf(file, "1\n")) {
    printf("Can't write pid.\n");
    close(fd);
    return 0;
  }
  fflush(file);
  close(fd);
  printf("  pidfile_read (2): %d\n", pidfile_read(pidfile));
  printf(" pidfile_check (2): %d\n", pidfile_check(pidfile));
  printf("pidfile_remove (2): %d\n", pidfile_remove(pidfile));
  printf("pidfile_remove (2): %d\n", pidfile_remove(pidfile));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
lea    0xf06(%rip),%rsi
call   10c0 <setlocale@plt>
lea    0xe25(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <pidfile_write@plt>
mov    %eax,%esi
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <pidfile_read@plt>
mov    %eax,%esi
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   10a0 <pidfile_check@plt>
mov    %eax,%esi
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <pidfile_remove@plt>
mov    %eax,%esi
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe8a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe29(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2d79(%rip),%rax
mov    (%rax),%esi
mov    0x2d88(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   10d0 <open@plt>
mov    %eax,%ecx
mov    %ecx,-0x1c(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
je     12af <main+0xff>
mov    -0x1c(%rbp),%edi
lea    0xe0c(%rip),%rsi
call   10b0 <fdopen@plt>
mov    %rax,%rcx
mov    %rcx,-0x18(%rbp)
xor    %eax,%eax
cmp    %rcx,%rax
jne    12d7 <main+0x127>
mov    0x2d12(%rip),%rax
movslq (%rax),%rdi
mov    -0x10(%rbp),%rdx
lea    0xde3(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1390 <main+0x1e0>
mov    -0x18(%rbp),%rdi
lea    0xddf(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
cmp    $0x0,%eax
jne    1314 <main+0x164>
lea    0xdcb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1390 <main+0x1e0>
mov    -0x18(%rbp),%rdi
call   1090 <fflush@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    -0x10(%rbp),%rdi
call   1040 <pidfile_read@plt>
mov    %eax,%esi
lea    0xd9f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   10a0 <pidfile_check@plt>
mov    %eax,%esi
lea    0xd9e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <pidfile_remove@plt>
mov    %eax,%esi
lea    0xd9d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <pidfile_remove@plt>
mov    %eax,%esi
lea    0xd84(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
lea    0xedc(%rip),%rsi
call   10e0 <setlocale@plt>
lea    0xe10(%rip),%rbx
mov    %rbx,%rdi
call   1080 <pidfile_write@plt>
lea    0xe22(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbx,%rdi
call   1050 <pidfile_read@plt>
lea    0xe22(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbx,%rdi
call   10c0 <pidfile_check@plt>
lea    0xe22(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %rbx,%rdi
call   1040 <pidfile_remove@plt>
lea    0xe22(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x2d7f(%rip),%rax
mov    0x2d90(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe0e(%rip),%rdi
mov    $0x1a4,%edx
call   10f0 <open@plt>
cmp    $0xffffffff,%eax
je     1332 <main+0x162>
mov    %eax,%ebx
lea    0xe15(%rip),%rsi
mov    %eax,%edi
call   10d0 <fdopen@plt>
test   %rax,%rax
je     1332 <main+0x162>
mov    %rax,%r14
lea    0xe18(%rip),%rsi
mov    %rax,%rdi
xor    %eax,%eax
call   10a0 <fprintf@plt>
test   %eax,%eax
je     1353 <main+0x183>
mov    %r14,%rdi
call   10b0 <fflush@plt>
mov    %ebx,%edi
call   1090 <close@plt>
lea    0xdb0(%rip),%r14
mov    %r14,%rdi
call   1050 <pidfile_read@plt>
lea    0xde4(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r14,%rdi
call   10c0 <pidfile_check@plt>
lea    0xde4(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r14,%rdi
call   1040 <pidfile_remove@plt>
lea    0xde4(%rip),%rbx
mov    %rbx,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r14,%rdi
call   1040 <pidfile_remove@plt>
mov    %rbx,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    1366 <main+0x196>
mov    0x2c8f(%rip),%rax
movslq (%rax),%rdi
lea    0xd64(%rip),%rsi
lea    0xd37(%rip),%rdx
xor    %eax,%eax
call   10a0 <fprintf@plt>
jmp    1366 <main+0x196>
lea    0xdb2(%rip),%rdi
call   1060 <puts@plt>
mov    %ebx,%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@LC_CTYPE = global i32 0
@.str = [1 x i8] zeroinitializer
@.str.1 = [33 x i8] c"/tmp/avm-motion-trigger-test.pid\00"
@.str.2 = [24 x i8] c" pidfile_write (1): %d\0A\00"
@.str.3 = [24 x i8] c" pidfile_read (1): %d\0A\00"
@.str.4 = [24 x i8] c" pidfile_check (1): %d\0A\00"
@.str.5 = [24 x i8] c"pidfile_remove (1): %d\0A\00"
@.str.6 = [2 x i8] c"\0A\00"
@.str.7 = [35 x i8] c"/tmp/avm-motion-trigger-test.1.pid\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.8 = [3 x i8] c"r+\00"
@stderr = global i32 0
@.str.9 = [26 x i8] c"Can't open or create %s.\0A\00"
@.str.10 = [3 x i8] c"1\0A\00"
@.str.11 = [18 x i8] c"Can't write pid.\0A\00"
@.str.12 = [24 x i8] c" pidfile_read (2): %d\0A\00"
@.str.13 = [24 x i8] c" pidfile_check (2): %d\0A\00"
@.str.14 = [24 x i8] c"pidfile_remove (2): %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
store i32 0, i32* %1
%5 = load i32, i32* @LC_CTYPE
%6 = call i32 @setlocale(i32 %5, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
store i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8** %2
%7 = load i8*, i8** %2
%8 = call i32 @pidfile_write(i8* %7)
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %8)
%10 = load i8*, i8** %2
%11 = call i32 @pidfile_read(i8* %10)
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %11)
%13 = load i8*, i8** %2
%14 = call i32 @pidfile_check(i8* %13)
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i32 %14)
%16 = load i8*, i8** %2
%17 = call i32 @pidfile_remove(i8* %16)
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i32 %17)
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
store i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.7, i64 0, i64 0), i8** %2
%20 = load i8*, i8** %2
%21 = load i32, i32* @O_RDWR
%22 = load i32, i32* @O_CREAT
%23 = or i32 %21, %22
%24 = call i32 @open(i8* %20, i32 %23, i32 420)
store i32 %24, i32* %4
%25 = icmp eq i32 -1, %24
br i1 %25, label %30, label %26
26: ; preds = %0
%27 = load i32, i32* %4
%28 = call i32* @fdopen(i32 %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0))
store i32* %28, i32** %3
%29 = icmp eq i32* null, %28
br i1 %29, label %30, label %36
30: ; preds = %26, %0
%31 = load i32, i32* @stderr
%32 = sext i32 %31 to i64
%33 = inttoptr i64 %32 to i32*
%34 = load i8*, i8** %2
%35 = call i32 (i32*, i8*, ...) @fprintf(i32* %33, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.9, i64 0, i64 0), i8* %34)
store i32 0, i32* %1
br label %61
36: ; preds = %26
%37 = load i32*, i32** %3
%38 = call i32 (i32*, i8*, ...) @fprintf(i32* %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.10, i64 0, i64 0))
%39 = icmp ne i32 %38, 0
br i1 %39, label %44, label %40
40: ; preds = %36
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.11, i64 0, i64 0))
%42 = load i32, i32* %4
%43 = call i32 @close(i32 %42)
store i32 0, i32* %1
br label %61
44: ; preds = %36
%45 = load i32*, i32** %3
%46 = call i32 @fflush(i32* %45)
%47 = load i32, i32* %4
%48 = call i32 @close(i32 %47)
%49 = load i8*, i8** %2
%50 = call i32 @pidfile_read(i8* %49)
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.12, i64 0, i64 0), i32 %50)
%52 = load i8*, i8** %2
%53 = call i32 @pidfile_check(i8* %52)
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.13, i64 0, i64 0), i32 %53)
%55 = load i8*, i8** %2
%56 = call i32 @pidfile_remove(i8* %55)
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.14, i64 0, i64 0), i32 %56)
%58 = load i8*, i8** %2
%59 = call i32 @pidfile_remove(i8* %58)
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.14, i64 0, i64 0), i32 %59)
store i32 0, i32* %1
br label %61
61: ; preds = %44, %40, %30
%62 = load i32, i32* %1
ret i32 %62
}

LLVM IR (-O2):
@LC_CTYPE = global i32 0
@.str = [1 x i8] zeroinitializer
@.str.1 = [33 x i8] c"/tmp/avm-motion-trigger-test.pid\00"
@.str.2 = [24 x i8] c" pidfile_write (1): %d\0A\00"
@.str.3 = [24 x i8] c" pidfile_read (1): %d\0A\00"
@.str.4 = [24 x i8] c" pidfile_check (1): %d\0A\00"
@.str.5 = [24 x i8] c"pidfile_remove (1): %d\0A\00"
@.str.7 = [35 x i8] c"/tmp/avm-motion-trigger-test.1.pid\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@.str.8 = [3 x i8] c"r+\00"
@stderr = global i32 0
@.str.9 = [26 x i8] c"Can't open or create %s.\0A\00"
@.str.10 = [3 x i8] c"1\0A\00"
@.str.12 = [24 x i8] c" pidfile_read (2): %d\0A\00"
@.str.13 = [24 x i8] c" pidfile_check (2): %d\0A\00"
@.str.14 = [24 x i8] c"pidfile_remove (2): %d\0A\00"
@str = [17 x i8] c"Can't write pid.\00"
define i32 @main() {
%1 = load i32, i32* @LC_CTYPE
%2 = tail call i32 @setlocale(i32 %1, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
%3 = tail call i32 @pidfile_write(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %3)
%5 = tail call i32 @pidfile_read(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%6 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %5)
%7 = tail call i32 @pidfile_check(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0), i32 %7)
%9 = tail call i32 @pidfile_remove(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i32 %9)
%11 = tail call i32 @putchar(i32 10)
%12 = load i32, i32* @O_RDWR
%13 = load i32, i32* @O_CREAT
%14 = or i32 %13, %12
%15 = tail call i32 @open(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.7, i64 0, i64 0), i32 %14, i32 420)
%16 = icmp eq i32 %15, -1
br i1 %16, label %20, label %17
17: ; preds = %0
%18 = tail call i32* @fdopen(i32 %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %25
20: ; preds = %17, %0
%21 = load i32, i32* @stderr
%22 = sext i32 %21 to i64
%23 = inttoptr i64 %22 to i32*
%24 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %23, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.9, i64 0, i64 0), i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.7, i64 0, i64 0))
br label %42
25: ; preds = %17
%26 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.10, i64 0, i64 0))
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%30 = tail call i32 @close(i32 %15)
br label %42
31: ; preds = %25
%32 = tail call i32 @fflush(i32* %18)
%33 = tail call i32 @close(i32 %15)
%34 = tail call i32 @pidfile_read(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.7, i64 0, i64 0))
%35 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.12, i64 0, i64 0), i32 %34)
%36 = tail call i32 @pidfile_check(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.7, i64 0, i64 0))
%37 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.13, i64 0, i64 0), i32 %36)
%38 = tail call i32 @pidfile_remove(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.7, i64 0, i64 0))
%39 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.14, i64 0, i64 0), i32 %38)
%40 = tail call i32 @pidfile_remove(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.7, i64 0, i64 0))
%41 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.14, i64 0, i64 0), i32 %40)
br label %42
42: ; preds = %31, %28, %20
ret i32 0
}