#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 EINVAL;
int O_CREAT;
int O_RDWR;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fatal_lock(char *, int);
int /*<<< orphan*/ fatal_set(char *, int, char *);
int fd_compare(char *, int, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int open(char *, int, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int main(int count, char **args) {
  char *name = "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
               "outputs/2022-01-22/02-19-57/repos/hobbitalastair/backhand/refs/"
               "heads/master/src/state.c";
  if (count > 0)
    name = args[0];
  if (count != 3) {
    fprintf(stderr, "usage: %s <lock file> <state>\n", name);
    return EINVAL;
  }
  char *op = args[2];
  int fd = open(args[1], O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
  if (fd == -1) {
    fprintf(stderr, "%s: open failed: %s\n", name, strerror(errno));
    return 2;
  }
  fatal_lock(name, fd);
  int ret = fd_compare(name, fd, op);
  if (ret == 0) {
    fatal_set(name, fd, op);
  }
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe83(%rip),%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    1196 <main+0x36>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11cc <main+0x6c>
mov    0x2e21(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xedc(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    129b <main+0x13b>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
mov    0x2df0(%rip),%rax
or     (%rax),%esi
mov    0x2dbf(%rip),%rax
mov    (%rax),%edx
mov    0x2dd6(%rip),%rax
or     (%rax),%edx
call   1050 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    125c <main+0xfc>
mov    0x2dab(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xe66(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    129b <main+0x13b>
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1070 <fatal_lock@plt>
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1080 <fd_compare@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    1295 <main+0x135>
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1030 <fatal_set@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
test   %edi,%edi
jle    11d4 <main+0x74>
mov    (%rsi),%rbx
cmp    $0x3,%edi
jne    11db <main+0x7b>
mov    0x8(%rsi),%rdi
mov    0x10(%rsi),%r14
mov    0x2e79(%rip),%rax
mov    0x2e5a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e27(%rip),%rax
mov    0x2e40(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     1203 <main+0xa3>
mov    %eax,%ebp
mov    %rbx,%rdi
mov    %eax,%esi
call   1070 <fatal_lock@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r14,%rdx
call   1080 <fd_compare@plt>
test   %eax,%eax
jne    11fe <main+0x9e>
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %r14,%rdx
call   1030 <fatal_set@plt>
xor    %eax,%eax
jmp    11fe <main+0x9e>
lea    0xe25(%rip),%rbx
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
lea    0xea5(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x2db4(%rip),%rax
mov    (%rax),%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2dbe(%rip),%rax
mov    (%rax),%ebp
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
lea    0xe8e(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x2,%eax
jmp    11fe <main+0x9e>

LLVM IR (-O0):
@.str = [144 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/hobbitalastair/backhand/refs/heads/master/src/state.c\00"
@stderr = global i32 0
@.str.1 = [31 x i8] c"usage: %s <lock file> <state>\0A\00"
@EINVAL = global i32 0
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.2 = [21 x i8] c"%s: open failed: %s\0A\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([144 x i8], [144 x i8]* @.str, i64 0, i64 0), i8** %6
%10 = load i32, i32* %4
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
store i8* %15, i8** %6
br label %16
16: ; preds = %12, %2
%17 = load i32, i32* %4
%18 = icmp ne i32 %17, 3
br i1 %18, label %19, label %24
19: ; preds = %16
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %6
%22 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%23 = load i32, i32* @EINVAL
store i32 %23, i32* %3
br label %63
24: ; preds = %16
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 2
%27 = load i8*, i8** %26
store i8* %27, i8** %7
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = load i32, i32* @O_CREAT
%32 = load i32, i32* @O_RDWR
%33 = or i32 %31, %32
%34 = load i32, i32* @S_IRUSR
%35 = load i32, i32* @S_IWUSR
%36 = or i32 %34, %35
%37 = call i32 @open(i8* %30, i32 %33, i32 %36)
store i32 %37, i32* %8
%38 = load i32, i32* %8
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %46
40: ; preds = %24
%41 = load i32, i32* @stderr
%42 = load i8*, i8** %6
%43 = load i32, i32* @errno
%44 = call i8* @strerror(i32 %43)
%45 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %42, i8* %44)
store i32 2, i32* %3
br label %63
46: ; preds = %24
%47 = load i8*, i8** %6
%48 = load i32, i32* %8
%49 = call i32 @fatal_lock(i8* %47, i32 %48)
%50 = load i8*, i8** %6
%51 = load i32, i32* %8
%52 = load i8*, i8** %7
%53 = call i32 @fd_compare(i8* %50, i32 %51, i8* %52)
store i32 %53, i32* %9
%54 = load i32, i32* %9
%55 = icmp eq i32 %54, 0
br i1 %55, label %56, label %61
56: ; preds = %46
%57 = load i8*, i8** %6
%58 = load i32, i32* %8
%59 = load i8*, i8** %7
%60 = call i32 @fatal_set(i8* %57, i32 %58, i8* %59)
br label %61
61: ; preds = %56, %46
%62 = load i32, i32* %9
store i32 %62, i32* %3
br label %63
63: ; preds = %61, %40, %19
%64 = load i32, i32* %3
ret i32 %64
}

LLVM IR (-O2):
@.str = [144 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/hobbitalastair/backhand/refs/heads/master/src/state.c\00"
@stderr = global i32 0
@.str.1 = [31 x i8] c"usage: %s <lock file> <state>\0A\00"
@EINVAL = global i32 0
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.2 = [21 x i8] c"%s: open failed: %s\0A\00"
@errno = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp sgt i32 %0, 0
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = icmp eq i32 %0, 3
br i1 %6, label %12, label %7
7: ; preds = %2, %4
%8 = phi i8* [ %5, %4 ], [ getelementptr inbounds ([144 x i8], [144 x i8]* @.str, i64 0, i64 0), %2 ]
%9 = load i32, i32* @stderr
%10 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %8)
%11 = load i32, i32* @EINVAL
br label %36
12: ; preds = %4
%13 = getelementptr inbounds i8*, i8** %1, i64 2
%14 = load i8*, i8** %13
%15 = getelementptr inbounds i8*, i8** %1, i64 1
%16 = load i8*, i8** %15
%17 = load i32, i32* @O_CREAT
%18 = load i32, i32* @O_RDWR
%19 = or i32 %18, %17
%20 = load i32, i32* @S_IRUSR
%21 = load i32, i32* @S_IWUSR
%22 = or i32 %21, %20
%23 = tail call i32 @open(i8* %16, i32 %19, i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %30
25: ; preds = %12
%26 = load i32, i32* @stderr
%27 = load i32, i32* @errno
%28 = tail call i8* @strerror(i32 %27)
%29 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %5, i8* %28)
br label %36
30: ; preds = %12
%31 = tail call i32 @fatal_lock(i8* %5, i32 %23)
%32 = tail call i32 @fd_compare(i8* %5, i32 %23, i8* %14)
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = tail call i32 @fatal_set(i8* %5, i32 %23, i8* %14)
br label %36
36: ; preds = %25, %34, %30, %7
%37 = phi i32 [ %11, %7 ], [ 2, %25 ], [ 0, %34 ], [ %32, %30 ]
ret i32 %37
}