#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*/ pthread_mutex_t;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ pthread_mutex_lock(int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_mutex_unlock(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
void SwitchMemory(int *currentMemory, void **writeMemory, void *memory1,
                  void *memory2, pthread_mutex_t *m1lockw,
                  pthread_mutex_t *m1lockr, pthread_mutex_t *m2lockw,
                  pthread_mutex_t *m2lockr) {
  *currentMemory = -(*currentMemory);
  switch (*currentMemory) {
  case -1:
    (*(int *)memory2) = 2;
    pthread_mutex_unlock(m2lockr);
    pthread_mutex_lock(m1lockw);
    pthread_mutex_lock(m1lockr);
    *writeMemory = memory1 + sizeof(int);
    break;
  case 1:
    (*(int *)memory1) = 2;
    pthread_mutex_unlock(m1lockr);
    pthread_mutex_lock(m2lockw);
    pthread_mutex_lock(m2lockr);
    *writeMemory = memory2 + sizeof(int);
    break;
  default:
    fprintf(stderr, "switch mem error");
    exit(1);
    break;
  }
}


x86 assembly (clang, -O0):
<SwitchMemory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x18(%rbp),%rax
mov    0x10(%rbp),%rax
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
neg    %ecx
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
sub    $0xffffffff,%eax
je     119a <SwitchMemory+0x5a>
jmp    1189 <SwitchMemory+0x49>
mov    -0x34(%rbp),%eax
sub    $0x1,%eax
je     11d3 <SwitchMemory+0x93>
jmp    120c <SwitchMemory+0xcc>
mov    -0x20(%rbp),%rax
movl   $0x2,(%rax)
mov    0x18(%rbp),%rdi
call   1040 <pthread_mutex_unlock@plt>
mov    -0x28(%rbp),%rdi
call   1060 <pthread_mutex_lock@plt>
mov    -0x30(%rbp),%rdi
call   1060 <pthread_mutex_lock@plt>
mov    -0x18(%rbp),%rcx
add    $0x4,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
jmp    122b <SwitchMemory+0xeb>
mov    -0x18(%rbp),%rax
movl   $0x2,(%rax)
mov    -0x30(%rbp),%rdi
call   1040 <pthread_mutex_unlock@plt>
mov    0x10(%rbp),%rdi
call   1060 <pthread_mutex_lock@plt>
mov    0x18(%rbp),%rdi
call   1060 <pthread_mutex_lock@plt>
mov    -0x20(%rbp),%rcx
add    $0x4,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
jmp    122b <SwitchMemory+0xeb>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
lea    0xde4(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SwitchMemory>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rcx,%rbx
mov    %rsi,%r14
mov    0x38(%rsp),%r15
xor    %eax,%eax
sub    (%rdi),%eax
mov    %eax,(%rdi)
cmp    $0xffffffff,%eax
je     1170 <SwitchMemory+0x30>
cmp    $0x1,%eax
jne    11ad <SwitchMemory+0x6d>
mov    0x30(%rsp),%r12
mov    %rdx,%rax
mov    %r9,%rdi
jmp    117f <SwitchMemory+0x3f>
mov    %r8,%r12
mov    %rbx,%rax
mov    %r15,%rdi
mov    %r9,%r15
mov    %rdx,%rbx
movl   $0x2,(%rax)
call   1040 <pthread_mutex_unlock@plt>
mov    %r12,%rdi
call   1060 <pthread_mutex_lock@plt>
mov    %r15,%rdi
call   1060 <pthread_mutex_lock@plt>
add    $0x4,%rbx
mov    %rbx,(%r14)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
lea    0xe43(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [17 x i8] c"switch mem error\00"
define void @SwitchMemory(i32* %0, i8** %1, i8* %2, i8* %3, i32* %4, i32* %5, i32* %6, i32* %7) {
%9 = alloca i32*
%10 = alloca i8**
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i32*
%14 = alloca i32*
%15 = alloca i32*
%16 = alloca i32*
store i32* %0, i32** %9
store i8** %1, i8*** %10
store i8* %2, i8** %11
store i8* %3, i8** %12
store i32* %4, i32** %13
store i32* %5, i32** %14
store i32* %6, i32** %15
store i32* %7, i32** %16
%17 = load i32*, i32** %9
%18 = load i32, i32* %17
%19 = sub nsw i32 0, %18
%20 = load i32*, i32** %9
store i32 %19, i32* %20
%21 = load i32*, i32** %9
%22 = load i32, i32* %21
switch i32 %22, label %47 [
i32 -1, label %23
i32 1, label %35
]
23: ; preds = %8
%24 = load i8*, i8** %12
%25 = bitcast i8* %24 to i32*
store i32 2, i32* %25
%26 = load i32*, i32** %16
%27 = call i32 @pthread_mutex_unlock(i32* %26)
%28 = load i32*, i32** %13
%29 = call i32 @pthread_mutex_lock(i32* %28)
%30 = load i32*, i32** %14
%31 = call i32 @pthread_mutex_lock(i32* %30)
%32 = load i8*, i8** %11
%33 = getelementptr i8, i8* %32, i64 4
%34 = load i8**, i8*** %10
store i8* %33, i8** %34
br label %51
35: ; preds = %8
%36 = load i8*, i8** %11
%37 = bitcast i8* %36 to i32*
store i32 2, i32* %37
%38 = load i32*, i32** %14
%39 = call i32 @pthread_mutex_unlock(i32* %38)
%40 = load i32*, i32** %15
%41 = call i32 @pthread_mutex_lock(i32* %40)
%42 = load i32*, i32** %16
%43 = call i32 @pthread_mutex_lock(i32* %42)
%44 = load i8*, i8** %12
%45 = getelementptr i8, i8* %44, i64 4
%46 = load i8**, i8*** %10
store i8* %45, i8** %46
br label %51
47: ; preds = %8
%48 = load i32, i32* @stderr
%49 = call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%50 = call i32 @exit(i32 1)
unreachable
51: ; preds = %35, %23
ret void
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [17 x i8] c"switch mem error\00"
define void @SwitchMemory(i32* nocapture %0, i8** nocapture writeonly %1, i8* %2, i8* %3, i32* %4, i32* %5, i32* %6, i32* %7) {
%9 = load i32, i32* %0
%10 = sub nsw i32 0, %9
store i32 %10, i32* %0
switch i32 %10, label %12 [
i32 -1, label %16
i32 1, label %11
]
11: ; preds = %8
br label %16
12: ; preds = %8
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %8, %11
%17 = phi i8* [ %2, %11 ], [ %3, %8 ]
%18 = phi i32* [ %5, %11 ], [ %7, %8 ]
%19 = phi i32* [ %6, %11 ], [ %4, %8 ]
%20 = phi i32* [ %7, %11 ], [ %5, %8 ]
%21 = phi i8* [ %3, %11 ], [ %2, %8 ]
%22 = bitcast i8* %17 to i32*
store i32 2, i32* %22
%23 = tail call i32 @pthread_mutex_unlock(i32* %18)
%24 = tail call i32 @pthread_mutex_lock(i32* %19)
%25 = tail call i32 @pthread_mutex_lock(i32* %20)
%26 = getelementptr i8, i8* %21, i64 4
store i8* %26, i8** %1
ret void
}