#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 /*<<< orphan*/);
int read(int, char *, int);
int sscanf(char *, char *, int *);
int msx_readval(char *fn, int *val) {
  int fd = open(fn, 0);
  int r;
  char num[30];
  if (fd == -1)
    return (0);
  num[sizeof(num) - 1] = '\0';
  r = read(fd, num, sizeof(num) - 1);
  close(fd);
  return (r >= 0 && sscanf(num, "%d", val) == 1);
}


x86 assembly (clang, -O0):
<msx_readval>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1174 <msx_readval+0x34>
movl   $0x0,-0x4(%rbp)
jmp    11cb <msx_readval+0x8b>
movb   $0x0,-0x23(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x1d,%edx
call   1040 <read@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
xor    %eax,%eax
cmpl   $0x0,-0x20(%rbp)
mov    %al,-0x41(%rbp)
jl     11c0 <msx_readval+0x80>
lea    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe4e(%rip),%rsi
call   1050 <sscanf@plt>
cmp    $0x1,%eax
sete   %al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%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_readval>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r14
xor    %ebx,%ebx
xor    %esi,%esi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     119a <msx_readval+0x5a>
mov    %eax,%ebp
movb   $0x0,0x1d(%rsp)
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x1d,%edx
call   1040 <read@plt>
mov    %eax,%r15d
mov    %ebp,%edi
call   1030 <close@plt>
test   %r15d,%r15d
js     119a <msx_readval+0x5a>
lea    0xe79(%rip),%rsi
mov    %rsp,%rdi
mov    %r14,%rdx
call   1050 <sscanf@plt>
xor    %ebx,%ebx
cmp    $0x1,%eax
sete   %bl
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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