#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 /*<<< orphan*/ GETVAL;
int errno;
int sem_test(int, struct sembuf *, struct sembuf *, int, int);
int semctl(int, int, int /*<<< orphan*/);
int semget(int, int, int);
int /*<<< orphan*/ strerror(int);
int /*<<< orphan*/ test_msg(char *, char *, int, int, int /*<<< orphan*/);
int check_sem_by_key(int key, int num) {
  int id;
  struct sembuf lock[2] = {
      {
          .sem_num = num,
          .sem_op = 0,
          .sem_flg = 0,
      },
      {
          .sem_num = num,
          .sem_op = 1,
          .sem_flg = 0,
      },
  };
  struct sembuf unlock[1] = {{
      .sem_num = num,
      .sem_op = -1,
      .sem_flg = 0,
  }};
  int val;
  id = semget(key, 10, 0777);
  if (id == -1) {
    test_msg("FAIL: %s:%d: "
             "Can't get sem"
             " (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",
             63, errno, strerror(errno));
    return -errno;
  }
  val = semctl(id, num, GETVAL);
  if (val < 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",
             69, errno, strerror(errno));
    return -errno;
  }
  return sem_test(id, lock, unlock, sizeof(lock) / sizeof(struct sembuf),
                  sizeof(unlock) / sizeof(struct sembuf));
}


x86 assembly (clang, -O0):
<check_sem_by_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x30(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x24(%rbp)
movl   $0x1,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x40(%rbp)
movl   $0xffffffff,-0x3c(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x8(%rbp),%edi
mov    $0xa,%esi
mov    $0x1ff,%edx
call   1030 <semget@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    1204 <check_sem_by_key+0xb4>
mov    0x2e20(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x48(%rbp),%ecx
mov    %eax,%r8d
lea    0xe20(%rip),%rdi
lea    0xe47(%rip),%rsi
mov    $0x3f,%edx
call   1050 <test_msg@plt>
xor    %eax,%eax
mov    0x2de6(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    128e <check_sem_by_key+0x13e>
mov    -0x10(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    0x2dc7(%rip),%rax
mov    (%rax),%edx
call   1060 <semctl@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    1270 <check_sem_by_key+0x120>
mov    0x2db4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x4c(%rbp),%ecx
mov    %eax,%r8d
lea    0xe7d(%rip),%rdi
lea    0xddb(%rip),%rsi
mov    $0x45,%edx
call   1050 <test_msg@plt>
xor    %eax,%eax
mov    0x2d7a(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    128e <check_sem_by_key+0x13e>
mov    -0x10(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x40(%rbp),%rdx
mov    $0x2,%ecx
mov    $0x1,%r8d
call   1040 <sem_test@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_sem_by_key>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    %esi,%ebx
mov    %esi,0x10(%rsp)
movq   $0x0,0x14(%rsp)
mov    %esi,0x1c(%rsp)
movq   $0x1,0x20(%rsp)
mov    %esi,(%rsp)
mov    $0xffffffff,%eax
mov    %rax,0x4(%rsp)
mov    $0xa,%esi
mov    $0x1ff,%edx
call   1030 <semget@plt>
cmp    $0xffffffff,%eax
je     11c7 <check_sem_by_key+0x77>
mov    %eax,%ebp
mov    0x2e3c(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    %ebx,%esi
call   1060 <semctl@plt>
test   %eax,%eax
js     11ed <check_sem_by_key+0x9d>
lea    0x10(%rsp),%rsi
mov    %rsp,%rdx
mov    %ebp,%edi
mov    $0x2,%ecx
mov    $0x1,%r8d
call   1040 <sem_test@plt>
jmp    1220 <check_sem_by_key+0xd0>
mov    0x2e12(%rip),%rbp
mov    0x0(%rbp),%ebx
mov    %ebx,%edi
call   1070 <strerror@plt>
lea    0xe21(%rip),%rdi
lea    0xe48(%rip),%rsi
mov    $0x3f,%edx
jmp    1211 <check_sem_by_key+0xc1>
mov    0x2dec(%rip),%rbp
mov    0x0(%rbp),%ebx
mov    %ebx,%edi
call   1070 <strerror@plt>
lea    0xec4(%rip),%rdi
lea    0xe22(%rip),%rsi
mov    $0x45,%edx
mov    %ebx,%ecx
mov    %eax,%r8d
call   1050 <test_msg@plt>
xor    %eax,%eax
sub    0x0(%rbp),%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sembuf = type { i32, i32, i32 }
@.str = [46 x i8] c"FAIL: %s:%d: Can't get sem (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
@GETVAL = global i32 0
@.str.2 = [56 x i8] c"FAIL: %s:%d: Failed to get sem value (errno = %d (%s))\0A\00"
define i32 @check_sem_by_key(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [2 x %struct.sembuf]
%8 = alloca [1 x %struct.sembuf]
%9 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%10 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %7, i64 0, i64 0
%11 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %10, i32 0, i32 0
%12 = load i32, i32* %5
store i32 %12, i32* %11
%13 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %10, i32 0, i32 1
store i32 0, i32* %13
%14 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %10, i32 0, i32 2
store i32 0, i32* %14
%15 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %10, i64 1
%16 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %15, i32 0, i32 0
%17 = load i32, i32* %5
store i32 %17, i32* %16
%18 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %15, i32 0, i32 1
store i32 1, i32* %18
%19 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %15, i32 0, i32 2
store i32 0, i32* %19
%20 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %8, i64 0, i64 0
%21 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %20, i32 0, i32 0
%22 = load i32, i32* %5
store i32 %22, i32* %21
%23 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %20, i32 0, i32 1
store i32 -1, i32* %23
%24 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %20, i32 0, i32 2
store i32 0, i32* %24
%25 = load i32, i32* %4
%26 = call i32 @semget(i32 %25, i32 10, i32 511)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %36
29: ; preds = %2
%30 = load i32, i32* @errno
%31 = load i32, i32* @errno
%32 = call i32 @strerror(i32 %31)
%33 = call i32 @test_msg(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([155 x i8], [155 x i8]* @.str.1, i64 0, i64 0), i32 63, i32 %30, i32 %32)
%34 = load i32, i32* @errno
%35 = sub nsw i32 0, %34
store i32 %35, i32* %3
br label %55
36: ; preds = %2
%37 = load i32, i32* %6
%38 = load i32, i32* %5
%39 = load i32, i32* @GETVAL
%40 = call i32 @semctl(i32 %37, i32 %38, i32 %39)
store i32 %40, i32* %9
%41 = load i32, i32* %9
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %50
43: ; preds = %36
%44 = load i32, i32* @errno
%45 = load i32, i32* @errno
%46 = call i32 @strerror(i32 %45)
%47 = call i32 @test_msg(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([155 x i8], [155 x i8]* @.str.1, i64 0, i64 0), i32 69, i32 %44, i32 %46)
%48 = load i32, i32* @errno
%49 = sub nsw i32 0, %48
store i32 %49, i32* %3
br label %55
50: ; preds = %36
%51 = load i32, i32* %6
%52 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %7, i64 0, i64 0
%53 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %8, i64 0, i64 0
%54 = call i32 @sem_test(i32 %51, %struct.sembuf* %52, %struct.sembuf* %53, i32 2, i32 1)
store i32 %54, i32* %3
br label %55
55: ; preds = %50, %43, %29
%56 = load i32, i32* %3
ret i32 %56
}

LLVM IR (-O2):
%struct.sembuf = type { i32, i32, i32 }
@.str = [46 x i8] c"FAIL: %s:%d: Can't get sem (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
@GETVAL = global i32 0
@.str.2 = [56 x i8] c"FAIL: %s:%d: Failed to get sem value (errno = %d (%s))\0A\00"
define i32 @check_sem_by_key(i32 %0, i32 %1) {
%3 = alloca [2 x %struct.sembuf]
%4 = alloca [1 x %struct.sembuf]
%5 = bitcast [2 x %struct.sembuf]* %3 to i8*
%6 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %3, i64 0, i64 0
%7 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %3, i64 0, i64 0, i32 0
store i32 %1, i32* %7
%8 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %3, i64 0, i64 0, i32 1
store i32 0, i32* %8
%9 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %3, i64 0, i64 0, i32 2
store i32 0, i32* %9
%10 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %3, i64 0, i64 1, i32 0
store i32 %1, i32* %10
%11 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %3, i64 0, i64 1, i32 1
store i32 1, i32* %11
%12 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %3, i64 0, i64 1, i32 2
store i32 0, i32* %12
%13 = bitcast [1 x %struct.sembuf]* %4 to i8*
%14 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %4, i64 0, i64 0
%15 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %4, i64 0, i64 0, i32 0
store i32 %1, i32* %15
%16 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %4, i64 0, i64 0, i32 1
store i32 -1, i32* %16
%17 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %4, i64 0, i64 0, i32 2
store i32 0, i32* %17
%18 = tail call i32 @semget(i32 %0, i32 10, i32 511)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %26
20: ; preds = %2
%21 = load i32, i32* @errno
%22 = tail call i32 @strerror(i32 %21)
%23 = tail call i32 @test_msg(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([155 x i8], [155 x i8]* @.str.1, i64 0, i64 0), i32 63, i32 %21, i32 %22)
%24 = load i32, i32* @errno
%25 = sub nsw i32 0, %24
br label %38
26: ; preds = %2
%27 = load i32, i32* @GETVAL
%28 = tail call i32 @semctl(i32 %18, i32 %1, i32 %27)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %36
30: ; preds = %26
%31 = load i32, i32* @errno
%32 = tail call i32 @strerror(i32 %31)
%33 = tail call i32 @test_msg(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([155 x i8], [155 x i8]* @.str.1, i64 0, i64 0), i32 69, i32 %31, i32 %32)
%34 = load i32, i32* @errno
%35 = sub nsw i32 0, %34
br label %38
36: ; preds = %26
%37 = call i32 @sem_test(i32 %18, %struct.sembuf* %6, %struct.sembuf* %14, i32 2, i32 1)
br label %38
38: ; preds = %36, %30, %20
%39 = phi i32 [ %25, %20 ], [ %35, %30 ], [ %37, %36 ]
ret i32 %39
}