#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_device {
  int dummy;
};
struct udev {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ print_device(struct udev_device *);
int /*<<< orphan*/ printf(char *, ...);
struct udev_device *udev_device_get_parent(struct udev_device *);
struct udev_device *udev_device_new_from_syspath(struct udev *, char const *);
int /*<<< orphan*/ udev_device_unref(struct udev_device *);
int test_device_parents(struct udev *udev, const char *syspath) {
  struct udev_device *device;
  struct udev_device *device_parent;
  printf("looking at device: %s\n", syspath);
  device = udev_device_new_from_syspath(udev, syspath);
  if (device == NULL)
    return -1;
  printf("looking at parents\n");
  device_parent = device;
  do {
    print_device(device_parent);
    device_parent = udev_device_get_parent(device_parent);
  } while (device_parent != NULL);
  printf("looking at parents again\n");
  device_parent = device;
  do {
    print_device(device_parent);
    device_parent = udev_device_get_parent(device_parent);
  } while (device_parent != NULL);
  udev_device_unref(device);
  return 0;
}


x86 assembly (clang, -O0):
<test_device_parents>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rsi
lea    0xe95(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <udev_device_new_from_syspath@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    119a <test_device_parents+0x4a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1218 <test_device_parents+0xc8>
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1070 <print_device@plt>
mov    -0x28(%rbp),%rdi
call   1050 <udev_device_get_parent@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11b0 <test_device_parents+0x60>
lea    0xe53(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1070 <print_device@plt>
mov    -0x28(%rbp),%rdi
call   1050 <udev_device_get_parent@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11e7 <test_device_parents+0x97>
mov    -0x20(%rbp),%rdi
call   1060 <udev_device_unref@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_device_parents>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xe8f(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <udev_device_new_from_syspath@plt>
test   %rax,%rax
je     11f4 <test_device_parents+0x94>
mov    %rax,%r14
lea    0xe85(%rip),%rdi
call   1030 <puts@plt>
mov    %r14,%rbx
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1080 <print_device@plt>
mov    %rbx,%rdi
call   1060 <udev_device_get_parent@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11a0 <test_device_parents+0x40>
lea    0xe6b(%rip),%rdi
call   1030 <puts@plt>
mov    %r14,%rbx
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1080 <print_device@plt>
mov    %rbx,%rdi
call   1060 <udev_device_get_parent@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11d0 <test_device_parents+0x70>
mov    %r14,%rdi
call   1070 <udev_device_unref@plt>
xor    %eax,%eax
jmp    11f9 <test_device_parents+0x99>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.udev = type { i32 }
%struct.udev_device = type { i32 }
@.str = [23 x i8] c"looking at device: %s\0A\00"
@.str.1 = [20 x i8] c"looking at parents\0A\00"
@.str.2 = [26 x i8] c"looking at parents again\0A\00"
define i32 @test_device_parents(%struct.udev* %0, i8* %1) {
%3 = alloca i32
%4 = alloca %struct.udev*
%5 = alloca i8*
%6 = alloca %struct.udev_device*
%7 = alloca %struct.udev_device*
store %struct.udev* %0, %struct.udev** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %5
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = load %struct.udev*, %struct.udev** %4
%11 = load i8*, i8** %5
%12 = call %struct.udev_device* @udev_device_new_from_syspath(%struct.udev* %10, i8* %11)
store %struct.udev_device* %12, %struct.udev_device** %6
%13 = load %struct.udev_device*, %struct.udev_device** %6
%14 = icmp eq %struct.udev_device* %13, null
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 -1, i32* %3
br label %41
16: ; preds = %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%18 = load %struct.udev_device*, %struct.udev_device** %6
store %struct.udev_device* %18, %struct.udev_device** %7
br label %19
19: ; preds = %24, %16
%20 = load %struct.udev_device*, %struct.udev_device** %7
%21 = call i32 @print_device(%struct.udev_device* %20)
%22 = load %struct.udev_device*, %struct.udev_device** %7
%23 = call %struct.udev_device* @udev_device_get_parent(%struct.udev_device* %22)
store %struct.udev_device* %23, %struct.udev_device** %7
br label %24
24: ; preds = %19
%25 = load %struct.udev_device*, %struct.udev_device** %7
%26 = icmp ne %struct.udev_device* %25, null
br i1 %26, label %19, label %27
27: ; preds = %24
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%29 = load %struct.udev_device*, %struct.udev_device** %6
store %struct.udev_device* %29, %struct.udev_device** %7
br label %30
30: ; preds = %35, %27
%31 = load %struct.udev_device*, %struct.udev_device** %7
%32 = call i32 @print_device(%struct.udev_device* %31)
%33 = load %struct.udev_device*, %struct.udev_device** %7
%34 = call %struct.udev_device* @udev_device_get_parent(%struct.udev_device* %33)
store %struct.udev_device* %34, %struct.udev_device** %7
br label %35
35: ; preds = %30
%36 = load %struct.udev_device*, %struct.udev_device** %7
%37 = icmp ne %struct.udev_device* %36, null
br i1 %37, label %30, label %38
38: ; preds = %35
%39 = load %struct.udev_device*, %struct.udev_device** %6
%40 = call i32 @udev_device_unref(%struct.udev_device* %39)
store i32 0, i32* %3
br label %41
41: ; preds = %38, %15
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
%struct.udev = type { i32 }
%struct.udev_device = type { i32 }
@.str = [23 x i8] c"looking at device: %s\0A\00"
@str = [19 x i8] c"looking at parents\00"
@str.3 = [25 x i8] c"looking at parents again\00"
define i32 @test_device_parents(%struct.udev* %0, i8* %1) {
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %1)
%4 = tail call %struct.udev_device* @udev_device_new_from_syspath(%struct.udev* %0, i8* %1)
%5 = icmp eq %struct.udev_device* %4, null
br i1 %5, label %22, label %6
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %8
8: ; preds = %8, %6
%9 = phi %struct.udev_device* [ %4, %6 ], [ %11, %8 ]
%10 = tail call i32 @print_device(%struct.udev_device* %9)
%11 = tail call %struct.udev_device* @udev_device_get_parent(%struct.udev_device* %9)
%12 = icmp eq %struct.udev_device* %11, null
br i1 %12, label %13, label %8
13: ; preds = %8
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.3, i64 0, i64 0))
br label %15
15: ; preds = %15, %13
%16 = phi %struct.udev_device* [ %4, %13 ], [ %18, %15 ]
%17 = tail call i32 @print_device(%struct.udev_device* %16)
%18 = tail call %struct.udev_device* @udev_device_get_parent(%struct.udev_device* %16)
%19 = icmp eq %struct.udev_device* %18, null
br i1 %19, label %20, label %15
20: ; preds = %15
%21 = tail call i32 @udev_device_unref(%struct.udev_device* %4)
br label %22
22: ; preds = %2, %20
%23 = phi i32 [ 0, %20 ], [ -1, %2 ]
ret i32 %23
}