#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 */
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int creat(char *, int);
char *insertpid(char *, char *);
int createfile(char *file) {
  char buf[1024];
  char *next, *endb;
  next = buf;
  endb = buf + sizeof(buf);
  while (*file) {
    if (*file == '%') {
      switch (file[1]) {
      case 'p':
        if (!(next = insertpid(next, endb)))
          return -1;
        file += 2;
        break;
      default:
        goto copy;
      }
    } else {
    copy:
      *next++ = *file++;
    }
    if (next >= endb)
      return -1;
  }
  *next = '\0';
  return creat(buf, 0644);
}


x86 assembly (clang, -O0):
<createfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x418(%rbp)
lea    -0x410(%rbp),%rax
add    $0x400,%rax
mov    %rax,-0x420(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     121f <createfile+0xff>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x25,%eax
jne    11cd <createfile+0xad>
mov    -0x10(%rbp),%rax
mov    0x1(%rax),%al
sub    $0x70,%al
jne    11c3 <createfile+0xa3>
jmp    1182 <createfile+0x62>
mov    -0x418(%rbp),%rdi
mov    -0x420(%rbp),%rsi
call   1030 <insertpid@plt>
mov    %rax,-0x418(%rbp)
cmp    $0x0,%rax
jne    11b2 <createfile+0x92>
movl   $0xffffffff,-0x4(%rbp)
jmp    123d <createfile+0x11d>
mov    -0x10(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x10(%rbp)
jmp    11c8 <createfile+0xa8>
jmp    11d2 <createfile+0xb2>
jmp    11fa <createfile+0xda>
jmp    11d2 <createfile+0xb2>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%cl
mov    -0x418(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x418(%rbp)
mov    %cl,(%rax)
mov    -0x418(%rbp),%rax
cmp    -0x420(%rbp),%rax
jb     121a <createfile+0xfa>
movl   $0xffffffff,-0x4(%rbp)
jmp    123d <createfile+0x11d>
jmp    1151 <createfile+0x31>
mov    -0x418(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x410(%rbp),%rdi
mov    $0x1a4,%esi
call   1040 <creat@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<createfile>:
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%rbx
mov    %rsp,%rax
lea    0x400(%rsp),%r14
jmp    1153 <createfile+0x33>
nopw   0x0(%rax,%rax,1)
test   %cl,%cl
je     1181 <createfile+0x61>
add    $0x1,%rbx
mov    %cl,(%rax)
add    $0x1,%rax
cmp    %r14,%rax
jae    117a <createfile+0x5a>
movzbl (%rbx),%ecx
cmp    $0x25,%cl
jne    1140 <createfile+0x20>
cmpb   $0x70,0x1(%rbx)
jne    1144 <createfile+0x24>
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <insertpid@plt>
test   %rax,%rax
je     117a <createfile+0x5a>
add    $0x2,%rbx
cmp    %r14,%rax
jb     1153 <createfile+0x33>
mov    $0xffffffff,%eax
jmp    1191 <createfile+0x71>
movb   $0x0,(%rax)
mov    %rsp,%rdi
mov    $0x1a4,%esi
call   1040 <creat@plt>
add    $0x408,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @createfile(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [1024 x i8]
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %3
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
store i8* %7, i8** %5
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%9 = getelementptr inbounds i8, i8* %8, i64 1024
store i8* %9, i8** %6
br label %10
10: ; preds = %47, %1
%11 = load i8*, i8** %3
%12 = load i8, i8* %11
%13 = icmp ne i8 %12, 0
br i1 %13, label %14, label %48
14: ; preds = %10
%15 = load i8*, i8** %3
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = icmp eq i32 %17, 37
br i1 %18, label %19, label %35
19: ; preds = %14
%20 = load i8*, i8** %3
%21 = getelementptr inbounds i8, i8* %20, i64 1
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
switch i32 %23, label %33 [
i32 112, label %24
]
24: ; preds = %19
%25 = load i8*, i8** %5
%26 = load i8*, i8** %6
%27 = call i8* @insertpid(i8* %25, i8* %26)
store i8* %27, i8** %5
%28 = icmp ne i8* %27, null
br i1 %28, label %30, label %29
29: ; preds = %24
store i32 -1, i32* %2
br label %52
30: ; preds = %24
%31 = load i8*, i8** %3
%32 = getelementptr inbounds i8, i8* %31, i64 2
store i8* %32, i8** %3
br label %34
33: ; preds = %19
br label %36
34: ; preds = %30
br label %42
35: ; preds = %14
br label %36
36: ; preds = %35, %33
%37 = load i8*, i8** %3
%38 = getelementptr inbounds i8, i8* %37, i32 1
store i8* %38, i8** %3
%39 = load i8, i8* %37
%40 = load i8*, i8** %5
%41 = getelementptr inbounds i8, i8* %40, i32 1
store i8* %41, i8** %5
store i8 %39, i8* %40
br label %42
42: ; preds = %36, %34
%43 = load i8*, i8** %5
%44 = load i8*, i8** %6
%45 = icmp uge i8* %43, %44
br i1 %45, label %46, label %47
46: ; preds = %42
store i32 -1, i32* %2
br label %52
47: ; preds = %42
br label %10
48: ; preds = %10
%49 = load i8*, i8** %5
store i8 0, i8* %49
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%51 = call i32 @creat(i8* %50, i32 420)
store i32 %51, i32* %2
br label %52
52: ; preds = %48, %46, %29
%53 = load i32, i32* %2
ret i32 %53
}

LLVM IR (-O2):
define i32 @createfile(i8* nocapture %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 1024
br label %5
5: ; preds = %21, %1
%6 = phi i8* [ %0, %1 ], [ %22, %21 ]
%7 = phi i8* [ %3, %1 ], [ %23, %21 ]
%8 = load i8, i8* %6
switch i8 %8, label %18 [
i8 0, label %25
i8 37, label %9
]
9: ; preds = %5
%10 = getelementptr inbounds i8, i8* %6, i64 1
%11 = load i8, i8* %10
%12 = icmp eq i8 %11, 112
br i1 %12, label %13, label %18
13: ; preds = %9
%14 = call i8* @insertpid(i8* %7, i8* %4)
%15 = icmp eq i8* %14, null
br i1 %15, label %27, label %16
16: ; preds = %13
%17 = getelementptr inbounds i8, i8* %6, i64 2
br label %21
18: ; preds = %5, %9
%19 = getelementptr inbounds i8, i8* %6, i64 1
%20 = getelementptr inbounds i8, i8* %7, i64 1
store i8 %8, i8* %7
br label %21
21: ; preds = %18, %16
%22 = phi i8* [ %17, %16 ], [ %19, %18 ]
%23 = phi i8* [ %14, %16 ], [ %20, %18 ]
%24 = icmp ult i8* %23, %4
br i1 %24, label %5, label %27
25: ; preds = %5
store i8 0, i8* %7
%26 = call i32 @creat(i8* %3, i32 420)
br label %27
27: ; preds = %21, %13, %25
%28 = phi i32 [ %26, %25 ], [ -1, %13 ], [ -1, %21 ]
ret i32 %28
}