#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 udev {
  int dummy;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ filename;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int O_CLOEXEC;
int O_RDONLY;
int /*<<< orphan*/ close(int);
int open(char *, int);
int read(int, char *, int);
unsigned long long strtoull(char *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ udev_get_sys_path(struct udev *);
int /*<<< orphan*/ util_strscpyl(char *, int, int /*<<< orphan*/, char *,
                                 int /*<<< orphan*/ *);
unsigned long long int udev_get_kernel_seqnum(struct udev *udev) {
  char filename[1024];
  unsigned long long int seqnum;
  int fd;
  char buf[32];
  ssize_t len;
  util_strscpyl(filename, sizeof(filename), udev_get_sys_path(udev),
                "/kernel/uevent_seqnum", NULL);
  fd = open(filename, O_RDONLY | O_CLOEXEC);
  if (fd < 0)
    return 0;
  len = read(fd, buf, sizeof(buf));
  close(fd);
  if (len <= 2)
    return 0;
  buf[len - 1] = '\0';
  seqnum = strtoull(buf, NULL, 10);
  return seqnum;
}


x86 assembly (clang, -O0):
<udev_get_kernel_seqnum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x450(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <udev_get_sys_path@plt>
mov    -0x450(%rbp),%rdi
mov    %eax,%edx
mov    $0x400,%esi
lea    0xe65(%rip),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   1070 <util_strscpyl@plt>
lea    -0x410(%rbp),%rdi
mov    0x2e35(%rip),%rax
mov    (%rax),%esi
mov    0x2e14(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x41c(%rbp)
cmpl   $0x0,-0x41c(%rbp)
jge    11e3 <udev_get_kernel_seqnum+0x83>
movq   $0x0,-0x8(%rbp)
jmp    125f <udev_get_kernel_seqnum+0xff>
mov    -0x41c(%rbp),%edi
lea    -0x440(%rbp),%rsi
mov    $0x20,%edx
call   1050 <read@plt>
mov    %eax,-0x444(%rbp)
mov    -0x41c(%rbp),%edi
call   1040 <close@plt>
cmpl   $0x2,-0x444(%rbp)
jg     1225 <udev_get_kernel_seqnum+0xc5>
movq   $0x0,-0x8(%rbp)
jmp    125f <udev_get_kernel_seqnum+0xff>
mov    -0x444(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x440(%rbp,%rax,1)
lea    -0x440(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1060 <strtoull@plt>
mov    %rax,-0x418(%rbp)
mov    -0x418(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udev_get_kernel_seqnum>:
push   %rbp
push   %rbx
sub    $0x428,%rsp
call   1030 <udev_get_sys_path@plt>
lea    0xe8b(%rip),%rcx
xor    %ebx,%ebx
lea    0x20(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x400,%esi
mov    %eax,%edx
xor    %r8d,%r8d
call   1070 <util_strscpyl@plt>
mov    0x2e53(%rip),%rax
mov    0x2e34(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbp,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11e8 <udev_get_kernel_seqnum+0x88>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x20,%edx
call   1050 <read@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1040 <close@plt>
cmp    $0x3,%ebx
jge    11cf <udev_get_kernel_seqnum+0x6f>
xor    %ebx,%ebx
jmp    11e8 <udev_get_kernel_seqnum+0x88>
add    $0xffffffff,%ebx
movb   $0x0,(%rsp,%rbx,1)
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1060 <strtoull@plt>
mov    %rax,%rbx
mov    %rbx,%rax
add    $0x428,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.udev = type { i32 }
@.str = [22 x i8] c"/kernel/uevent_seqnum\00"
@O_RDONLY = global i32 0
@O_CLOEXEC = global i32 0
define i64 @udev_get_kernel_seqnum(%struct.udev* %0) {
%2 = alloca i64
%3 = alloca %struct.udev*
%4 = alloca [1024 x i8]
%5 = alloca i64
%6 = alloca i32
%7 = alloca [32 x i8]
%8 = alloca i32
store %struct.udev* %0, %struct.udev** %3
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%10 = load %struct.udev*, %struct.udev** %3
%11 = call i32 @udev_get_sys_path(%struct.udev* %10)
%12 = call i32 @util_strscpyl(i8* %9, i32 1024, i32 %11, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32* null)
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%14 = load i32, i32* @O_RDONLY
%15 = load i32, i32* @O_CLOEXEC
%16 = or i32 %14, %15
%17 = call i32 @open(i8* %13, i32 %16)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %1
store i64 0, i64* %2
br label %38
21: ; preds = %1
%22 = load i32, i32* %6
%23 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%24 = call i32 @read(i32 %22, i8* %23, i32 32)
store i32 %24, i32* %8
%25 = load i32, i32* %6
%26 = call i32 @close(i32 %25)
%27 = load i32, i32* %8
%28 = icmp sle i32 %27, 2
br i1 %28, label %29, label %30
29: ; preds = %21
store i64 0, i64* %2
br label %38
30: ; preds = %21
%31 = load i32, i32* %8
%32 = sub nsw i32 %31, 1
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 %33
store i8 0, i8* %34
%35 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%36 = call i64 @strtoull(i8* %35, i32* null, i32 10)
store i64 %36, i64* %5
%37 = load i64, i64* %5
store i64 %37, i64* %2
br label %38
38: ; preds = %30, %29, %20
%39 = load i64, i64* %2
ret i64 %39
}

LLVM IR (-O2):
%struct.udev = type { i32 }
@.str = [22 x i8] c"/kernel/uevent_seqnum\00"
@O_RDONLY = global i32 0
@O_CLOEXEC = global i32 0
define i64 @udev_get_kernel_seqnum(%struct.udev* %0) {
%2 = alloca [1024 x i8]
%3 = alloca [32 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%6 = tail call i32 @udev_get_sys_path(%struct.udev* %0)
%7 = call i32 @util_strscpyl(i8* %4, i32 1024, i32 %6, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32* null)
%8 = load i32, i32* @O_RDONLY
%9 = load i32, i32* @O_CLOEXEC
%10 = or i32 %9, %8
%11 = call i32 @open(i8* %4, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %22, label %13
13: ; preds = %1
%14 = call i32 @read(i32 %11, i8* %5, i32 32)
%15 = call i32 @close(i32 %11)
%16 = icmp slt i32 %14, 3
br i1 %16, label %22, label %17
17: ; preds = %13
%18 = add nsw i32 %14, -1
%19 = zext i32 %18 to i64
%20 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 %19
store i8 0, i8* %20
%21 = call i64 @strtoull(i8* nocapture %5, i32* null, i32 10)
br label %22
22: ; preds = %13, %1, %17
%23 = phi i64 [ %21, %17 ], [ 0, %1 ], [ 0, %13 ]
ret i64 %23
}