#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*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char const *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ remove(char *);
int /*<<< orphan*/ rename(char *, char *);
int /*<<< orphan*/ strchomp(char *);
scalar_t__ strcmp(char *, char const *);
void insert_str_in_file(const char *str) {
  FILE *fpr;
  FILE *fpw;
  char buffer[1024];
  int printed = 0;
  fpr = fopen("names.txt", "r");
  if (fpr == NULL) {
    fpw = fopen("names.txt", "w");
    if (fpw == NULL) {
      perror("Could not open for write");
      return;
    }
    fprintf(fpw, "%s\n", str);
    fclose(fpw);
    return;
  }
  remove("names.txt");
  fpw = fopen("names.txt"
              ".new",
              "w");
  if (fpw == NULL) {
    perror("Could not open for write");
    fclose(fpr);
    return;
  }
  while (fgets(buffer, 1024, fpr) != NULL) {
    strchomp(buffer);
    if (printed != 1 && strcmp(buffer, str) > 0) {
      fprintf(fpw, "%s\n", str);
      printed = 1;
    }
    fprintf(fpw, "%s\n", buffer);
  }
  if (printed != 1) {
    fprintf(fpw, "%s\n", str);
  }
  fclose(fpr);
  fclose(fpw);
  rename("names.txt"
         ".new",
         "names.txt");
}


x86 assembly (clang, -O0):
<insert_str_in_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x424(%rbp)
lea    0xe50(%rip),%rdi
lea    0xe53(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1220 <insert_str_in_file+0x90>
lea    0xe2e(%rip),%rdi
lea    0xe5c(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11fe <insert_str_in_file+0x6e>
lea    0xe18(%rip),%rdi
call   10a0 <perror@plt>
jmp    133f <insert_str_in_file+0x1af>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe18(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
jmp    133f <insert_str_in_file+0x1af>
lea    0xdd9(%rip),%rdi
call   1030 <remove@plt>
lea    0xdf6(%rip),%rdi
lea    0xdfb(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1268 <insert_str_in_file+0xd8>
lea    0xdb7(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
jmp    133f <insert_str_in_file+0x1af>
jmp    126d <insert_str_in_file+0xdd>
lea    -0x420(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x400,%esi
call   1060 <fgets@plt>
cmp    $0x0,%rax
je     12f9 <insert_str_in_file+0x169>
lea    -0x420(%rbp),%rdi
call   1040 <strchomp@plt>
cmpl   $0x1,-0x424(%rbp)
je     12dd <insert_str_in_file+0x14d>
lea    -0x420(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jle    12dd <insert_str_in_file+0x14d>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xd57(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0x1,-0x424(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x420(%rbp),%rdx
lea    0xd36(%rip),%rsi
call   1080 <fprintf@plt>
jmp    126d <insert_str_in_file+0xdd>
cmpl   $0x1,-0x424(%rbp)
je     131a <insert_str_in_file+0x18a>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xd10(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
lea    0xcf6(%rip),%rdi
lea    0xcc6(%rip),%rsi
call   10b0 <rename@plt>
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<insert_str_in_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rdi,0x8(%rsp)
lea    0xe53(%rip),%rdi
lea    0xe56(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     129e <insert_str_in_file+0x10e>
mov    %rax,%r15
lea    0xe34(%rip),%rdi
call   1030 <remove@plt>
lea    0xe51(%rip),%rdi
lea    0xe56(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1338 <insert_str_in_file+0x1a8>
mov    %rax,%r12
lea    0x10(%rsp),%rdi
mov    $0x400,%esi
mov    %r15,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     12ef <insert_str_in_file+0x15f>
xor    %r13d,%r13d
lea    0x10(%rsp),%rbx
jmp    124b <insert_str_in_file+0xbb>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12,%rdi
lea    0xdfb(%rip),%rsi
mov    %rbx,%rdx
call   1080 <fprintf@plt>
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r15,%rdx
call   1060 <fgets@plt>
test   %rax,%rax
je     12ea <insert_str_in_file+0x15a>
mov    %r13d,%ebp
mov    %rbx,%rdi
call   1040 <strchomp@plt>
mov    $0x1,%r13d
mov    $0x1,%r14b
cmp    $0x1,%ebp
je     1220 <insert_str_in_file+0x90>
mov    %rbx,%rdi
mov    0x8(%rsp),%rsi
call   1070 <strcmp@plt>
xor    %r14d,%r14d
mov    $0x0,%r13d
test   %rax,%rax
jle    1220 <insert_str_in_file+0x90>
mov    %r12,%rdi
lea    0xd9c(%rip),%rsi
mov    0x8(%rsp),%rdx
call   1080 <fprintf@plt>
mov    $0x1,%r14b
mov    $0x1,%r13d
jmp    1220 <insert_str_in_file+0x90>
lea    0xd5b(%rip),%rdi
lea    0xd89(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1349 <insert_str_in_file+0x1b9>
mov    %rax,%rbx
lea    0xd61(%rip),%rsi
mov    %rax,%rdi
mov    0x8(%rsp),%rdx
call   1080 <fprintf@plt>
mov    %rbx,%rdi
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <fclose@plt>
test   %r14b,%r14b
jne    1303 <insert_str_in_file+0x173>
lea    0xd2f(%rip),%rsi
mov    %r12,%rdi
mov    0x8(%rsp),%rdx
call   1080 <fprintf@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
mov    %r12,%rdi
call   1050 <fclose@plt>
lea    0xd0f(%rip),%rdi
lea    0xcdf(%rip),%rsi
call   10b0 <rename@plt>
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xccd(%rip),%rdi
call   10a0 <perror@plt>
mov    %r15,%rdi
jmp    12d4 <insert_str_in_file+0x144>
lea    0xcbc(%rip),%rdi
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10a0 <perror@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"names.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [25 x i8] c"Could not open for write\00"
@.str.4 = [4 x i8] c"%s\0A\00"
@.str.5 = [14 x i8] c"names.txt.new\00"
define void @insert_str_in_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca [1024 x i8]
%6 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %6
%7 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %3
%8 = load i32*, i32** %3
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %22
10: ; preds = %1
%11 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %11, i32** %4
%12 = load i32*, i32** %4
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %68
16: ; preds = %10
%17 = load i32*, i32** %4
%18 = load i8*, i8** %2
%19 = call i32 @fprintf(i32* %17, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %18)
%20 = load i32*, i32** %4
%21 = call i32 @fclose(i32* %20)
br label %68
22: ; preds = %1
%23 = call i32 @remove(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%24 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %24, i32** %4
%25 = load i32*, i32** %4
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %31
27: ; preds = %22
%28 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
%29 = load i32*, i32** %3
%30 = call i32 @fclose(i32* %29)
br label %68
31: ; preds = %22
br label %32
32: ; preds = %51, %31
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%34 = load i32*, i32** %3
%35 = call i32* @fgets(i8* %33, i32 1024, i32* %34)
%36 = icmp ne i32* %35, null
br i1 %36, label %37, label %55
37: ; preds = %32
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%39 = call i32 @strchomp(i8* %38)
%40 = load i32, i32* %6
%41 = icmp ne i32 %40, 1
br i1 %41, label %42, label %51
42: ; preds = %37
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%44 = load i8*, i8** %2
%45 = call i64 @strcmp(i8* %43, i8* %44)
%46 = icmp sgt i64 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %42
%48 = load i32*, i32** %4
%49 = load i8*, i8** %2
%50 = call i32 @fprintf(i32* %48, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %49)
store i32 1, i32* %6
br label %51
51: ; preds = %47, %42, %37
%52 = load i32*, i32** %4
%53 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%54 = call i32 @fprintf(i32* %52, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %53)
br label %32
55: ; preds = %32
%56 = load i32, i32* %6
%57 = icmp ne i32 %56, 1
br i1 %57, label %58, label %62
58: ; preds = %55
%59 = load i32*, i32** %4
%60 = load i8*, i8** %2
%61 = call i32 @fprintf(i32* %59, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %60)
br label %62
62: ; preds = %58, %55
%63 = load i32*, i32** %3
%64 = call i32 @fclose(i32* %63)
%65 = load i32*, i32** %4
%66 = call i32 @fclose(i32* %65)
%67 = call i32 @rename(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %68
68: ; preds = %62, %27, %16, %14
ret void
}

LLVM IR (-O2):
@.str = [10 x i8] c"names.txt\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [25 x i8] c"Could not open for write\00"
@.str.4 = [4 x i8] c"%s\0A\00"
@.str.5 = [14 x i8] c"names.txt.new\00"
define void @insert_str_in_file(i8* %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %14
6: ; preds = %1
%7 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %46
11: ; preds = %6
%12 = tail call i32 @fprintf(i32* %7, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %0)
%13 = tail call i32 @fclose(i32* %7)
br label %46
14: ; preds = %1
%15 = tail call i32 @remove(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%16 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %21, label %18
18: ; preds = %14
%19 = call i32* @fgets(i8* %3, i32 1024, i32* %4)
%20 = icmp eq i32* %19, null
br i1 %20, label %40, label %24
21: ; preds = %14
%22 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
%23 = tail call i32 @fclose(i32* %4)
br label %46
24: ; preds = %18, %33
%25 = phi i32 [ %35, %33 ], [ 0, %18 ]
%26 = call i32 @strchomp(i8* %3)
%27 = icmp eq i32 %25, 1
br i1 %27, label %33, label %28
28: ; preds = %24
%29 = call i64 @strcmp(i8* %3, i8* %0)
%30 = icmp sgt i64 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = call i32 @fprintf(i32* %16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %0)
br label %33
33: ; preds = %31, %28, %24
%34 = phi i1 [ true, %31 ], [ false, %28 ], [ true, %24 ]
%35 = phi i32 [ 1, %31 ], [ 0, %28 ], [ 1, %24 ]
%36 = call i32 @fprintf(i32* %16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %3)
%37 = call i32* @fgets(i8* %3, i32 1024, i32* %4)
%38 = icmp eq i32* %37, null
br i1 %38, label %39, label %24
39: ; preds = %33
br i1 %34, label %42, label %40
40: ; preds = %18, %39
%41 = call i32 @fprintf(i32* %16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %0)
br label %42
42: ; preds = %40, %39
%43 = call i32 @fclose(i32* %4)
%44 = call i32 @fclose(i32* %16)
%45 = call i32 @rename(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %46
46: ; preds = %42, %21, %11, %9
ret void
}