#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 */
struct stat {
  int st_size;
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
scalar_t__ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ memcmp(char *, char *, int);
int read(int, char *, int);
int is_valid_tzfile(const struct stat *st, int fd) {
  if (fd) {
    char buf[20];
    if (read(fd, buf, 20) != 20) {
      return 0;
    }
    lseek(fd, SEEK_SET, 0);
    if (memcmp(buf, "TZif", 4)) {
      return 0;
    }
  }
  return S_ISREG(st->st_mode) && st->st_size > 20;
}


x86 assembly (clang, -O0):
<is_valid_tzfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11c2 <is_valid_tzfile+0x82>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x14,%edx
call   1040 <read@plt>
cmp    $0x14,%eax
je     117f <is_valid_tzfile+0x3f>
movl   $0x0,-0x4(%rbp)
jmp    11f8 <is_valid_tzfile+0xb8>
mov    -0x14(%rbp),%edi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <lseek@plt>
lea    -0x30(%rbp),%rdi
lea    0xe63(%rip),%rsi
mov    $0x4,%edx
call   1050 <memcmp@plt>
cmp    $0x0,%rax
je     11bd <is_valid_tzfile+0x7d>
movl   $0x0,-0x4(%rbp)
jmp    11f8 <is_valid_tzfile+0xb8>
jmp    11c2 <is_valid_tzfile+0x82>
mov    -0x10(%rbp),%rax
mov    0x4(%rax),%edi
call   1060 <S_ISREG@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x31(%rbp)
je     11ed <is_valid_tzfile+0xad>
mov    -0x10(%rbp),%rax
cmpl   $0x14,(%rax)
setg   %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<is_valid_tzfile>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
test   %esi,%esi
je     118e <is_valid_tzfile+0x4e>
mov    %esi,%ebp
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x14,%edx
call   1040 <read@plt>
cmp    $0x14,%eax
jne    11a5 <is_valid_tzfile+0x65>
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1030 <lseek@plt>
lea    0xe84(%rip),%rsi
mov    %rsp,%rdi
mov    $0x4,%edx
call   1050 <memcmp@plt>
test   %rax,%rax
jne    11a5 <is_valid_tzfile+0x65>
mov    0x4(%rbx),%edi
call   1060 <S_ISREG@plt>
test   %rax,%rax
je     11a5 <is_valid_tzfile+0x65>
xor    %eax,%eax
cmpl   $0x15,(%rbx)
setge  %al
jmp    11a7 <is_valid_tzfile+0x67>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@SEEK_SET = global i32 0
@.str = [5 x i8] c"TZif\00"
define i32 @is_valid_tzfile(%struct.stat* %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.stat*
%5 = alloca i32
%6 = alloca [20 x i8]
store %struct.stat* %0, %struct.stat** %4
store i32 %1, i32* %5
%7 = load i32, i32* %5
%8 = icmp ne i32 %7, 0
br i1 %8, label %9, label %24
9: ; preds = %2
%10 = load i32, i32* %5
%11 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%12 = call i32 @read(i32 %10, i8* %11, i32 20)
%13 = icmp ne i32 %12, 20
br i1 %13, label %14, label %15
14: ; preds = %9
store i32 0, i32* %3
br label %38
15: ; preds = %9
%16 = load i32, i32* %5
%17 = load i32, i32* @SEEK_SET
%18 = call i32 @lseek(i32 %16, i32 %17, i32 0)
%19 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%20 = call i64 @memcmp(i8* %19, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %15
store i32 0, i32* %3
br label %38
23: ; preds = %15
br label %24
24: ; preds = %23, %2
%25 = load %struct.stat*, %struct.stat** %4
%26 = getelementptr inbounds %struct.stat, %struct.stat* %25, i32 0, i32 1
%27 = load i32, i32* %26
%28 = call i64 @S_ISREG(i32 %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %24
%31 = load %struct.stat*, %struct.stat** %4
%32 = getelementptr inbounds %struct.stat, %struct.stat* %31, i32 0, i32 0
%33 = load i32, i32* %32
%34 = icmp sgt i32 %33, 20
br label %35
35: ; preds = %30, %24
%36 = phi i1 [ false, %24 ], [ %34, %30 ]
%37 = zext i1 %36 to i32
store i32 %37, i32* %3
br label %38
38: ; preds = %35, %22, %14
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@SEEK_SET = global i32 0
@.str = [5 x i8] c"TZif\00"
define i32 @is_valid_tzfile(%struct.stat* nocapture %0, i32 %1) {
%3 = alloca [20 x i8]
%4 = icmp eq i32 %1, 0
br i1 %4, label %16, label %5
5: ; preds = %2
%6 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%7 = call i32 @read(i32 %1, i8* %6, i32 20)
%8 = icmp eq i32 %7, 20
br i1 %8, label %9, label %14
9: ; preds = %5
%10 = load i32, i32* @SEEK_SET
%11 = tail call i32 @lseek(i32 %1, i32 %10, i32 0)
%12 = call i64 @memcmp(i8* %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%13 = icmp eq i64 %12, 0
br i1 %13, label %15, label %14
14: ; preds = %5, %9
br label %26
15: ; preds = %9
br label %16
16: ; preds = %15, %2
%17 = getelementptr inbounds %struct.stat, %struct.stat* %0, i64 0, i32 1
%18 = load i32, i32* %17
%19 = call i64 @S_ISREG(i32 %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %26, label %21
21: ; preds = %16
%22 = getelementptr inbounds %struct.stat, %struct.stat* %0, i64 0, i32 0
%23 = load i32, i32* %22
%24 = icmp sgt i32 %23, 20
%25 = zext i1 %24 to i32
br label %26
26: ; preds = %14, %16, %21
%27 = phi i32 [ 0, %16 ], [ %25, %21 ], [ 0, %14 ]
ret i32 %27
}