#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 */
char *safe_append_char(char *, char const, unsigned int *, unsigned int *);
scalar_t__ safe_malloc(unsigned int);
char *escape_windows_command_arg(const char *arg) {
  const char *p;
  char *ret;
  unsigned int rpos = 0, rsize = 1;
  ret = (char *)safe_malloc(rsize);
  ret = safe_append_char(ret, '"', &rpos, &rsize);
  for (p = arg; *p != '\0'; p++) {
    unsigned int num_backslashes;
    unsigned int i;
    num_backslashes = 0;
    for (; *p == '\\'; p++)
      num_backslashes++;
    if (*p == '\0') {
      for (i = 0; i < num_backslashes * 2; i++)
        ret = safe_append_char(ret, '\\', &rpos, &rsize);
      break;
    } else if (*p == '"') {
      for (i = 0; i < num_backslashes * 2 + 1; i++)
        ret = safe_append_char(ret, '\\', &rpos, &rsize);
      ret[rpos++] = *p;
    } else {
      for (i = 0; i < num_backslashes; i++)
        ret = safe_append_char(ret, '\\', &rpos, &rsize);
      ret = safe_append_char(ret, *p, &rpos, &rsize);
    }
  }
  ret = safe_append_char(ret, '"', &rpos, &rsize);
  ret = safe_append_char(ret, '\0', &rpos, &rsize);
  return ret;
}


x86 assembly (clang, -O0):
<escape_windows_command_arg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x1,-0x20(%rbp)
mov    -0x20(%rbp),%edi
call   1030 <safe_malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x22,%esi
lea    -0x1c(%rbp),%rdx
lea    -0x20(%rbp),%rcx
call   1040 <safe_append_char@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     12e2 <escape_windows_command_arg+0x1c2>
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5c,%eax
jne    11a9 <escape_windows_command_arg+0x89>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    117f <escape_windows_command_arg+0x5f>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11fe <escape_windows_command_arg+0xde>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    -0x24(%rbp),%ecx
shl    $0x1,%ecx
cmp    %ecx,%eax
jae    11f9 <escape_windows_command_arg+0xd9>
mov    -0x18(%rbp),%rdi
mov    $0x5c,%esi
lea    -0x1c(%rbp),%rdx
lea    -0x20(%rbp),%rcx
call   1040 <safe_append_char@plt>
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11c0 <escape_windows_command_arg+0xa0>
jmp    12e2 <escape_windows_command_arg+0x1c2>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x22,%eax
jne    1270 <escape_windows_command_arg+0x150>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    -0x24(%rbp),%ecx
shl    $0x1,%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jae    1251 <escape_windows_command_arg+0x131>
mov    -0x18(%rbp),%rdi
mov    $0x5c,%esi
lea    -0x1c(%rbp),%rdx
lea    -0x20(%rbp),%rcx
call   1040 <safe_append_char@plt>
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1215 <escape_windows_command_arg+0xf5>
mov    -0x10(%rbp),%rax
mov    (%rax),%dl
mov    -0x18(%rbp),%rax
mov    -0x1c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x1c(%rbp)
mov    %ecx,%ecx
mov    %dl,(%rax,%rcx,1)
jmp    12c7 <escape_windows_command_arg+0x1a7>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jae    12ab <escape_windows_command_arg+0x18b>
mov    -0x18(%rbp),%rdi
mov    $0x5c,%esi
lea    -0x1c(%rbp),%rdx
lea    -0x20(%rbp),%rcx
call   1040 <safe_append_char@plt>
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1277 <escape_windows_command_arg+0x157>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rax
lea    -0x1c(%rbp),%rdx
lea    -0x20(%rbp),%rcx
movsbl (%rax),%esi
call   1040 <safe_append_char@plt>
mov    %rax,-0x18(%rbp)
jmp    12cc <escape_windows_command_arg+0x1ac>
jmp    12d1 <escape_windows_command_arg+0x1b1>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    1168 <escape_windows_command_arg+0x48>
mov    -0x18(%rbp),%rdi
mov    $0x22,%esi
lea    -0x1c(%rbp),%rdx
lea    -0x20(%rbp),%rcx
call   1040 <safe_append_char@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
lea    -0x1c(%rbp),%rdx
lea    -0x20(%rbp),%rcx
call   1040 <safe_append_char@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<escape_windows_command_arg>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
movl   $0x0,(%rsp)
movl   $0x1,0x4(%rsp)
mov    $0x1,%edi
call   1030 <safe_malloc@plt>
mov    %rsp,%rdx
lea    0x4(%rsp),%rcx
mov    %rax,%rdi
mov    $0x22,%esi
call   1040 <safe_append_char@plt>
mov    (%r12),%cl
test   %cl,%cl
je     1269 <escape_windows_command_arg+0x149>
mov    %rsp,%r15
lea    0x4(%rsp),%r14
mov    %r15,%rdx
mov    $0x1,%ebp
xor    %ebx,%ebx
xor    %r13d,%r13d
xor    %r15d,%r15d
cmp    $0x5c,%cl
jne    11a9 <escape_windows_command_arg+0x89>
cs nopw 0x0(%rax,%rax,1)
nop
movzbl 0x1(%r12,%r13,1),%ecx
add    $0x2,%r15d
add    $0x1,%r13
add    $0x2,%ebp
add    $0x1,%ebx
cmp    $0x5c,%cl
je     1190 <escape_windows_command_arg+0x70>
test   %cl,%cl
je     1243 <escape_windows_command_arg+0x123>
cmp    $0x22,%cl
jne    11f0 <escape_windows_command_arg+0xd0>
mov    %rdx,%r15
nopl   0x0(%rax)
mov    %rax,%rdi
mov    $0x5c,%esi
mov    %r15,%rdx
mov    %r14,%rcx
call   1040 <safe_append_char@plt>
add    $0xffffffff,%ebp
jne    11c0 <escape_windows_command_arg+0xa0>
mov    (%r12,%r13,1),%cl
mov    (%rsp),%edx
lea    0x1(%rdx),%esi
mov    %esi,(%rsp)
mov    %cl,(%rax,%rdx,1)
jmp    122d <escape_windows_command_arg+0x10d>
nopw   0x0(%rax,%rax,1)
test   %r13d,%r13d
mov    %rdx,%r15
je     121c <escape_windows_command_arg+0xfc>
nopl   0x0(%rax,%rax,1)
mov    %rax,%rdi
mov    $0x5c,%esi
mov    %r15,%rdx
mov    %r14,%rcx
call   1040 <safe_append_char@plt>
add    $0xffffffff,%ebx
jne    1200 <escape_windows_command_arg+0xe0>
mov    (%r12,%r13,1),%cl
movsbl %cl,%esi
mov    %rax,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
call   1040 <safe_append_char@plt>
mov    0x1(%r12,%r13,1),%cl
add    %r13,%r12
add    $0x1,%r12
test   %cl,%cl
jne    1170 <escape_windows_command_arg+0x50>
jmp    1269 <escape_windows_command_arg+0x149>
test   %r15d,%r15d
je     1269 <escape_windows_command_arg+0x149>
mov    %rsp,%r14
lea    0x4(%rsp),%rbx
mov    %rax,%rdi
mov    $0x5c,%esi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1040 <safe_append_char@plt>
add    $0xffffffff,%r15d
jne    1250 <escape_windows_command_arg+0x130>
mov    %rsp,%rbx
lea    0x4(%rsp),%r14
mov    %rax,%rdi
mov    $0x22,%esi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1040 <safe_append_char@plt>
mov    %rax,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1040 <safe_append_char@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @escape_windows_command_arg(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %5
store i32 1, i32* %6
%9 = load i32, i32* %6
%10 = call i64 @safe_malloc(i32 %9)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %4
%12 = load i8*, i8** %4
%13 = call i8* @safe_append_char(i8* %12, i8 signext 34, i32* %5, i32* %6)
store i8* %13, i8** %4
%14 = load i8*, i8** %2
store i8* %14, i8** %3
br label %15
15: ; preds = %94, %1
%16 = load i8*, i8** %3
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %97
20: ; preds = %15
store i32 0, i32* %7
br label %21
21: ; preds = %29, %20
%22 = load i8*, i8** %3
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 92
br i1 %25, label %26, label %32
26: ; preds = %21
%27 = load i32, i32* %7
%28 = add i32 %27, 1
store i32 %28, i32* %7
br label %29
29: ; preds = %26
%30 = load i8*, i8** %3
%31 = getelementptr inbounds i8, i8* %30, i32 1
store i8* %31, i8** %3
br label %21
32: ; preds = %21
%33 = load i8*, i8** %3
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %50
37: ; preds = %32
store i32 0, i32* %8
br label %38
38: ; preds = %46, %37
%39 = load i32, i32* %8
%40 = load i32, i32* %7
%41 = mul i32 %40, 2
%42 = icmp ult i32 %39, %41
br i1 %42, label %43, label %49
43: ; preds = %38
%44 = load i8*, i8** %4
%45 = call i8* @safe_append_char(i8* %44, i8 signext 92, i32* %5, i32* %6)
store i8* %45, i8** %4
br label %46
46: ; preds = %43
%47 = load i32, i32* %8
%48 = add i32 %47, 1
store i32 %48, i32* %8
br label %38
49: ; preds = %38
br label %97
50: ; preds = %32
%51 = load i8*, i8** %3
%52 = load i8, i8* %51
%53 = sext i8 %52 to i32
%54 = icmp eq i32 %53, 34
br i1 %54, label %55, label %76
55: ; preds = %50
store i32 0, i32* %8
br label %56
56: ; preds = %65, %55
%57 = load i32, i32* %8
%58 = load i32, i32* %7
%59 = mul i32 %58, 2
%60 = add i32 %59, 1
%61 = icmp ult i32 %57, %60
br i1 %61, label %62, label %68
62: ; preds = %56
%63 = load i8*, i8** %4
%64 = call i8* @safe_append_char(i8* %63, i8 signext 92, i32* %5, i32* %6)
store i8* %64, i8** %4
br label %65
65: ; preds = %62
%66 = load i32, i32* %8
%67 = add i32 %66, 1
store i32 %67, i32* %8
br label %56
68: ; preds = %56
%69 = load i8*, i8** %3
%70 = load i8, i8* %69
%71 = load i8*, i8** %4
%72 = load i32, i32* %5
%73 = add i32 %72, 1
store i32 %73, i32* %5
%74 = zext i32 %72 to i64
%75 = getelementptr inbounds i8, i8* %71, i64 %74
store i8 %70, i8* %75
br label %92
76: ; preds = %50
store i32 0, i32* %8
br label %77
77: ; preds = %84, %76
%78 = load i32, i32* %8
%79 = load i32, i32* %7
%80 = icmp ult i32 %78, %79
br i1 %80, label %81, label %87
81: ; preds = %77
%82 = load i8*, i8** %4
%83 = call i8* @safe_append_char(i8* %82, i8 signext 92, i32* %5, i32* %6)
store i8* %83, i8** %4
br label %84
84: ; preds = %81
%85 = load i32, i32* %8
%86 = add i32 %85, 1
store i32 %86, i32* %8
br label %77
87: ; preds = %77
%88 = load i8*, i8** %4
%89 = load i8*, i8** %3
%90 = load i8, i8* %89
%91 = call i8* @safe_append_char(i8* %88, i8 signext %90, i32* %5, i32* %6)
store i8* %91, i8** %4
br label %92
92: ; preds = %87, %68
br label %93
93: ; preds = %92
br label %94
94: ; preds = %93
%95 = load i8*, i8** %3
%96 = getelementptr inbounds i8, i8* %95, i32 1
store i8* %96, i8** %3
br label %15
97: ; preds = %49, %15
%98 = load i8*, i8** %4
%99 = call i8* @safe_append_char(i8* %98, i8 signext 34, i32* %5, i32* %6)
store i8* %99, i8** %4
%100 = load i8*, i8** %4
%101 = call i8* @safe_append_char(i8* %100, i8 signext 0, i32* %5, i32* %6)
store i8* %101, i8** %4
%102 = load i8*, i8** %4
ret i8* %102
}

LLVM IR (-O2):
define i8* @escape_windows_command_arg(i8* nocapture %0) {
%2 = alloca i32
%3 = alloca i32
%4 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%5 = bitcast i32* %3 to i8*
store i32 1, i32* %3
%6 = tail call i64 @safe_malloc(i32 1)
%7 = inttoptr i64 %6 to i8*
%8 = call i8* @safe_append_char(i8* %7, i8 signext 34, i32* %2, i32* %3)
%9 = load i8, i8* %0
%10 = icmp eq i8 %9, 0
br i1 %10, label %66, label %11
11: ; preds = %1, %61
%12 = phi i8 [ %64, %61 ], [ %9, %1 ]
%13 = phi i8* [ %62, %61 ], [ %8, %1 ]
%14 = phi i8* [ %63, %61 ], [ %0, %1 ]
br label %15
15: ; preds = %11, %27
%16 = phi i8 [ %30, %27 ], [ %12, %11 ]
%17 = phi i8* [ %29, %27 ], [ %14, %11 ]
%18 = phi i32 [ %28, %27 ], [ 0, %11 ]
switch i8 %16, label %22 [
i8 92, label %27
i8 0, label %24
i8 34, label %19
]
19: ; preds = %15
%20 = shl i32 %18, 1
%21 = or i32 %20, 1
br label %37
22: ; preds = %15
%23 = icmp eq i32 %18, 0
br i1 %23, label %57, label %49
24: ; preds = %15
%25 = shl i32 %18, 1
%26 = icmp eq i32 %25, 0
br i1 %26, label %66, label %31
27: ; preds = %15
%28 = add i32 %18, 1
%29 = getelementptr inbounds i8, i8* %17, i64 1
%30 = load i8, i8* %29
br label %15
31: ; preds = %24, %31
%32 = phi i32 [ %35, %31 ], [ 0, %24 ]
%33 = phi i8* [ %34, %31 ], [ %13, %24 ]
%34 = call i8* @safe_append_char(i8* %33, i8 signext 92, i32* %2, i32* %3)
%35 = add nuw i32 %32, 1
%36 = icmp eq i32 %35, %25
br i1 %36, label %66, label %31
37: ; preds = %19, %37
%38 = phi i32 [ 0, %19 ], [ %41, %37 ]
%39 = phi i8* [ %13, %19 ], [ %40, %37 ]
%40 = call i8* @safe_append_char(i8* %39, i8 signext 92, i32* %2, i32* %3)
%41 = add nuw i32 %38, 1
%42 = icmp eq i32 %41, %21
br i1 %42, label %43, label %37
43: ; preds = %37
%44 = load i8, i8* %17
%45 = load i32, i32* %2
%46 = add i32 %45, 1
store i32 %46, i32* %2
%47 = zext i32 %45 to i64
%48 = getelementptr inbounds i8, i8* %40, i64 %47
store i8 %44, i8* %48
br label %61
49: ; preds = %22, %49
%50 = phi i32 [ %53, %49 ], [ 0, %22 ]
%51 = phi i8* [ %52, %49 ], [ %13, %22 ]
%52 = call i8* @safe_append_char(i8* %51, i8 signext 92, i32* %2, i32* %3)
%53 = add nuw i32 %50, 1
%54 = icmp eq i32 %53, %18
br i1 %54, label %55, label %49
55: ; preds = %49
%56 = load i8, i8* %17
br label %57
57: ; preds = %55, %22
%58 = phi i8 [ %16, %22 ], [ %56, %55 ]
%59 = phi i8* [ %13, %22 ], [ %52, %55 ]
%60 = call i8* @safe_append_char(i8* %59, i8 signext %58, i32* %2, i32* %3)
br label %61
61: ; preds = %43, %57
%62 = phi i8* [ %60, %57 ], [ %40, %43 ]
%63 = getelementptr inbounds i8, i8* %17, i64 1
%64 = load i8, i8* %63
%65 = icmp eq i8 %64, 0
br i1 %65, label %66, label %11
66: ; preds = %61, %31, %1, %24
%67 = phi i8* [ %13, %24 ], [ %8, %1 ], [ %34, %31 ], [ %62, %61 ]
%68 = call i8* @safe_append_char(i8* %67, i8 signext 34, i32* %2, i32* %3)
%69 = call i8* @safe_append_char(i8* %68, i8 signext 0, i32* %2, i32* %3)
ret i8* %69
}