#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 close(int);
int /*<<< orphan*/ dprintf(int, char *);
int /*<<< orphan*/ execv(char *, char **);
int mkstemp(char *);
int /*<<< orphan*/ perror(char *);
int main(int argc, char **argv) {
  int fd = mkstemp("/tmp/tuscan-native-XXXXXX");
  if (fd == -1) {
    perror("tuscan: mkstemp");
    return 1;
  }
  dprintf(fd, "{{ native_program }}\n");
  if (close(fd) == -1) {
    perror("tuscan: close");
    return 1;
  }
  char *toolchain_tool = "{{ toolchain_prefix }}{{ toolchain_program }}";
  char *new_argv[argc];
  new_argv[0] = toolchain_tool;
  for (int i = 1; i < argc; i++) {
    new_argv[i] = argv[i];
  }
  execv(toolchain_tool, new_argv);
}


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)
lea    0xe93(%rip),%rdi
call   1050 <mkstemp@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1197 <main+0x47>
lea    0xe94(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1257 <main+0x107>
mov    -0x14(%rbp),%edi
lea    0xe89(%rip),%rsi
call   1040 <dprintf@plt>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
cmp    $0xffffffff,%eax
jne    11cf <main+0x7f>
lea    0xe82(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1257 <main+0x107>
lea    0xe78(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,%ecx
mov    %rsp,%rax
mov    %rax,-0x28(%rbp)
lea    0xf(,%rcx,8),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rax
sub    %rdx,%rax
mov    %rax,-0x40(%rbp)
mov    %rax,%rsp
mov    %rcx,-0x30(%rbp)
mov    -0x20(%rbp),%rcx
mov    %rcx,(%rax)
movl   $0x1,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1243 <main+0xf3>
mov    -0x40(%rbp),%rax
mov    -0x10(%rbp),%rcx
movslq -0x34(%rbp),%rdx
mov    (%rcx,%rdx,8),%rdx
movslq -0x34(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1211 <main+0xc1>
mov    -0x40(%rbp),%rsi
mov    -0x20(%rbp),%rdi
call   1070 <execv@plt>
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %edi,%r15d
lea    0xe88(%rip),%rdi
call   1060 <mkstemp@plt>
cmp    $0xffffffff,%eax
je     1210 <main+0xb0>
mov    %eax,%ebx
lea    0xe9b(%rip),%rsi
mov    %eax,%edi
call   1040 <dprintf@plt>
mov    %ebx,%edi
call   1030 <close@plt>
cmp    $0xffffffff,%eax
je     1219 <main+0xb9>
mov    %rsp,%r12
mov    %r15d,%eax
mov    %rsp,%rcx
lea    0xf(,%rax,8),%rax
and    $0xfffffffffffffff0,%rax
mov    %rcx,%rbx
sub    %rax,%rbx
mov    %rbx,%rsp
neg    %rax
lea    0xe84(%rip),%rdx
mov    %rdx,(%rcx,%rax,1)
cmp    $0x2,%r15d
jl     11ef <main+0x8f>
lea    0x8(%rbx),%rdi
add    $0x8,%r14
add    $0xffffffff,%r15d
shl    $0x3,%r15
mov    %r14,%rsi
mov    %r15,%rdx
call   1050 <memcpy@plt>
lea    0xe58(%rip),%rdi
mov    %rbx,%rsi
call   1080 <execv@plt>
xor    %eax,%eax
mov    %r12,%rsp
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe03(%rip),%rdi
jmp    1220 <main+0xc0>
lea    0xe20(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%eax
jmp    1203 <main+0xa3>

LLVM IR (-O0):
@.str = [26 x i8] c"/tmp/tuscan-native-XXXXXX\00"
@.str.1 = [16 x i8] c"tuscan: mkstemp\00"
@.str.2 = [22 x i8] c"{{ native_program }}\0A\00"
@.str.3 = [14 x i8] c"tuscan: close\00"
@.str.4 = [46 x i8] c"{{ toolchain_prefix }}{{ toolchain_program }}\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = call i32 @mkstemp(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %51
16: ; preds = %2
%17 = load i32, i32* %6
%18 = call i32 @dprintf(i32 %17, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%19 = load i32, i32* %6
%20 = call i32 @close(i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %51
24: ; preds = %16
store i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0), i8** %7
%25 = load i32, i32* %4
%26 = zext i32 %25 to i64
%27 = call i8* @llvm.stacksave()
store i8* %27, i8** %8
%28 = alloca i8*, i64 %26
store i64 %26, i64* %9
%29 = load i8*, i8** %7
%30 = getelementptr inbounds i8*, i8** %28, i64 0
store i8* %29, i8** %30
store i32 1, i32* %10
br label %31
31: ; preds = %44, %24
%32 = load i32, i32* %10
%33 = load i32, i32* %4
%34 = icmp slt i32 %32, %33
br i1 %34, label %35, label %47
35: ; preds = %31
%36 = load i8**, i8*** %5
%37 = load i32, i32* %10
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8*, i8** %36, i64 %38
%40 = load i8*, i8** %39
%41 = load i32, i32* %10
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8*, i8** %28, i64 %42
store i8* %40, i8** %43
br label %44
44: ; preds = %35
%45 = load i32, i32* %10
%46 = add nsw i32 %45, 1
store i32 %46, i32* %10
br label %31
47: ; preds = %31
%48 = load i8*, i8** %7
%49 = call i32 @execv(i8* %48, i8** %28)
%50 = load i8*, i8** %8
br label %51
51: ; preds = %47, %22, %14
%52 = load i32, i32* %3
ret i32 %52
}

LLVM IR (-O2):
@.str = [26 x i8] c"/tmp/tuscan-native-XXXXXX\00"
@.str.1 = [16 x i8] c"tuscan: mkstemp\00"
@.str.2 = [22 x i8] c"{{ native_program }}\0A\00"
@.str.3 = [14 x i8] c"tuscan: close\00"
@.str.4 = [46 x i8] c"{{ toolchain_prefix }}{{ toolchain_program }}\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i32 @mkstemp(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %28
7: ; preds = %2
%8 = tail call i32 @dprintf(i32 %3, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%9 = tail call i32 @close(i32 %3)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %7
%12 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %28
13: ; preds = %7
%14 = zext i32 %0 to i64
%15 = tail call i8* @llvm.stacksave()
%16 = alloca i8*, i64 %14
store i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0), i8** %16
%17 = icmp sgt i32 %0, 1
br i1 %17, label %18, label %26
18: ; preds = %13
%19 = getelementptr inbounds i8*, i8** %16, i64 1
%20 = bitcast i8** %19 to i8*
%21 = getelementptr i8*, i8** %1, i64 1
%22 = bitcast i8** %21 to i8*
%23 = add nsw i32 %0, -1
%24 = zext i32 %23 to i64
%25 = shl nuw nsw i64 %24, 3
br label %26
26: ; preds = %18, %13
%27 = call i32 @execv(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0), i8** %16)
br label %28
28: ; preds = %26, %11, %5
%29 = phi i32 [ 1, %5 ], [ 1, %11 ], [ 0, %26 ]
ret i32 %29
}