#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 saferead(int, char *, int);
int safewrite(int, char *, int);
int init_sf_source(int fd) {
  char check[2], us[2];
  int version;
  us[0] = 'U';
  us[1] = ' ';
  if (safewrite(fd, us, 2) != 2 || saferead(fd, check, 2) != 2 ||
      check[0] != 'U')
    return -1;
  version = check[1];
  if (us[1] < version)
    version = us[1];
  switch (version) {
  case ' ':
    break;
  default:
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<init_sf_source>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
movb   $0x55,-0xc(%rbp)
movb   $0x20,-0xb(%rbp)
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
mov    $0x2,%edx
call   1030 <safewrite@plt>
cmp    $0x2,%eax
jne    1174 <init_sf_source+0x54>
mov    -0x8(%rbp),%edi
lea    -0xa(%rbp),%rsi
mov    $0x2,%edx
call   1040 <saferead@plt>
cmp    $0x2,%eax
jne    1174 <init_sf_source+0x54>
movsbl -0xa(%rbp),%eax
cmp    $0x55,%eax
je     1180 <init_sf_source+0x60>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c4 <init_sf_source+0xa4>
movsbl -0x9(%rbp),%eax
mov    %eax,-0x10(%rbp)
movsbl -0xb(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    119b <init_sf_source+0x7b>
movsbl -0xb(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
sub    $0x20,%eax
jne    11b1 <init_sf_source+0x91>
jmp    11ac <init_sf_source+0x8c>
jmp    11bd <init_sf_source+0x9d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c4 <init_sf_source+0xa4>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_sf_source>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
movw   $0x2055,0x4(%rsp)
lea    0x4(%rsp),%rsi
mov    $0x2,%edx
call   1030 <safewrite@plt>
mov    $0xffffffff,%ebx
cmp    $0x2,%eax
jne    117a <init_sf_source+0x5a>
lea    0x6(%rsp),%rsi
mov    %ebp,%edi
mov    $0x2,%edx
call   1040 <saferead@plt>
cmp    $0x2,%eax
jne    117a <init_sf_source+0x5a>
cmpb   $0x55,0x6(%rsp)
jne    117a <init_sf_source+0x5a>
movzbl 0x7(%rsp),%eax
movzbl 0x5(%rsp),%ecx
cmp    %al,%cl
cmovl  %ecx,%eax
xor    %ebx,%ebx
cmp    $0x20,%al
setne  %bl
neg    %ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @init_sf_source(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [2 x i8]
%5 = alloca [2 x i8]
%6 = alloca i32
store i32 %0, i32* %3
%7 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
store i8 85, i8* %7
%8 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 1
store i8 32, i8* %8
%9 = load i32, i32* %3
%10 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%11 = call i32 @safewrite(i32 %9, i8* %10, i32 2)
%12 = icmp ne i32 %11, 2
br i1 %12, label %23, label %13
13: ; preds = %1
%14 = load i32, i32* %3
%15 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
%16 = call i32 @saferead(i32 %14, i8* %15, i32 2)
%17 = icmp ne i32 %16, 2
br i1 %17, label %23, label %18
18: ; preds = %13
%19 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 0
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp ne i32 %21, 85
br i1 %22, label %23, label %24
23: ; preds = %18, %13, %1
store i32 -1, i32* %2
br label %42
24: ; preds = %18
%25 = getelementptr inbounds [2 x i8], [2 x i8]* %4, i64 0, i64 1
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
store i32 %27, i32* %6
%28 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 1
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = load i32, i32* %6
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %37
33: ; preds = %24
%34 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 1
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
store i32 %36, i32* %6
br label %37
37: ; preds = %33, %24
%38 = load i32, i32* %6
switch i32 %38, label %40 [
i32 32, label %39
]
39: ; preds = %37
br label %41
40: ; preds = %37
store i32 -1, i32* %2
br label %42
41: ; preds = %39
store i32 0, i32* %2
br label %42
42: ; preds = %41, %40, %23
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
define i32 @init_sf_source(i32 %0) {
%2 = alloca [2 x i8]
%3 = alloca [2 x i8]
%4 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 0
store i8 85, i8* %5
%6 = getelementptr inbounds [2 x i8], [2 x i8]* %3, i64 0, i64 1
store i8 32, i8* %6
%7 = call i32 @safewrite(i32 %0, i8* %5, i32 2)
%8 = icmp eq i32 %7, 2
br i1 %8, label %9, label %23
9: ; preds = %1
%10 = call i32 @saferead(i32 %0, i8* %4, i32 2)
%11 = icmp ne i32 %10, 2
%12 = load i8, i8* %4
%13 = icmp ne i8 %12, 85
%14 = select i1 %11, i1 true, i1 %13
br i1 %14, label %23, label %15
15: ; preds = %9
%16 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 1
%17 = load i8, i8* %16
%18 = load i8, i8* %6
%19 = icmp slt i8 %18, %17
%20 = select i1 %19, i8 %18, i8 %17
%21 = icmp ne i8 %20, 32
%22 = sext i1 %21 to i32
br label %23
23: ; preds = %15, %1, %9
%24 = phi i32 [ -1, %9 ], [ -1, %1 ], [ %22, %15 ]
ret i32 %24
}