#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 */
void *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int PATH_MAX;
int /*<<< orphan*/ PROT_WRITE;
int /*<<< orphan*/ close(int);
int ftruncate(int, int);
int /*<<< orphan*/ memset(void *, int, int);
int mkstemp(char *);
void *mmap(int /*<<< orphan*/, int, int /*<<< orphan*/, int /*<<< orphan*/, int,
           int /*<<< orphan*/);
int munmap(void *, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strcpy(char *, char const *);
int /*<<< orphan*/ unlink(char *);
int setup_test_input() {
  const char template[] = "tracetest.XXXXXX";
  char fname[PATH_MAX];
  strcpy(fname, template);
  int testfd = mkstemp(fname);
  if (testfd < 0) {
    perror("Could not create tempfile");
    return -1;
  }
  int rc = ftruncate(testfd, ((int)((4096) * 1e3)));
  if (rc < 0) {
    perror("Failed to resize tempfile");
    goto cleanup;
  }
  void *buf = mmap(0, ((int)((4096) * 1e3)), PROT_WRITE, MAP_SHARED, testfd, 0);
  if (buf == MAP_FAILED) {
    perror("Failed to create mapping");
    goto cleanup;
  }
  memset(buf, 1, ((int)((4096) * 1e3)));
  rc = munmap(buf, ((int)((4096) * 1e3)));
  if (rc < 0) {
    perror("Failed to unmap file");
    goto cleanup;
  }
  close(testfd);
  int testfd2 = open(fname, O_RDONLY);
  unlink(fname);
  if (testfd2 < 0) {
    perror("Fail to reopen testfile");
  }
  return testfd2;
cleanup:
  unlink(fname);
  close(testfd);
  return -1;
}


x86 assembly (clang, -O0):
<setup_test_input>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movaps 0xe51(%rip),%xmm0
movaps %xmm0,-0x20(%rbp)
movb   $0x0,-0x10(%rbp)
mov    0x2e32(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x58(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x30(%rbp)
lea    -0x20(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x58(%rbp),%rdi
call   1090 <mkstemp@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1221 <setup_test_input+0x81>
lea    0xe08(%rip),%rdi
call   10c0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    1347 <setup_test_input+0x1a7>
mov    -0x34(%rbp),%edi
mov    $0x3e8000,%esi
call   1060 <ftruncate@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    124c <setup_test_input+0xac>
lea    0xde9(%rip),%rdi
call   10c0 <perror@plt>
jmp    1328 <setup_test_input+0x188>
mov    0x2d75(%rip),%rax
mov    (%rax),%edx
mov    0x2d8c(%rip),%rax
mov    (%rax),%ecx
mov    -0x34(%rbp),%r8d
xor    %r9d,%r9d
mov    $0x3e8000,%esi
mov    %r9d,%edi
call   1050 <mmap@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    0x2d3f(%rip),%rcx
cmp    (%rcx),%rax
jne    129b <setup_test_input+0xfb>
lea    0xdb4(%rip),%rdi
call   10c0 <perror@plt>
jmp    1328 <setup_test_input+0x188>
mov    -0x48(%rbp),%rdi
mov    $0x1,%esi
mov    $0x3e8000,%edx
call   1070 <memset@plt>
mov    -0x48(%rbp),%rdi
mov    $0x3e8000,%esi
call   10a0 <munmap@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    12da <setup_test_input+0x13a>
lea    0xd8e(%rip),%rdi
call   10c0 <perror@plt>
jmp    1328 <setup_test_input+0x188>
mov    -0x34(%rbp),%edi
call   1080 <close@plt>
mov    -0x58(%rbp),%rdi
mov    0x2ceb(%rip),%rax
mov    (%rax),%esi
call   10b0 <open@plt>
mov    -0x58(%rbp),%rdi
mov    %eax,-0x4c(%rbp)
call   1030 <unlink@plt>
cmpl   $0x0,-0x4c(%rbp)
jge    1316 <setup_test_input+0x176>
lea    0xd62(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    1347 <setup_test_input+0x1a7>
mov    -0x58(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x34(%rbp),%edi
call   1080 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_test_input>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rsp,%r12
movaps 0xe47(%rip),%xmm0
movaps %xmm0,-0x40(%rbp)
movb   $0x0,-0x30(%rbp)
mov    0x2e28(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
lea    -0x40(%rbp),%rsi
mov    %r15,%rdi
call   1040 <strcpy@plt>
mov    %r15,%rdi
call   1090 <mkstemp@plt>
test   %eax,%eax
js     12a0 <setup_test_input+0x100>
mov    %eax,%r14d
mov    %eax,%edi
mov    $0x3e8000,%esi
call   1060 <ftruncate@plt>
test   %eax,%eax
js     12b4 <setup_test_input+0x114>
mov    0x2db3(%rip),%rax
mov    (%rax),%edx
mov    0x2dca(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x3e8000,%esi
mov    %r14d,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,%rbx
mov    0x2d84(%rip),%rax
cmp    (%rax),%rbx
je     12bd <setup_test_input+0x11d>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x3e8000,%edx
call   1070 <memset@plt>
mov    %rbx,%rdi
mov    $0x3e8000,%esi
call   10a0 <munmap@plt>
test   %eax,%eax
js     12c6 <setup_test_input+0x126>
mov    %r14d,%edi
call   1080 <close@plt>
mov    0x2d65(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   10b0 <open@plt>
mov    %eax,%r14d
mov    %r15,%rdi
call   1030 <unlink@plt>
test   %r14d,%r14d
js     12ea <setup_test_input+0x14a>
mov    %r12,%rsp
mov    %r14d,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd6a(%rip),%rdi
call   10c0 <perror@plt>
mov    $0xffffffff,%r14d
jmp    128d <setup_test_input+0xed>
lea    0xd70(%rip),%rdi
jmp    12cd <setup_test_input+0x12d>
lea    0xd81(%rip),%rdi
jmp    12cd <setup_test_input+0x12d>
lea    0xd91(%rip),%rdi
call   10c0 <perror@plt>
mov    %r15,%rdi
call   1030 <unlink@plt>
mov    %r14d,%edi
call   1080 <close@plt>
mov    $0xffffffff,%r14d
jmp    128d <setup_test_input+0xed>
lea    0xd82(%rip),%rdi
call   10c0 <perror@plt>
jmp    128d <setup_test_input+0xed>

LLVM IR (-O0):
@__const.setup_test_input.template = [17 x i8] c"tracetest.XXXXXX\00"
@PATH_MAX = global i32 0
@.str = [26 x i8] c"Could not create tempfile\00"
@.str.1 = [26 x i8] c"Failed to resize tempfile\00"
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [25 x i8] c"Failed to create mapping\00"
@.str.3 = [21 x i8] c"Failed to unmap file\00"
@O_RDONLY = global i32 0
@.str.4 = [24 x i8] c"Fail to reopen testfile\00"
define i32 @setup_test_input() {
%1 = alloca i32
%2 = alloca [17 x i8]
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = bitcast [17 x i8]* %2 to i8*
%11 = load i32, i32* @PATH_MAX
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %3
%14 = alloca i8, i64 %12
store i64 %12, i64* %4
%15 = getelementptr inbounds [17 x i8], [17 x i8]* %2, i64 0, i64 0
%16 = call i32 @strcpy(i8* %14, i8* %15)
%17 = call i32 @mkstemp(i8* %14)
store i32 %17, i32* %5
%18 = load i32, i32* %5
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %0
%21 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %1
store i32 1, i32* %6
br label %64
22: ; preds = %0
%23 = load i32, i32* %5
%24 = call i32 @ftruncate(i32 %23, i32 4096000)
store i32 %24, i32* %7
%25 = load i32, i32* %7
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %22
%28 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %60
29: ; preds = %22
%30 = load i32, i32* @PROT_WRITE
%31 = load i32, i32* @MAP_SHARED
%32 = load i32, i32* %5
%33 = call i8* @mmap(i32 0, i32 4096000, i32 %30, i32 %31, i32 %32, i32 0)
store i8* %33, i8** %8
%34 = load i8*, i8** %8
%35 = load i8*, i8** @MAP_FAILED
%36 = icmp eq i8* %34, %35
br i1 %36, label %37, label %39
37: ; preds = %29
%38 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %60
39: ; preds = %29
%40 = load i8*, i8** %8
%41 = call i32 @memset(i8* %40, i32 1, i32 4096000)
%42 = load i8*, i8** %8
%43 = call i32 @munmap(i8* %42, i32 4096000)
store i32 %43, i32* %7
%44 = load i32, i32* %7
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %39
%47 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
br label %60
48: ; preds = %39
%49 = load i32, i32* %5
%50 = call i32 @close(i32 %49)
%51 = load i32, i32* @O_RDONLY
%52 = call i32 @open(i8* %14, i32 %51)
store i32 %52, i32* %9
%53 = call i32 @unlink(i8* %14)
%54 = load i32, i32* %9
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %48
%57 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %58
58: ; preds = %56, %48
%59 = load i32, i32* %9
store i32 %59, i32* %1
store i32 1, i32* %6
br label %64
60: ; preds = %46, %37, %27
%61 = call i32 @unlink(i8* %14)
%62 = load i32, i32* %5
%63 = call i32 @close(i32 %62)
store i32 -1, i32* %1
store i32 1, i32* %6
br label %64
64: ; preds = %60, %58, %20
%65 = load i8*, i8** %3
%66 = load i32, i32* %1
ret i32 %66
}

LLVM IR (-O2):
@__const.setup_test_input.template = [17 x i8] c"tracetest.XXXXXX\00"
@PATH_MAX = global i32 0
@.str = [26 x i8] c"Could not create tempfile\00"
@.str.1 = [26 x i8] c"Failed to resize tempfile\00"
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i8* null
@.str.2 = [25 x i8] c"Failed to create mapping\00"
@.str.3 = [21 x i8] c"Failed to unmap file\00"
@O_RDONLY = global i32 0
@.str.4 = [24 x i8] c"Fail to reopen testfile\00"
define i32 @setup_test_input() {
%1 = alloca [17 x i8]
%2 = getelementptr inbounds [17 x i8], [17 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = tail call i8* @llvm.stacksave()
%6 = alloca i8, i64 %4
%7 = call i32 @strcpy(i8* %6, i8* %2)
%8 = call i32 @mkstemp(i8* %6)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %38
12: ; preds = %0
%13 = call i32 @ftruncate(i32 %8, i32 4096000)
%14 = icmp slt i32 %13, 0
br i1 %14, label %33, label %15
15: ; preds = %12
%16 = load i32, i32* @PROT_WRITE
%17 = load i32, i32* @MAP_SHARED
%18 = call i8* @mmap(i32 0, i32 4096000, i32 %16, i32 %17, i32 %8, i32 0)
%19 = load i8*, i8** @MAP_FAILED
%20 = icmp eq i8* %18, %19
br i1 %20, label %33, label %21
21: ; preds = %15
%22 = call i32 @memset(i8* %18, i32 1, i32 4096000)
%23 = call i32 @munmap(i8* %18, i32 4096000)
%24 = icmp slt i32 %23, 0
br i1 %24, label %33, label %25
25: ; preds = %21
%26 = call i32 @close(i32 %8)
%27 = load i32, i32* @O_RDONLY
%28 = call i32 @open(i8* %6, i32 %27)
%29 = call i32 @unlink(i8* %6)
%30 = icmp slt i32 %28, 0
br i1 %30, label %31, label %38
31: ; preds = %25
%32 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
br label %38
33: ; preds = %21, %15, %12
%34 = phi i8* [ getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), %12 ], [ getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), %15 ], [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), %21 ]
%35 = call i32 @perror(i8* %34)
%36 = call i32 @unlink(i8* %6)
%37 = call i32 @close(i32 %8)
br label %38
38: ; preds = %33, %31, %25, %10
%39 = phi i32 [ -1, %10 ], [ -1, %33 ], [ %28, %31 ], [ %28, %25 ]
ret i32 %39
}