#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 /*<<< orphan*/ unlock;
struct sembuf {
  int sem_op;
  int /*<<< orphan*/ sem_flg;
  int /*<<< orphan*/ sem_num;
};
typedef int /*<<< orphan*/ lock;

/* Variables and functions */
int EFAULT;
int /*<<< orphan*/ GETVAL;
int errno;
int sem_test(int, struct sembuf *, struct sembuf *, int, int);
int semctl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int);
int /*<<< orphan*/ test_msg(char *, char *, int, int, int, ...);
int check_sem_by_id(int id, int val) {
  int curr;
  struct sembuf lock[] = {
      {
          .sem_num = 0,
          .sem_op = val,
          .sem_flg = 0,
      },
  };
  struct sembuf unlock[] = {{
      .sem_num = 0,
      .sem_op = -val * 2,
      .sem_flg = 0,
  }};
  curr = semctl(id, 0, GETVAL);
  if (curr < 0) {
    test_msg("FAIL: %s:%d: "
             "Failed to get sem value"
             " (errno = %d (%s))\n",
             "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
             "2022-01-22/02-19-57/repos/LK4D4/criu/refs/heads/master/test/zdtm/"
             "live/static/sem.c",
             96, errno, strerror(errno));
    return -errno;
  }
  if (curr != val) {
    test_msg("FAIL: %s:%d: "
             "Sem has wrong value: %d instead of %d\n"
             " (errno = %d (%s))\n",
             "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
             "2022-01-22/02-19-57/repos/LK4D4/criu/refs/heads/master/test/zdtm/"
             "live/static/sem.c",
             100, curr, val, errno, strerror(errno));
    return -EFAULT;
  }
  return sem_test(id, lock, unlock, sizeof(lock) / sizeof(struct sembuf),
                  sizeof(unlock) / sizeof(struct sembuf));
}


x86 assembly (clang, -O0):
<check_sem_by_id>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
xor    %eax,%eax
sub    -0xc(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x30(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e4b(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <semctl@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    11e8 <check_sem_by_id+0xa8>
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x34(%rbp),%ecx
mov    %eax,%r8d
lea    0xe3e(%rip),%rdi
lea    0xe6f(%rip),%rsi
mov    $0x60,%edx
mov    $0x0,%al
call   1040 <test_msg@plt>
xor    %eax,%eax
mov    0x2dfa(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1271 <check_sem_by_id+0x131>
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
je     1255 <check_sem_by_id+0x115>
mov    -0x10(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2dd1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x40(%rbp),%ecx
mov    -0x3c(%rbp),%r8d
mov    -0x38(%rbp),%r9d
lea    0xe9f(%rip),%rdi
lea    0xe05(%rip),%rsi
mov    $0x64,%edx
mov    %eax,(%rsp)
mov    $0x0,%al
call   1040 <test_msg@plt>
xor    %eax,%eax
mov    0x2dad(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1271 <check_sem_by_id+0x131>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x30(%rbp),%rdx
mov    $0x1,%r8d
mov    %r8d,%ecx
call   1030 <sem_test@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_sem_by_id>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %esi,%ebx
mov    %edi,%r14d
mov    %esi,0x18(%rsp)
movq   $0x0,0x1c(%rsp)
lea    (%rbx,%rbx,1),%eax
neg    %eax
mov    %eax,0x8(%rsp)
movq   $0x0,0xc(%rsp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%edx
xor    %r15d,%r15d
xor    %esi,%esi
call   1050 <semctl@plt>
test   %eax,%eax
js     11ad <check_sem_by_id+0x6d>
mov    %eax,%ebp
cmp    %ebx,%eax
jne    11e3 <check_sem_by_id+0xa3>
lea    0x18(%rsp),%rsi
lea    0x8(%rsp),%rdx
mov    %r14d,%edi
mov    $0x1,%ecx
mov    $0x1,%r8d
call   1030 <sem_test@plt>
mov    %eax,%r15d
jmp    1227 <check_sem_by_id+0xe7>
mov    0x2e24(%rip),%rbp
mov    0x0(%rbp),%ebx
mov    %ebx,%edi
call   1060 <strerror@plt>
lea    0xe3b(%rip),%rdi
lea    0xe6c(%rip),%rsi
mov    $0x60,%edx
mov    %ebx,%ecx
mov    %eax,%r8d
xor    %eax,%eax
call   1040 <test_msg@plt>
sub    0x0(%rbp),%r15d
jmp    1227 <check_sem_by_id+0xe7>
mov    0x2dee(%rip),%rax
mov    (%rax),%r14d
mov    %r14d,%edi
call   1060 <strerror@plt>
mov    %eax,(%rsp)
lea    0xecc(%rip),%rdi
lea    0xe32(%rip),%rsi
xor    %r15d,%r15d
mov    $0x64,%edx
mov    %ebp,%ecx
mov    %ebx,%r8d
mov    %r14d,%r9d
xor    %eax,%eax
call   1040 <test_msg@plt>
mov    0x2dd4(%rip),%rax
sub    (%rax),%r15d
mov    %r15d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sembuf = type { i32, i32, i32 }
@GETVAL = global i32 0
@.str = [56 x i8] c"FAIL: %s:%d: Failed to get sem value (errno = %d (%s))\0A\00"
@.str.1 = [147 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/LK4D4/criu/refs/heads/master/test/zdtm/live/static/sem.c\00"
@errno = global i32 0
@.str.2 = [71 x i8] c"FAIL: %s:%d: Sem has wrong value: %d instead of %d\0A (errno = %d (%s))\0A\00"
@EFAULT = global i32 0
define i32 @check_sem_by_id(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [1 x %struct.sembuf]
%8 = alloca [1 x %struct.sembuf]
store i32 %0, i32* %4
store i32 %1, i32* %5
%9 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %7, i64 0, i64 0
%10 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %9, i32 0, i32 0
%11 = load i32, i32* %5
store i32 %11, i32* %10
%12 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %9, i32 0, i32 1
store i32 0, i32* %12
%13 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %9, i32 0, i32 2
store i32 0, i32* %13
%14 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %8, i64 0, i64 0
%15 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %14, i32 0, i32 0
%16 = load i32, i32* %5
%17 = sub nsw i32 0, %16
%18 = mul nsw i32 %17, 2
store i32 %18, i32* %15
%19 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %14, i32 0, i32 1
store i32 0, i32* %19
%20 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %14, i32 0, i32 2
store i32 0, i32* %20
%21 = load i32, i32* %4
%22 = load i32, i32* @GETVAL
%23 = call i32 @semctl(i32 %21, i32 0, i32 %22)
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %33
26: ; preds = %2
%27 = load i32, i32* @errno
%28 = load i32, i32* @errno
%29 = call i32 @strerror(i32 %28)
%30 = call i32 (i8*, i8*, i32, i32, i32, ...) @test_msg(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([147 x i8], [147 x i8]* @.str.1, i64 0, i64 0), i32 96, i32 %27, i32 %29)
%31 = load i32, i32* @errno
%32 = sub nsw i32 0, %31
store i32 %32, i32* %3
br label %51
33: ; preds = %2
%34 = load i32, i32* %6
%35 = load i32, i32* %5
%36 = icmp ne i32 %34, %35
br i1 %36, label %37, label %46
37: ; preds = %33
%38 = load i32, i32* %6
%39 = load i32, i32* %5
%40 = load i32, i32* @errno
%41 = load i32, i32* @errno
%42 = call i32 @strerror(i32 %41)
%43 = call i32 (i8*, i8*, i32, i32, i32, ...) @test_msg(i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([147 x i8], [147 x i8]* @.str.1, i64 0, i64 0), i32 100, i32 %38, i32 %39, i32 %40, i32 %42)
%44 = load i32, i32* @EFAULT
%45 = sub nsw i32 0, %44
store i32 %45, i32* %3
br label %51
46: ; preds = %33
%47 = load i32, i32* %4
%48 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %7, i64 0, i64 0
%49 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %8, i64 0, i64 0
%50 = call i32 @sem_test(i32 %47, %struct.sembuf* %48, %struct.sembuf* %49, i32 1, i32 1)
store i32 %50, i32* %3
br label %51
51: ; preds = %46, %37, %26
%52 = load i32, i32* %3
ret i32 %52
}

LLVM IR (-O2):
%struct.sembuf = type { i32, i32, i32 }
@GETVAL = global i32 0
@.str = [56 x i8] c"FAIL: %s:%d: Failed to get sem value (errno = %d (%s))\0A\00"
@.str.1 = [147 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/LK4D4/criu/refs/heads/master/test/zdtm/live/static/sem.c\00"
@errno = global i32 0
@.str.2 = [71 x i8] c"FAIL: %s:%d: Sem has wrong value: %d instead of %d\0A (errno = %d (%s))\0A\00"
@EFAULT = global i32 0
define i32 @check_sem_by_id(i32 %0, i32 %1) {
%3 = alloca [1 x %struct.sembuf]
%4 = alloca [1 x %struct.sembuf]
%5 = bitcast [1 x %struct.sembuf]* %3 to i8*
%6 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %3, i64 0, i64 0
%7 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %3, i64 0, i64 0, i32 0
store i32 %1, i32* %7
%8 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %3, i64 0, i64 0, i32 1
store i32 0, i32* %8
%9 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %3, i64 0, i64 0, i32 2
store i32 0, i32* %9
%10 = bitcast [1 x %struct.sembuf]* %4 to i8*
%11 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %4, i64 0, i64 0
%12 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %4, i64 0, i64 0, i32 0
%13 = mul i32 %1, -2
store i32 %13, i32* %12
%14 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %4, i64 0, i64 0, i32 1
store i32 0, i32* %14
%15 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %4, i64 0, i64 0, i32 2
store i32 0, i32* %15
%16 = load i32, i32* @GETVAL
%17 = tail call i32 @semctl(i32 %0, i32 0, i32 %16)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %2
%20 = load i32, i32* @errno
%21 = tail call i32 @strerror(i32 %20)
%22 = tail call i32 (i8*, i8*, i32, i32, i32, ...) @test_msg(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([147 x i8], [147 x i8]* @.str.1, i64 0, i64 0), i32 96, i32 %20, i32 %21)
%23 = load i32, i32* @errno
%24 = sub nsw i32 0, %23
br label %35
25: ; preds = %2
%26 = icmp eq i32 %17, %1
br i1 %26, label %33, label %27
27: ; preds = %25
%28 = load i32, i32* @errno
%29 = tail call i32 @strerror(i32 %28)
%30 = tail call i32 (i8*, i8*, i32, i32, i32, ...) @test_msg(i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([147 x i8], [147 x i8]* @.str.1, i64 0, i64 0), i32 100, i32 %17, i32 %1, i32 %28, i32 %29)
%31 = load i32, i32* @EFAULT
%32 = sub nsw i32 0, %31
br label %35
33: ; preds = %25
%34 = call i32 @sem_test(i32 %0, %struct.sembuf* %6, %struct.sembuf* %11, i32 1, i32 1)
br label %35
35: ; preds = %33, %27, %19
%36 = phi i32 [ %24, %19 ], [ %32, %27 ], [ %34, %33 ]
ret i32 %36
}