#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 /*<<< orphan*/ GETVAL;
int errno;
int sem_test(int, struct sembuf *, struct sembuf *, int, int);
int semctl(int, int /*<<< orphan*/, 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 id;
  struct sembuf lock[2] = {
      {
          .sem_num = 0,
          .sem_op = 0,
          .sem_flg = 0,
      },
      {
          .sem_num = 0,
          .sem_op = 1,
          .sem_flg = 0,
      },
  };
  struct sembuf unlock[1] = {{
      .sem_num = 0,
      .sem_op = -1,
      .sem_flg = 0,
  }};
  int val;
  id = semget(key, 1, 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/fbocharov/criu/refs/heads/master/test/"
             "zdtm/live/static/sem.c",
             61, errno, strerror(errno));
    return -errno;
  }
  val = semctl(id, 0, 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/fbocharov/criu/refs/heads/master/test/"
             "zdtm/live/static/sem.c",
             67, 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    0xe9e(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe9b(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xe98(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe95(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xe93(%rip),%eax
mov    %eax,-0x38(%rbp)
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
mov    $0x1ff,%edx
call   1030 <semget@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11fa <check_sem_by_key+0xaa>
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x48(%rbp),%ecx
mov    %eax,%r8d
lea    0xe4e(%rip),%rdi
lea    0xe75(%rip),%rsi
mov    $0x3d,%edx
call   1050 <test_msg@plt>
xor    %eax,%eax
mov    0x2df0(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1283 <check_sem_by_key+0x133>
mov    -0xc(%rbp),%edi
mov    0x2dd4(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <semctl@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    1265 <check_sem_by_key+0x115>
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x4c(%rbp),%ecx
mov    %eax,%r8d
lea    0xea8(%rip),%rdi
lea    0xe0a(%rip),%rsi
mov    $0x43,%edx
call   1050 <test_msg@plt>
xor    %eax,%eax
mov    0x2d85(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1283 <check_sem_by_key+0x133>
mov    -0xc(%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   %r14
push   %rbx
sub    $0x30,%rsp
mov    0xeb1(%rip),%rax
mov    %rax,0x20(%rsp)
movaps 0xe95(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
mov    0xeaa(%rip),%eax
mov    %eax,0x8(%rsp)
mov    0xe97(%rip),%rax
mov    %rax,(%rsp)
mov    $0x1,%esi
mov    $0x1ff,%edx
call   1030 <semget@plt>
cmp    $0xffffffff,%eax
je     11d1 <check_sem_by_key+0x81>
mov    %eax,%ebp
mov    0x2e36(%rip),%rax
mov    (%rax),%edx
xor    %ebx,%ebx
mov    %ebp,%edi
xor    %esi,%esi
call   1060 <semctl@plt>
test   %eax,%eax
js     1206 <check_sem_by_key+0xb6>
lea    0x10(%rsp),%rsi
mov    %rsp,%rdx
mov    %ebp,%edi
mov    $0x2,%ecx
mov    $0x1,%r8d
call   1040 <sem_test@plt>
mov    %eax,%ebx
jmp    1237 <check_sem_by_key+0xe7>
mov    0x2e08(%rip),%rbp
mov    0x0(%rbp),%ebx
mov    %ebx,%edi
call   1070 <strerror@plt>
lea    0xe3b(%rip),%rdi
lea    0xe62(%rip),%rsi
mov    $0x3d,%edx
mov    %ebx,%ecx
mov    %eax,%r8d
call   1050 <test_msg@plt>
xor    %ebx,%ebx
sub    0x0(%rbp),%ebx
jmp    1237 <check_sem_by_key+0xe7>
mov    0x2dd3(%rip),%r14
mov    (%r14),%ebp
mov    %ebp,%edi
call   1070 <strerror@plt>
lea    0xecb(%rip),%rdi
lea    0xe2d(%rip),%rsi
mov    $0x43,%edx
mov    %ebp,%ecx
mov    %eax,%r8d
call   1050 <test_msg@plt>
sub    (%r14),%ebx
mov    %ebx,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.sembuf = type { i32, i32, i32 }
@__const.check_sem_by_key.lock = [2 x %struct.sembuf] [%struct.sembuf zeroinitializer, %struct.sembuf { i32 1, i32 0, i32 0 }]
@__const.check_sem_by_key.unlock = [1 x %struct.sembuf] [%struct.sembuf { i32 -1, i32 0, i32 0 }]
@.str = [46 x i8] c"FAIL: %s:%d: Can't get sem (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
@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) {
%2 = alloca [2 x %struct.sembuf]
%3 = alloca [1 x %struct.sembuf]
%4 = bitcast [2 x %struct.sembuf]* %2 to i8*
%5 = bitcast [1 x %struct.sembuf]* %3 to i8*
%6 = tail call i32 @semget(i32 %0, i32 1, i32 511)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %14
8: ; preds = %1
%9 = load i32, i32* @errno
%10 = tail call i32 @strerror(i32 %9)
%11 = tail call i32 @test_msg(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([151 x i8], [151 x i8]* @.str.1, i64 0, i64 0), i32 61, i32 %9, i32 %10)
%12 = load i32, i32* @errno
%13 = sub nsw i32 0, %12
br label %28
14: ; preds = %1
%15 = load i32, i32* @GETVAL
%16 = tail call i32 @semctl(i32 %6, i32 0, i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %14
%19 = load i32, i32* @errno
%20 = tail call i32 @strerror(i32 %19)
%21 = tail call i32 @test_msg(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([151 x i8], [151 x i8]* @.str.1, i64 0, i64 0), i32 67, i32 %19, i32 %20)
%22 = load i32, i32* @errno
%23 = sub nsw i32 0, %22
br label %28
24: ; preds = %14
%25 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %2, i64 0, i64 0
%26 = getelementptr inbounds [1 x %struct.sembuf], [1 x %struct.sembuf]* %3, i64 0, i64 0
%27 = call i32 @sem_test(i32 %6, %struct.sembuf* %25, %struct.sembuf* %26, i32 2, i32 1)
br label %28
28: ; preds = %24, %18, %8
%29 = phi i32 [ %13, %8 ], [ %23, %18 ], [ %27, %24 ]
ret i32 %29
}