#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_num;
  int sem_op;
  int /*<<< orphan*/ sem_flg;
};
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, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int);
int /*<<< orphan*/ test_msg(char *, char *, int, int, int, ...);
int check_sem_by_id(int id, int num, int val) {
  int curr;
  struct sembuf lock[] = {
      {
          .sem_num = num,
          .sem_op = val,
          .sem_flg = 0,
      },
  };
  struct sembuf unlock[] = {{
      .sem_num = num,
      .sem_op = -val * 2,
      .sem_flg = 0,
  }};
  curr = semctl(id, num, 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/AuthenticEshkinKot/criu/refs/heads/"
             "master/test/zdtm/static/sem.c",
             98, 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/AuthenticEshkinKot/criu/refs/heads/"
             "master/test/zdtm/static/sem.c",
             102, 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    %edx,-0x10(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x30(%rbp)
xor    %eax,%eax
sub    -0x10(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    0x2e47(%rip),%rax
mov    (%rax),%edx
call   1050 <semctl@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11ea <check_sem_by_id+0xaa>
mov    0x2e34(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x34(%rbp),%ecx
mov    %eax,%r8d
lea    0xe3c(%rip),%rdi
lea    0xe6d(%rip),%rsi
mov    $0x62,%edx
mov    $0x0,%al
call   1040 <test_msg@plt>
xor    %eax,%eax
mov    0x2df8(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1273 <check_sem_by_id+0x133>
mov    -0x14(%rbp),%eax
cmp    -0x10(%rbp),%eax
je     1257 <check_sem_by_id+0x117>
mov    -0x14(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x10(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2dcf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2dc3(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    -0x40(%rbp),%ecx
mov    -0x3c(%rbp),%r8d
mov    -0x38(%rbp),%r9d
lea    0xea5(%rip),%rdi
lea    0xe03(%rip),%rsi
mov    $0x66,%edx
mov    %eax,(%rsp)
mov    $0x0,%al
call   1040 <test_msg@plt>
xor    %eax,%eax
mov    0x2dab(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1273 <check_sem_by_id+0x133>
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    %edx,%r14d
mov    %edi,%ebp
mov    %esi,0x18(%rsp)
mov    %edx,0x1c(%rsp)
movl   $0x0,0x20(%rsp)
mov    %esi,0x8(%rsp)
lea    (%r14,%r14,1),%eax
neg    %eax
mov    %eax,0xc(%rsp)
movl   $0x0,0x10(%rsp)
mov    0x2e54(%rip),%rax
mov    (%rax),%edx
call   1050 <semctl@plt>
test   %eax,%eax
js     11ae <check_sem_by_id+0x6e>
mov    %eax,%ebx
cmp    %r14d,%eax
jne    11e5 <check_sem_by_id+0xa5>
lea    0x18(%rsp),%rsi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
mov    $0x1,%ecx
mov    $0x1,%r8d
call   1030 <sem_test@plt>
mov    %eax,%ebp
jmp    1227 <check_sem_by_id+0xe7>
mov    0x2e23(%rip),%r14
mov    (%r14),%ebx
mov    %ebx,%edi
call   1060 <strerror@plt>
lea    0xe3a(%rip),%rdi
lea    0xe6b(%rip),%rsi
xor    %ebp,%ebp
mov    $0x62,%edx
mov    %ebx,%ecx
mov    %eax,%r8d
xor    %eax,%eax
call   1040 <test_msg@plt>
sub    (%r14),%ebp
jmp    1227 <check_sem_by_id+0xe7>
mov    0x2dec(%rip),%rax
mov    (%rax),%r15d
mov    %r15d,%edi
call   1060 <strerror@plt>
mov    %eax,(%rsp)
lea    0xed2(%rip),%rdi
lea    0xe30(%rip),%rsi
xor    %ebp,%ebp
mov    $0x66,%edx
mov    %ebx,%ecx
mov    %r14d,%r8d
mov    %r15d,%r9d
xor    %eax,%eax
call   1040 <test_msg@plt>
mov    0x2dd3(%rip),%rax
sub    (%rax),%ebp
mov    %ebp,%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 = [155 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/AuthenticEshkinKot/criu/refs/heads/master/test/zdtm/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, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [1 x %struct.sembuf]
%10 = alloca [1 x %struct.sembuf]
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%11 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %9, i64 0, i64 0
%12 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %11, i32 0, i32 0
%13 = load i32, i32* %6
store i32 %13, i32* %12
%14 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %11, i32 0, i32 1
%15 = load i32, i32* %7
store i32 %15, i32* %14
%16 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %11, i32 0, i32 2
store i32 0, i32* %16
%17 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %10, i64 0, i64 0
%18 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %17, i32 0, i32 0
%19 = load i32, i32* %6
store i32 %19, i32* %18
%20 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %17, i32 0, i32 1
%21 = load i32, i32* %7
%22 = sub nsw i32 0, %21
%23 = mul nsw i32 %22, 2
store i32 %23, i32* %20
%24 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %17, i32 0, i32 2
store i32 0, i32* %24
%25 = load i32, i32* %5
%26 = load i32, i32* %6
%27 = load i32, i32* @GETVAL
%28 = call i32 @semctl(i32 %25, i32 %26, i32 %27)
store i32 %28, i32* %8
%29 = load i32, i32* %8
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %38
31: ; preds = %3
%32 = load i32, i32* @errno
%33 = load i32, i32* @errno
%34 = call i32 @strerror(i32 %33)
%35 = 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 ([155 x i8], [155 x i8]* @.str.1, i64 0, i64 0), i32 98, i32 %32, i32 %34)
%36 = load i32, i32* @errno
%37 = sub nsw i32 0, %36
store i32 %37, i32* %4
br label %56
38: ; preds = %3
%39 = load i32, i32* %8
%40 = load i32, i32* %7
%41 = icmp ne i32 %39, %40
br i1 %41, label %42, label %51
42: ; preds = %38
%43 = load i32, i32* %8
%44 = load i32, i32* %7
%45 = load i32, i32* @errno
%46 = load i32, i32* @errno
%47 = call i32 @strerror(i32 %46)
%48 = 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 ([155 x i8], [155 x i8]* @.str.1, i64 0, i64 0), i32 102, i32 %43, i32 %44, i32 %45, i32 %47)
%49 = load i32, i32* @EFAULT
%50 = sub nsw i32 0, %49
store i32 %50, i32* %4
br label %56
51: ; preds = %38
%52 = load i32, i32* %5
%53 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %9, i64 0, i64 0
%54 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %10, i64 0, i64 0
%55 = call i32 @sem_test(i32 %52, %struct.sembuf* %53, %struct.sembuf* %54, i32 1, i32 1)
store i32 %55, i32* %4
br label %56
56: ; preds = %51, %42, %31
%57 = load i32, i32* %4
ret i32 %57
}

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 = [155 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/AuthenticEshkinKot/criu/refs/heads/master/test/zdtm/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, i32 %2) {
%4 = alloca [1 x %struct.sembuf]
%5 = alloca [1 x %struct.sembuf]
%6 = bitcast [1 x %struct.sembuf]* %4 to i8*
%7 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %4, i64 0, i64 0
%8 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %4, i64 0, i64 0, i32 0
store i32 %1, i32* %8
%9 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %4, i64 0, i64 0, i32 1
store i32 %2, i32* %9
%10 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %4, i64 0, i64 0, i32 2
store i32 0, i32* %10
%11 = bitcast [1 x %struct.sembuf]* %5 to i8*
%12 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %5, i64 0, i64 0
%13 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %5, i64 0, i64 0, i32 0
store i32 %1, i32* %13
%14 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %5, i64 0, i64 0, i32 1
%15 = mul i32 %2, -2
store i32 %15, i32* %14
%16 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %5, i64 0, i64 0, i32 2
store i32 0, i32* %16
%17 = load i32, i32* @GETVAL
%18 = tail call i32 @semctl(i32 %0, i32 %1, i32 %17)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %3
%21 = load i32, i32* @errno
%22 = tail call i32 @strerror(i32 %21)
%23 = 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 ([155 x i8], [155 x i8]* @.str.1, i64 0, i64 0), i32 98, i32 %21, i32 %22)
%24 = load i32, i32* @errno
%25 = sub nsw i32 0, %24
br label %36
26: ; preds = %3
%27 = icmp eq i32 %18, %2
br i1 %27, label %34, label %28
28: ; preds = %26
%29 = load i32, i32* @errno
%30 = tail call i32 @strerror(i32 %29)
%31 = 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 ([155 x i8], [155 x i8]* @.str.1, i64 0, i64 0), i32 102, i32 %18, i32 %2, i32 %29, i32 %30)
%32 = load i32, i32* @EFAULT
%33 = sub nsw i32 0, %32
br label %36
34: ; preds = %26
%35 = call i32 @sem_test(i32 %0, %struct.sembuf* %7, %struct.sembuf* %12, i32 1, i32 1)
br label %36
36: ; preds = %34, %28, %20
%37 = phi i32 [ %25, %20 ], [ %33, %28 ], [ %35, %34 ]
ret i32 %37
}