#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__ chmod(char *, int);
int /*<<< orphan*/ execl(char *, char *, char *, char *, int /*<<< orphan*/ *);
char *getenv(char *);
int /*<<< orphan*/ msg(char *, ...);
int /*<<< orphan*/ msg_errno(char *, ...);
scalar_t__ rename(char *, char *);
scalar_t__ truncate(char *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  char *dospath = NULL;
  if (argc != 2) {
    msg("bad number of arguments: %d", argc);
  }
  if (!(dospath = getenv("DOSPATH"))) {
    msg("DOSPATH environment variable is not set");
  }
  if (chmod(".", 0755) < 0) {
    msg_errno("chmod(\".\",0755) failed");
  }
  if (rename(argv[1], "./program.exe") < 0) {
    msg_errno("rename(\"%s\",\"%s\") failed", argv[1], "./program.exe");
  }
  if (truncate("./output", 0) < 0) {
    msg_errno("truncate(\"%s\", 0) failed", "./output");
  }
  if (truncate("./error", 0) < 0) {
    msg_errno("truncate(\"%s\", 0) failed", "./error");
  }
  if (truncate("./e", 0) < 0) {
    msg_errno("truncate(\"%s\", 0) failed", "./e");
  }
  if (chmod(".", 0555) < 0) {
    msg_errno("chmod(\".\",0555) failed");
  }
  execl(dospath, dospath, "-I", "video { none }", NULL);
  msg_errno("execl(\"%s\",...) failed", dospath);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11a9 <main+0x39>
mov    -0x8(%rbp),%esi
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1090 <msg@plt>
lea    0xe6c(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11d1 <main+0x61>
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1090 <msg@plt>
lea    0xe74(%rip),%rdi
mov    $0x1ed,%esi
call   1060 <chmod@plt>
cmp    $0x0,%rax
jge    11fa <main+0x8a>
lea    0xe5b(%rip),%rdi
mov    $0x0,%al
call   1040 <msg_errno@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe5c(%rip),%rsi
call   1070 <rename@plt>
cmp    $0x0,%rax
jge    1235 <main+0xc5>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe4c(%rip),%rdi
lea    0xe37(%rip),%rdx
mov    $0x0,%al
call   1040 <msg_errno@plt>
lea    0xe50(%rip),%rdi
xor    %esi,%esi
call   1050 <truncate@plt>
cmp    $0x0,%rax
jge    1262 <main+0xf2>
lea    0xe41(%rip),%rdi
lea    0xe31(%rip),%rsi
mov    $0x0,%al
call   1040 <msg_errno@plt>
lea    0xe45(%rip),%rdi
xor    %esi,%esi
call   1050 <truncate@plt>
cmp    $0x0,%rax
jge    128f <main+0x11f>
lea    0xe14(%rip),%rdi
lea    0xe26(%rip),%rsi
mov    $0x0,%al
call   1040 <msg_errno@plt>
lea    0xe20(%rip),%rdi
xor    %esi,%esi
call   1050 <truncate@plt>
cmp    $0x0,%rax
jge    12bc <main+0x14c>
lea    0xde7(%rip),%rdi
lea    0xe01(%rip),%rsi
mov    $0x0,%al
call   1040 <msg_errno@plt>
lea    0xd89(%rip),%rdi
mov    $0x16d,%esi
call   1060 <chmod@plt>
cmp    $0x0,%rax
jge    12e5 <main+0x175>
lea    0xddc(%rip),%rdi
mov    $0x0,%al
call   1040 <msg_errno@plt>
mov    -0x18(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    0xddd(%rip),%rdx
lea    0xdd9(%rip),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   1080 <execl@plt>
mov    -0x18(%rbp),%rsi
lea    0xdd3(%rip),%rdi
mov    $0x0,%al
call   1040 <msg_errno@plt>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
cmp    $0x2,%edi
je     118c <main+0x1c>
mov    %edi,%esi
lea    0xe7b(%rip),%rdi
xor    %eax,%eax
call   1090 <msg@plt>
lea    0xe89(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11ae <main+0x3e>
lea    0xe7d(%rip),%rdi
xor    %eax,%eax
call   1090 <msg@plt>
lea    0xe97(%rip),%rdi
mov    $0x1ed,%esi
call   1060 <chmod@plt>
test   %rax,%rax
jns    11d2 <main+0x62>
lea    0xe83(%rip),%rdi
xor    %eax,%eax
call   1040 <msg_errno@plt>
mov    0x8(%r14),%rdi
lea    0xe88(%rip),%rsi
call   1070 <rename@plt>
test   %rax,%rax
jns    1200 <main+0x90>
mov    0x8(%r14),%rsi
lea    0xe81(%rip),%rdi
lea    0xe6c(%rip),%rdx
xor    %eax,%eax
call   1040 <msg_errno@plt>
lea    0xe85(%rip),%rdi
xor    %esi,%esi
call   1050 <truncate@plt>
test   %rax,%rax
jns    1228 <main+0xb8>
lea    0xe7b(%rip),%rdi
lea    0xe6b(%rip),%rsi
xor    %eax,%eax
call   1040 <msg_errno@plt>
lea    0xe7f(%rip),%rdi
xor    %esi,%esi
call   1050 <truncate@plt>
test   %rax,%rax
jns    1250 <main+0xe0>
lea    0xe53(%rip),%rdi
lea    0xe65(%rip),%rsi
xor    %eax,%eax
call   1040 <msg_errno@plt>
lea    0xe5f(%rip),%rdi
xor    %esi,%esi
call   1050 <truncate@plt>
test   %rax,%rax
jns    1278 <main+0x108>
lea    0xe2b(%rip),%rdi
lea    0xe45(%rip),%rsi
xor    %eax,%eax
call   1040 <msg_errno@plt>
lea    0xdcd(%rip),%rdi
mov    $0x16d,%esi
call   1060 <chmod@plt>
test   %rax,%rax
jns    129c <main+0x12c>
lea    0xe25(%rip),%rdi
xor    %eax,%eax
call   1040 <msg_errno@plt>
lea    0xe2e(%rip),%rdx
lea    0xe2a(%rip),%rcx
mov    %rbx,%rdi
mov    %rbx,%rsi
xor    %r8d,%r8d
call   1080 <execl@plt>
lea    0xe24(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <msg_errno@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [28 x i8] c"bad number of arguments: %d\00"
@.str.1 = [8 x i8] c"DOSPATH\00"
@.str.2 = [40 x i8] c"DOSPATH environment variable is not set\00"
@.str.3 = [2 x i8] c".\00"
@.str.4 = [23 x i8] c"chmod(\22.\22,0755) failed\00"
@.str.5 = [14 x i8] c"./program.exe\00"
@.str.6 = [25 x i8] c"rename(\22%s\22,\22%s\22) failed\00"
@.str.7 = [9 x i8] c"./output\00"
@.str.8 = [25 x i8] c"truncate(\22%s\22, 0) failed\00"
@.str.9 = [8 x i8] c"./error\00"
@.str.10 = [4 x i8] c"./e\00"
@.str.11 = [23 x i8] c"chmod(\22.\22,0555) failed\00"
@.str.12 = [3 x i8] c"-I\00"
@.str.13 = [15 x i8] c"video { none }\00"
@.str.14 = [23 x i8] c"execl(\22%s\22,...) failed\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %6
%7 = load i32, i32* %4
%8 = icmp ne i32 %7, 2
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i32, i32* %4
%11 = call i32 (i8*, ...) @msg(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %10)
br label %12
12: ; preds = %9, %2
%13 = call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i8* %13, i8** %6
%14 = icmp ne i8* %13, null
br i1 %14, label %17, label %15
15: ; preds = %12
%16 = call i32 (i8*, ...) @msg(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %15, %12
%18 = call i64 @chmod(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 493)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %17
%21 = call i32 (i8*, ...) @msg_errno(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
br label %22
22: ; preds = %20, %17
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call i64 @rename(i8* %25, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %22
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call i32 (i8*, ...) @msg_errno(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0), i8* %31, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
br label %33
33: ; preds = %28, %22
%34 = call i64 @truncate(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i32 0)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = call i32 (i8*, ...) @msg_errno(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0))
br label %38
38: ; preds = %36, %33
%39 = call i64 @truncate(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0), i32 0)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = call i32 (i8*, ...) @msg_errno(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0))
br label %43
43: ; preds = %41, %38
%44 = call i64 @truncate(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.10, i64 0, i64 0), i32 0)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %43
%47 = call i32 (i8*, ...) @msg_errno(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.10, i64 0, i64 0))
br label %48
48: ; preds = %46, %43
%49 = call i64 @chmod(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 365)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %48
%52 = call i32 (i8*, ...) @msg_errno(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.11, i64 0, i64 0))
br label %53
53: ; preds = %51, %48
%54 = load i8*, i8** %6
%55 = load i8*, i8** %6
%56 = call i32 @execl(i8* %54, i8* %55, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.12, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.13, i64 0, i64 0), i32* null)
%57 = load i8*, i8** %6
%58 = call i32 (i8*, ...) @msg_errno(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.14, i64 0, i64 0), i8* %57)
%59 = load i32, i32* %3
ret i32 %59
}

LLVM IR (-O2):
@.str = [28 x i8] c"bad number of arguments: %d\00"
@.str.1 = [8 x i8] c"DOSPATH\00"
@.str.2 = [40 x i8] c"DOSPATH environment variable is not set\00"
@.str.3 = [2 x i8] c".\00"
@.str.4 = [23 x i8] c"chmod(\22.\22,0755) failed\00"
@.str.5 = [14 x i8] c"./program.exe\00"
@.str.6 = [25 x i8] c"rename(\22%s\22,\22%s\22) failed\00"
@.str.7 = [9 x i8] c"./output\00"
@.str.8 = [25 x i8] c"truncate(\22%s\22, 0) failed\00"
@.str.9 = [8 x i8] c"./error\00"
@.str.10 = [4 x i8] c"./e\00"
@.str.11 = [23 x i8] c"chmod(\22.\22,0555) failed\00"
@.str.12 = [3 x i8] c"-I\00"
@.str.13 = [15 x i8] c"video { none }\00"
@.str.14 = [23 x i8] c"execl(\22%s\22,...) failed\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = tail call i32 (i8*, ...) @msg(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %6
6: ; preds = %4, %2
%7 = tail call i8* @getenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 (i8*, ...) @msg(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
br label %11
11: ; preds = %9, %6
%12 = tail call i64 @chmod(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 493)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = tail call i32 (i8*, ...) @msg_errno(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
br label %16
16: ; preds = %14, %11
%17 = getelementptr inbounds i8*, i8** %1, i64 1
%18 = load i8*, i8** %17
%19 = tail call i64 @rename(i8* %18, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = load i8*, i8** %17
%23 = tail call i32 (i8*, ...) @msg_errno(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0), i8* %22, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0))
br label %24
24: ; preds = %21, %16
%25 = tail call i64 @truncate(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i32 0)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = tail call i32 (i8*, ...) @msg_errno(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0))
br label %29
29: ; preds = %27, %24
%30 = tail call i64 @truncate(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0), i32 0)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = tail call i32 (i8*, ...) @msg_errno(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.9, i64 0, i64 0))
br label %34
34: ; preds = %32, %29
%35 = tail call i64 @truncate(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.10, i64 0, i64 0), i32 0)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = tail call i32 (i8*, ...) @msg_errno(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.10, i64 0, i64 0))
br label %39
39: ; preds = %37, %34
%40 = tail call i64 @chmod(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 365)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %39
%43 = tail call i32 (i8*, ...) @msg_errno(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.11, i64 0, i64 0))
br label %44
44: ; preds = %42, %39
%45 = tail call i32 @execl(i8* %7, i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.12, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.13, i64 0, i64 0), i32* null)
%46 = tail call i32 (i8*, ...) @msg_errno(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.14, i64 0, i64 0), i8* %7)
ret i32 0
}