#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 */
int O_CREAT;
int O_RDWR;
int S_IRWXG;
int S_IRWXU;
int /*<<< orphan*/ close(int);
int errno;
scalar_t__ lockfile(int);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, int, char *);
int /*<<< orphan*/ sleep(int);
char *strerror(int);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int fd;
  if ((fd = open("temp.foo", O_RDWR | O_CREAT, S_IRWXU | S_IRWXG)) < 0) {
    printf("open failed.[%d][%s]\n", errno, strerror(errno));
    return -1;
  }
  if (lockfile(fd) < 0) {
    printf("lockfile failed[%d][%s]\n", errno, strerror(errno));
    close(fd);
    return -1;
  }
  write(fd, "ww", 3);
  sleep(100);
  if (lockfile(fd) < 0) {
    printf("lockfile failed[%d][%s]\n", errno, strerror(errno));
    close(fd);
    return -1;
  }
  close(fd);
  return -1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
mov    0x2e69(%rip),%rax
or     (%rax),%esi
mov    0x2e48(%rip),%rax
mov    (%rax),%edx
mov    0x2e2f(%rip),%rax
or     (%rax),%edx
lea    0xe56(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11f3 <main+0x83>
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0xc(%rbp),%esi
mov    %rax,%rdx
lea    0xe27(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c4 <main+0x154>
mov    -0x8(%rbp),%edi
call   1060 <lockfile@plt>
cmp    $0x0,%rax
jge    1245 <main+0xd5>
mov    0x2db4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x10(%rbp),%esi
mov    %rax,%rdx
lea    0xdf3(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c4 <main+0x154>
mov    -0x8(%rbp),%edi
lea    0xde9(%rip),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
mov    $0x64,%edi
call   1090 <sleep@plt>
mov    -0x8(%rbp),%edi
call   1060 <lockfile@plt>
cmp    $0x0,%rax
jge    12b5 <main+0x145>
mov    0x2d44(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x14(%rbp),%esi
mov    %rax,%rdx
lea    0xd83(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c4 <main+0x154>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e5e(%rip),%rax
mov    0x2e77(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e54(%rip),%rax
mov    0x2e3d(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe62(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     1208 <main+0x98>
mov    %eax,%ebx
mov    %eax,%edi
call   1060 <lockfile@plt>
test   %rax,%rax
js     11de <main+0x6e>
lea    0xe7c(%rip),%rsi
mov    %ebx,%edi
mov    $0x3,%edx
call   1030 <write@plt>
mov    $0x64,%edi
call   1090 <sleep@plt>
mov    %ebx,%edi
call   1060 <lockfile@plt>
test   %rax,%rax
jns    11ff <main+0x8f>
mov    0x2ddb(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   1080 <strerror@plt>
lea    0xe2a(%rip),%rdi
mov    %ebp,%esi
mov    %rax,%rdx
call   1040 <printf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    1229 <main+0xb9>
mov    0x2db1(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1080 <strerror@plt>
lea    0xdea(%rip),%rdi
mov    %ebx,%esi
mov    %rax,%rdx
call   1040 <printf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"temp.foo\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@.str.1 = [22 x i8] c"open failed.[%d][%s]\0A\00"
@errno = global i32 0
@.str.2 = [25 x i8] c"lockfile failed[%d][%s]\0A\00"
@.str.3 = [3 x i8] c"ww\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_CREAT
%5 = or i32 %3, %4
%6 = load i32, i32* @S_IRWXU
%7 = load i32, i32* @S_IRWXG
%8 = or i32 %6, %7
%9 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %8)
store i32 %9, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %0
%12 = load i32, i32* @errno
%13 = load i32, i32* @errno
%14 = call i8* @strerror(i32 %13)
%15 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %12, i8* %14)
store i32 -1, i32* %1
br label %44
16: ; preds = %0
%17 = load i32, i32* %2
%18 = call i64 @lockfile(i32 %17)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %27
20: ; preds = %16
%21 = load i32, i32* @errno
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %21, i8* %23)
%25 = load i32, i32* %2
%26 = call i32 @close(i32 %25)
store i32 -1, i32* %1
br label %44
27: ; preds = %16
%28 = load i32, i32* %2
%29 = call i32 @write(i32 %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 3)
%30 = call i32 @sleep(i32 100)
%31 = load i32, i32* %2
%32 = call i64 @lockfile(i32 %31)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %41
34: ; preds = %27
%35 = load i32, i32* @errno
%36 = load i32, i32* @errno
%37 = call i8* @strerror(i32 %36)
%38 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %35, i8* %37)
%39 = load i32, i32* %2
%40 = call i32 @close(i32 %39)
store i32 -1, i32* %1
br label %44
41: ; preds = %27
%42 = load i32, i32* %2
%43 = call i32 @close(i32 %42)
store i32 -1, i32* %1
br label %44
44: ; preds = %41, %34, %20, %11
%45 = load i32, i32* %1
ret i32 %45
}

LLVM IR (-O2):
@.str = [9 x i8] c"temp.foo\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@.str.1 = [22 x i8] c"open failed.[%d][%s]\0A\00"
@errno = global i32 0
@.str.2 = [25 x i8] c"lockfile failed[%d][%s]\0A\00"
@.str.3 = [3 x i8] c"ww\00"
define i32 @main() {
%1 = load i32, i32* @O_RDWR
%2 = load i32, i32* @O_CREAT
%3 = or i32 %2, %1
%4 = load i32, i32* @S_IRWXU
%5 = load i32, i32* @S_IRWXG
%6 = or i32 %5, %4
%7 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %3, i32 %6)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %0
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %10, i8* %11)
br label %33
13: ; preds = %0
%14 = tail call i64 @lockfile(i32 %7)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %13
%17 = load i32, i32* @errno
%18 = tail call i8* @strerror(i32 %17)
%19 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %17, i8* %18)
%20 = tail call i32 @close(i32 %7)
br label %33
21: ; preds = %13
%22 = tail call i32 @write(i32 %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 3)
%23 = tail call i32 @sleep(i32 100)
%24 = tail call i64 @lockfile(i32 %7)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %31
26: ; preds = %21
%27 = load i32, i32* @errno
%28 = tail call i8* @strerror(i32 %27)
%29 = tail call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %27, i8* %28)
%30 = tail call i32 @close(i32 %7)
br label %33
31: ; preds = %21
%32 = tail call i32 @close(i32 %7)
br label %33
33: ; preds = %31, %26, %16, %9
ret i32 -1
}