#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__ EAGAIN;
int /*<<< orphan*/ EXIT_FAILURE;
int LOCK_EX;
int LOCK_NB;
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ flock(int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
scalar_t__ strtol(char *, char **, int);
int main(int argc, char **argv) {
  int fd;
  if (argc != 2) {
    fprintf(stderr, "Usage: %s [fd]\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  fd = (int)strtol(argv[1], (char **)NULL, 10);
  errno = 0;
  if (errno != 0) {
    perror("strtol");
    exit(EXIT_FAILURE);
  }
  if (flock(fd, LOCK_EX | LOCK_NB)) {
    if (errno != EAGAIN) {
      perror("flock");
    }
    exit(EXIT_FAILURE);
  }
  return 0;
}


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)
cmpl   $0x2,-0x8(%rbp)
je     119a <main+0x4a>
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe79(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e16(%rip),%rax
movq   $0x0,(%rax)
mov    0x2e08(%rip),%rax
cmpq   $0x0,(%rax)
je     11ec <main+0x9c>
lea    0xe37(%rip),%rdi
call   1050 <perror@plt>
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
mov    0x2db1(%rip),%rax
or     (%rax),%esi
call   1060 <flock@plt>
cmp    $0x0,%rax
je     1244 <main+0xf4>
mov    0x2db9(%rip),%rax
mov    (%rax),%rax
mov    0x2d97(%rip),%rcx
cmp    (%rcx),%rax
je     1236 <main+0xe6>
lea    0xde6(%rip),%rdi
call   1050 <perror@plt>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
cmp    $0x2,%edi
jne    1196 <main+0x46>
mov    0x8(%rsi),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    0x2e63(%rip),%rbx
movq   $0x0,(%rbx)
mov    0x2e6d(%rip),%rcx
mov    0x2e2e(%rip),%rdx
mov    (%rdx),%esi
or     (%rcx),%esi
mov    %eax,%edi
call   1060 <flock@plt>
test   %rax,%rax
jne    11b0 <main+0x60>
xor    %eax,%eax
pop    %rbx
ret
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xe57(%rip),%rsi
call   1030 <fprintf@plt>
jmp    11cb <main+0x7b>
mov    (%rbx),%rax
mov    0x2dfe(%rip),%rcx
cmp    (%rcx),%rax
je     11cb <main+0x7b>
lea    0xe4a(%rip),%rdi
call   1050 <perror@plt>
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [16 x i8] c"Usage: %s [fd]\0A\00"
@EXIT_FAILURE = global i32 0
@errno = global i64 0
@.str.1 = [7 x i8] c"strtol\00"
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@EAGAIN = global i64 0
@.str.2 = [6 x i8] c"flock\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = load i32, i32* %4
%8 = icmp ne i32 %7, 2
br i1 %8, label %9, label %17
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 0
%13 = load i8*, i8** %12
%14 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i64 @strtol(i8* %20, i8** null, i32 10)
%22 = trunc i64 %21 to i32
store i32 %22, i32* %6
store i64 0, i64* @errno
%23 = load i64, i64* @errno
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %17
%26 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %17
%30 = load i32, i32* %6
%31 = load i32, i32* @LOCK_EX
%32 = load i32, i32* @LOCK_NB
%33 = or i32 %31, %32
%34 = call i64 @flock(i32 %30, i32 %33)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %45
36: ; preds = %29
%37 = load i64, i64* @errno
%38 = load i64, i64* @EAGAIN
%39 = icmp ne i64 %37, %38
br i1 %39, label %40, label %42
40: ; preds = %36
%41 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %42
42: ; preds = %40, %36
%43 = load i32, i32* @EXIT_FAILURE
%44 = call i32 @exit(i32 %43)
unreachable
45: ; preds = %29
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [16 x i8] c"Usage: %s [fd]\0A\00"
@EXIT_FAILURE = global i32 0
@errno = global i64 0
@LOCK_EX = global i32 0
@LOCK_NB = global i32 0
@EAGAIN = global i64 0
@.str.2 = [6 x i8] c"flock\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %10, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = tail call i64 @strtol(i8* nocapture %12, i8** null, i32 10)
store i64 0, i64* @errno
%14 = trunc i64 %13 to i32
%15 = load i32, i32* @LOCK_EX
%16 = load i32, i32* @LOCK_NB
%17 = or i32 %16, %15
%18 = tail call i64 @flock(i32 %14, i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %29, label %20
20: ; preds = %10
%21 = load i64, i64* @errno
%22 = load i64, i64* @EAGAIN
%23 = icmp eq i64 %21, %22
br i1 %23, label %26, label %24
24: ; preds = %20
%25 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %26
26: ; preds = %24, %20
%27 = load i32, i32* @EXIT_FAILURE
%28 = tail call i32 @exit(i32 %27)
unreachable
29: ; preds = %10
ret i32 0
}