#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 */
typedef int key_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int getpid();
int /*<<< orphan*/ printf(char *, int, ...);
int semaphore(int);
int /*<<< orphan*/ semaphore_signal(int);
int /*<<< orphan*/ semaphore_wait(int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int main(void) {
  key_t key = 54331;
  int semid, pid = getpid();
  if ((semid = semaphore(key)) < 0) {
    fprintf(stderr, "Error: %s failed.\n",
            "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
            "2022-01-22/02-19-57/repos/timhriley/appaserver/refs/heads/master/"
            "library/semaphore_dvr.c");
    exit(1);
  }
  printf("Process %d with semid = %d before critical section.\n", pid, semid);
  semaphore_wait(semid);
  printf("Process %d inside critical section.\n", pid);
  sleep(5);
  semaphore_signal(semid);
  printf("Process %d outside critical section.\n", pid);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xd43b,-0x8(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   1070 <semaphore@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    11da <main+0x5a>
mov    0x2e25(%rip),%rax
mov    (%rax),%edi
lea    0xe3c(%rip),%rsi
lea    0xe48(%rip),%rdx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xec5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
call   1060 <semaphore_wait@plt>
mov    -0x10(%rbp),%esi
lea    0xee1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x5,%edi
call   1090 <sleep@plt>
mov    -0xc(%rbp),%edi
call   10a0 <semaphore_signal@plt>
mov    -0x10(%rbp),%esi
lea    0xee3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%ebx
mov    $0xd43b,%edi
call   1070 <semaphore@plt>
test   %eax,%eax
js     11ef <main+0x6f>
mov    %eax,%ebp
lea    0xf09(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%edi
call   1060 <semaphore_wait@plt>
lea    0xf25(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x5,%edi
call   1090 <sleep@plt>
mov    %ebp,%edi
call   10a0 <semaphore_signal@plt>
lea    0xf29(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
lea    0xe01(%rip),%rsi
lea    0xe0d(%rip),%rdx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [19 x i8] c"Error: %s failed.\0A\00"
@.str.1 = [153 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/timhriley/appaserver/refs/heads/master/library/semaphore_dvr.c\00"
@.str.2 = [53 x i8] c"Process %d with semid = %d before critical section.\0A\00"
@.str.3 = [37 x i8] c"Process %d inside critical section.\0A\00"
@.str.4 = [38 x i8] c"Process %d outside critical section.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
store i32 54331, i32* %2
%5 = call i32 (...) @getpid()
store i32 %5, i32* %4
%6 = load i32, i32* %2
%7 = call i32 @semaphore(i32 %6)
store i32 %7, i32* %3
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %0
%10 = load i32, i32* @stderr
%11 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([153 x i8], [153 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %0
%14 = load i32, i32* %4
%15 = load i32, i32* %3
%16 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0), i32 %14, i32 %15)
%17 = load i32, i32* %3
%18 = call i32 @semaphore_wait(i32 %17)
%19 = load i32, i32* %4
%20 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i32 %19)
%21 = call i32 @sleep(i32 5)
%22 = load i32, i32* %3
%23 = call i32 @semaphore_signal(i32 %22)
%24 = load i32, i32* %4
%25 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i32 %24)
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [19 x i8] c"Error: %s failed.\0A\00"
@.str.1 = [153 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/timhriley/appaserver/refs/heads/master/library/semaphore_dvr.c\00"
@.str.2 = [53 x i8] c"Process %d with semid = %d before critical section.\0A\00"
@.str.3 = [37 x i8] c"Process %d inside critical section.\0A\00"
@.str.4 = [38 x i8] c"Process %d outside critical section.\0A\00"
define i32 @main() {
%1 = tail call i32 (...) @getpid()
%2 = tail call i32 @semaphore(i32 54331)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %8
4: ; preds = %0
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([153 x i8], [153 x i8]* @.str.1, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %0
%9 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.2, i64 0, i64 0), i32 %1, i32 %2)
%10 = tail call i32 @semaphore_wait(i32 %2)
%11 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.3, i64 0, i64 0), i32 %1)
%12 = tail call i32 @sleep(i32 5)
%13 = tail call i32 @semaphore_signal(i32 %2)
%14 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0), i32 %1)
ret i32 0
}