#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 stat {
  int /*<<< orphan*/ st_rdev;
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int /*<<< orphan*/ S_ISCHR(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int major(int /*<<< orphan*/);
int minor(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ stat(char *, struct stat *);
char *strerror(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  struct stat st;
  int mjr, mnr;
  char *dev = "/dev/tty0";
  if (stat(dev, &st)) {
    printf("stat failed: %s\n", strerror(errno));
    return -1;
  }
  if (!S_ISCHR(st.st_mode)) {
    printf("not char\n");
    return -2;
  }
  printf("%lx\n", st.st_rdev);
  mjr = major(st.st_rdev);
  mnr = minor(st.st_rdev);
  printf("%s: %d:%d\n", dev, mjr, mnr);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe83(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
je     11c3 <main+0x63>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    %rax,%rsi
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1234 <main+0xd4>
mov    -0x14(%rbp),%edi
call   1070 <S_ISCHR@plt>
cmp    $0x0,%eax
jne    11ee <main+0x8e>
lea    0xe40(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1234 <main+0xd4>
mov    -0x18(%rbp),%esi
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
call   1050 <major@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
call   1030 <minor@plt>
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%rsi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
lea    0xe86(%rip),%rdi
mov    %rsp,%rsi
call   1070 <stat@plt>
test   %rax,%rax
je     11ad <main+0x3d>
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xe6e(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%ebx
jmp    120b <main+0x9b>
mov    0x4(%rsp),%edi
call   1080 <S_ISCHR@plt>
test   %eax,%eax
je     11fa <main+0x8a>
mov    (%rsp),%esi
lea    0xe57(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rsp),%edi
call   1060 <major@plt>
mov    %eax,%ebp
mov    (%rsp),%edi
call   1040 <minor@plt>
lea    0xe3a(%rip),%rdi
lea    0xe13(%rip),%rsi
mov    %ebp,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    120b <main+0x9b>
lea    0xe2a(%rip),%rdi
call   1030 <puts@plt>
mov    $0xfffffffe,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@.str = [10 x i8] c"/dev/tty0\00"
@.str.1 = [17 x i8] c"stat failed: %s\0A\00"
@errno = global i32 0
@.str.2 = [10 x i8] c"not char\0A\00"
@.str.3 = [5 x i8] c"%lx\0A\00"
@.str.4 = [11 x i8] c"%s: %d:%d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.stat
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %9
%10 = load i8*, i8** %9
%11 = call i64 @stat(i8* %10, %struct.stat* %6)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i32, i32* @errno
%15 = call i8* @strerror(i32 %14)
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %15)
store i32 -1, i32* %3
br label %38
17: ; preds = %2
%18 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 1
%19 = load i32, i32* %18
%20 = call i32 @S_ISCHR(i32 %19)
%21 = icmp ne i32 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %17
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
store i32 -2, i32* %3
br label %38
24: ; preds = %17
%25 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%26 = load i32, i32* %25
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %26)
%28 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%29 = load i32, i32* %28
%30 = call i32 @major(i32 %29)
store i32 %30, i32* %7
%31 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%32 = load i32, i32* %31
%33 = call i32 @minor(i32 %32)
store i32 %33, i32* %8
%34 = load i8*, i8** %9
%35 = load i32, i32* %7
%36 = load i32, i32* %8
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* %34, i32 %35, i32 %36)
store i32 0, i32* %3
br label %38
38: ; preds = %24, %22, %13
%39 = load i32, i32* %3
ret i32 %39
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@.str = [10 x i8] c"/dev/tty0\00"
@.str.1 = [17 x i8] c"stat failed: %s\0A\00"
@errno = global i32 0
@.str.3 = [5 x i8] c"%lx\0A\00"
@.str.4 = [11 x i8] c"%s: %d:%d\0A\00"
@str = [9 x i8] c"not char\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i64 @stat(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), %struct.stat* %3)
%6 = icmp eq i64 %5, 0
br i1 %6, label %11, label %7
7: ; preds = %2
%8 = load i32, i32* @errno
%9 = call i8* @strerror(i32 %8)
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %9)
br label %27
11: ; preds = %2
%12 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%13 = load i32, i32* %12
%14 = call i32 @S_ISCHR(i32 %13)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
br label %27
18: ; preds = %11
%19 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%20 = load i32, i32* %19
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %20)
%22 = load i32, i32* %19
%23 = call i32 @major(i32 %22)
%24 = load i32, i32* %19
%25 = call i32 @minor(i32 %24)
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %23, i32 %25)
br label %27
27: ; preds = %18, %16, %7
%28 = phi i32 [ -1, %7 ], [ 0, %18 ], [ -2, %16 ]
ret i32 %28
}