#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 */
struct buildopt {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
struct buildopt *cli_buildopt_find(char const *);
int /*<<< orphan*/ cli_clean(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ cli_fatal(char *, ...);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fprintf(int /*<<< orphan*/ *, char *, char const *);
int rename(char *, char *);
int strlen(char const *);
void cli_flavor_change(const char *name) {
  FILE *fp;
  int ret;
  struct buildopt *bopt;
  if ((bopt = cli_buildopt_find(name)) == NULL)
    cli_fatal("no such flavor: %s", name);
  if ((fp = fopen(".flavor.tmp", "w")) == NULL)
    cli_fatal("failed to open temporary file to save flavor");
  ret = fprintf(fp, "%s\n", name);
  if (ret == -1 || (size_t)ret != (strlen(name) + 1))
    cli_fatal("failed to write new build flavor");
  (void)fclose(fp);
  if (rename(".flavor.tmp", ".flavor") == -1)
    cli_fatal("failed to replace build flavor");
  cli_clean(0, NULL);
}


x86 assembly (clang, -O0):
<cli_flavor_change>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <cli_buildopt_find@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11b5 <cli_flavor_change+0x35>
mov    -0x8(%rbp),%rsi
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1080 <cli_fatal@plt>
lea    0xe57(%rip),%rdi
lea    0xe5c(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11e4 <cli_flavor_change+0x64>
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1080 <cli_fatal@plt>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe5b(%rip),%rsi
call   1060 <fprintf@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     122b <cli_flavor_change+0xab>
movslq -0x14(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
add    $0x1,%ecx
movslq %ecx,%rcx
cmp    %rcx,%rax
je     1239 <cli_flavor_change+0xb9>
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1080 <cli_fatal@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
lea    0xdca(%rip),%rdi
lea    0xe23(%rip),%rsi
call   10a0 <rename@plt>
cmp    $0xffffffff,%eax
jne    126c <cli_flavor_change+0xec>
lea    0xe16(%rip),%rdi
mov    $0x0,%al
call   1080 <cli_fatal@plt>
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <cli_clean@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cli_flavor_change>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
call   1030 <cli_buildopt_find@plt>
test   %rax,%rax
jne    11a2 <cli_flavor_change+0x22>
lea    0xe68(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1080 <cli_fatal@plt>
lea    0xe6a(%rip),%rdi
lea    0xe6f(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11cb <cli_flavor_change+0x4b>
lea    0xe5d(%rip),%rdi
xor    %eax,%eax
call   1080 <cli_fatal@plt>
lea    0xe7c(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   1060 <fprintf@plt>
cmp    $0xffffffff,%eax
je     11f3 <cli_flavor_change+0x73>
mov    %eax,%ebp
mov    %r14,%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
cmp    %eax,%ebp
je     1201 <cli_flavor_change+0x81>
lea    0xe58(%rip),%rdi
xor    %eax,%eax
call   1080 <cli_fatal@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
lea    0xe03(%rip),%rdi
lea    0xe5c(%rip),%rsi
call   10a0 <rename@plt>
cmp    $0xffffffff,%eax
jne    122f <cli_flavor_change+0xaf>
lea    0xe53(%rip),%rdi
xor    %eax,%eax
call   1080 <cli_fatal@plt>
xor    %edi,%edi
xor    %esi,%esi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1070 <cli_clean@plt>

LLVM IR (-O0):
%struct.buildopt = type { i32 }
@.str = [19 x i8] c"no such flavor: %s\00"
@.str.1 = [12 x i8] c".flavor.tmp\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [45 x i8] c"failed to open temporary file to save flavor\00"
@.str.4 = [4 x i8] c"%s\0A\00"
@.str.5 = [33 x i8] c"failed to write new build flavor\00"
@.str.6 = [8 x i8] c".flavor\00"
@.str.7 = [31 x i8] c"failed to replace build flavor\00"
define void @cli_flavor_change(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca %struct.buildopt*
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call %struct.buildopt* @cli_buildopt_find(i8* %6)
store %struct.buildopt* %7, %struct.buildopt** %5
%8 = icmp eq %struct.buildopt* %7, null
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = load i8*, i8** %2
%11 = call i32 (i8*, ...) @cli_fatal(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %12
12: ; preds = %9, %1
%13 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %13, i32** %3
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 (i8*, ...) @cli_fatal(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0))
br label %17
17: ; preds = %15, %12
%18 = load i32*, i32** %3
%19 = load i8*, i8** %2
%20 = call i32 @fprintf(i32* %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %19)
store i32 %20, i32* %4
%21 = load i32, i32* %4
%22 = icmp eq i32 %21, -1
br i1 %22, label %31, label %23
23: ; preds = %17
%24 = load i32, i32* %4
%25 = sext i32 %24 to i64
%26 = load i8*, i8** %2
%27 = call i32 @strlen(i8* %26)
%28 = add nsw i32 %27, 1
%29 = sext i32 %28 to i64
%30 = icmp ne i64 %25, %29
br i1 %30, label %31, label %33
31: ; preds = %23, %17
%32 = call i32 (i8*, ...) @cli_fatal(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0))
br label %33
33: ; preds = %31, %23
%34 = load i32*, i32** %3
%35 = call i32 @fclose(i32* %34)
%36 = call i32 @rename(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %33
%39 = call i32 (i8*, ...) @cli_fatal(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.7, i64 0, i64 0))
br label %40
40: ; preds = %38, %33
%41 = call i32 @cli_clean(i32 0, i32* null)
ret void
}

LLVM IR (-O2):
%struct.buildopt = type { i32 }
@.str = [19 x i8] c"no such flavor: %s\00"
@.str.1 = [12 x i8] c".flavor.tmp\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [45 x i8] c"failed to open temporary file to save flavor\00"
@.str.4 = [4 x i8] c"%s\0A\00"
@.str.5 = [33 x i8] c"failed to write new build flavor\00"
@.str.6 = [8 x i8] c".flavor\00"
@.str.7 = [31 x i8] c"failed to replace build flavor\00"
define void @cli_flavor_change(i8* %0) {
%2 = tail call %struct.buildopt* @cli_buildopt_find(i8* %0)
%3 = icmp eq %struct.buildopt* %2, null
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 (i8*, ...) @cli_fatal(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %6
6: ; preds = %4, %1
%7 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, 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 (i8*, ...) @cli_fatal(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.3, i64 0, i64 0))
br label %11
11: ; preds = %9, %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 = icmp eq i32 %12, -1
br i1 %13, label %18, label %14
14: ; preds = %11
%15 = tail call i32 @strlen(i8* %0)
%16 = add nsw i32 %15, 1
%17 = icmp eq i32 %12, %16
br i1 %17, label %20, label %18
18: ; preds = %14, %11
%19 = tail call i32 (i8*, ...) @cli_fatal(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0))
br label %20
20: ; preds = %18, %14
%21 = tail call i32 @fclose(i32* %7)
%22 = tail call i32 @rename(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = tail call i32 (i8*, ...) @cli_fatal(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.7, i64 0, i64 0))
br label %26
26: ; preds = %24, %20
%27 = tail call i32 @cli_clean(i32 0, i32* null)
ret void
}