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

/* Variables and functions */
void *finddevice(char *);
scalar_t__ getprop(void *, char *, char *, int);
int /*<<< orphan*/ strcmp(char *, char *);
void *serial_get_stdout_devp(void) {
  void *devp;
  char devtype[256];
  char path[256];
  devp = finddevice("/chosen");
  if (devp == NULL) {
    goto err_out;
  }
  if (getprop(devp, "linux,stdout-path", path, 256) > 0) {
    devp = finddevice(path);
    if (devp == NULL) {
      goto err_out;
    }
    if ((getprop(devp, "device_type", devtype, sizeof(devtype)) > 0) &&
        !strcmp(devtype, "serial")) {
      return devp;
    }
  }
err_out:
  return NULL;
}


x86 assembly (clang, -O0):
<serial_get_stdout_devp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x210,%rsp
lea    0xebe(%rip),%rdi
call   1050 <finddevice@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    115b <serial_get_stdout_devp+0x2b>
jmp    11fa <serial_get_stdout_devp+0xca>
mov    -0x10(%rbp),%rdi
lea    -0x210(%rbp),%rdx
lea    0xe9b(%rip),%rsi
mov    $0x100,%ecx
call   1030 <getprop@plt>
cmp    $0x0,%rax
jle    11f5 <serial_get_stdout_devp+0xc5>
lea    -0x210(%rbp),%rdi
call   1050 <finddevice@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11a1 <serial_get_stdout_devp+0x71>
jmp    11fa <serial_get_stdout_devp+0xca>
mov    -0x10(%rbp),%rdi
lea    -0x110(%rbp),%rdx
lea    0xe67(%rip),%rsi
mov    $0x100,%ecx
call   1030 <getprop@plt>
cmp    $0x0,%rax
jle    11f0 <serial_get_stdout_devp+0xc0>
lea    -0x110(%rbp),%rdi
lea    0xe51(%rip),%rsi
call   1040 <strcmp@plt>
cmp    $0x0,%eax
jne    11f0 <serial_get_stdout_devp+0xc0>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1202 <serial_get_stdout_devp+0xd2>
jmp    11f5 <serial_get_stdout_devp+0xc5>
jmp    11fa <serial_get_stdout_devp+0xca>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x210,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_get_stdout_devp>:
push   %rbx
sub    $0x200,%rsp
lea    0xed1(%rip),%rdi
call   1040 <finddevice@plt>
test   %rax,%rax
je     11a0 <serial_get_stdout_devp+0x80>
lea    0xec8(%rip),%rsi
lea    0x100(%rsp),%rdx
mov    %rax,%rdi
mov    $0x100,%ecx
call   1030 <getprop@plt>
test   %rax,%rax
jle    11a0 <serial_get_stdout_devp+0x80>
lea    0x100(%rsp),%rdi
call   1040 <finddevice@plt>
test   %rax,%rax
je     11a0 <serial_get_stdout_devp+0x80>
mov    %rax,%rbx
lea    0xea4(%rip),%rsi
mov    %rsp,%rdx
mov    %rax,%rdi
mov    $0x100,%ecx
call   1030 <getprop@plt>
test   %rax,%rax
jle    11a0 <serial_get_stdout_devp+0x80>
mov    $0x69726573,%eax
xor    (%rsp),%eax
mov    $0x6c6169,%ecx
xor    0x3(%rsp),%ecx
or     %eax,%ecx
je     11a2 <serial_get_stdout_devp+0x82>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x200,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [8 x i8] c"/chosen\00"
@.str.1 = [18 x i8] c"linux,stdout-path\00"
@.str.2 = [12 x i8] c"device_type\00"
@.str.3 = [7 x i8] c"serial\00"
define i8* @serial_get_stdout_devp() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca [256 x i8]
%4 = alloca [256 x i8]
%5 = call i8* @finddevice(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i8* %5, i8** %2
%6 = load i8*, i8** %2
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %9
8: ; preds = %0
br label %33
9: ; preds = %0
%10 = load i8*, i8** %2
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%12 = call i64 @getprop(i8* %10, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %11, i32 256)
%13 = icmp sgt i64 %12, 0
br i1 %13, label %14, label %32
14: ; preds = %9
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%16 = call i8* @finddevice(i8* %15)
store i8* %16, i8** %2
%17 = load i8*, i8** %2
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %20
19: ; preds = %14
br label %33
20: ; preds = %14
%21 = load i8*, i8** %2
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%23 = call i64 @getprop(i8* %21, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %22, i32 256)
%24 = icmp sgt i64 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %20
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%27 = call i32 @strcmp(i8* %26, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%28 = icmp ne i32 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %25
%30 = load i8*, i8** %2
store i8* %30, i8** %1
br label %34
31: ; preds = %25, %20
br label %32
32: ; preds = %31, %9
br label %33
33: ; preds = %32, %19, %8
store i8* null, i8** %1
br label %34
34: ; preds = %33, %29
%35 = load i8*, i8** %1
ret i8* %35
}

LLVM IR (-O2):
@.str = [8 x i8] c"/chosen\00"
@.str.1 = [18 x i8] c"linux,stdout-path\00"
@.str.2 = [12 x i8] c"device_type\00"
@.str.3 = [7 x i8] c"serial\00"
define i8* @serial_get_stdout_devp() {
%1 = alloca [256 x i8]
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%5 = tail call i8* @finddevice(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i8* %5, null
br i1 %6, label %19, label %7
7: ; preds = %0
%8 = call i64 @getprop(i8* %5, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %4, i32 256)
%9 = icmp sgt i64 %8, 0
br i1 %9, label %10, label %19
10: ; preds = %7
%11 = call i8* @finddevice(i8* %4)
%12 = icmp eq i8* %11, null
br i1 %12, label %19, label %13
13: ; preds = %10
%14 = call i64 @getprop(i8* %11, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %3, i32 256)
%15 = icmp sgt i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = call i32 @bcmp(i8* dereferenceable(7) %3, i8* dereferenceable(7) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i64 7)
%18 = icmp eq i32 %17, 0
br i1 %18, label %20, label %19
19: ; preds = %7, %16, %13, %10, %0
br label %20
20: ; preds = %16, %19
%21 = phi i8* [ null, %19 ], [ %11, %16 ]
ret i8* %21
}