#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:
    pthread_mutex_unlock(m2lockr);
    pthread_mutex_unlock(m2lockw);
    pthread_mutex_lock(m1lockr);
    *writeMemory = memory1 + sizeof(int);
    break;
  case 1:
    pthread_mutex_unlock(m1lockr);
    pthread_mutex_unlock(m1lockw);
    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     11c9 <switchMemory+0x89>
jmp    11f8 <switchMemory+0xb8>
mov    0x18(%rbp),%rdi
call   1040 <pthread_mutex_unlock@plt>
mov    0x10(%rbp),%rdi
call   1040 <pthread_mutex_unlock@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    1217 <switchMemory+0xd7>
mov    -0x30(%rbp),%rdi
call   1040 <pthread_mutex_unlock@plt>
mov    -0x28(%rbp),%rdi
call   1040 <pthread_mutex_unlock@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    1217 <switchMemory+0xd7>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
lea    0xdf8(%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    %rsi,%r14
mov    0x38(%rsp),%r12
xor    %eax,%eax
sub    (%rdi),%eax
mov    %eax,(%rdi)
cmp    $0xffffffff,%eax
je     1188 <switchMemory+0x48>
mov    %r8,%r15
mov    %rcx,%rbx
mov    %r9,%rdi
cmp    $0x1,%eax
je     1196 <switchMemory+0x56>
mov    0x2e70(%rip),%rax
mov    (%rax),%edi
lea    0xe87(%rip),%rsi
call   1030 <fprintf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    0x30(%rsp),%r15
mov    %r12,%rdi
mov    %r9,%r12
mov    %rdx,%rbx
call   1040 <pthread_mutex_unlock@plt>
mov    %r15,%rdi
call   1040 <pthread_mutex_unlock@plt>
mov    %r12,%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

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 %43 [
i32 -1, label %23
i32 1, label %33
]
23: ; preds = %8
%24 = load i32*, i32** %16
%25 = call i32 @pthread_mutex_unlock(i32* %24)
%26 = load i32*, i32** %15
%27 = call i32 @pthread_mutex_unlock(i32* %26)
%28 = load i32*, i32** %14
%29 = call i32 @pthread_mutex_lock(i32* %28)
%30 = load i8*, i8** %11
%31 = getelementptr i8, i8* %30, i64 4
%32 = load i8**, i8*** %10
store i8* %31, i8** %32
br label %47
33: ; preds = %8
%34 = load i32*, i32** %14
%35 = call i32 @pthread_mutex_unlock(i32* %34)
%36 = load i32*, i32** %13
%37 = call i32 @pthread_mutex_unlock(i32* %36)
%38 = load i32*, i32** %16
%39 = call i32 @pthread_mutex_lock(i32* %38)
%40 = load i8*, i8** %12
%41 = getelementptr i8, i8* %40, i64 4
%42 = load i8**, i8*** %10
store i8* %41, i8** %42
br label %47
43: ; preds = %8
%44 = load i32, i32* @stderr
%45 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %33, %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 i32* [ %5, %11 ], [ %7, %8 ]
%18 = phi i32* [ %4, %11 ], [ %6, %8 ]
%19 = phi i32* [ %7, %11 ], [ %5, %8 ]
%20 = phi i8* [ %3, %11 ], [ %2, %8 ]
%21 = tail call i32 @pthread_mutex_unlock(i32* %17)
%22 = tail call i32 @pthread_mutex_unlock(i32* %18)
%23 = tail call i32 @pthread_mutex_lock(i32* %19)
%24 = getelementptr i8, i8* %20, i64 4
store i8* %24, i8** %1
ret void
}