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

/* Variables and functions */
int /*<<< orphan*/ ARG0(struct dune_tf *);
scalar_t__ ARG1(struct dune_tf *);
int /*<<< orphan*/ ARG2(struct dune_tf *);
int /*<<< orphan*/ ARG3(struct dune_tf *);
int /*<<< orphan*/ ARG4(struct dune_tf *);
int /*<<< orphan*/ SYS_clone;
int /*<<< orphan*/ dune_enter();
unsigned long dune_get_user_fs();
long dune_pthread_create(struct dune_tf *);
int /*<<< orphan*/ dune_set_user_fs(unsigned long);
long errno;
unsigned long native_read_msr(int);
int syscall(int /*<<< orphan*/, int /*<<< orphan*/, scalar_t__,
            int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
long dune_clone(struct dune_tf *tf) {
  unsigned long fs;
  int rc;
  unsigned long pc;
  ((pc) = native_read_msr((0xc0000101)));
  if (ARG1(tf) != 0)
    return dune_pthread_create(tf);
  fs = dune_get_user_fs();
  rc = syscall(SYS_clone, ARG0(tf), ARG1(tf), ARG2(tf), ARG3(tf), ARG4(tf));
  if (rc < 0)
    return -errno;
  if (rc == 0) {
    dune_enter();
    dune_set_user_fs(fs);
  }
  return rc;
}


x86 assembly (clang, -O0):
<dune_clone>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0xc0000101,%edi
call   10a0 <native_read_msr@plt>
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   10b0 <ARG1@plt>
cmp    $0x0,%rax
je     11ef <dune_clone+0x3f>
mov    -0x10(%rbp),%rdi
call   10d0 <dune_pthread_create@plt>
mov    %rax,-0x8(%rbp)
jmp    129d <dune_clone+0xed>
mov    $0x0,%al
call   1060 <dune_get_user_fs@plt>
mov    %rax,-0x18(%rbp)
mov    0x2de7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <ARG0@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
call   10b0 <ARG1@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <ARG2@plt>
mov    %eax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   10c0 <ARG3@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <ARG4@plt>
mov    -0x40(%rbp),%edi
mov    -0x3c(%rbp),%esi
mov    -0x38(%rbp),%rdx
mov    -0x30(%rbp),%ecx
mov    -0x2c(%rbp),%r8d
mov    %eax,%r9d
call   1090 <syscall@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    127b <dune_clone+0xcb>
xor    %eax,%eax
mov    0x2d69(%rip),%rcx
sub    (%rcx),%rax
mov    %rax,-0x8(%rbp)
jmp    129d <dune_clone+0xed>
cmpl   $0x0,-0x1c(%rbp)
jne    1295 <dune_clone+0xe5>
mov    $0x0,%al
call   1080 <dune_enter@plt>
mov    -0x18(%rbp),%rdi
call   1070 <dune_set_user_fs@plt>
movslq -0x1c(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dune_clone>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
mov    $0xc0000101,%edi
call   10a0 <native_read_msr@plt>
mov    %rbx,%rdi
call   10b0 <ARG1@plt>
test   %rax,%rax
je     11eb <dune_clone+0x3b>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10d0 <dune_pthread_create@plt>
xor    %eax,%eax
call   1060 <dune_get_user_fs@plt>
mov    %rax,(%rsp)
mov    0x2deb(%rip),%rax
mov    (%rax),%r15d
mov    %rbx,%rdi
call   1030 <ARG0@plt>
mov    %eax,%r12d
mov    %rbx,%rdi
call   10b0 <ARG1@plt>
mov    %rax,%r13
mov    %rbx,%rdi
call   1050 <ARG2@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   10c0 <ARG3@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1040 <ARG4@plt>
mov    %r15d,%edi
mov    %r12d,%esi
mov    %r13,%rdx
mov    %ebp,%ecx
mov    %r14d,%r8d
mov    %eax,%r9d
call   1090 <syscall@plt>
test   %eax,%eax
js     1265 <dune_clone+0xb5>
mov    %eax,%ebx
jne    1261 <dune_clone+0xb1>
xor    %eax,%eax
call   1080 <dune_enter@plt>
mov    (%rsp),%rdi
call   1070 <dune_set_user_fs@plt>
mov    %ebx,%eax
jmp    1271 <dune_clone+0xc1>
mov    0x2d6c(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dune_tf = type { i32 }
@SYS_clone = global i32 0
@errno = global i64 0
define i64 @dune_clone(%struct.dune_tf* %0) {
%2 = alloca i64
%3 = alloca %struct.dune_tf*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i64
store %struct.dune_tf* %0, %struct.dune_tf** %3
%7 = call i64 @native_read_msr(i32 -1073741567)
store i64 %7, i64* %6
%8 = load %struct.dune_tf*, %struct.dune_tf** %3
%9 = call i64 @ARG1(%struct.dune_tf* %8)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load %struct.dune_tf*, %struct.dune_tf** %3
%13 = call i64 @dune_pthread_create(%struct.dune_tf* %12)
store i64 %13, i64* %2
br label %43
14: ; preds = %1
%15 = call i64 (...) @dune_get_user_fs()
store i64 %15, i64* %4
%16 = load i32, i32* @SYS_clone
%17 = load %struct.dune_tf*, %struct.dune_tf** %3
%18 = call i32 @ARG0(%struct.dune_tf* %17)
%19 = load %struct.dune_tf*, %struct.dune_tf** %3
%20 = call i64 @ARG1(%struct.dune_tf* %19)
%21 = load %struct.dune_tf*, %struct.dune_tf** %3
%22 = call i32 @ARG2(%struct.dune_tf* %21)
%23 = load %struct.dune_tf*, %struct.dune_tf** %3
%24 = call i32 @ARG3(%struct.dune_tf* %23)
%25 = load %struct.dune_tf*, %struct.dune_tf** %3
%26 = call i32 @ARG4(%struct.dune_tf* %25)
%27 = call i32 @syscall(i32 %16, i32 %18, i64 %20, i32 %22, i32 %24, i32 %26)
store i32 %27, i32* %5
%28 = load i32, i32* %5
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %14
%31 = load i64, i64* @errno
%32 = sub nsw i64 0, %31
store i64 %32, i64* %2
br label %43
33: ; preds = %14
%34 = load i32, i32* %5
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %33
%37 = call i32 (...) @dune_enter()
%38 = load i64, i64* %4
%39 = call i32 @dune_set_user_fs(i64 %38)
br label %40
40: ; preds = %36, %33
%41 = load i32, i32* %5
%42 = sext i32 %41 to i64
store i64 %42, i64* %2
br label %43
43: ; preds = %40, %30, %11
%44 = load i64, i64* %2
ret i64 %44
}

LLVM IR (-O2):
%struct.dune_tf = type { i32 }
@SYS_clone = global i32 0
@errno = global i64 0
define i64 @dune_clone(%struct.dune_tf* %0) {
%2 = tail call i64 @native_read_msr(i32 -1073741567)
%3 = tail call i64 @ARG1(%struct.dune_tf* %0)
%4 = icmp eq i64 %3, 0
br i1 %4, label %7, label %5
5: ; preds = %1
%6 = tail call i64 @dune_pthread_create(%struct.dune_tf* %0)
br label %27
7: ; preds = %1
%8 = tail call i64 (...) @dune_get_user_fs()
%9 = load i32, i32* @SYS_clone
%10 = tail call i32 @ARG0(%struct.dune_tf* %0)
%11 = tail call i64 @ARG1(%struct.dune_tf* %0)
%12 = tail call i32 @ARG2(%struct.dune_tf* %0)
%13 = tail call i32 @ARG3(%struct.dune_tf* %0)
%14 = tail call i32 @ARG4(%struct.dune_tf* %0)
%15 = tail call i32 @syscall(i32 %9, i32 %10, i64 %11, i32 %12, i32 %13, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %7
%18 = load i64, i64* @errno
%19 = sub nsw i64 0, %18
br label %27
20: ; preds = %7
%21 = icmp eq i32 %15, 0
br i1 %21, label %22, label %25
22: ; preds = %20
%23 = tail call i32 (...) @dune_enter()
%24 = tail call i32 @dune_set_user_fs(i64 %8)
br label %25
25: ; preds = %22, %20
%26 = zext i32 %15 to i64
br label %27
27: ; preds = %25, %17, %5
%28 = phi i64 [ %6, %5 ], [ %19, %17 ], [ %26, %25 ]
ret i64 %28
}