#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ die(char *, ...);
int /*<<< orphan*/ *fopen(char *, char *);
char *memchr(char *, char, int);
scalar_t__ memcmp(char *, char *, int);
int /*<<< orphan*/ memmove(char *, char *, int);
FILE *open_file(char *buf, int *length) {
  char *file_name = NULL;
  char *file_name_eos = NULL;
  char *c = NULL;
  FILE *out_file = NULL;
  char t = 0;
  for (file_name = buf;
       file_name <
       buf + *length - (sizeof("filename=\"") / sizeof("filename=\""[0]) - 1);
       file_name++) {
    if (memcmp(file_name, "filename=\"",
               (sizeof("filename=\"") / sizeof("filename=\""[0]) - 1)) != 0) {
      continue;
    }
    file_name += (sizeof("filename=\"") / sizeof("filename=\""[0]) - 1);
    file_name_eos = memchr(file_name, '"', *length - (file_name - buf));
    if (file_name_eos == NULL) {
      die("Malformed filename in \"%.*s\" string\n", *length, buf);
    }
    for (c = file_name_eos - 1; c > file_name; c--) {
      if (*c == '\\' || *c == '/') {
        c += 1;
        memmove(file_name, c, *length - (c - buf));
        *length -= (c - file_name);
        file_name_eos -= (c - file_name);
        break;
      }
    }
    t = *file_name_eos;
    *file_name_eos = 0;
    out_file = fopen(file_name, "wx");
    if (out_file == NULL) {
      die("Failed to open file %s for writing\n", file_name);
    }
    *file_name_eos = t;
    return out_file;
  }
  return NULL;
}


x86 assembly (clang, -O0):
<open_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movb   $0x0,-0x39(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%rdx
movslq (%rdx),%rdx
add    %rdx,%rcx
add    $0xfffffffffffffff6,%rcx
cmp    %rcx,%rax
jae    1346 <open_file+0x1f6>
mov    -0x20(%rbp),%rdi
lea    0xe4a(%rip),%rsi
mov    $0xa,%edx
call   1040 <memcmp@plt>
cmp    $0x0,%rax
je     11cf <open_file+0x7f>
jmp    1335 <open_file+0x1e5>
mov    -0x20(%rbp),%rax
add    $0xa,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rax
movslq (%rax),%rax
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rdx
sub    %rdx,%rcx
sub    %rcx,%rax
mov    %eax,%edx
mov    $0x22,%esi
call   1030 <memchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1227 <open_file+0xd7>
mov    -0x18(%rbp),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
lea    0xdeb(%rip),%rdi
mov    $0x0,%al
call   1070 <die@plt>
mov    -0x28(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x20(%rbp),%rax
jbe    12de <open_file+0x18e>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x5c,%eax
je     1261 <open_file+0x111>
mov    -0x30(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    12c8 <open_file+0x178>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%rax
movslq (%rax),%rax
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rdx
sub    %rdx,%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1050 <memmove@plt>
mov    -0x30(%rbp),%rdx
mov    -0x20(%rbp),%rax
sub    %rax,%rdx
mov    -0x18(%rbp),%rax
movslq (%rax),%rcx
sub    %rdx,%rcx
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rdx
mov    -0x20(%rbp),%rax
sub    %rax,%rdx
mov    -0x28(%rbp),%rax
xor    %ecx,%ecx
sub    %rdx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    12de <open_file+0x18e>
jmp    12cd <open_file+0x17d>
mov    -0x30(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x30(%rbp)
jmp    1233 <open_file+0xe3>
mov    -0x28(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x39(%rbp)
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
lea    0xd37(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    131f <open_file+0x1cf>
mov    -0x20(%rbp),%rsi
lea    0xd1b(%rip),%rdi
mov    $0x0,%al
call   1070 <die@plt>
mov    -0x39(%rbp),%cl
mov    -0x28(%rbp),%rax
mov    %cl,(%rax)
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    134e <open_file+0x1fe>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    118c <open_file+0x3c>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
movslq (%rsi),%rax
add    %rdi,%rax
add    $0xfffffffffffffff6,%rax
cmp    %rdi,%rax
jbe    11bc <open_file+0x6c>
mov    %rsi,%r14
lea    -0xa(%rdi),%r15
lea    0x9(%rdi),%rbp
mov    $0xa,%r13d
lea    0xe7e(%rip),%r12
mov    %rdi,(%rsp)
mov    %rdi,%rbx
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0xa,%edx
call   1040 <memcmp@plt>
test   %rax,%rax
je     11c3 <open_file+0x73>
add    $0x1,%rbx
movslq (%r14),%rax
add    %r15,%rax
add    $0x1,%rbp
add    $0x1,%r13d
cmp    %rax,%rbx
jb     1190 <open_file+0x40>
xor    %ebp,%ebp
jmp    1286 <open_file+0x136>
add    $0xa,%rbx
mov    (%r14),%edx
sub    %r13d,%edx
mov    %rbx,%rdi
mov    $0x22,%esi
call   1030 <memchr@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11f7 <open_file+0xa7>
mov    (%r14),%esi
lea    0xe1f(%rip),%rdi
mov    (%rsp),%rdx
xor    %eax,%eax
call   1070 <die@plt>
xor    %r13d,%r13d
nopw   0x0(%rax,%rax,1)
lea    (%r12,%r13,1),%rax
add    $0xffffffffffffffff,%rax
cmp    %rbx,%rax
jbe    1251 <open_file+0x101>
movzbl (%rax),%eax
add    $0xffffffffffffffff,%r13
cmp    $0x5c,%al
je     121c <open_file+0xcc>
cmp    $0x2f,%al
jne    1200 <open_file+0xb0>
mov    (%rsp),%rax
sub    %r12d,%eax
add    (%r14),%eax
mov    %r13d,%edx
not    %edx
add    %eax,%edx
lea    (%r12,%r13,1),%rsi
add    $0x1,%rsi
mov    %rbx,%rdi
call   1050 <memmove@plt>
mov    %ebp,%eax
sub    %r12d,%eax
add    (%r14),%eax
sub    %r13d,%eax
mov    %eax,(%r14)
sub    %r13,%rbp
mov    %rbp,%r12
mov    (%r12),%r14b
movb   $0x0,(%r12)
lea    0xdcf(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    1282 <open_file+0x132>
lea    0xdbb(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <die@plt>
mov    %r14b,(%r12)
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"filename=\22\00"
@.str.1 = [37 x i8] c"Malformed filename in \22%.*s\22 string\0A\00"
@.str.2 = [3 x i8] c"wx\00"
@.str.3 = [36 x i8] c"Failed to open file %s for writing\0A\00"
define i32* @open_file(i8* %0, i32* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i8
store i8* %0, i8** %4
store i32* %1, i32** %5
store i8* null, i8** %6
store i8* null, i8** %7
store i8* null, i8** %8
store i32* null, i32** %9
store i8 0, i8* %10
%11 = load i8*, i8** %4
store i8* %11, i8** %6
br label %12
12: ; preds = %118, %2
%13 = load i8*, i8** %6
%14 = load i8*, i8** %4
%15 = load i32*, i32** %5
%16 = load i32, i32* %15
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %14, i64 %17
%19 = getelementptr inbounds i8, i8* %18, i64 -10
%20 = icmp ult i8* %13, %19
br i1 %20, label %21, label %121
21: ; preds = %12
%22 = load i8*, i8** %6
%23 = call i64 @memcmp(i8* %22, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 10)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %21
br label %118
26: ; preds = %21
%27 = load i8*, i8** %6
%28 = getelementptr inbounds i8, i8* %27, i64 10
store i8* %28, i8** %6
%29 = load i8*, i8** %6
%30 = load i32*, i32** %5
%31 = load i32, i32* %30
%32 = sext i32 %31 to i64
%33 = load i8*, i8** %6
%34 = load i8*, i8** %4
%35 = ptrtoint i8* %33 to i64
%36 = ptrtoint i8* %34 to i64
%37 = sub i64 %35, %36
%38 = sub nsw i64 %32, %37
%39 = trunc i64 %38 to i32
%40 = call i8* @memchr(i8* %29, i8 signext 34, i32 %39)
store i8* %40, i8** %7
%41 = load i8*, i8** %7
%42 = icmp eq i8* %41, null
br i1 %42, label %43, label %48
43: ; preds = %26
%44 = load i32*, i32** %5
%45 = load i32, i32* %44
%46 = load i8*, i8** %4
%47 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %45, i8* %46)
br label %48
48: ; preds = %43, %26
%49 = load i8*, i8** %7
%50 = getelementptr inbounds i8, i8* %49, i64 -1
store i8* %50, i8** %8
br label %51
51: ; preds = %100, %48
%52 = load i8*, i8** %8
%53 = load i8*, i8** %6
%54 = icmp ugt i8* %52, %53
br i1 %54, label %55, label %103
55: ; preds = %51
%56 = load i8*, i8** %8
%57 = load i8, i8* %56
%58 = sext i8 %57 to i32
%59 = icmp eq i32 %58, 92
br i1 %59, label %65, label %60
60: ; preds = %55
%61 = load i8*, i8** %8
%62 = load i8, i8* %61
%63 = sext i8 %62 to i32
%64 = icmp eq i32 %63, 47
br i1 %64, label %65, label %99
65: ; preds = %60, %55
%66 = load i8*, i8** %8
%67 = getelementptr inbounds i8, i8* %66, i64 1
store i8* %67, i8** %8
%68 = load i8*, i8** %6
%69 = load i8*, i8** %8
%70 = load i32*, i32** %5
%71 = load i32, i32* %70
%72 = sext i32 %71 to i64
%73 = load i8*, i8** %8
%74 = load i8*, i8** %4
%75 = ptrtoint i8* %73 to i64
%76 = ptrtoint i8* %74 to i64
%77 = sub i64 %75, %76
%78 = sub nsw i64 %72, %77
%79 = trunc i64 %78 to i32
%80 = call i32 @memmove(i8* %68, i8* %69, i32 %79)
%81 = load i8*, i8** %8
%82 = load i8*, i8** %6
%83 = ptrtoint i8* %81 to i64
%84 = ptrtoint i8* %82 to i64
%85 = sub i64 %83, %84
%86 = load i32*, i32** %5
%87 = load i32, i32* %86
%88 = sext i32 %87 to i64
%89 = sub nsw i64 %88, %85
%90 = trunc i64 %89 to i32
store i32 %90, i32* %86
%91 = load i8*, i8** %8
%92 = load i8*, i8** %6
%93 = ptrtoint i8* %91 to i64
%94 = ptrtoint i8* %92 to i64
%95 = sub i64 %93, %94
%96 = load i8*, i8** %7
%97 = sub i64 0, %95
%98 = getelementptr inbounds i8, i8* %96, i64 %97
store i8* %98, i8** %7
br label %103
99: ; preds = %60
br label %100
100: ; preds = %99
%101 = load i8*, i8** %8
%102 = getelementptr inbounds i8, i8* %101, i32 -1
store i8* %102, i8** %8
br label %51
103: ; preds = %65, %51
%104 = load i8*, i8** %7
%105 = load i8, i8* %104
store i8 %105, i8* %10
%106 = load i8*, i8** %7
store i8 0, i8* %106
%107 = load i8*, i8** %6
%108 = call i32* @fopen(i8* %107, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %108, i32** %9
%109 = load i32*, i32** %9
%110 = icmp eq i32* %109, null
br i1 %110, label %111, label %114
111: ; preds = %103
%112 = load i8*, i8** %6
%113 = call i32 (i8*, ...) @die(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i8* %112)
br label %114
114: ; preds = %111, %103
%115 = load i8, i8* %10
%116 = load i8*, i8** %7
store i8 %115, i8* %116
%117 = load i32*, i32** %9
store i32* %117, i32** %3
br label %122
118: ; preds = %25
%119 = load i8*, i8** %6
%120 = getelementptr inbounds i8, i8* %119, i32 1
store i8* %120, i8** %6
br label %12
121: ; preds = %12
store i32* null, i32** %3
br label %122
122: ; preds = %121, %114
%123 = load i32*, i32** %3
ret i32* %123
}

LLVM IR (-O2):
@.str = [11 x i8] c"filename=\22\00"
@.str.1 = [37 x i8] c"Malformed filename in \22%.*s\22 string\0A\00"
@.str.2 = [3 x i8] c"wx\00"
@.str.3 = [36 x i8] c"Failed to open file %s for writing\0A\00"
define noalias i32* @open_file(i8* %0, i32* nocapture %1) {
%3 = load i32, i32* %1
%4 = sext i32 %3 to i64
%5 = getelementptr inbounds i8, i8* %0, i64 %4
%6 = getelementptr inbounds i8, i8* %5, i64 -10
%7 = icmp ugt i8* %6, %0
br i1 %7, label %8, label %61
8: ; preds = %2
%9 = getelementptr inbounds i8, i8* %0, i64 -10
br label %10
10: ; preds = %8, %55
%11 = phi i8* [ %56, %55 ], [ %0, %8 ]
%12 = tail call i64 @memcmp(i8* %11, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 10)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %55
14: ; preds = %10
%15 = getelementptr inbounds i8, i8* %11, i64 10
%16 = load i32, i32* %1
%17 = ptrtoint i8* %15 to i64
%18 = ptrtoint i8* %0 to i64
%19 = sub i64 %18, %17
%20 = trunc i64 %19 to i32
%21 = add i32 %16, %20
%22 = tail call i8* @memchr(i8* %15, i8 signext 34, i32 %21)
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %27
24: ; preds = %14
%25 = load i32, i32* %1
%26 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i32 %25, i8* %0)
br label %27
27: ; preds = %24, %14
br label %28
28: ; preds = %27, %32
%29 = phi i8* [ %30, %32 ], [ %22, %27 ]
%30 = getelementptr inbounds i8, i8* %29, i64 -1
%31 = icmp ugt i8* %30, %15
br i1 %31, label %32, label %47
32: ; preds = %28
%33 = load i8, i8* %30
switch i8 %33, label %28 [
i8 92, label %34
i8 47, label %34
]
34: ; preds = %32, %32
%35 = load i32, i32* %1
%36 = ptrtoint i8* %29 to i64
%37 = sub i64 %18, %36
%38 = trunc i64 %37 to i32
%39 = add i32 %35, %38
%40 = tail call i32 @memmove(i8* %15, i8* %29, i32 %39)
%41 = sub i64 %36, %17
%42 = load i32, i32* %1
%43 = trunc i64 %41 to i32
%44 = sub i32 %42, %43
store i32 %44, i32* %1
%45 = sub i64 0, %41
%46 = getelementptr inbounds i8, i8* %22, i64 %45
br label %47
47: ; preds = %28, %34
%48 = phi i8* [ %46, %34 ], [ %22, %28 ]
%49 = load i8, i8* %48
store i8 0, i8* %48
%50 = tail call i32* @fopen(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%51 = icmp eq i32* %50, null
br i1 %51, label %52, label %54
52: ; preds = %47
%53 = tail call i32 (i8*, ...) @die(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i8* %15)
br label %54
54: ; preds = %52, %47
store i8 %49, i8* %48
br label %61
55: ; preds = %10
%56 = getelementptr inbounds i8, i8* %11, i64 1
%57 = load i32, i32* %1
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8, i8* %9, i64 %58
%60 = icmp ult i8* %56, %59
br i1 %60, label %10, label %61
61: ; preds = %55, %2, %54
%62 = phi i32* [ %50, %54 ], [ null, %2 ], [ null, %55 ]
ret i32* %62
}