#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 */
struct sembuf {
  int dummy;
};

/* Variables and functions */
int errno;
int semop(int, struct sembuf *, int);
int /*<<< orphan*/ strerror(int);
int /*<<< orphan*/ test_msg(char *, char *, int, int, int /*<<< orphan*/);
int sem_test(int id, struct sembuf *lock, struct sembuf *unlock, int lock_ops,
             int unlock_ops) {
  if (semop(id, lock, lock_ops) == -1) {
    test_msg("FAIL: %s:%d: "
             "Failed to lock semaphore"
             " (errno = %d (%s))\n",
             "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
             "2022-01-22/02-19-57/repos/fbocharov/criu/refs/heads/master/test/"
             "zdtm/live/static/sem.c",
             25, errno, strerror(errno));
    return -errno;
  }
  if (semop(id, unlock, unlock_ops) == -1) {
    test_msg("FAIL: %s:%d: "
             "Failed to unlock semaphore"
             " (errno = %d (%s))\n",
             "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
             "2022-01-22/02-19-57/repos/fbocharov/criu/refs/heads/master/test/"
             "zdtm/live/static/sem.c",
             29, errno, strerror(errno));
    return -errno;
  }
  return 0;
}


x86 assembly (clang, -O0):
<sem_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <semop@plt>
cmp    $0xffffffff,%eax
jne    11ad <sem_test+0x7d>
mov    0x2e77(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    0x2e6b(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x24(%rbp),%ecx
mov    %eax,%r8d
lea    0xe77(%rip),%rdi
lea    0xea9(%rip),%rsi
mov    $0x19,%edx
call   1030 <test_msg@plt>
xor    %eax,%eax
mov    0x2e3d(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1217 <sem_test+0xe7>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1040 <semop@plt>
cmp    $0xffffffff,%eax
jne    1210 <sem_test+0xe0>
mov    0x2e14(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
call   1050 <strerror@plt>
mov    -0x28(%rbp),%ecx
mov    %eax,%r8d
lea    0xee4(%rip),%rdi
lea    0xe46(%rip),%rsi
mov    $0x1d,%edx
call   1030 <test_msg@plt>
xor    %eax,%eax
mov    0x2dda(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1217 <sem_test+0xe7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sem_test>:
push   %rbp
push   %r14
push   %rbx
mov    %r8d,%r14d
mov    %rdx,%rbx
mov    %edi,%ebp
mov    %ecx,%edx
call   1040 <semop@plt>
cmp    $0xffffffff,%eax
je     1184 <sem_test+0x54>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1040 <semop@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
jne    11b7 <sem_test+0x87>
mov    0x2e7b(%rip),%rbp
mov    0x0(%rbp),%ebx
mov    %ebx,%edi
call   1050 <strerror@plt>
lea    0xf5a(%rip),%rdi
lea    0xebc(%rip),%rsi
mov    $0x1d,%edx
jmp    11a8 <sem_test+0x78>
mov    0x2e55(%rip),%rbp
mov    0x0(%rbp),%ebx
mov    %ebx,%edi
call   1050 <strerror@plt>
lea    0xe64(%rip),%rdi
lea    0xe96(%rip),%rsi
mov    $0x19,%edx
mov    %ebx,%ecx
mov    %eax,%r8d
call   1030 <test_msg@plt>
xor    %eax,%eax
sub    0x0(%rbp),%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sembuf = type { i32 }
@.str = [57 x i8] c"FAIL: %s:%d: Failed to lock semaphore (errno = %d (%s))\0A\00"
@.str.1 = [151 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/fbocharov/criu/refs/heads/master/test/zdtm/live/static/sem.c\00"
@errno = global i32 0
@.str.2 = [59 x i8] c"FAIL: %s:%d: Failed to unlock semaphore (errno = %d (%s))\0A\00"
define i32 @sem_test(i32 %0, %struct.sembuf* %1, %struct.sembuf* %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sembuf*
%9 = alloca %struct.sembuf*
%10 = alloca i32
%11 = alloca i32
store i32 %0, i32* %7
store %struct.sembuf* %1, %struct.sembuf** %8
store %struct.sembuf* %2, %struct.sembuf** %9
store i32 %3, i32* %10
store i32 %4, i32* %11
%12 = load i32, i32* %7
%13 = load %struct.sembuf*, %struct.sembuf** %8
%14 = load i32, i32* %10
%15 = call i32 @semop(i32 %12, %struct.sembuf* %13, i32 %14)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %24
17: ; preds = %5
%18 = load i32, i32* @errno
%19 = load i32, i32* @errno
%20 = call i32 @strerror(i32 %19)
%21 = call i32 @test_msg(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([151 x i8], [151 x i8]* @.str.1, i64 0, i64 0), i32 25, i32 %18, i32 %20)
%22 = load i32, i32* @errno
%23 = sub nsw i32 0, %22
store i32 %23, i32* %6
br label %38
24: ; preds = %5
%25 = load i32, i32* %7
%26 = load %struct.sembuf*, %struct.sembuf** %9
%27 = load i32, i32* %11
%28 = call i32 @semop(i32 %25, %struct.sembuf* %26, i32 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %37
30: ; preds = %24
%31 = load i32, i32* @errno
%32 = load i32, i32* @errno
%33 = call i32 @strerror(i32 %32)
%34 = call i32 @test_msg(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([151 x i8], [151 x i8]* @.str.1, i64 0, i64 0), i32 29, i32 %31, i32 %33)
%35 = load i32, i32* @errno
%36 = sub nsw i32 0, %35
store i32 %36, i32* %6
br label %38
37: ; preds = %24
store i32 0, i32* %6
br label %38
38: ; preds = %37, %30, %17
%39 = load i32, i32* %6
ret i32 %39
}

LLVM IR (-O2):
%struct.sembuf = type { i32 }
@.str = [57 x i8] c"FAIL: %s:%d: Failed to lock semaphore (errno = %d (%s))\0A\00"
@.str.1 = [151 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/fbocharov/criu/refs/heads/master/test/zdtm/live/static/sem.c\00"
@errno = global i32 0
@.str.2 = [59 x i8] c"FAIL: %s:%d: Failed to unlock semaphore (errno = %d (%s))\0A\00"
define i32 @sem_test(i32 %0, %struct.sembuf* %1, %struct.sembuf* %2, i32 %3, i32 %4) {
%6 = tail call i32 @semop(i32 %0, %struct.sembuf* %1, i32 %3)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %14
8: ; preds = %5
%9 = load i32, i32* @errno
%10 = tail call i32 @strerror(i32 %9)
%11 = tail call i32 @test_msg(i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([151 x i8], [151 x i8]* @.str.1, i64 0, i64 0), i32 25, i32 %9, i32 %10)
%12 = load i32, i32* @errno
%13 = sub nsw i32 0, %12
br label %23
14: ; preds = %5
%15 = tail call i32 @semop(i32 %0, %struct.sembuf* %2, i32 %4)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %23
17: ; preds = %14
%18 = load i32, i32* @errno
%19 = tail call i32 @strerror(i32 %18)
%20 = tail call i32 @test_msg(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([151 x i8], [151 x i8]* @.str.1, i64 0, i64 0), i32 29, i32 %18, i32 %19)
%21 = load i32, i32* @errno
%22 = sub nsw i32 0, %21
br label %23
23: ; preds = %14, %17, %8
%24 = phi i32 [ %13, %8 ], [ %22, %17 ], [ 0, %14 ]
ret i32 %24
}