#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;
  scalar_t__ sem_flg;
};

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ perror(char *);
int semop(int, struct sembuf *, int);
int /*<<< orphan*/ stderr;
void *Thread2(void *arg) {
  int semid;
  semid = (int)arg;
  struct sembuf op1, op2;
  op1.sem_num = 0;
  op1.sem_op = -1;
  op1.sem_flg = 0;
  op2.sem_num = 1;
  op2.sem_op = -1;
  op2.sem_flg = 0;
  if (semop(semid, &op1, 1) == -1) {
    perror("Reason:");
    exit(-1);
  } else
    fprintf(stderr, "Thread2:Successfully locked 0th semaphore\n");
  if (semop(semid, &op2, 1) == -1) {
    perror("Reason:");
    exit(-1);
  } else
    fprintf(stderr, "Thread2:Successfully locked 1th semaphore\n");
  op1.sem_num = 0;
  op1.sem_op = 1;
  op1.sem_flg = 0;
  if (semop(semid, &op1, 1) == -1) {
    perror("Reason:");
    exit(-1);
  } else
    fprintf(stderr, "Thread2:Successfully unlocked 0th semaphore\n");
  op2.sem_num = 1;
  op2.sem_op = 1;
  op2.sem_flg = 0;
  if (semop(semid, &op2, 1) == -1) {
    perror("Reason:");
    exit(-1);
  } else
    fprintf(stderr, "Thread2:Successfully unlocked 1th semaphore\n");
}


x86 assembly (clang, -O0):
<Thread2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0xffffffff,-0x24(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x1,-0x38(%rbp)
movl   $0xffffffff,-0x34(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x1,%edx
call   1050 <semop@plt>
cmp    $0xffffffff,%eax
jne    11af <Thread2+0x6f>
lea    0xe60(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
lea    0xe49(%rip),%rsi
call   1030 <fprintf@plt>
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0x1,%edx
call   1050 <semop@plt>
cmp    $0xffffffff,%eax
jne    11f4 <Thread2+0xb4>
lea    0xe1b(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
lea    0xe2f(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0x0,-0x28(%rbp)
movl   $0x1,-0x24(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x1,%edx
call   1050 <semop@plt>
cmp    $0xffffffff,%eax
jne    124f <Thread2+0x10f>
lea    0xdc0(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
lea    0xdff(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0x1,-0x38(%rbp)
movl   $0x1,-0x34(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0x1,%edx
call   1050 <semop@plt>
cmp    $0xffffffff,%eax
jne    12aa <Thread2+0x16a>
lea    0xd65(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
lea    0xdd1(%rip),%rsi
call   1030 <fprintf@plt>
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Thread2>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%rbx
movabs $0xffffffff00000000,%rax
ff ff ff
mov    %rax,0x18(%rsp)
movq   $0x0,0x20(%rsp)
movabs $0xffffffff00000001,%rax
ff ff ff
mov    %rax,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x18(%rsp),%rsi
mov    $0x1,%edx
call   1050 <semop@plt>
cmp    $0xffffffff,%eax
je     1253 <Thread2+0x113>
mov    0x2e47(%rip),%r14
mov    (%r14),%edi
lea    0xe65(%rip),%rsi
call   1030 <fprintf@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1050 <semop@plt>
cmp    $0xffffffff,%eax
je     1253 <Thread2+0x113>
mov    (%r14),%edi
lea    0xe67(%rip),%rsi
call   1030 <fprintf@plt>
movabs $0x100000000,%rax
mov    %rax,0x18(%rsp)
movq   $0x0,0x20(%rsp)
lea    0x18(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1050 <semop@plt>
cmp    $0xffffffff,%eax
je     1253 <Thread2+0x113>
mov    (%r14),%edi
lea    0xe55(%rip),%rsi
call   1030 <fprintf@plt>
movabs $0x100000001,%rax
mov    %rax,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1050 <semop@plt>
cmp    $0xffffffff,%eax
je     1253 <Thread2+0x113>
mov    (%r14),%edi
lea    0xe45(%rip),%rsi
call   1030 <fprintf@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret
lea    0xda6(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.sembuf = type { i32, i32, i64 }
@.str = [8 x i8] c"Reason:\00"
@stderr = global i32 0
@.str.1 = [43 x i8] c"Thread2:Successfully locked 0th semaphore\0A\00"
@.str.2 = [43 x i8] c"Thread2:Successfully locked 1th semaphore\0A\00"
@.str.3 = [45 x i8] c"Thread2:Successfully unlocked 0th semaphore\0A\00"
@.str.4 = [45 x i8] c"Thread2:Successfully unlocked 1th semaphore\0A\00"
define i8* @Thread2(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.sembuf
%6 = alloca %struct.sembuf
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = ptrtoint i8* %7 to i32
store i32 %8, i32* %4
%9 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %5, i32 0, i32 0
store i32 0, i32* %9
%10 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %5, i32 0, i32 1
store i32 -1, i32* %10
%11 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %5, i32 0, i32 2
store i64 0, i64* %11
%12 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %6, i32 0, i32 0
store i32 1, i32* %12
%13 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %6, i32 0, i32 1
store i32 -1, i32* %13
%14 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %6, i32 0, i32 2
store i64 0, i64* %14
%15 = load i32, i32* %4
%16 = call i32 @semop(i32 %15, %struct.sembuf* %5, i32 1)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %1
%19 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 -1)
unreachable
21: ; preds = %1
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %21
%25 = load i32, i32* %4
%26 = call i32 @semop(i32 %25, %struct.sembuf* %6, i32 1)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%30 = call i32 @exit(i32 -1)
unreachable
31: ; preds = %24
%32 = load i32, i32* @stderr
%33 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %31
%35 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %5, i32 0, i32 0
store i32 0, i32* %35
%36 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %5, i32 0, i32 1
store i32 1, i32* %36
%37 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %5, i32 0, i32 2
store i64 0, i64* %37
%38 = load i32, i32* %4
%39 = call i32 @semop(i32 %38, %struct.sembuf* %5, i32 1)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %44
41: ; preds = %34
%42 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%43 = call i32 @exit(i32 -1)
unreachable
44: ; preds = %34
%45 = load i32, i32* @stderr
%46 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0))
br label %47
47: ; preds = %44
%48 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %6, i32 0, i32 0
store i32 1, i32* %48
%49 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %6, i32 0, i32 1
store i32 1, i32* %49
%50 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %6, i32 0, i32 2
store i64 0, i64* %50
%51 = load i32, i32* %4
%52 = call i32 @semop(i32 %51, %struct.sembuf* %6, i32 1)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %57
54: ; preds = %47
%55 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%56 = call i32 @exit(i32 -1)
unreachable
57: ; preds = %47
%58 = load i32, i32* @stderr
%59 = call i32 @fprintf(i32 %58, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0))
br label %60
60: ; preds = %57
%61 = load i8*, i8** %2
ret i8* %61
}

LLVM IR (-O2):
%struct.sembuf = type { i32, i32, i64 }
@.str = [8 x i8] c"Reason:\00"
@stderr = global i32 0
@.str.1 = [43 x i8] c"Thread2:Successfully locked 0th semaphore\0A\00"
@.str.2 = [43 x i8] c"Thread2:Successfully locked 1th semaphore\0A\00"
@.str.3 = [45 x i8] c"Thread2:Successfully unlocked 0th semaphore\0A\00"
@.str.4 = [45 x i8] c"Thread2:Successfully unlocked 1th semaphore\0A\00"
define noalias i8* @Thread2(i8* %0) {
%2 = alloca %struct.sembuf
%3 = alloca %struct.sembuf
%4 = ptrtoint i8* %0 to i64
%5 = trunc i64 %4 to i32
%6 = bitcast %struct.sembuf* %2 to i8*
%7 = bitcast %struct.sembuf* %3 to i8*
%8 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %2, i64 0, i32 0
store i32 0, i32* %8
%9 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %2, i64 0, i32 1
store i32 -1, i32* %9
%10 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %2, i64 0, i32 2
store i64 0, i64* %10
%11 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %3, i64 0, i32 0
store i32 1, i32* %11
%12 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %3, i64 0, i32 1
store i32 -1, i32* %12
%13 = getelementptr inbounds %struct.sembuf, %struct.sembuf* %3, i64 0, i32 2
store i64 0, i64* %13
%14 = call i32 @semop(i32 %5, %struct.sembuf* %2, i32 1)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %1
%17 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 -1)
unreachable
19: ; preds = %1
%20 = load i32, i32* @stderr
%21 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.1, i64 0, i64 0))
%22 = call i32 @semop(i32 %5, %struct.sembuf* %3, i32 1)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%26 = call i32 @exit(i32 -1)
unreachable
27: ; preds = %19
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %8
store i32 1, i32* %9
store i64 0, i64* %10
%30 = call i32 @semop(i32 %5, %struct.sembuf* %2, i32 1)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %27
%33 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%34 = call i32 @exit(i32 -1)
unreachable
35: ; preds = %27
%36 = load i32, i32* @stderr
%37 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %11
store i32 1, i32* %12
store i64 0, i64* %13
%38 = call i32 @semop(i32 %5, %struct.sembuf* %3, i32 1)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %43
40: ; preds = %35
%41 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%42 = call i32 @exit(i32 -1)
unreachable
43: ; preds = %35
%44 = load i32, i32* @stderr
%45 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.4, i64 0, i64 0))
ret i8* undef
}