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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int open(char *, int);
int read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int sscanf(char *, char *, int *);
int strlen(char *);
int write(int, char *, int);
int msx_replace(char *fn, int val) {
  int fd = open(fn, 2);
  int l, r;
  char num[30];
  int ret;
  if (fd == -1)
    return (0);
  num[sizeof(num) - 1] = '\0';
  r = read(fd, num, sizeof(num) - 1);
  if (r < 0 || sscanf(num, "%d", &ret) != 1)
    return (-1);
  sprintf(num, "%d\n", val);
  r = write(fd, num, l = strlen(num));
  close(fd);
  return (r == l ? ret : -1);
}


x86 assembly (clang, -O0):
<msx_replace>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2,%esi
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11a6 <msx_replace+0x36>
movl   $0x0,-0x4(%rbp)
jmp    1261 <msx_replace+0xf1>
movb   $0x0,-0x23(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x1d,%edx
call   1060 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jl     11e5 <msx_replace+0x75>
lea    -0x40(%rbp),%rdi
lea    0xe2d(%rip),%rsi
lea    -0x44(%rbp),%rdx
call   1070 <sscanf@plt>
cmp    $0x1,%eax
je     11f1 <msx_replace+0x81>
movl   $0xffffffff,-0x4(%rbp)
jmp    1261 <msx_replace+0xf1>
lea    -0x40(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xe04(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x54(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
lea    -0x40(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%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
jne    124e <msx_replace+0xde>
mov    -0x44(%rbp),%eax
mov    %eax,-0x58(%rbp)
jmp    125b <msx_replace+0xeb>
mov    $0xffffffff,%eax
mov    %eax,-0x58(%rbp)
jmp    125b <msx_replace+0xeb>
mov    -0x58(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<msx_replace>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    %esi,%r14d
mov    $0x2,%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1214 <msx_replace+0xa4>
mov    %eax,%ebx
movb   $0x0,0x2d(%rsp)
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0x1d,%edx
call   1060 <read@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
js     1216 <msx_replace+0xa6>
lea    0xe48(%rip),%rsi
lea    0x10(%rsp),%rdi
lea    0xc(%rsp),%rdx
call   1070 <sscanf@plt>
cmp    $0x1,%eax
jne    1216 <msx_replace+0xa6>
lea    0xe30(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    %r14d,%edx
call   1090 <sprintf@plt>
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %eax,%r14d
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,%r15d
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebp
cmp    %r14d,%r15d
jne    1216 <msx_replace+0xa6>
mov    0xc(%rsp),%ebp
jmp    1216 <msx_replace+0xa6>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@.str.1 = [4 x i8] c"%d\0A\00"
define i32 @msx_replace(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]
%10 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%11 = load i8*, i8** %4
%12 = call i32 @open(i8* %11, i32 2)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 0, i32* %3
br label %47
16: ; preds = %2
%17 = getelementptr inbounds [30 x i8], [30 x i8]* %9, i64 0, i64 29
store i8 0, i8* %17
%18 = load i32, i32* %6
%19 = getelementptr inbounds [30 x i8], [30 x i8]* %9, i64 0, i64 0
%20 = call i32 @read(i32 %18, i8* %19, i32 29)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp slt i32 %21, 0
br i1 %22, label %27, label %23
23: ; preds = %16
%24 = getelementptr inbounds [30 x i8], [30 x i8]* %9, i64 0, i64 0
%25 = call i32 @sscanf(i8* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %10)
%26 = icmp ne i32 %25, 1
br i1 %26, label %27, label %28
27: ; preds = %23, %16
store i32 -1, i32* %3
br label %47
28: ; preds = %23
%29 = getelementptr inbounds [30 x i8], [30 x i8]* %9, i64 0, i64 0
%30 = load i32, i32* %5
%31 = call i32 @sprintf(i8* %29, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %30)
%32 = load i32, i32* %6
%33 = getelementptr inbounds [30 x i8], [30 x i8]* %9, i64 0, i64 0
%34 = getelementptr inbounds [30 x i8], [30 x i8]* %9, i64 0, i64 0
%35 = call i32 @strlen(i8* %34)
store i32 %35, i32* %7
%36 = call i32 @write(i32 %32, i8* %33, i32 %35)
store i32 %36, i32* %8
%37 = load i32, i32* %6
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* %8
%40 = load i32, i32* %7
%41 = icmp eq i32 %39, %40
br i1 %41, label %42, label %44
42: ; preds = %28
%43 = load i32, i32* %10
br label %45
44: ; preds = %28
br label %45
45: ; preds = %44, %42
%46 = phi i32 [ %43, %42 ], [ -1, %44 ]
store i32 %46, i32* %3
br label %47
47: ; preds = %45, %27, %15
%48 = load i32, i32* %3
ret i32 %48
}

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