#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*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int /*<<< orphan*/ stderr;
char *writeconf(char *fname, char *name, char *key, int n) {
  FILE *f;
  if (n)
    f = fopen(fname, "w");
  else
    f = fopen(fname, "a");
  if (f) {
    fprintf(f, "%s %s\n", name, key);
    fclose(f);
  } else {
    fprintf(stderr, "no such config file: '%s'\n", fname);
    return NULL;
  }
  return key;
}


x86 assembly (clang, -O0):
<writeconf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     116a <writeconf+0x3a>
mov    -0x10(%rbp),%rdi
lea    0xea4(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x30(%rbp)
jmp    117e <writeconf+0x4e>
mov    -0x10(%rbp),%rdi
lea    0xe8d(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11b3 <writeconf+0x83>
mov    -0x30(%rbp),%rax
mov    %eax,%edi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
lea    0xe66(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
jmp    11db <writeconf+0xab>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe44(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11e3 <writeconf+0xb3>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeconf>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
test   %ecx,%ecx
lea    0xeb8(%rip),%rax
lea    0xeaf(%rip),%rsi
cmove  %rax,%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1182 <writeconf+0x52>
mov    %rax,%rbx
lea    0xe9b(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %r15,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
jmp    119f <writeconf+0x6f>
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
lea    0xe79(%rip),%rsi
xor    %r15d,%r15d
mov    %r12,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [2 x i8] c"a\00"
@.str.2 = [7 x i8] c"%s %s\0A\00"
@stderr = global i32 0
@.str.3 = [27 x i8] c"no such config file: '%s'\0A\00"
define i8* @writeconf(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%11 = load i32, i32* %9
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %4
%14 = load i8*, i8** %6
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %10
br label %19
16: ; preds = %4
%17 = load i8*, i8** %6
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %18, i32** %10
br label %19
19: ; preds = %16, %13
%20 = load i32*, i32** %10
%21 = icmp ne i32* %20, null
br i1 %21, label %22, label %30
22: ; preds = %19
%23 = load i32*, i32** %10
%24 = ptrtoint i32* %23 to i32
%25 = load i8*, i8** %7
%26 = load i8*, i8** %8
%27 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %25, i8* %26)
%28 = load i32*, i32** %10
%29 = call i32 @fclose(i32* %28)
br label %34
30: ; preds = %19
%31 = load i32, i32* @stderr
%32 = load i8*, i8** %6
%33 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %32)
store i8* null, i8** %5
br label %36
34: ; preds = %22
%35 = load i8*, i8** %8
store i8* %35, i8** %5
br label %36
36: ; preds = %34, %30
%37 = load i8*, i8** %5
ret i8* %37
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [2 x i8] c"a\00"
@.str.2 = [7 x i8] c"%s %s\0A\00"
@stderr = global i32 0
@.str.3 = [27 x i8] c"no such config file: '%s'\0A\00"
define i8* @writeconf(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = icmp eq i32 %3, 0
%6 = select i1 %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0)
%7 = tail call i32* @fopen(i8* %0, i8* %6)
%8 = icmp eq i32* %7, null
br i1 %8, label %14, label %9
9: ; preds = %4
%10 = ptrtoint i32* %7 to i64
%11 = trunc i64 %10 to i32
%12 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %1, i8* %2)
%13 = tail call i32 @fclose(i32* %7)
br label %17
14: ; preds = %4
%15 = load i32, i32* @stderr
%16 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %17
17: ; preds = %9, %14
%18 = phi i8* [ %2, %9 ], [ null, %14 ]
ret i8* %18
}