#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*/ seqnum;
typedef int /*<<< orphan*/ filename;

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int, ...);
int read(int, char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ udev_get_dev_path(struct udev *);
int /*<<< orphan*/ udev_get_sys_path(struct udev *);
int /*<<< orphan*/ util_create_path(struct udev *, char *);
int /*<<< orphan*/ util_strlcat(char *, char *, int);
int /*<<< orphan*/ util_strlcpy(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ write(int, char *, int);
void export_initial_seqnum(struct udev *udev) {
  char filename[1024];
  int fd;
  char seqnum[32];
  ssize_t len = 0;
  util_strlcpy(filename, udev_get_sys_path(udev), sizeof(filename));
  util_strlcat(filename, "/kernel/uevent_seqnum", sizeof(filename));
  fd = open(filename, O_RDONLY);
  if (fd >= 0) {
    len = read(fd, seqnum, sizeof(seqnum) - 1);
    close(fd);
  }
  if (len <= 0) {
    strcpy(seqnum, "0\n");
    len = 3;
  }
  util_strlcpy(filename, udev_get_dev_path(udev), sizeof(filename));
  util_strlcat(filename, "/.udev/uevent_seqnum", sizeof(filename));
  util_create_path(udev, filename);
  fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT, 0644);
  if (fd >= 0) {
    write(fd, seqnum, len);
    close(fd);
  }
}


x86 assembly (clang, -O0):
<export_initial_seqnum>:
push   %rbp
mov    %rsp,%rbp
sub    $0x460,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x444(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x450(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <udev_get_sys_path@plt>
mov    -0x450(%rbp),%rdi
mov    %eax,%esi
mov    $0x400,%edx
call   10b0 <util_strlcpy@plt>
lea    -0x410(%rbp),%rdi
lea    0xe0f(%rip),%rsi
mov    $0x400,%edx
call   10c0 <util_strlcat@plt>
lea    -0x410(%rbp),%rdi
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   10a0 <open@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0x0,-0x414(%rbp)
jl     124d <export_initial_seqnum+0xad>
mov    -0x414(%rbp),%edi
lea    -0x440(%rbp),%rsi
mov    $0x1f,%edx
call   1070 <read@plt>
mov    %eax,-0x444(%rbp)
mov    -0x414(%rbp),%edi
call   1060 <close@plt>
cmpl   $0x0,-0x444(%rbp)
jg     1277 <export_initial_seqnum+0xd7>
lea    -0x440(%rbp),%rdi
lea    0xdae(%rip),%rsi
call   1030 <strcpy@plt>
movl   $0x3,-0x444(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x458(%rbp)
mov    -0x8(%rbp),%rdi
call   1090 <udev_get_dev_path@plt>
mov    -0x458(%rbp),%rdi
mov    %eax,%esi
mov    $0x400,%edx
call   10b0 <util_strlcpy@plt>
lea    -0x410(%rbp),%rdi
lea    0xd6a(%rip),%rsi
mov    $0x400,%edx
call   10c0 <util_strlcat@plt>
mov    -0x8(%rbp),%rdi
lea    -0x410(%rbp),%rsi
call   1080 <util_create_path@plt>
lea    -0x410(%rbp),%rdi
mov    0x2ce9(%rip),%rax
mov    (%rax),%esi
mov    0x2d10(%rip),%rax
or     (%rax),%esi
mov    0x2d0f(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
mov    $0x0,%al
call   10a0 <open@plt>
mov    %eax,-0x414(%rbp)
cmpl   $0x0,-0x414(%rbp)
jl     132d <export_initial_seqnum+0x18d>
mov    -0x414(%rbp),%edi
lea    -0x440(%rbp),%rsi
mov    -0x444(%rbp),%edx
call   1040 <write@plt>
mov    -0x414(%rbp),%edi
call   1060 <close@plt>
add    $0x460,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<export_initial_seqnum>:
push   %rbp
push   %r14
push   %rbx
sub    $0x420,%rsp
mov    %rdi,%rbx
call   1050 <udev_get_sys_path@plt>
lea    0x20(%rsp),%rbp
mov    %rbp,%rdi
mov    %eax,%esi
mov    $0x400,%edx
call   10b0 <util_strlcpy@plt>
lea    0xe32(%rip),%rsi
mov    %rbp,%rdi
mov    $0x400,%edx
call   10c0 <util_strlcat@plt>
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
xor    %eax,%eax
call   10a0 <open@plt>
test   %eax,%eax
js     1212 <export_initial_seqnum+0x72>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x1f,%edx
call   1070 <read@plt>
mov    %eax,%r14d
mov    %ebp,%edi
call   1060 <close@plt>
test   %r14d,%r14d
jg     1227 <export_initial_seqnum+0x87>
lea    0xdfd(%rip),%rsi
mov    %rsp,%rdi
call   1030 <strcpy@plt>
mov    $0x3,%r14d
mov    %rbx,%rdi
call   1090 <udev_get_dev_path@plt>
lea    0x20(%rsp),%rbp
mov    %rbp,%rdi
mov    %eax,%esi
mov    $0x400,%edx
call   10b0 <util_strlcpy@plt>
lea    0xdcf(%rip),%rsi
mov    %rbp,%rdi
mov    $0x400,%edx
call   10c0 <util_strlcat@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1080 <util_create_path@plt>
mov    0x2d57(%rip),%rax
mov    0x2d80(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d7d(%rip),%rax
or     (%rax),%esi
mov    %rbp,%rdi
mov    $0x1a4,%edx
xor    %eax,%eax
call   10a0 <open@plt>
test   %eax,%eax
js     12a6 <export_initial_seqnum+0x106>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    %r14d,%edx
call   1040 <write@plt>
mov    %ebx,%edi
call   1060 <close@plt>
add    $0x420,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.udev = type { i32 }
@.str = [22 x i8] c"/kernel/uevent_seqnum\00"
@O_RDONLY = global i32 0
@.str.1 = [3 x i8] c"0\0A\00"
@.str.2 = [21 x i8] c"/.udev/uevent_seqnum\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
define void @export_initial_seqnum(%struct.udev* %0) {
%2 = alloca %struct.udev*
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = alloca [32 x i8]
%6 = alloca i32
store %struct.udev* %0, %struct.udev** %2
store i32 0, i32* %6
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%8 = load %struct.udev*, %struct.udev** %2
%9 = call i32 @udev_get_sys_path(%struct.udev* %8)
%10 = call i32 @util_strlcpy(i8* %7, i32 %9, i32 1024)
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%12 = call i32 @util_strlcat(i8* %11, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 1024)
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%14 = load i32, i32* @O_RDONLY
%15 = call i32 (i8*, i32, ...) @open(i8* %13, i32 %14)
store i32 %15, i32* %4
%16 = load i32, i32* %4
%17 = icmp sge i32 %16, 0
br i1 %17, label %18, label %24
18: ; preds = %1
%19 = load i32, i32* %4
%20 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%21 = call i32 @read(i32 %19, i8* %20, i32 31)
store i32 %21, i32* %6
%22 = load i32, i32* %4
%23 = call i32 @close(i32 %22)
br label %24
24: ; preds = %18, %1
%25 = load i32, i32* %6
%26 = icmp sle i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%29 = call i32 @strcpy(i8* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32 3, i32* %6
br label %30
30: ; preds = %27, %24
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%32 = load %struct.udev*, %struct.udev** %2
%33 = call i32 @udev_get_dev_path(%struct.udev* %32)
%34 = call i32 @util_strlcpy(i8* %31, i32 %33, i32 1024)
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%36 = call i32 @util_strlcat(i8* %35, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 1024)
%37 = load %struct.udev*, %struct.udev** %2
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%39 = call i32 @util_create_path(%struct.udev* %37, i8* %38)
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%41 = load i32, i32* @O_WRONLY
%42 = load i32, i32* @O_TRUNC
%43 = or i32 %41, %42
%44 = load i32, i32* @O_CREAT
%45 = or i32 %43, %44
%46 = call i32 (i8*, i32, ...) @open(i8* %40, i32 %45, i32 420)
store i32 %46, i32* %4
%47 = load i32, i32* %4
%48 = icmp sge i32 %47, 0
br i1 %48, label %49, label %56
49: ; preds = %30
%50 = load i32, i32* %4
%51 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%52 = load i32, i32* %6
%53 = call i32 @write(i32 %50, i8* %51, i32 %52)
%54 = load i32, i32* %4
%55 = call i32 @close(i32 %54)
br label %56
56: ; preds = %49, %30
ret void
}

LLVM IR (-O2):
%struct.udev = type { i32 }
@.str = [22 x i8] c"/kernel/uevent_seqnum\00"
@O_RDONLY = global i32 0
@.str.1 = [3 x i8] c"0\0A\00"
@.str.2 = [21 x i8] c"/.udev/uevent_seqnum\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
define void @export_initial_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_strlcpy(i8* %4, i32 %6, i32 1024)
%8 = call i32 @util_strlcat(i8* %4, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 1024)
%9 = load i32, i32* @O_RDONLY
%10 = call i32 (i8*, i32, ...) @open(i8* %4, i32 %9)
%11 = icmp sgt i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %1
%13 = call i32 @read(i32 %10, i8* %5, i32 31)
%14 = call i32 @close(i32 %10)
%15 = icmp slt i32 %13, 1
br i1 %15, label %16, label %18
16: ; preds = %1, %12
%17 = call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %18
18: ; preds = %16, %12
%19 = phi i32 [ 3, %16 ], [ %13, %12 ]
%20 = call i32 @udev_get_dev_path(%struct.udev* %0)
%21 = call i32 @util_strlcpy(i8* %4, i32 %20, i32 1024)
%22 = call i32 @util_strlcat(i8* %4, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i32 1024)
%23 = call i32 @util_create_path(%struct.udev* %0, i8* %4)
%24 = load i32, i32* @O_WRONLY
%25 = load i32, i32* @O_TRUNC
%26 = or i32 %25, %24
%27 = load i32, i32* @O_CREAT
%28 = or i32 %26, %27
%29 = call i32 (i8*, i32, ...) @open(i8* %4, i32 %28, i32 420)
%30 = icmp sgt i32 %29, -1
br i1 %30, label %31, label %34
31: ; preds = %18
%32 = call i32 @write(i32 %29, i8* %5, i32 %19)
%33 = call i32 @close(i32 %29)
br label %34
34: ; preds = %31, %18
ret void
}