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

/* Variables and functions */
int /*<<< orphan*/ elock_acquire(int *);
int /*<<< orphan*/ elock_release(int *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(void *);
scalar_t__ malloc(int);
int /*<<< orphan*/ stderr;
int testfn();
int /*<<< orphan*/ xabort(int);
int xbegin();
int /*<<< orphan*/ xend();
scalar_t__ xtest();
int main() {
  fprintf(stderr, "htm test\n");
  if (xtest())
    fprintf(stderr, "In txn\n");
  else
    fprintf(stderr, "Not in txn\n");
  int lock = 0;
  elock_acquire(&lock);
  elock_release(&lock);
  fprintf(stderr, "Did lock test\n");
  int volatile *temp = (int volatile *)malloc(sizeof(int));
  *temp = 5;
  fprintf(stderr, "Do TXN test\n");
  int reason;
  reason = xbegin();
  if (reason == 0xFFFFFFFF) {
    int val = testfn();
    *temp += 100;
    if (*temp == 105) {
      xabort(3);
    } else {
      xabort(2);
    }
    xend();
    fprintf(stderr, "TXN 1 Ended\n");
  } else {
    fprintf(stderr, "TXN aborted, code %x\n", reason);
  }
  fprintf(stderr, "Temporary value is %x\n", *temp);
  reason = xbegin();
  if (reason == 0xFFFFFFFF) {
    int val = testfn();
    *temp += 100;
    xend();
    fprintf(stderr, "TXN 2 Ended\n");
  } else {
    fprintf(stderr, "ERROR: Correct txn aborted\n");
  }
  fprintf(stderr, "Temporary value is %d\n", *temp);
  free((void *)temp);
  if (xtest())
    fprintf(stderr, "Major error outside of txn 1\n");
  if (xtest())
    fprintf(stderr, "Major error outside of txn 3\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
lea    0xe41(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x0,%al
call   1080 <xtest@plt>
cmp    $0x0,%rax
je     11f3 <main+0x53>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
lea    0xe23(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    120a <main+0x6a>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
lea    0xe0f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x8(%rbp)
lea    -0x8(%rbp),%rdi
call   10b0 <elock_acquire@plt>
lea    -0x8(%rbp),%rdi
call   1070 <elock_release@plt>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
lea    0xdeb(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x4,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movl   $0x5,(%rax)
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
lea    0xdcb(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x0,%al
call   1090 <xbegin@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    12dd <main+0x13d>
mov    $0x0,%al
call   10c0 <testfn@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
add    $0x64,%ecx
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
cmp    $0x69,%eax
jne    12b0 <main+0x110>
mov    $0x3,%edi
call   10a0 <xabort@plt>
jmp    12ba <main+0x11a>
mov    $0x2,%edi
call   10a0 <xabort@plt>
mov    $0x0,%al
call   1040 <xend@plt>
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
lea    0xd69(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    12f7 <main+0x157>
mov    0x2cfc(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
lea    0xd57(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2ce2(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
lea    0xd50(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x0,%al
call   1090 <xbegin@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1360 <main+0x1c0>
mov    $0x0,%al
call   10c0 <testfn@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
add    $0x64,%ecx
mov    %ecx,(%rax)
mov    $0x0,%al
call   1040 <xend@plt>
mov    0x2c95(%rip),%rax
mov    (%rax),%edi
lea    0xd20(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1377 <main+0x1d7>
mov    0x2c79(%rip),%rax
mov    (%rax),%edi
lea    0xd11(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2c62(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
lea    0xd10(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    $0x0,%al
call   1080 <xtest@plt>
cmp    $0x0,%rax
je     13c5 <main+0x225>
mov    0x2c2b(%rip),%rax
mov    (%rax),%edi
lea    0xcf6(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x0,%al
call   1080 <xtest@plt>
cmp    $0x0,%rax
je     13ed <main+0x24d>
mov    0x2c03(%rip),%rax
mov    (%rax),%edi
lea    0xcec(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    0x2e35(%rip),%r14
mov    (%r14),%edi
lea    0xe4b(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
xor    %eax,%eax
call   1080 <xtest@plt>
test   %rax,%rax
mov    (%r14),%edi
lea    0xe42(%rip),%rax
lea    0xe33(%rip),%rsi
cmove  %rax,%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
movl   $0x0,0x4(%rsp)
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   10b0 <elock_acquire@plt>
mov    %rbx,%rdi
call   1070 <elock_release@plt>
mov    (%r14),%edi
lea    0xe15(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x4,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
movl   $0x5,(%rax)
mov    (%r14),%edi
lea    0xe00(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
xor    %eax,%eax
call   1090 <xbegin@plt>
cmp    $0xffffffff,%eax
je     1255 <main+0xb5>
mov    (%r14),%edi
lea    0xdfd(%rip),%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    128a <main+0xea>
xor    %eax,%eax
call   10c0 <testfn@plt>
addl   $0x64,(%rbx)
mov    (%rbx),%eax
xor    %edi,%edi
cmp    $0x69,%eax
sete   %dil
or     $0x2,%edi
call   10a0 <xabort@plt>
xor    %eax,%eax
call   1040 <xend@plt>
mov    (%r14),%edi
lea    0xdb7(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%r14),%edi
mov    (%rbx),%edx
lea    0xdc7(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
xor    %eax,%eax
call   1090 <xbegin@plt>
cmp    $0xffffffff,%eax
je     12b2 <main+0x112>
lea    0xdd1(%rip),%rsi
jmp    12ca <main+0x12a>
xor    %eax,%eax
call   10c0 <testfn@plt>
addl   $0x64,(%rbx)
xor    %eax,%eax
call   1040 <xend@plt>
lea    0xdaa(%rip),%rsi
mov    (%r14),%edi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%r14),%edi
mov    (%rbx),%edx
lea    0xdbd(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
call   1080 <xtest@plt>
test   %rax,%rax
je     130c <main+0x16c>
mov    (%r14),%edi
lea    0xdaf(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
xor    %eax,%eax
call   1080 <xtest@plt>
test   %rax,%rax
je     1329 <main+0x189>
mov    (%r14),%edi
lea    0xdb0(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [10 x i8] c"htm test\0A\00"
@.str.1 = [8 x i8] c"In txn\0A\00"
@.str.2 = [12 x i8] c"Not in txn\0A\00"
@.str.3 = [15 x i8] c"Did lock test\0A\00"
@.str.4 = [13 x i8] c"Do TXN test\0A\00"
@.str.5 = [13 x i8] c"TXN 1 Ended\0A\00"
@.str.6 = [22 x i8] c"TXN aborted, code %x\0A\00"
@.str.7 = [23 x i8] c"Temporary value is %x\0A\00"
@.str.8 = [13 x i8] c"TXN 2 Ended\0A\00"
@.str.9 = [28 x i8] c"ERROR: Correct txn aborted\0A\00"
@.str.10 = [23 x i8] c"Temporary value is %d\0A\00"
@.str.11 = [30 x i8] c"Major error outside of txn 1\0A\00"
@.str.12 = [30 x i8] c"Major error outside of txn 3\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = load i32, i32* @stderr
%8 = call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%9 = call i64 (...) @xtest()
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = load i32, i32* @stderr
%13 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %17
14: ; preds = %0
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %14, %11
store i32 0, i32* %2
%18 = call i32 @elock_acquire(i32* %2)
%19 = call i32 @elock_release(i32* %2)
%20 = load i32, i32* @stderr
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%22 = call i64 @malloc(i32 4)
%23 = inttoptr i64 %22 to i32*
store i32* %23, i32** %3
%24 = load i32*, i32** %3
store volatile i32 5, i32* %24
%25 = load i32, i32* @stderr
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%27 = call i32 (...) @xbegin()
store i32 %27, i32* %4
%28 = load i32, i32* %4
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %46
30: ; preds = %17
%31 = call i32 (...) @testfn()
store i32 %31, i32* %5
%32 = load i32*, i32** %3
%33 = load volatile i32, i32* %32
%34 = add nsw i32 %33, 100
store volatile i32 %34, i32* %32
%35 = load i32*, i32** %3
%36 = load volatile i32, i32* %35
%37 = icmp eq i32 %36, 105
br i1 %37, label %38, label %40
38: ; preds = %30
%39 = call i32 @xabort(i32 3)
br label %42
40: ; preds = %30
%41 = call i32 @xabort(i32 2)
br label %42
42: ; preds = %40, %38
%43 = call i32 (...) @xend()
%44 = load i32, i32* @stderr
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
br label %50
46: ; preds = %17
%47 = load i32, i32* @stderr
%48 = load i32, i32* %4
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i32 %48)
br label %50
50: ; preds = %46, %42
%51 = load i32, i32* @stderr
%52 = load i32*, i32** %3
%53 = load volatile i32, i32* %52
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0), i32 %53)
%55 = call i32 (...) @xbegin()
store i32 %55, i32* %4
%56 = load i32, i32* %4
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %66
58: ; preds = %50
%59 = call i32 (...) @testfn()
store i32 %59, i32* %6
%60 = load i32*, i32** %3
%61 = load volatile i32, i32* %60
%62 = add nsw i32 %61, 100
store volatile i32 %62, i32* %60
%63 = call i32 (...) @xend()
%64 = load i32, i32* @stderr
%65 = call i32 (i32, i8*, ...) @fprintf(i32 %64, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0))
br label %69
66: ; preds = %50
%67 = load i32, i32* @stderr
%68 = call i32 (i32, i8*, ...) @fprintf(i32 %67, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.9, i64 0, i64 0))
br label %69
69: ; preds = %66, %58
%70 = load i32, i32* @stderr
%71 = load i32*, i32** %3
%72 = load volatile i32, i32* %71
%73 = call i32 (i32, i8*, ...) @fprintf(i32 %70, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.10, i64 0, i64 0), i32 %72)
%74 = load i32*, i32** %3
%75 = bitcast i32* %74 to i8*
%76 = call i32 @free(i8* %75)
%77 = call i64 (...) @xtest()
%78 = icmp ne i64 %77, 0
br i1 %78, label %79, label %82
79: ; preds = %69
%80 = load i32, i32* @stderr
%81 = call i32 (i32, i8*, ...) @fprintf(i32 %80, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.11, i64 0, i64 0))
br label %82
82: ; preds = %79, %69
%83 = call i64 (...) @xtest()
%84 = icmp ne i64 %83, 0
br i1 %84, label %85, label %88
85: ; preds = %82
%86 = load i32, i32* @stderr
%87 = call i32 (i32, i8*, ...) @fprintf(i32 %86, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.12, i64 0, i64 0))
br label %88
88: ; preds = %85, %82
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [10 x i8] c"htm test\0A\00"
@.str.1 = [8 x i8] c"In txn\0A\00"
@.str.2 = [12 x i8] c"Not in txn\0A\00"
@.str.3 = [15 x i8] c"Did lock test\0A\00"
@.str.4 = [13 x i8] c"Do TXN test\0A\00"
@.str.5 = [13 x i8] c"TXN 1 Ended\0A\00"
@.str.6 = [22 x i8] c"TXN aborted, code %x\0A\00"
@.str.7 = [23 x i8] c"Temporary value is %x\0A\00"
@.str.8 = [13 x i8] c"TXN 2 Ended\0A\00"
@.str.9 = [28 x i8] c"ERROR: Correct txn aborted\0A\00"
@.str.10 = [23 x i8] c"Temporary value is %d\0A\00"
@.str.11 = [30 x i8] c"Major error outside of txn 1\0A\00"
@.str.12 = [30 x i8] c"Major error outside of txn 3\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = load i32, i32* @stderr
%3 = tail call i32 (i32, i8*, ...) @fprintf(i32 %2, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%4 = tail call i64 (...) @xtest()
%5 = icmp eq i64 %4, 0
%6 = load i32, i32* @stderr
%7 = select i1 %5, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0)
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* %7)
%9 = bitcast i32* %1 to i8*
store i32 0, i32* %1
%10 = call i32 @elock_acquire(i32* %1)
%11 = call i32 @elock_release(i32* %1)
%12 = load i32, i32* @stderr
%13 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%14 = call i64 @malloc(i32 4)
%15 = inttoptr i64 %14 to i32*
store volatile i32 5, i32* %15
%16 = load i32, i32* @stderr
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%18 = call i32 (...) @xbegin()
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %31
20: ; preds = %0
%21 = call i32 (...) @testfn()
%22 = load volatile i32, i32* %15
%23 = add nsw i32 %22, 100
store volatile i32 %23, i32* %15
%24 = load volatile i32, i32* %15
%25 = icmp eq i32 %24, 105
%26 = select i1 %25, i32 3, i32 2
%27 = call i32 @xabort(i32 %26)
%28 = call i32 (...) @xend()
%29 = load i32, i32* @stderr
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
br label %34
31: ; preds = %0
%32 = load i32, i32* @stderr
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i32 %18)
br label %34
34: ; preds = %31, %20
%35 = load i32, i32* @stderr
%36 = load volatile i32, i32* %15
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.7, i64 0, i64 0), i32 %36)
%38 = call i32 (...) @xbegin()
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %45
40: ; preds = %34
%41 = call i32 (...) @testfn()
%42 = load volatile i32, i32* %15
%43 = add nsw i32 %42, 100
store volatile i32 %43, i32* %15
%44 = call i32 (...) @xend()
br label %45
45: ; preds = %34, %40
%46 = phi i8* [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.8, i64 0, i64 0), %40 ], [ getelementptr inbounds ([28 x i8], [28 x i8]* @.str.9, i64 0, i64 0), %34 ]
%47 = load i32, i32* @stderr
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* %46)
%49 = load i32, i32* @stderr
%50 = load volatile i32, i32* %15
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.10, i64 0, i64 0), i32 %50)
%52 = inttoptr i64 %14 to i8*
%53 = call i32 @free(i8* %52)
%54 = call i64 (...) @xtest()
%55 = icmp eq i64 %54, 0
br i1 %55, label %59, label %56
56: ; preds = %45
%57 = load i32, i32* @stderr
%58 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.11, i64 0, i64 0))
br label %59
59: ; preds = %56, %45
%60 = call i64 (...) @xtest()
%61 = icmp eq i64 %60, 0
br i1 %61, label %65, label %62
62: ; preds = %59
%63 = load i32, i32* @stderr
%64 = call i32 (i32, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.12, i64 0, i64 0))
br label %65
65: ; preds = %62, %59
ret i32 0
}