#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*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
void write_integer_to_file(const char *filename, int value) {
  FILE *fp = fopen(filename, "w");
  if (!fp) {
    fprintf(stderr, "Failed to open file '%s'", filename);
    exit(EXIT_FAILURE);
  }
  int ret = fprintf(fp, "%d\n", value);
  if (ret <= 0) {
    fprintf(stderr, "Failed to write to file '%s'\n", filename);
    exit(EXIT_FAILURE);
  }
  fclose(fp);
}


x86 assembly (clang, -O0):
<write_integer_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xea6(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1197 <write_integer_to_file+0x57>
mov    0x2e6b(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe80(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rax
mov    %eax,%edi
mov    -0xc(%rbp),%edx
lea    0xe74(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jg     11e4 <write_integer_to_file+0xa4>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe50(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_integer_to_file>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
lea    0xeb0(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     117f <write_integer_to_file+0x3f>
mov    %rax,%rbx
lea    0xeb7(%rip),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
xor    %eax,%eax
call   1040 <fprintf@plt>
test   %eax,%eax
jle    1191 <write_integer_to_file+0x51>
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <fclose@plt>
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
lea    0xe73(%rip),%rsi
jmp    11a1 <write_integer_to_file+0x61>
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
lea    0xe7e(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x2e26(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

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

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"Failed to open file '%s'\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [4 x i8] c"%d\0A\00"
@.str.3 = [30 x i8] c"Failed to write to file '%s'\0A\00"
define void @write_integer_to_file(i8* %0, i32 %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %10
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%8 = load i32, i32* @EXIT_FAILURE
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %2
%11 = ptrtoint i32* %3 to i64
%12 = trunc i64 %11 to i32
%13 = tail call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %1)
%14 = icmp slt i32 %13, 1
br i1 %14, label %15, label %20
15: ; preds = %10
%16 = load i32, i32* @stderr
%17 = tail call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %0)
%18 = load i32, i32* @EXIT_FAILURE
%19 = tail call i32 @exit(i32 %18)
unreachable
20: ; preds = %10
%21 = tail call i32 @fclose(i32* %3)
ret void
}