#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 spinlock {
  int dummy;
};

/* Variables and functions */
int MAP_ANONYMOUS;
char *MAP_FAILED;
int MAP_SHARED;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ assert(int);
int fork();
char *mmap(int /*<<< orphan*/ *, long, int, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ spin_lock(struct spinlock *);
int /*<<< orphan*/ spin_lock_init(struct spinlock *);
int /*<<< orphan*/ spin_unlock(struct spinlock *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main(int argc, char **argv) {
  char *mem;
  int *buf;
  struct spinlock *lock;
  long bufsz;
  int i, j;
  int rc;
  mem = mmap(NULL, (1L << 20), PROT_READ | PROT_WRITE,
             MAP_SHARED | MAP_ANONYMOUS, -1, 0);
  assert(mem != MAP_FAILED);
  lock = (struct spinlock *)mem;
  spin_lock_init(lock);
  buf = (int *)(mem + 4096);
  bufsz = ((1L << 20) - 4096) / sizeof(*buf);
  printf("buf @ %p sz %ld\n", buf, bufsz);
  rc = fork();
  for (j = 0; j < 100L; j++)
    for (i = 0; i < bufsz; i++) {
      spin_lock(lock);
      buf[i]++;
      spin_unlock(lock);
    }
  if (rc)
    wait(NULL);
  else
    _exit(0);
  for (i = 0; i < bufsz; i++)
    assert(buf[i] == 2 * 100L);
  printf("SUCCESS! exiting\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e4b(%rip),%rax
mov    (%rax),%edx
mov    0x2e12(%rip),%rax
or     (%rax),%edx
mov    0x2e29(%rip),%rax
mov    (%rax),%ecx
mov    0x2e10(%rip),%rax
or     (%rax),%ecx
xor    %eax,%eax
mov    %eax,%edi
mov    $0x100000,%esi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    0x2dd0(%rip),%rcx
cmp    (%rcx),%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <spin_lock_init@plt>
mov    -0x18(%rbp),%rax
add    $0x1000,%rax
mov    %rax,-0x20(%rbp)
movq   $0x3fc00,-0x30(%rbp)
mov    -0x20(%rbp),%rsi
mov    -0x30(%rbp),%rdx
lea    0xdca(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x3c(%rbp)
movl   $0x0,-0x38(%rbp)
movslq -0x38(%rbp),%rax
cmp    $0x64,%rax
jge    12b5 <main+0x125>
movl   $0x0,-0x34(%rbp)
movslq -0x34(%rbp),%rax
cmp    -0x30(%rbp),%rax
jge    12a2 <main+0x112>
mov    -0x28(%rbp),%rdi
call   1070 <spin_lock@plt>
mov    -0x20(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
add    $0x1,%edx
mov    %edx,(%rax,%rcx,4)
mov    -0x28(%rbp),%rdi
call   1090 <spin_unlock@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1263 <main+0xd3>
jmp    12a7 <main+0x117>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    124e <main+0xbe>
cmpl   $0x0,-0x3c(%rbp)
je     12cd <main+0x13d>
xor    %eax,%eax
mov    %eax,%edi
call   10a0 <wait@plt>
jmp    12d4 <main+0x144>
xor    %edi,%edi
call   1030 <_exit@plt>
movl   $0x0,-0x34(%rbp)
movslq -0x34(%rbp),%rax
cmp    -0x30(%rbp),%rax
jge    1316 <main+0x186>
mov    -0x20(%rbp),%rax
movslq -0x34(%rbp),%rcx
movslq (%rax,%rcx,4),%rax
cmp    $0xc8,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    12db <main+0x14b>
lea    0xcf4(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e4a(%rip),%rax
mov    0x2e13(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e28(%rip),%rax
mov    0x2e11(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %r15d,%r15d
mov    $0x100000,%esi
xor    %edi,%edi
mov    $0xffffffff,%r8d
xor    %r9d,%r9d
call   1060 <mmap@plt>
mov    %rax,%rbx
mov    0x2dd3(%rip),%rax
xor    %edi,%edi
cmp    (%rax),%rbx
setne  %dil
call   1090 <assert@plt>
mov    %rbx,%rdi
call   1050 <spin_lock_init@plt>
mov    %rbx,%rsi
add    $0x1000,%rsi
lea    0xdec(%rip),%rdi
mov    $0x3fc00,%edx
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %eax,%r14d
nopw   0x0(%rax,%rax,1)
mov    $0x400,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
call   1080 <spin_lock@plt>
addl   $0x1,(%rbx,%rbp,4)
mov    %rbx,%rdi
call   10a0 <spin_unlock@plt>
add    $0x1,%rbp
cmp    $0x40000,%rbp
jne    1240 <main+0xa0>
add    $0x1,%r15d
cmp    $0x64,%r15d
jne    1230 <main+0x90>
test   %r14d,%r14d
je     12b8 <main+0x118>
xor    %edi,%edi
call   10b0 <wait@plt>
mov    $0x400,%ebp
nopl   0x0(%rax)
xor    %edi,%edi
cmpl   $0xc8,(%rbx,%rbp,4)
sete   %dil
call   1090 <assert@plt>
add    $0x1,%rbp
cmp    $0x40000,%rbp
jne    1280 <main+0xe0>
lea    0xd6b(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edi,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
%struct.spinlock = type { i32 }
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_FAILED = global i8* null
@.str = [17 x i8] c"buf @ %p sz %ld\0A\00"
@.str.1 = [18 x i8] c"SUCCESS! exiting\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca %struct.spinlock*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* @PROT_READ
%14 = load i32, i32* @PROT_WRITE
%15 = or i32 %13, %14
%16 = load i32, i32* @MAP_SHARED
%17 = load i32, i32* @MAP_ANONYMOUS
%18 = or i32 %16, %17
%19 = call i8* @mmap(i32* null, i64 1048576, i32 %15, i32 %18, i32 -1, i32 0)
store i8* %19, i8** %6
%20 = load i8*, i8** %6
%21 = load i8*, i8** @MAP_FAILED
%22 = icmp ne i8* %20, %21
%23 = zext i1 %22 to i32
%24 = call i32 @assert(i32 %23)
%25 = load i8*, i8** %6
%26 = bitcast i8* %25 to %struct.spinlock*
store %struct.spinlock* %26, %struct.spinlock** %8
%27 = load %struct.spinlock*, %struct.spinlock** %8
%28 = call i32 @spin_lock_init(%struct.spinlock* %27)
%29 = load i8*, i8** %6
%30 = getelementptr inbounds i8, i8* %29, i64 4096
%31 = bitcast i8* %30 to i32*
store i32* %31, i32** %7
store i64 261120, i64* %9
%32 = load i32*, i32** %7
%33 = load i64, i64* %9
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32* %32, i64 %33)
%35 = call i32 (...) @fork()
store i32 %35, i32* %12
store i32 0, i32* %11
br label %36
36: ; preds = %61, %2
%37 = load i32, i32* %11
%38 = sext i32 %37 to i64
%39 = icmp slt i64 %38, 100
br i1 %39, label %40, label %64
40: ; preds = %36
store i32 0, i32* %10
br label %41
41: ; preds = %57, %40
%42 = load i32, i32* %10
%43 = sext i32 %42 to i64
%44 = load i64, i64* %9
%45 = icmp slt i64 %43, %44
br i1 %45, label %46, label %60
46: ; preds = %41
%47 = load %struct.spinlock*, %struct.spinlock** %8
%48 = call i32 @spin_lock(%struct.spinlock* %47)
%49 = load i32*, i32** %7
%50 = load i32, i32* %10
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i32, i32* %49, i64 %51
%53 = load i32, i32* %52
%54 = add nsw i32 %53, 1
store i32 %54, i32* %52
%55 = load %struct.spinlock*, %struct.spinlock** %8
%56 = call i32 @spin_unlock(%struct.spinlock* %55)
br label %57
57: ; preds = %46
%58 = load i32, i32* %10
%59 = add nsw i32 %58, 1
store i32 %59, i32* %10
br label %41
60: ; preds = %41
br label %61
61: ; preds = %60
%62 = load i32, i32* %11
%63 = add nsw i32 %62, 1
store i32 %63, i32* %11
br label %36
64: ; preds = %36
%65 = load i32, i32* %12
%66 = icmp ne i32 %65, 0
br i1 %66, label %67, label %69
67: ; preds = %64
%68 = call i32 @wait(i32* null)
br label %71
69: ; preds = %64
%70 = call i32 @_exit(i32 0)
unreachable
71: ; preds = %67
store i32 0, i32* %10
br label %72
72: ; preds = %87, %71
%73 = load i32, i32* %10
%74 = sext i32 %73 to i64
%75 = load i64, i64* %9
%76 = icmp slt i64 %74, %75
br i1 %76, label %77, label %90
77: ; preds = %72
%78 = load i32*, i32** %7
%79 = load i32, i32* %10
%80 = sext i32 %79 to i64
%81 = getelementptr inbounds i32, i32* %78, i64 %80
%82 = load i32, i32* %81
%83 = sext i32 %82 to i64
%84 = icmp eq i64 %83, 200
%85 = zext i1 %84 to i32
%86 = call i32 @assert(i32 %85)
br label %87
87: ; preds = %77
%88 = load i32, i32* %10
%89 = add nsw i32 %88, 1
store i32 %89, i32* %10
br label %72
90: ; preds = %72
%91 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
%struct.spinlock = type { i32 }
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_ANONYMOUS = global i32 0
@MAP_FAILED = global i8* null
@.str = [17 x i8] c"buf @ %p sz %ld\0A\00"
@str = [17 x i8] c"SUCCESS! exiting\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @PROT_READ
%4 = load i32, i32* @PROT_WRITE
%5 = or i32 %4, %3
%6 = load i32, i32* @MAP_SHARED
%7 = load i32, i32* @MAP_ANONYMOUS
%8 = or i32 %7, %6
%9 = tail call i8* @mmap(i32* null, i64 1048576, i32 %5, i32 %8, i32 -1, i32 0)
%10 = load i8*, i8** @MAP_FAILED
%11 = icmp ne i8* %9, %10
%12 = zext i1 %11 to i32
%13 = tail call i32 @assert(i32 %12)
%14 = bitcast i8* %9 to %struct.spinlock*
%15 = tail call i32 @spin_lock_init(%struct.spinlock* %14)
%16 = getelementptr inbounds i8, i8* %9, i64 4096
%17 = bitcast i8* %16 to i32*
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %16, i64 261120)
%19 = tail call i32 (...) @fork()
br label %20
20: ; preds = %2, %31
%21 = phi i32 [ 0, %2 ], [ %32, %31 ]
br label %22
22: ; preds = %20, %22
%23 = phi i64 [ 0, %20 ], [ %29, %22 ]
%24 = tail call i32 @spin_lock(%struct.spinlock* %14)
%25 = getelementptr inbounds i32, i32* %17, i64 %23
%26 = load i32, i32* %25
%27 = add nsw i32 %26, 1
store i32 %27, i32* %25
%28 = tail call i32 @spin_unlock(%struct.spinlock* %14)
%29 = add nuw nsw i64 %23, 1
%30 = icmp eq i64 %29, 261120
br i1 %30, label %31, label %22
31: ; preds = %22
%32 = add nuw nsw i32 %21, 1
%33 = icmp eq i32 %32, 100
br i1 %33, label %34, label %20
34: ; preds = %31
%35 = icmp eq i32 %19, 0
br i1 %35, label %38, label %36
36: ; preds = %34
%37 = tail call i32 @wait(i32* null)
br label %40
38: ; preds = %34
%39 = tail call i32 @_exit(i32 0)
unreachable
40: ; preds = %36, %40
%41 = phi i64 [ 0, %36 ], [ %47, %40 ]
%42 = getelementptr inbounds i32, i32* %17, i64 %41
%43 = load i32, i32* %42
%44 = icmp eq i32 %43, 200
%45 = zext i1 %44 to i32
%46 = tail call i32 @assert(i32 %45)
%47 = add nuw nsw i64 %41, 1
%48 = icmp eq i64 %47, 261120
br i1 %48, label %49, label %40
49: ; preds = %40
%50 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
ret i32 0
}