#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*/ path;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *);
char *getenv(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int change_state(char *cmd) {
  FILE *fp;
  char path[2048];
  char *bh;
  bh = getenv("BOITHOHOME");
  if (bh == NULL)
    return 300;
  snprintf(path, sizeof(path), "%s/var/phonehome.state", bh);
  fp = fopen(path, "w");
  if (fp == NULL) {
    return 301;
  }
  fprintf(fp, "%s\n", cmd);
  fclose(fp);
  return 1;
}


x86 assembly (clang, -O0):
<change_state>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xe9a(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x828(%rbp)
cmpq   $0x0,-0x828(%rbp)
jne    118c <change_state+0x3c>
movl   $0x12c,-0x4(%rbp)
jmp    11fd <change_state+0xad>
lea    -0x820(%rbp),%rdi
mov    -0x828(%rbp),%rcx
mov    $0x800,%esi
lea    0xe65(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x820(%rbp),%rdi
lea    0xe69(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d9 <change_state+0x89>
movl   $0x12d,-0x4(%rbp)
jmp    11fd <change_state+0xad>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe3c(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<change_state>:
push   %r14
push   %rbx
sub    $0x808,%rsp
mov    %rdi,%r14
lea    0xe9c(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11c0 <change_state+0x70>
lea    0xe96(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x800,%esi
mov    %rax,%rcx
call   1050 <snprintf@plt>
lea    0xe93(%rip),%rsi
mov    %rbx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11c7 <change_state+0x77>
mov    %rax,%rbx
lea    0xe7e(%rip),%rsi
mov    %rax,%rdi
mov    %r14,%rdx
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    $0x1,%eax
jmp    11cc <change_state+0x7c>
mov    $0x12c,%eax
jmp    11cc <change_state+0x7c>
mov    $0x12d,%eax
add    $0x808,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [11 x i8] c"BOITHOHOME\00"
@.str.1 = [23 x i8] c"%s/var/phonehome.state\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [4 x i8] c"%s\0A\00"
define i32 @change_state(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca [2048 x i8]
%6 = alloca i8*
store i8* %0, i8** %3
%7 = call i8* @getenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i8* %7, i8** %6
%8 = load i8*, i8** %6
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 300, i32* %2
br label %26
11: ; preds = %1
%12 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%13 = load i8*, i8** %6
%14 = call i32 @snprintf(i8* %12, i32 2048, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %13)
%15 = getelementptr inbounds [2048 x i8], [2048 x i8]* %5, i64 0, i64 0
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %16, i32** %4
%17 = load i32*, i32** %4
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %20
19: ; preds = %11
store i32 301, i32* %2
br label %26
20: ; preds = %11
%21 = load i32*, i32** %4
%22 = load i8*, i8** %3
%23 = call i32 @fprintf(i32* %21, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %22)
%24 = load i32*, i32** %4
%25 = call i32 @fclose(i32* %24)
store i32 1, i32* %2
br label %26
26: ; preds = %20, %19, %10
%27 = load i32, i32* %2
ret i32 %27
}

LLVM IR (-O2):
@.str = [11 x i8] c"BOITHOHOME\00"
@.str.1 = [23 x i8] c"%s/var/phonehome.state\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [4 x i8] c"%s\0A\00"
define i32 @change_state(i8* %0) {
%2 = alloca [2048 x i8]
%3 = getelementptr inbounds [2048 x i8], [2048 x i8]* %2, i64 0, i64 0
%4 = tail call i8* @getenv(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i8* %4, null
br i1 %5, label %13, label %6
6: ; preds = %1
%7 = call i32 @snprintf(i8* %3, i32 2048, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%8 = call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %13, label %10
10: ; preds = %6
%11 = call i32 @fprintf(i32* %8, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%12 = call i32 @fclose(i32* %8)
br label %13
13: ; preds = %6, %1, %10
%14 = phi i32 [ 1, %10 ], [ 300, %1 ], [ 301, %6 ]
ret i32 %14
}