#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*/ O_RDONLY;
int /*<<< orphan*/ SWAPL(int *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, ...);
int open(char const *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int read(int, int *, int);
int /*<<< orphan*/ sprintf(char *, char *, char const *);
int /*<<< orphan*/ stderr;
int peek_length(char const *file) {
  int fd, len;
  int buff;
  if ((fd = open(file, O_RDONLY, 0644)) < 0) {
    char temp[200];
    sprintf(temp, "peek_length: '%s'", file);
    perror(temp);
    return -1;
  }
  if (read(fd, &buff, 4) != 4) {
    fprintf(stderr, "peek_length: %s: can't read length\n", file);
    close(fd);
    return -1;
  }
  SWAPL(&buff);
  close(fd);
  len = buff;
  if (len < 0)
    fprintf(stderr, "Warning: peek_length: %s: length(%d) < 0\n", file, len);
  return len;
}


x86 assembly (clang, -O0):
<peek_length>:
push   %rbp
mov    %rsp,%rbp
sub    $0xf0,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
mov    $0x1a4,%edx
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    11d1 <peek_length+0x61>
lea    -0xf0(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe4c(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0xf0(%rbp),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125f <peek_length+0xef>
mov    -0x14(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
call   1040 <read@plt>
cmp    $0x4,%eax
je     121a <peek_length+0xaa>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe13(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125f <peek_length+0xef>
lea    -0x1c(%rbp),%rdi
call   1090 <SWAPL@plt>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1259 <peek_length+0xe9>
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%ecx
lea    0xde4(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xf0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<peek_length>:
push   %rbp
push   %rbx
sub    $0xc8,%rsp
mov    %rdi,%rbx
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
mov    $0x1a4,%edx
call   1060 <open@plt>
test   %eax,%eax
js     120f <peek_length+0x9f>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1040 <read@plt>
cmp    $0x4,%eax
jne    11dd <peek_length+0x6d>
mov    %rsp,%rdi
call   1090 <SWAPL@plt>
mov    %ebp,%edi
call   1030 <close@plt>
mov    (%rsp),%ebp
test   %ebp,%ebp
jns    1203 <peek_length+0x93>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
lea    0xe67(%rip),%rsi
mov    %rbx,%rdx
mov    %ebp,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1203 <peek_length+0x93>
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
lea    0xe25(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %ebp,%edi
call   1030 <close@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0xc8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdea(%rip),%rsi
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %rbx,%rdx
call   1080 <sprintf@plt>
mov    %rbp,%rdi
call   1070 <perror@plt>
jmp    11fe <peek_length+0x8e>

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [18 x i8] c"peek_length: '%s'\00"
@stderr = global i32 0
@.str.1 = [36 x i8] c"peek_length: %s: can't read length\0A\00"
@.str.2 = [42 x i8] c"Warning: peek_length: %s: length(%d) < 0\0A\00"
define i32 @peek_length(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [200 x i8]
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* %8, i32 %9, i32 420)
store i32 %10, i32* %4
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %1
%13 = getelementptr inbounds [200 x i8], [200 x i8]* %7, i64 0, i64 0
%14 = load i8*, i8** %3
%15 = call i32 @sprintf(i8* %13, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = getelementptr inbounds [200 x i8], [200 x i8]* %7, i64 0, i64 0
%17 = call i32 @perror(i8* %16)
store i32 -1, i32* %2
br label %42
18: ; preds = %1
%19 = load i32, i32* %4
%20 = call i32 @read(i32 %19, i32* %6, i32 4)
%21 = icmp ne i32 %20, 4
br i1 %21, label %22, label %28
22: ; preds = %18
%23 = load i32, i32* @stderr
%24 = load i8*, i8** %3
%25 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %24)
%26 = load i32, i32* %4
%27 = call i32 @close(i32 %26)
store i32 -1, i32* %2
br label %42
28: ; preds = %18
%29 = call i32 @SWAPL(i32* %6)
%30 = load i32, i32* %4
%31 = call i32 @close(i32 %30)
%32 = load i32, i32* %6
store i32 %32, i32* %5
%33 = load i32, i32* %5
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %40
35: ; preds = %28
%36 = load i32, i32* @stderr
%37 = load i8*, i8** %3
%38 = load i32, i32* %5
%39 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i8* %37, i32 %38)
br label %40
40: ; preds = %35, %28
%41 = load i32, i32* %5
store i32 %41, i32* %2
br label %42
42: ; preds = %40, %22, %12
%43 = load i32, i32* %2
ret i32 %43
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [18 x i8] c"peek_length: '%s'\00"
@stderr = global i32 0
@.str.1 = [36 x i8] c"peek_length: %s: can't read length\0A\00"
@.str.2 = [42 x i8] c"Warning: peek_length: %s: length(%d) < 0\0A\00"
define i32 @peek_length(i8* %0) {
%2 = alloca i32
%3 = alloca [200 x i8]
%4 = bitcast i32* %2 to i8*
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %0, i32 %5, i32 420)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %1
%9 = getelementptr inbounds [200 x i8], [200 x i8]* %3, i64 0, i64 0
%10 = call i32 @sprintf(i8* %9, i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %0)
%11 = call i32 @perror(i8* %9)
br label %27
12: ; preds = %1
%13 = call i32 @read(i32 %6, i32* %2, i32 4)
%14 = icmp eq i32 %13, 4
br i1 %14, label %19, label %15
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%18 = tail call i32 @close(i32 %6)
br label %27
19: ; preds = %12
%20 = call i32 @SWAPL(i32* %2)
%21 = call i32 @close(i32 %6)
%22 = load i32, i32* %2
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = load i32, i32* @stderr
%26 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %22)
br label %27
27: ; preds = %19, %24, %15, %8
%28 = phi i32 [ -1, %8 ], [ -1, %15 ], [ %22, %24 ], [ %22, %19 ]
ret i32 %28
}