#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 mx_ftruncate(int, int);
int /*<<< orphan*/ printf(char *, int, int);
int u_ftruncate(int fd, int length) {
  printf("DEBUG: u_ftruncate (%d,%d) entered\n", fd, length);
  if (fd < 0)
    return -1;
  int fs = fd >> 8;
  int localfd = fd & 0xff;
  switch (fs) {
  case 1:
    return mx_ftruncate(localfd, length);
  case 2:
    return -1;
  case 3:
    return -1;
  case 0:
    return -1;
  default:
    return -1;
  }
}


x86 assembly (clang, -O0):
<u_ftruncate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
lea    0xed5(%rip),%rdi
call   1030 <printf@plt>
cmpl   $0x0,-0x8(%rbp)
jge    1156 <u_ftruncate+0x36>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c9 <u_ftruncate+0xa9>
mov    -0x8(%rbp),%eax
sar    $0x8,%eax
mov    %eax,-0x10(%rbp)
movzbl -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%eax
mov    %rax,-0x20(%rbp)
sub    $0x3,%rax
ja     11c2 <u_ftruncate+0xa2>
mov    -0x20(%rbp),%rax
lea    0xe7e(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x14(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1040 <mx_ftruncate@plt>
mov    %eax,-0x4(%rbp)
jmp    11c9 <u_ftruncate+0xa9>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c9 <u_ftruncate+0xa9>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c9 <u_ftruncate+0xa9>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c9 <u_ftruncate+0xa9>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<u_ftruncate>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%ebp
lea    0xed2(%rip),%rdi
mov    %ebp,%esi
mov    %ebx,%edx
call   1030 <printf@plt>
mov    %ebp,%eax
and    $0xffffff00,%eax
cmp    $0x100,%eax
jne    1156 <u_ftruncate+0x36>
movzbl %bpl,%edi
mov    %ebx,%esi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <mx_ftruncate@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [36 x i8] c"DEBUG: u_ftruncate (%d,%d) entered\0A\00"
define i32 @u_ftruncate(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%8 = load i32, i32* %4
%9 = load i32, i32* %5
%10 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %8, i32 %9)
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 -1, i32* %3
br label %28
14: ; preds = %2
%15 = load i32, i32* %4
%16 = ashr i32 %15, 8
store i32 %16, i32* %6
%17 = load i32, i32* %4
%18 = and i32 %17, 255
store i32 %18, i32* %7
%19 = load i32, i32* %6
switch i32 %19, label %27 [
i32 1, label %20
i32 2, label %24
i32 3, label %25
i32 0, label %26
]
20: ; preds = %14
%21 = load i32, i32* %7
%22 = load i32, i32* %5
%23 = call i32 @mx_ftruncate(i32 %21, i32 %22)
store i32 %23, i32* %3
br label %28
24: ; preds = %14
store i32 -1, i32* %3
br label %28
25: ; preds = %14
store i32 -1, i32* %3
br label %28
26: ; preds = %14
store i32 -1, i32* %3
br label %28
27: ; preds = %14
store i32 -1, i32* %3
br label %28
28: ; preds = %27, %26, %25, %24, %20, %13
%29 = load i32, i32* %3
ret i32 %29
}

LLVM IR (-O2):
@.str = [36 x i8] c"DEBUG: u_ftruncate (%d,%d) entered\0A\00"
define i32 @u_ftruncate(i32 %0, i32 %1) {
%3 = tail call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %1)
%4 = and i32 %0, -256
%5 = icmp eq i32 %4, 256
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = and i32 %0, 255
%8 = tail call i32 @mx_ftruncate(i32 %7, i32 %1)
br label %9
9: ; preds = %6, %2
%10 = phi i32 [ -1, %2 ], [ %8, %6 ]
ret i32 %10
}