#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 EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ F_LOCK;
int /*<<< orphan*/ F_ULOCK;
int /*<<< orphan*/ O_RDONLY;
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ lockf(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  char *file_name = NULL;
  int seconds = 10;
  if (argc <= 1) {
    fprintf(stderr, "USAGE: %s [file] {[sec]}\n", argv[0]);
    return EXIT_FAILURE;
  } else {
    file_name = argv[1];
    if (argc == 3) {
      seconds = atoi(argv[2]);
    } else if (argc != 2) {
      fprintf(stderr, "ERROR: unknown option %s\n", argv[3]);
      return EXIT_FAILURE;
    }
  }
  int fd = open(file_name, O_RDONLY);
  if (fd == -1) {
    fprintf(stderr, "ERROR: fail to open file %s\n", file_name);
    return EXIT_FAILURE;
  }
  if (lockf(fd, F_LOCK, 0) < 0) {
    fprintf(stderr, "ERROR: fail to lock file %s\n", file_name);
    close(fd);
    return EXIT_FAILURE;
  }
  sleep(seconds);
  lockf(fd, F_ULOCK, 0);
  close(fd);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0xa,-0x1c(%rbp)
cmpl   $0x1,-0x8(%rbp)
jg     11bc <main+0x5c>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe5a(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12f0 <main+0x190>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x3,-0x8(%rbp)
jne    11e7 <main+0x87>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x1c(%rbp)
jmp    1224 <main+0xc4>
cmpl   $0x2,-0x8(%rbp)
je     121f <main+0xbf>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdx
lea    0xe11(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2dab(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12f0 <main+0x190>
jmp    1224 <main+0xc4>
jmp    1229 <main+0xc9>
mov    -0x18(%rbp),%rdi
mov    0x2db4(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1272 <main+0x112>
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xdd8(%rip),%rsi
call   1050 <fprintf@plt>
mov    0x2d58(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12f0 <main+0x190>
mov    -0x20(%rbp),%edi
mov    0x2d5c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <lockf@plt>
cmp    $0x0,%rax
jge    12c1 <main+0x161>
mov    0x2d32(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xdae(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    0x2d09(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12f0 <main+0x190>
mov    -0x1c(%rbp),%edi
call   1080 <sleep@plt>
mov    -0x20(%rbp),%edi
mov    0x2d0d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <lockf@plt>
mov    -0x20(%rbp),%edi
call   1040 <close@plt>
mov    0x2ccd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
cmp    $0x1,%edi
jg     1181 <main+0x21>
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xe84(%rip),%rsi
jmp    1225 <main+0xc5>
mov    0x8(%rsi),%rbx
cmp    $0x2,%edi
je     119d <main+0x3d>
cmp    $0x3,%edi
jne    11fc <main+0x9c>
mov    0x10(%rsi),%rdi
call   1070 <atoi@plt>
mov    %eax,%r14d
jmp    11a3 <main+0x43>
mov    $0xa,%r14d
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     1212 <main+0xb2>
mov    %eax,%ebp
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1030 <lockf@plt>
test   %rax,%rax
js     1238 <main+0xd8>
mov    %r14d,%edi
call   1080 <sleep@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1030 <lockf@plt>
mov    %ebp,%edi
call   1040 <close@plt>
mov    0x2dbe(%rip),%rax
jmp    1231 <main+0xd1>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
mov    0x18(%rsi),%rdx
lea    0xe0a(%rip),%rsi
jmp    1225 <main+0xc5>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
lea    0xe12(%rip),%rsi
mov    %rbx,%rdx
call   1050 <fprintf@plt>
mov    0x2d8f(%rip),%rax
mov    (%rax),%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
lea    0xe09(%rip),%rsi
mov    %rbx,%rdx
call   1050 <fprintf@plt>
mov    %ebp,%edi
call   1040 <close@plt>
jmp    122a <main+0xca>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [26 x i8] c"USAGE: %s [file] {[sec]}\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [26 x i8] c"ERROR: unknown option %s\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [29 x i8] c"ERROR: fail to open file %s\0A\00"
@F_LOCK = global i32 0
@.str.3 = [29 x i8] c"ERROR: fail to lock file %s\0A\00"
@F_ULOCK = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %6
store i32 10, i32* %7
%9 = load i32, i32* %4
%10 = icmp sle i32 %9, 1
br i1 %10, label %11, label %18
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = load i32, i32* @EXIT_FAILURE
store i32 %17, i32* %3
br label %73
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
store i8* %21, i8** %6
%22 = load i32, i32* %4
%23 = icmp eq i32 %22, 3
br i1 %23, label %24, label %29
24: ; preds = %18
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 2
%27 = load i8*, i8** %26
%28 = call i32 @atoi(i8* %27)
store i32 %28, i32* %7
br label %40
29: ; preds = %18
%30 = load i32, i32* %4
%31 = icmp ne i32 %30, 2
br i1 %31, label %32, label %39
32: ; preds = %29
%33 = load i32, i32* @stderr
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 3
%36 = load i8*, i8** %35
%37 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %36)
%38 = load i32, i32* @EXIT_FAILURE
store i32 %38, i32* %3
br label %73
39: ; preds = %29
br label %40
40: ; preds = %39, %24
br label %41
41: ; preds = %40
%42 = load i8*, i8** %6
%43 = load i32, i32* @O_RDONLY
%44 = call i32 @open(i8* %42, i32 %43)
store i32 %44, i32* %8
%45 = load i32, i32* %8
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %52
47: ; preds = %41
%48 = load i32, i32* @stderr
%49 = load i8*, i8** %6
%50 = call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %49)
%51 = load i32, i32* @EXIT_FAILURE
store i32 %51, i32* %3
br label %73
52: ; preds = %41
%53 = load i32, i32* %8
%54 = load i32, i32* @F_LOCK
%55 = call i64 @lockf(i32 %53, i32 %54, i32 0)
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %64
57: ; preds = %52
%58 = load i32, i32* @stderr
%59 = load i8*, i8** %6
%60 = call i32 @fprintf(i32 %58, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %59)
%61 = load i32, i32* %8
%62 = call i32 @close(i32 %61)
%63 = load i32, i32* @EXIT_FAILURE
store i32 %63, i32* %3
br label %73
64: ; preds = %52
%65 = load i32, i32* %7
%66 = call i32 @sleep(i32 %65)
%67 = load i32, i32* %8
%68 = load i32, i32* @F_ULOCK
%69 = call i64 @lockf(i32 %67, i32 %68, i32 0)
%70 = load i32, i32* %8
%71 = call i32 @close(i32 %70)
%72 = load i32, i32* @EXIT_SUCCESS
store i32 %72, i32* %3
br label %73
73: ; preds = %64, %57, %47, %32, %11
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [26 x i8] c"USAGE: %s [file] {[sec]}\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [26 x i8] c"ERROR: unknown option %s\0A\00"
@O_RDONLY = global i32 0
@.str.2 = [29 x i8] c"ERROR: fail to open file %s\0A\00"
@F_LOCK = global i32 0
@.str.3 = [29 x i8] c"ERROR: fail to lock file %s\0A\00"
@F_ULOCK = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %8
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %41
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
switch i32 %0, label %15 [
i32 3, label %11
i32 2, label %20
]
11: ; preds = %8
%12 = getelementptr inbounds i8*, i8** %1, i64 2
%13 = load i8*, i8** %12
%14 = tail call i32 @atoi(i8* %13)
br label %20
15: ; preds = %8
%16 = load i32, i32* @stderr
%17 = getelementptr inbounds i8*, i8** %1, i64 3
%18 = load i8*, i8** %17
%19 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %18)
br label %41
20: ; preds = %8, %11
%21 = phi i32 [ %14, %11 ], [ 10, %8 ]
%22 = load i32, i32* @O_RDONLY
%23 = tail call i32 @open(i8* %10, i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = load i32, i32* @stderr
%27 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0), i8* %10)
br label %41
28: ; preds = %20
%29 = load i32, i32* @F_LOCK
%30 = tail call i64 @lockf(i32 %23, i32 %29, i32 0)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %28
%33 = load i32, i32* @stderr
%34 = tail call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %10)
%35 = tail call i32 @close(i32 %23)
br label %41
36: ; preds = %28
%37 = tail call i32 @sleep(i32 %21)
%38 = load i32, i32* @F_ULOCK
%39 = tail call i64 @lockf(i32 %23, i32 %38, i32 0)
%40 = tail call i32 @close(i32 %23)
br label %41
41: ; preds = %25, %32, %36, %15, %4
%42 = phi i32* [ @EXIT_FAILURE, %4 ], [ @EXIT_FAILURE, %15 ], [ @EXIT_FAILURE, %25 ], [ @EXIT_FAILURE, %32 ], [ @EXIT_SUCCESS, %36 ]
%43 = load i32, i32* %42
ret i32 %43
}