#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 */

/* Variables and functions */
scalar_t__ EACCES;
scalar_t__ EAGAIN;
int O_CREAT;
int O_RDWR;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int /*<<< orphan*/ ftruncate(int, int /*<<< orphan*/);
scalar_t__ getpid();
scalar_t__ lockfile(int);
int open(char *, int, int);
int /*<<< orphan*/ sprintf(char *, char *, long);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
scalar_t__ strlen(char *);
int /*<<< orphan*/ write(int, char *, scalar_t__);
int already_running(void) {
  int fd;
  char buf[16];
  char *LOCKFILE = "/usr/local/adon/fumin/myserver/test/this.pid";
  fd = open(LOCKFILE, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  if (fd < 0) {
    fprintf(stderr, "can't open %s: %s", LOCKFILE, strerror(errno));
    exit(1);
  }
  if (lockfile(fd) < 0) {
    if (errno == EACCES || errno == EAGAIN) {
      close(fd);
      return (1);
    }
    fprintf(stderr, "can't lock %s: %s", LOCKFILE, strerror(errno));
    exit(1);
  }
  ftruncate(fd, 0);
  sprintf(buf, "%ld", (long)getpid());
  write(fd, buf, strlen(buf) + 1);
  return (0);
}


x86 assembly (clang, -O0):
<already_running>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
lea    0xe41(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
mov    0x2e21(%rip),%rax
or     (%rax),%esi
mov    0x2dc0(%rip),%rax
mov    (%rax),%edx
mov    0x2def(%rip),%rax
or     (%rax),%edx
mov    0x2dbe(%rip),%rax
or     (%rax),%edx
mov    0x2dad(%rip),%rax
or     (%rax),%edx
call   10a0 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1252 <already_running+0xa2>
mov    0x2daa(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2d8e(%rip),%rax
mov    (%rax),%rdi
call   10d0 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xdea(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x8(%rbp),%edi
call   1090 <lockfile@plt>
cmp    $0x0,%rax
jge    12ef <already_running+0x13f>
mov    0x2d4d(%rip),%rax
mov    (%rax),%rax
mov    0x2d5b(%rip),%rcx
cmp    (%rcx),%rax
je     1298 <already_running+0xe8>
mov    0x2d33(%rip),%rax
mov    (%rax),%rax
mov    0x2d01(%rip),%rcx
cmp    (%rcx),%rax
jne    12ac <already_running+0xfc>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    134c <already_running+0x19c>
mov    0x2d0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2cf1(%rip),%rax
mov    (%rax),%rdi
call   10d0 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xd5f(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1060 <ftruncate@plt>
lea    -0x20(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x60(%rbp),%rdi
mov    %rax,%rdx
lea    0xd3b(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x54(%rbp)
lea    -0x20(%rbp),%rax
mov    %rax,-0x50(%rbp)
lea    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1030 <write@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<already_running>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e21(%rip),%rax
mov    0x2e32(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dcf(%rip),%rax
mov    0x2e00(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2dcd(%rip),%rax
or     (%rax),%edx
mov    0x2dbc(%rip),%rax
or     (%rax),%edx
lea    0xe0b(%rip),%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     1288 <already_running+0xd8>
mov    %eax,%ebx
mov    %eax,%edi
call   1090 <lockfile@plt>
test   %rax,%rax
js     124f <already_running+0x9f>
xor    %ebp,%ebp
mov    %ebx,%edi
xor    %esi,%esi
call   1060 <ftruncate@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe28(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
mov    %rax,%rdx
call   10b0 <sprintf@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %ebx,%edi
mov    %r14,%rsi
call   1030 <write@plt>
jmp    127d <already_running+0xcd>
mov    0x2d62(%rip),%rax
mov    (%rax),%rdi
mov    0x2d70(%rip),%rax
cmp    (%rax),%rdi
je     1271 <already_running+0xc1>
mov    0x2d24(%rip),%rax
cmp    (%rax),%rdi
jne    12a9 <already_running+0xf9>
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d31(%rip),%rax
mov    (%rax),%ebx
mov    0x2d20(%rip),%rax
mov    (%rax),%rdi
call   10d0 <strerror@plt>
lea    0xd86(%rip),%rsi
jmp    12be <already_running+0x10e>
mov    0x2d10(%rip),%rax
mov    (%rax),%ebx
call   10d0 <strerror@plt>
lea    0xd81(%rip),%rsi
lea    0xd3b(%rip),%rdx
mov    %ebx,%edi
mov    %rax,%rcx
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [45 x i8] c"/usr/local/adon/fumin/myserver/test/this.pid\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@stderr = global i32 0
@.str.1 = [18 x i8] c"can't open %s: %s\00"
@errno = global i64 0
@EACCES = global i64 0
@EAGAIN = global i64 0
@.str.2 = [18 x i8] c"can't lock %s: %s\00"
@.str.3 = [4 x i8] c"%ld\00"
define i32 @already_running() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [16 x i8]
%4 = alloca i8*
store i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i8** %4
%5 = load i8*, i8** %4
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_CREAT
%8 = or i32 %6, %7
%9 = load i32, i32* @S_IRUSR
%10 = load i32, i32* @S_IWUSR
%11 = or i32 %9, %10
%12 = load i32, i32* @S_IRGRP
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IROTH
%15 = or i32 %13, %14
%16 = call i32 @open(i8* %5, i32 %8, i32 %15)
store i32 %16, i32* %2
%17 = load i32, i32* %2
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %26
19: ; preds = %0
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %4
%22 = load i64, i64* @errno
%23 = call i8* @strerror(i64 %22)
%24 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %21, i8* %23)
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %0
%27 = load i32, i32* %2
%28 = call i64 @lockfile(i32 %27)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %48
30: ; preds = %26
%31 = load i64, i64* @errno
%32 = load i64, i64* @EACCES
%33 = icmp eq i64 %31, %32
br i1 %33, label %38, label %34
34: ; preds = %30
%35 = load i64, i64* @errno
%36 = load i64, i64* @EAGAIN
%37 = icmp eq i64 %35, %36
br i1 %37, label %38, label %41
38: ; preds = %34, %30
%39 = load i32, i32* %2
%40 = call i32 @close(i32 %39)
store i32 1, i32* %1
br label %60
41: ; preds = %34
%42 = load i32, i32* @stderr
%43 = load i8*, i8** %4
%44 = load i64, i64* @errno
%45 = call i8* @strerror(i64 %44)
%46 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %43, i8* %45)
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %26
%49 = load i32, i32* %2
%50 = call i32 @ftruncate(i32 %49, i32 0)
%51 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%52 = call i64 (...) @getpid()
%53 = call i32 @sprintf(i8* %51, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i64 %52)
%54 = load i32, i32* %2
%55 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%56 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%57 = call i64 @strlen(i8* %56)
%58 = add nsw i64 %57, 1
%59 = call i32 @write(i32 %54, i8* %55, i64 %58)
store i32 0, i32* %1
br label %60
60: ; preds = %48, %38
%61 = load i32, i32* %1
ret i32 %61
}

LLVM IR (-O2):
@.str = [45 x i8] c"/usr/local/adon/fumin/myserver/test/this.pid\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@stderr = global i32 0
@.str.1 = [18 x i8] c"can't open %s: %s\00"
@errno = global i64 0
@EACCES = global i64 0
@EAGAIN = global i64 0
@.str.2 = [18 x i8] c"can't lock %s: %s\00"
@.str.3 = [4 x i8] c"%ld\00"
define i32 @already_running() {
%1 = alloca [16 x i8]
%2 = getelementptr inbounds [16 x i8], [16 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = load i32, i32* @S_IRUSR
%7 = load i32, i32* @S_IWUSR
%8 = or i32 %7, %6
%9 = load i32, i32* @S_IRGRP
%10 = or i32 %8, %9
%11 = load i32, i32* @S_IROTH
%12 = or i32 %10, %11
%13 = tail call i32 @open(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %0
%16 = load i32, i32* @stderr
%17 = load i64, i64* @errno
%18 = tail call i8* @strerror(i64 %17)
%19 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %0
%22 = tail call i64 @lockfile(i32 %13)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %38
24: ; preds = %21
%25 = load i64, i64* @errno
%26 = load i64, i64* @EACCES
%27 = icmp eq i64 %25, %26
%28 = load i64, i64* @EAGAIN
%29 = icmp eq i64 %25, %28
%30 = select i1 %27, i1 true, i1 %29
br i1 %30, label %31, label %33
31: ; preds = %24
%32 = tail call i32 @close(i32 %13)
br label %45
33: ; preds = %24
%34 = load i32, i32* @stderr
%35 = tail call i8* @strerror(i64 %25)
%36 = tail call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str, i64 0, i64 0), i8* %35)
%37 = tail call i32 @exit(i32 1)
unreachable
38: ; preds = %21
%39 = tail call i32 @ftruncate(i32 %13, i32 0)
%40 = tail call i64 (...) @getpid()
%41 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i64 %40)
%42 = call i64 @strlen(i8* %2)
%43 = add nsw i64 %42, 1
%44 = call i32 @write(i32 %13, i8* %2, i64 %43)
br label %45
45: ; preds = %38, %31
%46 = phi i32 [ 1, %31 ], [ 0, %38 ]
ret i32 %46
}