#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;
int /*<<< orphan*/ close(int);
int errno;
int open(char *, int /*<<< orphan*/);
int snprintf(char *, int, char *, int);
int write(int, char *, int);
int write_proc(char *filename, int value) {
  char buf[100];
  int fd, rc, n;
  n = snprintf(buf, 100, "%d", value);
  fd = open(filename, O_WRONLY);
  if (fd < 0)
    return -1;
  rc = write(fd, buf, n);
  if (rc < n) {
    int saved_errno = errno;
    close(fd);
    errno = saved_errno;
    return -1;
  }
  close(fd);
  return 1;
}


x86 assembly (clang, -O0):
<write_proc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x80(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0x64,%esi
lea    0xe9b(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x8c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x84(%rbp)
cmpl   $0x0,-0x84(%rbp)
jge    11a1 <write_proc+0x61>
movl   $0xffffffff,-0x4(%rbp)
jmp    1215 <write_proc+0xd5>
mov    -0x84(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    -0x8c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x88(%rbp)
mov    -0x88(%rbp),%eax
cmp    -0x8c(%rbp),%eax
jge    1203 <write_proc+0xc3>
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x90(%rbp)
mov    -0x84(%rbp),%edi
call   1050 <close@plt>
mov    -0x90(%rbp),%ecx
mov    0x2deb(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1215 <write_proc+0xd5>
mov    -0x84(%rbp),%edi
call   1050 <close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_proc>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x68,%rsp
mov    %esi,%ecx
mov    %rdi,%rbp
lea    0xeaa(%rip),%rdx
mov    %rsp,%rdi
mov    $0x64,%esi
call   1040 <snprintf@plt>
mov    %eax,%ebx
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1060 <open@plt>
mov    $0xffffffff,%r14d
test   %eax,%eax
js     11b5 <write_proc+0x75>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
jge    11a8 <write_proc+0x68>
mov    0x2e47(%rip),%r15
mov    (%r15),%ebx
mov    %ebp,%edi
call   1050 <close@plt>
mov    %ebx,(%r15)
jmp    11b5 <write_proc+0x75>
mov    %ebp,%edi
call   1050 <close@plt>
mov    $0x1,%r14d
mov    %r14d,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@O_WRONLY = global i32 0
@errno = global i32 0
define i32 @write_proc(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [100 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%11 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%12 = load i32, i32* %5
%13 = call i32 @snprintf(i8* %11, i32 100, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 %13, i32* %9
%14 = load i8*, i8** %4
%15 = load i32, i32* @O_WRONLY
%16 = call i32 @open(i8* %14, i32 %15)
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %2
store i32 -1, i32* %3
br label %36
20: ; preds = %2
%21 = load i32, i32* %7
%22 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%23 = load i32, i32* %9
%24 = call i32 @write(i32 %21, i8* %22, i32 %23)
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = load i32, i32* %9
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %33
28: ; preds = %20
%29 = load i32, i32* @errno
store i32 %29, i32* %10
%30 = load i32, i32* %7
%31 = call i32 @close(i32 %30)
%32 = load i32, i32* %10
store i32 %32, i32* @errno
store i32 -1, i32* %3
br label %36
33: ; preds = %20
%34 = load i32, i32* %7
%35 = call i32 @close(i32 %34)
store i32 1, i32* %3
br label %36
36: ; preds = %33, %28, %19
%37 = load i32, i32* %3
ret i32 %37
}

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