#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*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *, ...);
int /*<<< orphan*/ rtrim(char *);
int /*<<< orphan*/ strcmp(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncmp(char *, char *, int);
int insertFile(char *path, char *file, char *title) {
  FILE *fp;
  int got = 0, num = 0;
  char buf[256];
  if ((fp = fopen(path, "a+")) != NULL) {
    while (fgets(buf, 256, fp)) {
      rtrim(buf);
      if (!strncmp(buf, "Path=", 5)) {
        num++;
        if (!strcmp(buf + 7, file)) {
          got = 1;
          break;
        }
      }
    }
    if (!got) {
      if (strlen(title) > 38)
        fprintf(fp, "Name=%s\n", title);
      else
        fprintf(fp, "Name=%-38sBBS\xcf\xb5\xcd\xb3\n", title);
      fprintf(fp, "Path=~/%s\nNumb=%d\n#\n", file, num + 1);
    }
    fclose(fp);
  }
}


x86 assembly (clang, -O0):
<insertFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe50(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     12c2 <insertFile+0x142>
jmp    11c8 <insertFile+0x48>
lea    -0x130(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    $0x100,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     1250 <insertFile+0xd0>
lea    -0x130(%rbp),%rdi
call   1080 <rtrim@plt>
lea    -0x130(%rbp),%rdi
lea    0xe02(%rip),%rsi
mov    $0x5,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    124b <insertFile+0xcb>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
lea    -0x130(%rbp),%rdi
add    $0x7,%rdi
mov    -0x18(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    1246 <insertFile+0xc6>
movl   $0x1,-0x2c(%rbp)
jmp    1250 <insertFile+0xd0>
jmp    124b <insertFile+0xcb>
jmp    11c8 <insertFile+0x48>
cmpl   $0x0,-0x2c(%rbp)
jne    12b9 <insertFile+0x139>
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x26,%eax
jle    1287 <insertFile+0x107>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xd8e(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
jmp    129d <insertFile+0x11d>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xd7c(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x30(%rbp),%ecx
add    $0x1,%ecx
lea    0xd73(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<insertFile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %rdx,%rbp
mov    %rsi,%r14
lea    0xe62(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1288 <insertFile+0x108>
mov    %rax,%rbx
mov    %rbp,0x8(%rsp)
lea    0x10(%rsp),%rdi
mov    $0x100,%esi
mov    %rax,%rdx
call   1060 <fgets@plt>
xor    %r13d,%r13d
test   %rax,%rax
je     1236 <insertFile+0xb6>
lea    0x17(%rsp),%r12
lea    0x10(%rsp),%rbp
lea    0xe24(%rip),%r15
jmp    1209 <insertFile+0x89>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%r13d
mov    %rbp,%rdi
mov    $0x100,%esi
mov    %rbx,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     1236 <insertFile+0xb6>
mov    %rbp,%rdi
call   1080 <rtrim@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    $0x5,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
jne    11f4 <insertFile+0x74>
mov    %r12,%rdi
mov    %r14,%rsi
call   1070 <strcmp@plt>
test   %eax,%eax
jne    11f0 <insertFile+0x70>
jmp    1280 <insertFile+0x100>
mov    0x8(%rsp),%rbp
mov    %rbp,%rdi
call   1050 <strlen@plt>
cmp    $0x27,%eax
lea    0xdbc(%rip),%rax
lea    0xdbe(%rip),%rsi
cmovge %rax,%rsi
mov    %rbx,%rdi
mov    %rbp,%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
add    $0x1,%r13d
lea    0xdb5(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
mov    %r13d,%ecx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"a+\00"
@.str.1 = [6 x i8] c"Path=\00"
@.str.2 = [9 x i8] c"Name=%s\0A\00"
@.str.3 = [19 x i8] c"Name=%-38sBBS\CF\B5\CD\B3\0A\00"
@.str.4 = [21 x i8] c"Path=~/%s\0ANumb=%d\0A#\0A\00"
define i32 @insertFile(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca [256 x i8]
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 0, i32* %9
store i32 0, i32* %10
%12 = load i8*, i8** %5
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %8
%14 = icmp ne i32* %13, null
br i1 %14, label %15, label %62
15: ; preds = %3
br label %16
16: ; preds = %37, %15
%17 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%18 = load i32*, i32** %8
%19 = call i64 @fgets(i8* %17, i32 256, i32* %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %38
21: ; preds = %16
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%23 = call i32 @rtrim(i8* %22)
%24 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%25 = call i32 @strncmp(i8* %24, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 5)
%26 = icmp ne i32 %25, 0
br i1 %26, label %37, label %27
27: ; preds = %21
%28 = load i32, i32* %10
%29 = add nsw i32 %28, 1
store i32 %29, i32* %10
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%31 = getelementptr inbounds i8, i8* %30, i64 7
%32 = load i8*, i8** %6
%33 = call i32 @strcmp(i8* %31, i8* %32)
%34 = icmp ne i32 %33, 0
br i1 %34, label %36, label %35
35: ; preds = %27
store i32 1, i32* %9
br label %38
36: ; preds = %27
br label %37
37: ; preds = %36, %21
br label %16
38: ; preds = %35, %16
%39 = load i32, i32* %9
%40 = icmp ne i32 %39, 0
br i1 %40, label %59, label %41
41: ; preds = %38
%42 = load i8*, i8** %7
%43 = call i32 @strlen(i8* %42)
%44 = icmp sgt i32 %43, 38
br i1 %44, label %45, label %49
45: ; preds = %41
%46 = load i32*, i32** %8
%47 = load i8*, i8** %7
%48 = call i32 (i32*, i8*, i8*, ...) @fprintf(i32* %46, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* %47)
br label %53
49: ; preds = %41
%50 = load i32*, i32** %8
%51 = load i8*, i8** %7
%52 = call i32 (i32*, i8*, i8*, ...) @fprintf(i32* %50, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i8* %51)
br label %53
53: ; preds = %49, %45
%54 = load i32*, i32** %8
%55 = load i8*, i8** %6
%56 = load i32, i32* %10
%57 = add nsw i32 %56, 1
%58 = call i32 (i32*, i8*, i8*, ...) @fprintf(i32* %54, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i8* %55, i32 %57)
br label %59
59: ; preds = %53, %38
%60 = load i32*, i32** %8
%61 = call i32 @fclose(i32* %60)
br label %62
62: ; preds = %59, %3
%63 = load i32, i32* %4
ret i32 %63
}

LLVM IR (-O2):
@.str = [3 x i8] c"a+\00"
@.str.1 = [6 x i8] c"Path=\00"
@.str.2 = [9 x i8] c"Name=%s\0A\00"
@.str.3 = [19 x i8] c"Name=%-38sBBS\CF\B5\CD\B3\0A\00"
@.str.4 = [21 x i8] c"Path=~/%s\0ANumb=%d\0A#\0A\00"
define i32 @insertFile(i8* nocapture %0, i8* %1, i8* %2) {
%4 = alloca [256 x i8]
%5 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %36, label %8
8: ; preds = %3
%9 = call i64 @fgets(i8* %5, i32 256, i32* %6)
%10 = icmp eq i64 %9, 0
br i1 %10, label %26, label %11
11: ; preds = %8
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 7
br label %13
13: ; preds = %11, %22
%14 = phi i32 [ 0, %11 ], [ %23, %22 ]
%15 = call i32 @rtrim(i8* %5)
%16 = call i32 @strncmp(i8* %5, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 5)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %13
%19 = add nsw i32 %14, 1
%20 = call i32 @strcmp(i8* %12, i8* dereferenceable(1) %1)
%21 = icmp eq i32 %20, 0
br i1 %21, label %34, label %22
22: ; preds = %18, %13
%23 = phi i32 [ %14, %13 ], [ %19, %18 ]
%24 = call i64 @fgets(i8* %5, i32 256, i32* %6)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %13
26: ; preds = %22, %8
%27 = phi i32 [ 0, %8 ], [ %23, %22 ]
%28 = call i32 @strlen(i8* %2)
%29 = icmp sgt i32 %28, 38
%30 = select i1 %29, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0)
%31 = call i32 (i32*, i8*, i8*, ...) @fprintf(i32* %6, i8* %30, i8* %2)
%32 = add nsw i32 %27, 1
%33 = call i32 (i32*, i8*, i8*, ...) @fprintf(i32* %6, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i8* %1, i32 %32)
br label %34
34: ; preds = %18, %26
%35 = call i32 @fclose(i32* %6)
br label %36
36: ; preds = %34, %3
ret i32 undef
}