#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*/ F_TLOCK;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int getpid();
int lockf(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int, int);
int /*<<< orphan*/ stderr;
void single_process() {
  int pid_file = open("/tmp/gxrandr.pid", O_CREAT | O_RDWR, 0666);
  FILE *fp = fopen("/tmp/gxrandr.pid", "w");
  fprintf(fp, "%d\n", getpid());
  fclose(fp);
  int lock = lockf(pid_file, F_TLOCK, 0);
  if (lock != 0) {
    fprintf(stderr, "gxrandr is already running\n");
    exit(1);
  }
}


x86 assembly (clang, -O0):
<single_process>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
mov    0x2e50(%rip),%rax
or     (%rax),%esi
lea    0xe6f(%rip),%rdi
mov    $0x1b6,%edx
call   1070 <open@plt>
mov    %eax,-0x4(%rbp)
lea    0xe5b(%rip),%rdi
lea    0xe65(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x18(%rbp),%edi
mov    %eax,%edx
lea    0xe44(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x4(%rbp),%edi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <lockf@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1220 <single_process+0xb0>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
lea    0xe08(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<single_process>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e7e(%rip),%rax
mov    0x2e57(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe74(%rip),%rbx
mov    %rbx,%rdi
mov    $0x1b6,%edx
call   1070 <open@plt>
mov    %eax,%ebp
lea    0xe6f(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xe58(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1050 <lockf@plt>
test   %eax,%eax
jne    11eb <single_process+0x7b>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
lea    0xe1c(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [17 x i8] c"/tmp/gxrandr.pid\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@F_TLOCK = global i32 0
@stderr = global i32 0
@.str.3 = [28 x i8] c"gxrandr is already running\0A\00"
define void @single_process() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = load i32, i32* @O_CREAT
%5 = load i32, i32* @O_RDWR
%6 = or i32 %4, %5
%7 = call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %6, i32 438)
store i32 %7, i32* %1
%8 = call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** %2
%9 = load i32*, i32** %2
%10 = ptrtoint i32* %9 to i32
%11 = call i32 (...) @getpid()
%12 = call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %11)
%13 = load i32*, i32** %2
%14 = call i32 @fclose(i32* %13)
%15 = load i32, i32* %1
%16 = load i32, i32* @F_TLOCK
%17 = call i32 @lockf(i32 %15, i32 %16, i32 0)
store i32 %17, i32* %3
%18 = load i32, i32* %3
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %0
%21 = load i32, i32* @stderr
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %0
ret void
}

LLVM IR (-O2):
@.str = [17 x i8] c"/tmp/gxrandr.pid\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [4 x i8] c"%d\0A\00"
@F_TLOCK = global i32 0
@stderr = global i32 0
@.str.3 = [28 x i8] c"gxrandr is already running\0A\00"
define void @single_process() {
%1 = load i32, i32* @O_CREAT
%2 = load i32, i32* @O_RDWR
%3 = or i32 %2, %1
%4 = tail call i32 @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %3, i32 438)
%5 = tail call i32* @fopen(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = ptrtoint i32* %5 to i64
%7 = trunc i64 %6 to i32
%8 = tail call i32 (...) @getpid()
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %8)
%10 = tail call i32 @fclose(i32* %5)
%11 = load i32, i32* @F_TLOCK
%12 = tail call i32 @lockf(i32 %4, i32 %11, i32 0)
%13 = icmp eq i32 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %0
%15 = load i32, i32* @stderr
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %0
ret void
}