#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 sem_num;
  int sem_op;
  void *sem_flg;
};

/* Variables and functions */
void *SEM_UNDO;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int semop(int, struct sembuf *, int);
void wait_and_lock(int sem_id) {
  struct sembuf actions[2];
  actions[0].sem_num = 0;
  actions[0].sem_op = 1;
  actions[0].sem_flg = SEM_UNDO;
  actions[1].sem_num = 1;
  actions[1].sem_op = 0;
  actions[1].sem_flg = SEM_UNDO;
  if (semop(sem_id, actions, 2) == -1) {
    perror("semop: locking");
    exit(10);
  };
}


x86 assembly (clang, -O0):
<wait_and_lock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x30(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    0x2ea0(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
movl   $0x1,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    0x2e84(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x2,%edx
call   1040 <semop@plt>
cmp    $0xffffffff,%eax
jne    11a3 <wait_and_lock+0x73>
lea    0xe6c(%rip),%rdi
call   1030 <perror@plt>
mov    $0xa,%edi
call   1050 <exit@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_and_lock>:
sub    $0x28,%rsp
movabs $0x100000000,%rax
mov    %rax,(%rsp)
mov    0x2ea7(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x8(%rsp)
movq   $0x1,0x10(%rsp)
mov    %rax,0x18(%rsp)
mov    %rsp,%rsi
mov    $0x2,%edx
call   1040 <semop@plt>
cmp    $0xffffffff,%eax
je     1176 <wait_and_lock+0x46>
add    $0x28,%rsp
ret
lea    0xe83(%rip),%rdi
call   1030 <perror@plt>
mov    $0xa,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
%struct.sembuf = type { i32, i32, i8* }
@SEM_UNDO = global i8* null
@.str = [15 x i8] c"semop: locking\00"
define void @wait_and_lock(i32 %0) {
%2 = alloca i32
%3 = alloca [2 x %struct.sembuf]
store i32 %0, i32* %2
%4 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %3, i64 0, i64 0
%5 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %4, i32 0, i32 0
store i32 0, i32* %5
%6 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %3, i64 0, i64 0
%7 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %6, i32 0, i32 1
store i32 1, i32* %7
%8 = load i8*, i8** @SEM_UNDO
%9 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %3, i64 0, i64 0
%10 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %9, i32 0, i32 2
store i8* %8, i8** %10
%11 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %3, i64 0, i64 1
%12 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %11, i32 0, i32 0
store i32 1, i32* %12
%13 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %3, i64 0, i64 1
%14 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %13, i32 0, i32 1
store i32 0, i32* %14
%15 = load i8*, i8** @SEM_UNDO
%16 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %3, i64 0, i64 1
%17 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %16, i32 0, i32 2
store i8* %15, i8** %17
%18 = load i32, i32* %2
%19 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %3, i64 0, i64 0
%20 = call i32 @semop(i32 %18, %struct.sembuf* %19, i32 2)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %1
%23 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @exit(i32 10)
unreachable
25: ; preds = %1
ret void
}

LLVM IR (-O2):
%struct.sembuf = type { i32, i32, i8* }
@SEM_UNDO = global i8* null
@.str = [15 x i8] c"semop: locking\00"
define void @wait_and_lock(i32 %0) {
%2 = alloca [2 x %struct.sembuf]
%3 = bitcast [2 x %struct.sembuf]* %2 to i8*
%4 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %2, i64 0, i64 0
%5 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %2, i64 0, i64 0, i32 0
store i32 0, i32* %5
%6 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %2, i64 0, i64 0, i32 1
store i32 1, i32* %6
%7 = load i8*, i8** @SEM_UNDO
%8 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %2, i64 0, i64 0, i32 2
store i8* %7, i8** %8
%9 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %2, i64 0, i64 1, i32 0
store i32 1, i32* %9
%10 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %2, i64 0, i64 1, i32 1
store i32 0, i32* %10
%11 = getelementptr inbounds [2 x %struct.sembuf], [2 x %struct.sembuf]* %2, i64 0, i64 1, i32 2
store i8* %7, i8** %11
%12 = call i32 @semop(i32 %0, %struct.sembuf* %4, i32 2)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 10)
unreachable
17: ; preds = %1
ret void
}