#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*/ 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);
unsigned long long int udev_get_kernel_seqnum(struct udev *udev) {
  unsigned long long int seqnum;
  int fd;
  char buf[32];
  ssize_t len;
  fd = open("/sys/kernel/uevent_seqnum", 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    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e95(%rip),%rax
mov    (%rax),%esi
mov    0x2e74(%rip),%rax
or     (%rax),%esi
lea    0xe9b(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1184 <udev_get_kernel_seqnum+0x44>
movq   $0x0,-0x8(%rbp)
jmp    11e2 <udev_get_kernel_seqnum+0xa2>
mov    -0x1c(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x20,%edx
call   1040 <read@plt>
mov    %eax,-0x44(%rbp)
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
cmpl   $0x2,-0x44(%rbp)
jg     11b7 <udev_get_kernel_seqnum+0x77>
movq   $0x0,-0x8(%rbp)
jmp    11e2 <udev_get_kernel_seqnum+0xa2>
mov    -0x44(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x40(%rbp,%rax,1)
lea    -0x40(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   1050 <strtoull@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udev_get_kernel_seqnum>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    0x2e9a(%rip),%rax
mov    0x2e7b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xea0(%rip),%rdi
call   1060 <open@plt>
test   %eax,%eax
js     118a <udev_get_kernel_seqnum+0x4a>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x20,%edx
call   1040 <read@plt>
mov    %eax,%r14d
mov    %ebx,%edi
call   1030 <close@plt>
cmp    $0x3,%r14d
jge    118e <udev_get_kernel_seqnum+0x4e>
xor    %eax,%eax
jmp    11a6 <udev_get_kernel_seqnum+0x66>
add    $0xffffffff,%r14d
movb   $0x0,(%rsp,%r14,1)
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   1050 <strtoull@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.udev = type { i32 }
@.str = [26 x i8] c"/sys/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 i64
%5 = alloca i32
%6 = alloca [32 x i8]
%7 = alloca i32
store %struct.udev* %0, %struct.udev** %3
%8 = load i32, i32* @O_RDONLY
%9 = load i32, i32* @O_CLOEXEC
%10 = or i32 %8, %9
%11 = call i32 @open(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %5
%12 = load i32, i32* %5
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %1
store i64 0, i64* %2
br label %32
15: ; preds = %1
%16 = load i32, i32* %5
%17 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%18 = call i32 @read(i32 %16, i8* %17, i32 32)
store i32 %18, i32* %7
%19 = load i32, i32* %5
%20 = call i32 @close(i32 %19)
%21 = load i32, i32* %7
%22 = icmp sle i32 %21, 2
br i1 %22, label %23, label %24
23: ; preds = %15
store i64 0, i64* %2
br label %32
24: ; preds = %15
%25 = load i32, i32* %7
%26 = sub nsw i32 %25, 1
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 %27
store i8 0, i8* %28
%29 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%30 = call i64 @strtoull(i8* %29, i32* null, i32 10)
store i64 %30, i64* %4
%31 = load i64, i64* %4
store i64 %31, i64* %2
br label %32
32: ; preds = %24, %23, %14
%33 = load i64, i64* %2
ret i64 %33
}

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