#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*/ close(int);
int open(char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int strlen(char *);
int write(int, char *, int);
int msx_write(char *fn, int val) {
  int fd = open(fn, 1);
  int l, r;
  char num[30];
  if (fd == -1)
    return (0);
  sprintf(num, "%d\n", val);
  r = write(fd, num, l = strlen(num));
  close(fd);
  return (r == l);
}


x86 assembly (clang, -O0):
<msx_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x1,%esi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1186 <msx_write+0x36>
movl   $0x0,-0x4(%rbp)
jmp    11dd <msx_write+0x8d>
lea    -0x40(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xe6c(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4c(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
lea    -0x40(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
mov    %edx,-0x1c(%rbp)
call   1030 <write@plt>
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<msx_write>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %esi,%ebp
mov    $0x1,%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     11a9 <msx_write+0x59>
mov    %eax,%ebx
lea    0xe8e(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
mov    %ebp,%edx
call   1070 <sprintf@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,%r14d
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
cmp    %ebp,%r14d
sete   %al
jmp    11ab <msx_write+0x5b>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%d\0A\00"
define i32 @msx_write(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [30 x i8]
store i8* %0, i8** %4
store i32 %1, i32* %5
%10 = load i8*, i8** %4
%11 = call i32 @open(i8* %10, i32 1)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %15
14: ; preds = %2
store i32 0, i32* %3
br label %30
15: ; preds = %2
%16 = getelementptr inbounds [30 x i8], [30 x i8]* %9, i64 0, i64 0
%17 = load i32, i32* %5
%18 = call i32 @sprintf(i8* %16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = load i32, i32* %6
%20 = getelementptr inbounds [30 x i8], [30 x i8]* %9, i64 0, i64 0
%21 = getelementptr inbounds [30 x i8], [30 x i8]* %9, i64 0, i64 0
%22 = call i32 @strlen(i8* %21)
store i32 %22, i32* %7
%23 = call i32 @write(i32 %19, i8* %20, i32 %22)
store i32 %23, i32* %8
%24 = load i32, i32* %6
%25 = call i32 @close(i32 %24)
%26 = load i32, i32* %8
%27 = load i32, i32* %7
%28 = icmp eq i32 %26, %27
%29 = zext i1 %28 to i32
store i32 %29, i32* %3
br label %30
30: ; preds = %15, %14
%31 = load i32, i32* %3
ret i32 %31
}

LLVM IR (-O2):
@.str = [4 x i8] c"%d\0A\00"
define i32 @msx_write(i8* nocapture %0, i32 %1) {
%3 = alloca [30 x i8]
%4 = tail call i32 @open(i8* %0, i32 1)
%5 = getelementptr inbounds [30 x i8], [30 x i8]* %3, i64 0, i64 0
%6 = icmp eq i32 %4, -1
br i1 %6, label %14, label %7
7: ; preds = %2
%8 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %1)
%9 = call i32 @strlen(i8* %5)
%10 = call i32 @write(i32 %4, i8* %5, i32 %9)
%11 = call i32 @close(i32 %4)
%12 = icmp eq i32 %10, %9
%13 = zext i1 %12 to i32
br label %14
14: ; preds = %2, %7
%15 = phi i32 [ %13, %7 ], [ 0, %2 ]
ret i32 %15
}