#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 stat {
  int /*<<< orphan*/ st_mode;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ ENOENT;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ S_ISDIR(int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *);
int stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int strlen(char *);
void state_set(char *dir, char *name, char *value) {
  FILE *fp;
  char *path;
  int path_len;
  struct stat st;
  int err;
  err = stat(dir, &st);
  if (err == -1) {
    if (errno == ENOENT) {
      fprintf(stderr, "Error: %s directory does not exist\n", dir);
    } else {
      perror("stat error");
    }
    exit(EXIT_FAILURE);
  } else {
    if (!S_ISDIR(st.st_mode)) {
      fprintf(stderr, "Error: %s is not a directory\n", dir);
      exit(EXIT_FAILURE);
    }
  }
  path_len = strlen(dir) + strlen(name) + 2;
  path = malloc(path_len);
  if (path == NULL) {
    perror("malloc path");
    return;
  }
  snprintf(path, path_len, "%s/%s", dir, name);
  fp = fopen(path, "w");
  if (fp == NULL) {
    perror("State file open failed");
    return;
  }
  fwrite(value, 1, strlen(value), fp);
  fclose(fp);
  free(path);
}


x86 assembly (clang, -O0):
<state_set>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1090 <stat@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1240 <state_set+0x80>
mov    0x2de3(%rip),%rax
mov    (%rax),%rax
mov    0x2dc1(%rip),%rcx
cmp    (%rcx),%rax
jne    1226 <state_set+0x66>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xde4(%rip),%rsi
call   1080 <fprintf@plt>
jmp    1232 <state_set+0x72>
lea    0xdf7(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x30(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%eax
jne    1278 <state_set+0xb8>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdca(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
jmp    127d <state_set+0xbd>
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%eax
add    %ecx,%eax
add    $0x2,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
call   10a0 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12c7 <state_set+0x107>
lea    0xd90(%rip),%rdi
call   10c0 <perror@plt>
jmp    1349 <state_set+0x189>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    -0x8(%rbp),%rcx
mov    -0x10(%rbp),%r8
lea    0xd7c(%rip),%rdx
call   1070 <snprintf@plt>
mov    -0x28(%rbp),%rdi
lea    0xd72(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1312 <state_set+0x152>
lea    0xd59(%rip),%rdi
call   10c0 <perror@plt>
jmp    1349 <state_set+0x189>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   10e0 <fwrite@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<state_set>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
lea    0x8(%rsp),%rsi
call   1090 <stat@plt>
cmp    $0xffffffff,%eax
je     12a7 <state_set+0xe7>
mov    0x8(%rsp),%edi
call   1060 <S_ISDIR@plt>
test   %eax,%eax
je     12cb <state_set+0x10b>
mov    %r12,%rdi
call   1050 <strlen@plt>
mov    %eax,%ebx
mov    %r15,%rdi
call   1050 <strlen@plt>
add    %eax,%ebx
add    $0x2,%ebx
mov    %ebx,%edi
call   10a0 <malloc@plt>
test   %rax,%rax
je     1285 <state_set+0xc5>
mov    %rax,%r13
lea    0xe32(%rip),%rdx
mov    %rax,%rdi
mov    %ebx,%esi
mov    %r12,%rcx
mov    %r15,%r8
call   1070 <snprintf@plt>
lea    0xe21(%rip),%rsi
mov    %r13,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     128e <state_set+0xce>
mov    %rax,%rbx
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %rbx,%rcx
call   10e0 <fwrite@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r13,%rdi
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1030 <free@plt>
lea    0xdc1(%rip),%rdi
jmp    1295 <state_set+0xd5>
lea    0xdcc(%rip),%rdi
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    10c0 <perror@plt>
mov    0x2d2a(%rip),%rax
mov    (%rax),%rax
mov    0x2d08(%rip),%rcx
cmp    (%rcx),%rax
je     12f1 <state_set+0x131>
lea    0xd60(%rip),%rdi
call   10c0 <perror@plt>
jmp    12e3 <state_set+0x123>
mov    0x2d0e(%rip),%rax
mov    (%rax),%edi
lea    0xd54(%rip),%rsi
mov    %r12,%rdx
call   1080 <fprintf@plt>
mov    0x2ce6(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    0x2ce8(%rip),%rax
mov    (%rax),%edi
lea    0xcff(%rip),%rsi
jmp    12db <state_set+0x11b>

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
@stderr = global i32 0
@.str = [36 x i8] c"Error: %s directory does not exist\0A\00"
@.str.1 = [11 x i8] c"stat error\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [30 x i8] c"Error: %s is not a directory\0A\00"
@.str.3 = [12 x i8] c"malloc path\00"
@.str.4 = [6 x i8] c"%s/%s\00"
@.str.5 = [2 x i8] c"w\00"
@.str.6 = [23 x i8] c"State file open failed\00"
define void @state_set(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca %struct.stat
%11 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%12 = load i8*, i8** %4
%13 = call i32 @stat(i8* %12, %struct.stat* %10)
store i32 %13, i32* %11
%14 = load i32, i32* %11
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %29
16: ; preds = %3
%17 = load i64, i64* @errno
%18 = load i64, i64* @ENOENT
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %24
20: ; preds = %16
%21 = load i32, i32* @stderr
%22 = load i8*, i8** %4
%23 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %22)
br label %26
24: ; preds = %16
%25 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %20
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %3
%30 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%31 = load i32, i32* %30
%32 = call i32 @S_ISDIR(i32 %31)
%33 = icmp ne i32 %32, 0
br i1 %33, label %40, label %34
34: ; preds = %29
%35 = load i32, i32* @stderr
%36 = load i8*, i8** %4
%37 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %36)
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %29
br label %41
41: ; preds = %40
%42 = load i8*, i8** %4
%43 = call i32 @strlen(i8* %42)
%44 = load i8*, i8** %5
%45 = call i32 @strlen(i8* %44)
%46 = add nsw i32 %43, %45
%47 = add nsw i32 %46, 2
store i32 %47, i32* %9
%48 = load i32, i32* %9
%49 = call i8* @malloc(i32 %48)
store i8* %49, i8** %8
%50 = load i8*, i8** %8
%51 = icmp eq i8* %50, null
br i1 %51, label %52, label %54
52: ; preds = %41
%53 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %76
54: ; preds = %41
%55 = load i8*, i8** %8
%56 = load i32, i32* %9
%57 = load i8*, i8** %4
%58 = load i8*, i8** %5
%59 = call i32 @snprintf(i8* %55, i32 %56, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %57, i8* %58)
%60 = load i8*, i8** %8
%61 = call i32* @fopen(i8* %60, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %61, i32** %7
%62 = load i32*, i32** %7
%63 = icmp eq i32* %62, null
br i1 %63, label %64, label %66
64: ; preds = %54
%65 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0))
br label %76
66: ; preds = %54
%67 = load i8*, i8** %6
%68 = load i8*, i8** %6
%69 = call i32 @strlen(i8* %68)
%70 = load i32*, i32** %7
%71 = call i32 @fwrite(i8* %67, i32 1, i32 %69, i32* %70)
%72 = load i32*, i32** %7
%73 = call i32 @fclose(i32* %72)
%74 = load i8*, i8** %8
%75 = call i32 @free(i8* %74)
br label %76
76: ; preds = %66, %64, %52
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
@stderr = global i32 0
@.str = [36 x i8] c"Error: %s directory does not exist\0A\00"
@.str.1 = [11 x i8] c"stat error\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [30 x i8] c"Error: %s is not a directory\0A\00"
@.str.3 = [12 x i8] c"malloc path\00"
@.str.4 = [6 x i8] c"%s/%s\00"
@.str.5 = [2 x i8] c"w\00"
@.str.6 = [23 x i8] c"State file open failed\00"
define void @state_set(i8* %0, i8* %1, i8* %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = call i32 @stat(i8* %0, %struct.stat* %4)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %20
8: ; preds = %3
%9 = load i64, i64* @errno
%10 = load i64, i64* @ENOENT
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %15
12: ; preds = %8
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %17
15: ; preds = %8
%16 = tail call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %12
%18 = load i32, i32* @EXIT_FAILURE
%19 = tail call i32 @exit(i32 %18)
unreachable
20: ; preds = %3
%21 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%22 = load i32, i32* %21
%23 = tail call i32 @S_ISDIR(i32 %22)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %20
%26 = load i32, i32* @stderr
%27 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%28 = load i32, i32* @EXIT_FAILURE
%29 = tail call i32 @exit(i32 %28)
unreachable
30: ; preds = %20
%31 = tail call i32 @strlen(i8* %0)
%32 = tail call i32 @strlen(i8* %1)
%33 = add i32 %31, 2
%34 = add i32 %33, %32
%35 = tail call i8* @malloc(i32 %34)
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %39
37: ; preds = %30
%38 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %50
39: ; preds = %30
%40 = tail call i32 @snprintf(i8* %35, i32 %34, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %0, i8* %1)
%41 = tail call i32* @fopen(i8* %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%42 = icmp eq i32* %41, null
br i1 %42, label %43, label %45
43: ; preds = %39
%44 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0))
br label %50
45: ; preds = %39
%46 = tail call i32 @strlen(i8* %2)
%47 = tail call i32 @fwrite(i8* %2, i32 1, i32 %46, i32* %41)
%48 = tail call i32 @fclose(i32* %41)
%49 = tail call i32 @free(i8* %35)
br label %50
50: ; preds = %45, %43, %37
ret void
}