#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 */

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
scalar_t__ close(int);
int dprintf(int, char *, int);
int errno;
int open(char const *, int /*<<< orphan*/);
int write_int_to_file(const char *filename, int value) {
  int ret;
  int fd = open(filename, O_WRONLY);
  if (fd < 0) {
    ret = -errno;
    goto out;
  }
  ret = dprintf(fd, "%d", value);
  if (ret < 0)
    goto out_close;
out_close:
  if (close(fd) < 0)
    ret = -errno;
out:
  return ret;
}


x86 assembly (clang, -O0):
<write_int_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e86(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1171 <write_int_to_file+0x41>
xor    %eax,%eax
mov    0x2e79(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x10(%rbp)
jmp    11bf <write_int_to_file+0x8f>
mov    -0x14(%rbp),%edi
mov    -0xc(%rbp),%edx
lea    0xe82(%rip),%rsi
call   1040 <dprintf@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    1195 <write_int_to_file+0x65>
jmp    119a <write_int_to_file+0x6a>
jmp    119a <write_int_to_file+0x6a>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
cmp    $0x0,%rax
jge    11ba <write_int_to_file+0x8a>
xor    %eax,%eax
mov    0x2e2b(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x10(%rbp)
jmp    11bf <write_int_to_file+0x8f>
mov    -0x10(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_int_to_file>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebx
mov    0x2e94(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
test   %eax,%eax
js     1167 <write_int_to_file+0x37>
mov    %eax,%ebp
lea    0xeb0(%rip),%rsi
mov    %eax,%edi
mov    %ebx,%edx
call   1040 <dprintf@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1030 <close@plt>
test   %rax,%rax
jns    1172 <write_int_to_file+0x42>
mov    0x2e72(%rip),%rax
xor    %ebx,%ebx
sub    (%rax),%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@O_WRONLY = global i32 0
@errno = global i32 0
@.str = [3 x i8] c"%d\00"
define i32 @write_int_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 = load i32, i32* @O_WRONLY
%9 = call i32 @open(i8* %7, i32 %8)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* @errno
%14 = sub nsw i32 0, %13
store i32 %14, i32* %5
br label %31
15: ; preds = %2
%16 = load i32, i32* %6
%17 = load i32, i32* %4
%18 = call i32 @dprintf(i32 %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %17)
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %15
br label %23
22: ; preds = %15
br label %23
23: ; preds = %22, %21
%24 = load i32, i32* %6
%25 = call i64 @close(i32 %24)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = load i32, i32* @errno
%29 = sub nsw i32 0, %28
store i32 %29, i32* %5
br label %30
30: ; preds = %27, %23
br label %31
31: ; preds = %30, %12
%32 = load i32, i32* %5
ret i32 %32
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@errno = global i32 0
@.str = [3 x i8] c"%d\00"
define i32 @write_int_to_file(i8* nocapture %0, i32 %1) {
%3 = load i32, i32* @O_WRONLY
%4 = tail call i32 @open(i8* %0, i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i32, i32* @errno
%8 = sub nsw i32 0, %7
br label %16
9: ; preds = %2
%10 = tail call i32 @dprintf(i32 %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %1)
%11 = tail call i64 @close(i32 %4)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = load i32, i32* @errno
%15 = sub nsw i32 0, %14
br label %16
16: ; preds = %9, %13, %6
%17 = phi i32 [ %8, %6 ], [ %15, %13 ], [ %10, %9 ]
ret i32 %17
}