#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 */
scalar_t__ ERANGE;
char *basename(char *);
int /*<<< orphan*/ chdir(char *);
scalar_t__ errno;
int /*<<< orphan*/ execl(char *, char *, char *);
int /*<<< orphan*/ free(char *);
char *getexename(char *, int);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int main() {
  char *buf;
  int size;
  buf = NULL;
  size = 32;
  for (;;) {
    char *res;
    buf = (char *)malloc(size);
    res = getexename(buf, size);
    if (res)
      break;
    if (errno != ERANGE) {
      perror("getexename() failed");
      free(buf);
      buf = NULL;
      break;
    }
    free(buf);
    size *= 2;
  }
  if (buf == NULL)
    return -1;
  buf = basename(buf);
  char newname[size];
  strcpy(newname, buf);
  strcat(newname, " data");
  chdir(newname);
  execl("runtime", "runtime", (char *)0);
  free(buf);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
movl   $0x20,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   10a0 <getexename@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11da <main+0x4a>
jmp    122d <main+0x9d>
mov    0x2df7(%rip),%rax
mov    (%rax),%rax
mov    0x2dfd(%rip),%rcx
cmp    (%rcx),%rax
je     1216 <main+0x86>
lea    0xe05(%rip),%rdi
call   1080 <perror@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x10(%rbp)
jmp    122d <main+0x9d>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11ae <main+0x1e>
cmpq   $0x0,-0x10(%rbp)
jne    1244 <main+0xb4>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c3 <main+0x133>
mov    -0x10(%rbp),%rdi
call   1060 <basename@plt>
mov    %rax,-0x10(%rbp)
mov    -0x14(%rbp),%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,-0x38(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x38(%rbp),%rdi
lea    0xd89(%rip),%rsi
call   1090 <strcat@plt>
mov    -0x38(%rbp),%rdi
call   1050 <chdir@plt>
lea    0xd7a(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
call   10b0 <execl@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
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    $0x20,%edi
call   1070 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    $0x20,%esi
call   10a0 <getexename@plt>
mov    $0x20,%r12d
test   %rax,%rax
je     11ca <main+0x3a>
test   %r14,%r14
jne    123a <main+0xaa>
jmp    129b <main+0x10b>
mov    0x2e07(%rip),%r15
mov    (%r15),%rax
mov    0x2e0d(%rip),%r12
cmp    (%r12),%rax
jne    121c <main+0x8c>
mov    $0x20,%ebx
cs nopw 0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1030 <free@plt>
add    %ebx,%ebx
mov    %ebx,%edi
call   1070 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %ebx,%esi
call   10a0 <getexename@plt>
test   %rax,%rax
jne    1232 <main+0xa2>
mov    (%r15),%rax
cmp    (%r12),%rax
je     11f0 <main+0x60>
lea    0xddd(%rip),%rdi
call   1080 <perror@plt>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    129b <main+0x10b>
mov    %ebx,%r12d
test   %r14,%r14
je     129b <main+0x10b>
mov    %r14,%rdi
call   1060 <basename@plt>
mov    %rax,%r14
mov    %rsp,%r15
mov    %rsp,%rbx
add    $0xf,%r12
and    $0xfffffffffffffff0,%r12
sub    %r12,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
mov    %rax,%rsi
call   1040 <strcpy@plt>
lea    0xda9(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcat@plt>
mov    %rbx,%rdi
call   1050 <chdir@plt>
lea    0xd98(%rip),%rdi
xor    %ebx,%ebx
mov    %rdi,%rsi
xor    %edx,%edx
call   10b0 <execl@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rsp
jmp    12a0 <main+0x110>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@ERANGE = global i64 0
@.str = [20 x i8] c"getexename() failed\00"
@.str.1 = [6 x i8] c" data\00"
@.str.2 = [8 x i8] c"runtime\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
store i32 0, i32* %1
store i8* null, i8** %2
store i32 32, i32* %3
br label %7
7: ; preds = %25, %0
%8 = load i32, i32* %3
%9 = call i64 @malloc(i32 %8)
%10 = inttoptr i64 %9 to i8*
store i8* %10, i8** %2
%11 = load i8*, i8** %2
%12 = load i32, i32* %3
%13 = call i8* @getexename(i8* %11, i32 %12)
store i8* %13, i8** %4
%14 = load i8*, i8** %4
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %17
16: ; preds = %7
br label %30
17: ; preds = %7
%18 = load i64, i64* @errno
%19 = load i64, i64* @ERANGE
%20 = icmp ne i64 %18, %19
br i1 %20, label %21, label %25
21: ; preds = %17
%22 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%23 = load i8*, i8** %2
%24 = call i32 @free(i8* %23)
store i8* null, i8** %2
br label %30
25: ; preds = %17
%26 = load i8*, i8** %2
%27 = call i32 @free(i8* %26)
%28 = load i32, i32* %3
%29 = mul nsw i32 %28, 2
store i32 %29, i32* %3
br label %7
30: ; preds = %21, %16
%31 = load i8*, i8** %2
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %34
33: ; preds = %30
store i32 -1, i32* %1
br label %49
34: ; preds = %30
%35 = load i8*, i8** %2
%36 = call i8* @basename(i8* %35)
store i8* %36, i8** %2
%37 = load i32, i32* %3
%38 = zext i32 %37 to i64
%39 = call i8* @llvm.stacksave()
store i8* %39, i8** %5
%40 = alloca i8, i64 %38
store i64 %38, i64* %6
%41 = load i8*, i8** %2
%42 = call i32 @strcpy(i8* %40, i8* %41)
%43 = call i32 @strcat(i8* %40, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%44 = call i32 @chdir(i8* %40)
%45 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* null)
%46 = load i8*, i8** %2
%47 = call i32 @free(i8* %46)
store i32 0, i32* %1
%48 = load i8*, i8** %5
br label %49
49: ; preds = %34, %33
%50 = load i32, i32* %1
ret i32 %50
}

LLVM IR (-O2):
@errno = global i64 0
@ERANGE = global i64 0
@.str = [20 x i8] c"getexename() failed\00"
@.str.1 = [6 x i8] c" data\00"
@.str.2 = [8 x i8] c"runtime\00"
define i32 @main() {
%1 = tail call i64 @malloc(i32 32)
%2 = inttoptr i64 %1 to i8*
%3 = tail call i8* @getexename(i8* %2, i32 32)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %29
5: ; preds = %0
%6 = load i64, i64* @errno
%7 = load i64, i64* @ERANGE
%8 = icmp eq i64 %6, %7
br i1 %8, label %18, label %13
9: ; preds = %18
%10 = load i64, i64* @errno
%11 = load i64, i64* @ERANGE
%12 = icmp eq i64 %10, %11
br i1 %12, label %18, label %13
13: ; preds = %9, %5
%14 = phi i64 [ %1, %5 ], [ %23, %9 ]
%15 = inttoptr i64 %14 to i8*
%16 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%17 = tail call i32 @free(i8* %15)
br label %43
18: ; preds = %5, %9
%19 = phi i32 [ %22, %9 ], [ 32, %5 ]
%20 = phi i8* [ %24, %9 ], [ %2, %5 ]
%21 = tail call i32 @free(i8* %20)
%22 = shl nsw i32 %19, 1
%23 = tail call i64 @malloc(i32 %22)
%24 = inttoptr i64 %23 to i8*
%25 = tail call i8* @getexename(i8* %24, i32 %22)
%26 = icmp eq i8* %25, null
br i1 %26, label %9, label %27
27: ; preds = %18
%28 = zext i32 %22 to i64
br label %29
29: ; preds = %27, %0
%30 = phi i64 [ %1, %0 ], [ %23, %27 ]
%31 = phi i64 [ 32, %0 ], [ %28, %27 ]
%32 = icmp eq i64 %30, 0
br i1 %32, label %43, label %33
33: ; preds = %29
%34 = inttoptr i64 %30 to i8*
%35 = tail call i8* @basename(i8* %34)
%36 = tail call i8* @llvm.stacksave()
%37 = alloca i8, i64 %31
%38 = call i32 @strcpy(i8* %37, i8* %35)
%39 = call i32 @strcat(i8* %37, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%40 = call i32 @chdir(i8* %37)
%41 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* null)
%42 = call i32 @free(i8* %35)
br label %43
43: ; preds = %13, %29, %33
%44 = phi i32 [ 0, %33 ], [ -1, %29 ], [ -1, %13 ]
ret i32 %44
}