#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 *, ...);
char *getenv(char *);
int /*<<< orphan*/ rename(char *, char *);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int strlen(char *);
scalar_t__ strncmp(char *, char *, int);
void storesetting(char *setting, char *value) {
  FILE *f1;
  FILE *f2;
  char *home;
  char temp1[1024];
  char temp2[1024];
  if (!setting) {
    return;
  }
  home = getenv("HOME");
  sprintf(temp1, "%s/.libufs.bak", home);
  sprintf(temp2, "%s/.libufs", home);
  rename(temp2, temp1);
  f1 = fopen(temp1, "r");
  if (!f1) {
    return;
  }
  f2 = fopen(temp2, "w");
  if (!f2) {
    fclose(f2);
    return;
  }
  sprintf(temp2, "%s=", setting);
  while (fgets(temp1, 1024, f1)) {
    if (strncmp(temp1, temp2, strlen(temp2)) != 0) {
      if (strlen(temp1) > 2) {
        fprintf(f2, "%s", temp1);
      }
    }
  }
  if (value) {
    fprintf(f2, "%s=%s\n", setting, value);
  }
  fclose(f1);
  fclose(f2);
}


x86 assembly (clang, -O0):
<storesetting>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11b3 <storesetting+0x23>
jmp    1347 <storesetting+0x1b7>
lea    0xe46(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
lea    -0x430(%rbp),%rdi
mov    -0x28(%rbp),%rdx
lea    0xe30(%rip),%rsi
call   10b0 <sprintf@plt>
lea    -0x830(%rbp),%rdi
mov    -0x28(%rbp),%rdx
lea    0xe28(%rip),%rsi
call   10b0 <sprintf@plt>
lea    -0x830(%rbp),%rdi
lea    -0x430(%rbp),%rsi
call   10a0 <rename@plt>
lea    -0x430(%rbp),%rdi
lea    0xe0d(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    122b <storesetting+0x9b>
jmp    1347 <storesetting+0x1b7>
lea    -0x830(%rbp),%rdi
lea    0xde8(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    125b <storesetting+0xcb>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
jmp    1347 <storesetting+0x1b7>
lea    -0x830(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xdb6(%rip),%rsi
call   10b0 <sprintf@plt>
lea    -0x430(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x400,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     1310 <storesetting+0x180>
lea    -0x430(%rbp),%rax
mov    %rax,-0x840(%rbp)
lea    -0x830(%rbp),%rax
mov    %rax,-0x838(%rbp)
lea    -0x830(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x840(%rbp),%rdi
mov    -0x838(%rbp),%rsi
mov    %eax,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
je     130b <storesetting+0x17b>
lea    -0x430(%rbp),%rdi
call   1060 <strlen@plt>
cmp    $0x2,%eax
jle    1306 <storesetting+0x176>
mov    -0x20(%rbp),%rdi
lea    -0x430(%rbp),%rdx
lea    0xd28(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
jmp    130b <storesetting+0x17b>
jmp    1272 <storesetting+0xe2>
cmpq   $0x0,-0x10(%rbp)
je     1335 <storesetting+0x1a5>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xcfc(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<storesetting>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x808,%rsp
test   %rdi,%rdi
je     12f5 <storesetting+0x155>
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe39(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%rbx
lea    0xe2f(%rip),%rsi
lea    0x400(%rsp),%r12
mov    %r12,%rdi
mov    %rax,%rdx
call   10c0 <sprintf@plt>
lea    0xe24(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %rbx,%rdx
call   10c0 <sprintf@plt>
mov    %rbp,%rdi
mov    %r12,%rsi
call   10b0 <rename@plt>
lea    0xe0f(%rip),%rsi
mov    %r12,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12f5 <storesetting+0x155>
mov    %rax,%r13
lea    0xdf6(%rip),%rsi
mov    %rsp,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1307 <storesetting+0x167>
mov    %rax,%r12
lea    0xddd(%rip),%rsi
mov    %rsp,%rdi
mov    %r15,%rdx
call   10c0 <sprintf@plt>
lea    0x400(%rsp),%rdi
mov    $0x400,%esi
mov    %r13,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     12c9 <storesetting+0x129>
mov    %rsp,%rbx
lea    0x400(%rsp),%rbp
jmp    1295 <storesetting+0xf5>
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    $0x400,%esi
mov    %r13,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     12c9 <storesetting+0x129>
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     1280 <storesetting+0xe0>
mov    %rbp,%rdi
call   1060 <strlen@plt>
cmp    $0x3,%eax
jl     1280 <storesetting+0xe0>
mov    %rbp,%rdi
mov    %r12,%rsi
call   1070 <fputs@plt>
jmp    1280 <storesetting+0xe0>
test   %r14,%r14
je     12e5 <storesetting+0x145>
lea    0xd52(%rip),%rsi
mov    %r12,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    %r13,%rdi
call   1050 <fclose@plt>
mov    %r12,%rdi
call   1050 <fclose@plt>
add    $0x808,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edi,%edi
add    $0x808,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <fclose@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"HOME\00"
@.str.1 = [15 x i8] c"%s/.libufs.bak\00"
@.str.2 = [11 x i8] c"%s/.libufs\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [4 x i8] c"%s=\00"
@.str.6 = [3 x i8] c"%s\00"
@.str.7 = [7 x i8] c"%s=%s\0A\00"
define void @storesetting(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca [1024 x i8]
%9 = alloca [1024 x i8]
store i8* %0, i8** %3
store i8* %1, i8** %4
%10 = load i8*, i8** %3
%11 = icmp ne i8* %10, null
br i1 %11, label %13, label %12
12: ; preds = %2
br label %76
13: ; preds = %2
%14 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %14, i8** %7
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%16 = load i8*, i8** %7
%17 = call i32 @sprintf(i8* %15, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%19 = load i8*, i8** %7
%20 = call i32 @sprintf(i8* %18, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %19)
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%23 = call i32 @rename(i8* %21, i8* %22)
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%25 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %25, i32** %5
%26 = load i32*, i32** %5
%27 = icmp ne i32* %26, null
br i1 %27, label %29, label %28
28: ; preds = %13
br label %76
29: ; preds = %13
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%31 = call i32* @fopen(i8* %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %31, i32** %6
%32 = load i32*, i32** %6
%33 = icmp ne i32* %32, null
br i1 %33, label %37, label %34
34: ; preds = %29
%35 = load i32*, i32** %6
%36 = call i32 @fclose(i32* %35)
br label %76
37: ; preds = %29
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%39 = load i8*, i8** %3
%40 = call i32 @sprintf(i8* %38, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %39)
br label %41
41: ; preds = %62, %37
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%43 = load i32*, i32** %5
%44 = call i64 @fgets(i8* %42, i32 1024, i32* %43)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %63
46: ; preds = %41
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%48 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%49 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%50 = call i32 @strlen(i8* %49)
%51 = call i64 @strncmp(i8* %47, i8* %48, i32 %50)
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %62
53: ; preds = %46
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%55 = call i32 @strlen(i8* %54)
%56 = icmp sgt i32 %55, 2
br i1 %56, label %57, label %61
57: ; preds = %53
%58 = load i32*, i32** %6
%59 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%60 = call i32 (i32*, i8*, i8*, ...) @fprintf(i32* %58, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i8* %59)
br label %61
61: ; preds = %57, %53
br label %62
62: ; preds = %61, %46
br label %41
63: ; preds = %41
%64 = load i8*, i8** %4
%65 = icmp ne i8* %64, null
br i1 %65, label %66, label %71
66: ; preds = %63
%67 = load i32*, i32** %6
%68 = load i8*, i8** %3
%69 = load i8*, i8** %4
%70 = call i32 (i32*, i8*, i8*, ...) @fprintf(i32* %67, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0), i8* %68, i8* %69)
br label %71
71: ; preds = %66, %63
%72 = load i32*, i32** %5
%73 = call i32 @fclose(i32* %72)
%74 = load i32*, i32** %6
%75 = call i32 @fclose(i32* %74)
br label %76
76: ; preds = %71, %34, %28, %12
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"HOME\00"
@.str.1 = [15 x i8] c"%s/.libufs.bak\00"
@.str.2 = [11 x i8] c"%s/.libufs\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [4 x i8] c"%s=\00"
@.str.7 = [7 x i8] c"%s=%s\0A\00"
define void @storesetting(i8* %0, i8* %1) {
%3 = alloca [1024 x i8]
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%7 = icmp eq i8* %0, null
br i1 %7, label %43, label %8
8: ; preds = %2
%9 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %9)
%11 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %9)
%12 = call i32 @rename(i8* %6, i8* %5)
%13 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%14 = icmp eq i32* %13, null
br i1 %14, label %43, label %15
15: ; preds = %8
%16 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = tail call i32 @fclose(i32* null)
br label %43
20: ; preds = %15
%21 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %0)
%22 = call i64 @fgets(i8* %5, i32 1024, i32* %13)
%23 = icmp eq i64 %22, 0
br i1 %23, label %36, label %24
24: ; preds = %20, %33
%25 = call i32 @strlen(i8* %6)
%26 = call i64 @strncmp(i8* %5, i8* %6, i32 %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %33, label %28
28: ; preds = %24
%29 = call i32 @strlen(i8* %5)
%30 = icmp sgt i32 %29, 2
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = call i32 @fputs(i8* %5, i32* %16)
br label %33
33: ; preds = %28, %31, %24
%34 = call i64 @fgets(i8* %5, i32 1024, i32* %13)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %24
36: ; preds = %33, %20
%37 = icmp eq i8* %1, null
br i1 %37, label %40, label %38
38: ; preds = %36
%39 = call i32 (i32*, i8*, i8*, ...) @fprintf(i32* %16, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.7, i64 0, i64 0), i8* %0, i8* %1)
br label %40
40: ; preds = %38, %36
%41 = call i32 @fclose(i32* %13)
%42 = call i32 @fclose(i32* %16)
br label %43
43: ; preds = %8, %2, %40, %18
ret void
}