#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 fatal_get(char *, int);
int /*<<< orphan*/ fatal_lock(char *, int);
int /*<<< orphan*/ fatal_set(char *, int, int);
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/semaphore.c";
  if (count > 0)
    name = args[0];
  if (count != 3) {
    fprintf(stderr, "usage: %s <lock file> (+|-)\n", name);
    return EINVAL;
  }
  char op = args[2][0];
  if ((op != '+' && op != '-') || args[2][1] != '\0') {
    fprintf(stderr, "%s: expected '+' or '-', got '%s'\n", name, args[2]);
    return EINVAL;
  }
  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 value = fatal_get(name, fd);
  int ret = 1;
  if (op == '+') {
    if (value == 0)
      ret = 0;
    value++;
  } else if (op == '-') {
    value--;
    if (value < 0)
      value = 0;
    if (value == 0)
      ret = 0;
  }
  fatal_set(name, fd, value);
  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    0xee0(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1365 <main+0x205>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    (%rax),%al
mov    %al,-0x19(%rbp)
movsbl -0x19(%rbp),%eax
cmp    $0x2b,%eax
je     11f3 <main+0x93>
movsbl -0x19(%rbp),%eax
cmp    $0x2d,%eax
jne    1208 <main+0xa8>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
movsbl 0x1(%rax),%eax
cmp    $0x0,%eax
je     123c <main+0xdc>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rcx
lea    0xe8d(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2d7e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1365 <main+0x205>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dad(%rip),%rax
mov    (%rax),%esi
mov    0x2d8c(%rip),%rax
or     (%rax),%esi
mov    0x2d5b(%rip),%rax
mov    (%rax),%edx
mov    0x2d72(%rip),%rax
or     (%rax),%edx
call   1050 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    12c0 <main+0x160>
mov    0x2d47(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2d2b(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xe27(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1365 <main+0x205>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1080 <fatal_lock@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1060 <fatal_get@plt>
mov    %eax,-0x24(%rbp)
movl   $0x1,-0x28(%rbp)
movsbl -0x19(%rbp),%eax
cmp    $0x2b,%eax
jne    130e <main+0x1ae>
cmpl   $0x0,-0x24(%rbp)
jne    1300 <main+0x1a0>
movl   $0x0,-0x28(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1350 <main+0x1f0>
movsbl -0x19(%rbp),%eax
cmp    $0x2d,%eax
jne    134b <main+0x1eb>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1335 <main+0x1d5>
movl   $0x0,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    1346 <main+0x1e6>
movl   $0x0,-0x28(%rbp)
jmp    134b <main+0x1eb>
jmp    1350 <main+0x1f0>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    -0x24(%rbp),%edx
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    11a5 <main+0x45>
mov    (%rsi),%rbx
cmp    $0x3,%edi
jne    11ac <main+0x4c>
mov    0x10(%rsi),%rcx
mov    (%rcx),%bpl
cmp    $0x2d,%bpl
je     1183 <main+0x23>
cmp    $0x2b,%bpl
jne    1189 <main+0x29>
cmpb   $0x0,0x1(%rcx)
je     11d6 <main+0x76>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
lea    0xf18(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    11c6 <main+0x66>
lea    0xe54(%rip),%rbx
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
lea    0xed8(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x2de3(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x8(%rsi),%rdi
mov    0x2e17(%rip),%rax
mov    0x2df8(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dc5(%rip),%rax
mov    0x2dde(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     1236 <main+0xd6>
mov    %eax,%r14d
mov    %rbx,%rdi
mov    %eax,%esi
call   1080 <fatal_lock@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
call   1060 <fatal_get@plt>
cmp    $0x2d,%bpl
je     126d <main+0x10d>
cmp    $0x2b,%bpl
jne    1281 <main+0x121>
test   %eax,%eax
setne  %cl
add    $0x1,%eax
jmp    1283 <main+0x123>
mov    0x2d8b(%rip),%rax
mov    (%rax),%ebp
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe80(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x2,%ebp
jmp    11cf <main+0x6f>
test   %eax,%eax
mov    $0x1,%edx
cmovg  %eax,%edx
add    $0xffffffff,%edx
setne  %cl
mov    %edx,%eax
jmp    1283 <main+0x123>
mov    $0x1,%cl
movzbl %cl,%ebp
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %eax,%edx
call   1030 <fatal_set@plt>
jmp    11cf <main+0x6f>

LLVM IR (-O0):
@.str = [148 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/semaphore.c\00"
@stderr = global i32 0
@.str.1 = [29 x i8] c"usage: %s <lock file> (+|-)\0A\00"
@EINVAL = global i32 0
@.str.2 = [35 x i8] c"%s: expected '+' or '-', got '%s'\0A\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.3 = [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
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([148 x i8], [148 x i8]* @.str, i64 0, i64 0), i8** %6
%11 = load i32, i32* %4
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
store i8* %16, i8** %6
br label %17
17: ; preds = %13, %2
%18 = load i32, i32* %4
%19 = icmp ne i32 %18, 3
br i1 %19, label %20, label %25
20: ; preds = %17
%21 = load i32, i32* @stderr
%22 = load i8*, i8** %6
%23 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %22)
%24 = load i32, i32* @EINVAL
store i32 %24, i32* %3
br label %112
25: ; preds = %17
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 2
%28 = load i8*, i8** %27
%29 = getelementptr inbounds i8, i8* %28, i64 0
%30 = load i8, i8* %29
store i8 %30, i8* %7
%31 = load i8, i8* %7
%32 = sext i8 %31 to i32
%33 = icmp ne i32 %32, 43
br i1 %33, label %34, label %38
34: ; preds = %25
%35 = load i8, i8* %7
%36 = sext i8 %35 to i32
%37 = icmp ne i32 %36, 45
br i1 %37, label %46, label %38
38: ; preds = %34, %25
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 2
%41 = load i8*, i8** %40
%42 = getelementptr inbounds i8, i8* %41, i64 1
%43 = load i8, i8* %42
%44 = sext i8 %43 to i32
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %54
46: ; preds = %38, %34
%47 = load i32, i32* @stderr
%48 = load i8*, i8** %6
%49 = load i8**, i8*** %5
%50 = getelementptr inbounds i8*, i8** %49, i64 2
%51 = load i8*, i8** %50
%52 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* %48, i8* %51)
%53 = load i32, i32* @EINVAL
store i32 %53, i32* %3
br label %112
54: ; preds = %38
%55 = load i8**, i8*** %5
%56 = getelementptr inbounds i8*, i8** %55, i64 1
%57 = load i8*, i8** %56
%58 = load i32, i32* @O_CREAT
%59 = load i32, i32* @O_RDWR
%60 = or i32 %58, %59
%61 = load i32, i32* @S_IRUSR
%62 = load i32, i32* @S_IWUSR
%63 = or i32 %61, %62
%64 = call i32 @open(i8* %57, i32 %60, i32 %63)
store i32 %64, i32* %8
%65 = load i32, i32* %8
%66 = icmp eq i32 %65, -1
br i1 %66, label %67, label %73
67: ; preds = %54
%68 = load i32, i32* @stderr
%69 = load i8*, i8** %6
%70 = load i32, i32* @errno
%71 = call i8* @strerror(i32 %70)
%72 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %68, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %69, i8* %71)
store i32 2, i32* %3
br label %112
73: ; preds = %54
%74 = load i8*, i8** %6
%75 = load i32, i32* %8
%76 = call i32 @fatal_lock(i8* %74, i32 %75)
%77 = load i8*, i8** %6
%78 = load i32, i32* %8
%79 = call i32 @fatal_get(i8* %77, i32 %78)
store i32 %79, i32* %9
store i32 1, i32* %10
%80 = load i8, i8* %7
%81 = sext i8 %80 to i32
%82 = icmp eq i32 %81, 43
br i1 %82, label %83, label %90
83: ; preds = %73
%84 = load i32, i32* %9
%85 = icmp eq i32 %84, 0
br i1 %85, label %86, label %87
86: ; preds = %83
store i32 0, i32* %10
br label %87
87: ; preds = %86, %83
%88 = load i32, i32* %9
%89 = add nsw i32 %88, 1
store i32 %89, i32* %9
br label %106
90: ; preds = %73
%91 = load i8, i8* %7
%92 = sext i8 %91 to i32
%93 = icmp eq i32 %92, 45
br i1 %93, label %94, label %105
94: ; preds = %90
%95 = load i32, i32* %9
%96 = add nsw i32 %95, -1
store i32 %96, i32* %9
%97 = load i32, i32* %9
%98 = icmp slt i32 %97, 0
br i1 %98, label %99, label %100
99: ; preds = %94
store i32 0, i32* %9
br label %100
100: ; preds = %99, %94
%101 = load i32, i32* %9
%102 = icmp eq i32 %101, 0
br i1 %102, label %103, label %104
103: ; preds = %100
store i32 0, i32* %10
br label %104
104: ; preds = %103, %100
br label %105
105: ; preds = %104, %90
br label %106
106: ; preds = %105, %87
%107 = load i8*, i8** %6
%108 = load i32, i32* %8
%109 = load i32, i32* %9
%110 = call i32 @fatal_set(i8* %107, i32 %108, i32 %109)
%111 = load i32, i32* %10
store i32 %111, i32* %3
br label %112
112: ; preds = %106, %67, %46, %20
%113 = load i32, i32* %3
ret i32 %113
}

LLVM IR (-O2):
@.str = [148 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/semaphore.c\00"
@stderr = global i32 0
@.str.1 = [29 x i8] c"usage: %s <lock file> (+|-)\0A\00"
@EINVAL = global i32 0
@.str.2 = [35 x i8] c"%s: expected '+' or '-', got '%s'\0A\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.3 = [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 ([148 x i8], [148 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 ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %8)
%11 = load i32, i32* @EINVAL
br label %56
12: ; preds = %4
%13 = getelementptr inbounds i8*, i8** %1, i64 2
%14 = load i8*, i8** %13
%15 = load i8, i8* %14
switch i8 %15, label %20 [
i8 45, label %16
i8 43, label %16
]
16: ; preds = %12, %12
%17 = getelementptr inbounds i8, i8* %14, i64 1
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 0
br i1 %19, label %24, label %20
20: ; preds = %12, %16
%21 = load i32, i32* @stderr
%22 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0), i8* %5, i8* %14)
%23 = load i32, i32* @EINVAL
br label %56
24: ; preds = %16
%25 = getelementptr inbounds i8*, i8** %1, i64 1
%26 = load i8*, i8** %25
%27 = load i32, i32* @O_CREAT
%28 = load i32, i32* @O_RDWR
%29 = or i32 %28, %27
%30 = load i32, i32* @S_IRUSR
%31 = load i32, i32* @S_IWUSR
%32 = or i32 %31, %30
%33 = tail call i32 @open(i8* %26, i32 %29, i32 %32)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %40
35: ; preds = %24
%36 = load i32, i32* @stderr
%37 = load i32, i32* @errno
%38 = tail call i8* @strerror(i32 %37)
%39 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i8* %5, i8* %38)
br label %56
40: ; preds = %24
%41 = tail call i32 @fatal_lock(i8* %5, i32 %33)
%42 = tail call i32 @fatal_get(i8* %5, i32 %33)
switch i8 %15, label %51 [
i8 43, label %43
i8 45, label %46
]
43: ; preds = %40
%44 = icmp ne i32 %42, 0
%45 = add nsw i32 %42, 1
br label %51
46: ; preds = %40
%47 = icmp sgt i32 %42, 1
%48 = select i1 %47, i32 %42, i32 1
%49 = add nsw i32 %48, -1
%50 = icmp ne i32 %49, 0
br label %51
51: ; preds = %46, %40, %43
%52 = phi i32 [ %45, %43 ], [ %42, %40 ], [ %49, %46 ]
%53 = phi i1 [ %44, %43 ], [ true, %40 ], [ %50, %46 ]
%54 = zext i1 %53 to i32
%55 = tail call i32 @fatal_set(i8* %5, i32 %33, i32 %52)
br label %56
56: ; preds = %20, %51, %35, %7
%57 = phi i32 [ %11, %7 ], [ %23, %20 ], [ 2, %35 ], [ %54, %51 ]
ret i32 %57
}