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

/* Variables and functions */
uintptr_t O_RDWR;
uintptr_t open(char *, uintptr_t, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, char *, int, int);
char *strchr(char *, char);
int /*<<< orphan*/ strncpy(char *, char *, int);
uintptr_t syz_open_dev(uintptr_t a0, uintptr_t a1, uintptr_t a2) {
  if (a0 == 0xc || a0 == 0xb) {
    char buf[128];
    sprintf(buf, "/dev/%s/%d:%d", a0 == 0xc ? "char" : "block", (uint8_t)a1,
            (uint8_t)a2);
    return open(buf, O_RDWR, 0);
  } else {
    char buf[1024];
    char *hash;
    strncpy(buf, (char *)a0, sizeof(buf));
    buf[sizeof(buf) - 1] = 0;
    while ((hash = strchr(buf, '#'))) {
      *hash = '0' + (char)(a1 % 10);
      a1 /= 10;
    }
    return open(buf, a2, 0);
  }
}


x86 assembly (clang, -O0):
<syz_open_dev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4b0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0xc,-0x10(%rbp)
je     116d <syz_open_dev+0x2d>
cmpq   $0xb,-0x10(%rbp)
jne    11c8 <syz_open_dev+0x88>
lea    -0xa0(%rbp),%rdi
mov    -0x10(%rbp),%rcx
lea    0xe94(%rip),%rdx
lea    0xe88(%rip),%rax
cmp    $0xc,%rcx
cmove  %rax,%rdx
mov    -0x18(%rbp),%rax
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %eax,%r8d
lea    0xe5e(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0xa0(%rbp),%rdi
mov    0x2e33(%rip),%rax
mov    (%rax),%rsi
xor    %edx,%edx
call   1050 <open@plt>
mov    %rax,-0x8(%rbp)
jmp    1254 <syz_open_dev+0x114>
lea    -0x4a0(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x400,%edx
call   1030 <strncpy@plt>
movb   $0x0,-0xa1(%rbp)
lea    -0x4a0(%rbp),%rdi
mov    $0x23,%esi
call   1040 <strchr@plt>
mov    %rax,-0x4a8(%rbp)
cmp    $0x0,%rax
je     123e <syz_open_dev+0xfe>
mov    -0x18(%rbp),%rax
mov    $0xa,%ecx
xor    %edx,%edx
div    %rcx
mov    %dl,%al
movsbl %al,%eax
add    $0x30,%eax
mov    %al,%cl
mov    -0x4a8(%rbp),%rax
mov    %cl,(%rax)
mov    -0x18(%rbp),%rax
mov    $0xa,%ecx
xor    %edx,%edx
div    %rcx
mov    %rax,-0x18(%rbp)
jmp    11e4 <syz_open_dev+0xa4>
lea    -0x4a0(%rbp),%rdi
mov    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1050 <open@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x4b0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<syz_open_dev>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %rdi,%rsi
lea    -0xb(%rdi),%rax
cmp    $0x1,%rax
ja     119f <syz_open_dev+0x5f>
cmp    $0xc,%rsi
lea    0xea0(%rip),%rax
lea    0xe9e(%rip),%rdx
cmove  %rax,%rdx
lea    0xe80(%rip),%rsi
mov    %rsp,%r15
mov    %r15,%rdi
mov    %ebx,%ecx
mov    %r14d,%r8d
call   1060 <sprintf@plt>
mov    0x2e51(%rip),%rax
mov    (%rax),%rsi
mov    %r15,%rdi
jmp    1219 <syz_open_dev+0xd9>
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0x400,%edx
call   1030 <strncpy@plt>
movb   $0x0,0x3ff(%rsp)
mov    %r15,%rdi
mov    $0x23,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     1213 <syz_open_dev+0xd3>
mov    %rax,%rcx
movabs $0xcccccccccccccccd,%r13
cc cc cc
mov    %rsp,%r15
nopl   0x0(%rax)
mov    %rbx,%rax
mul    %r13
mov    %rdx,%r12
shr    $0x3,%r12
lea    (%r12,%r12,1),%eax
lea    (%rax,%rax,4),%eax
sub    %eax,%ebx
or     $0x30,%bl
mov    %bl,(%rcx)
mov    %r15,%rdi
mov    $0x23,%esi
call   1040 <strchr@plt>
mov    %rax,%rcx
mov    %r12,%rbx
test   %rax,%rax
jne    11e0 <syz_open_dev+0xa0>
mov    %rsp,%rdi
mov    %r14,%rsi
xor    %edx,%edx
call   1050 <open@plt>
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/dev/%s/%d:%d\00"
@.str.1 = [5 x i8] c"char\00"
@.str.2 = [6 x i8] c"block\00"
@O_RDWR = global i64 0
define i64 @syz_open_dev(i64 %0, i64 %1, i64 %2) {
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca [128 x i8]
%9 = alloca [1024 x i8]
%10 = alloca i8*
store i64 %0, i64* %5
store i64 %1, i64* %6
store i64 %2, i64* %7
%11 = load i64, i64* %5
%12 = icmp eq i64 %11, 12
br i1 %12, label %16, label %13
13: ; preds = %3
%14 = load i64, i64* %5
%15 = icmp eq i64 %14, 11
br i1 %15, label %16, label %30
16: ; preds = %13, %3
%17 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%18 = load i64, i64* %5
%19 = icmp eq i64 %18, 12
%20 = zext i1 %19 to i64
%21 = select i1 %19, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0)
%22 = load i64, i64* %6
%23 = trunc i64 %22 to i32
%24 = load i64, i64* %7
%25 = trunc i64 %24 to i32
%26 = call i32 @sprintf(i8* %17, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %21, i32 %23, i32 %25)
%27 = getelementptr inbounds [128 x i8], [128 x i8]* %8, i64 0, i64 0
%28 = load i64, i64* @O_RDWR
%29 = call i64 @open(i8* %27, i64 %28, i32 0)
store i64 %29, i64* %4
br label %54
30: ; preds = %13
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%32 = load i64, i64* %5
%33 = inttoptr i64 %32 to i8*
%34 = call i32 @strncpy(i8* %31, i8* %33, i32 1024)
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 1023
store i8 0, i8* %35
br label %36
36: ; preds = %40, %30
%37 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%38 = call i8* @strchr(i8* %37, i8 signext 35)
store i8* %38, i8** %10
%39 = icmp ne i8* %38, null
br i1 %39, label %40, label %50
40: ; preds = %36
%41 = load i64, i64* %6
%42 = urem i64 %41, 10
%43 = trunc i64 %42 to i8
%44 = sext i8 %43 to i32
%45 = add nsw i32 48, %44
%46 = trunc i32 %45 to i8
%47 = load i8*, i8** %10
store i8 %46, i8* %47
%48 = load i64, i64* %6
%49 = udiv i64 %48, 10
store i64 %49, i64* %6
br label %36
50: ; preds = %36
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%52 = load i64, i64* %7
%53 = call i64 @open(i8* %51, i64 %52, i32 0)
store i64 %53, i64* %4
br label %54
54: ; preds = %50, %16
%55 = load i64, i64* %4
ret i64 %55
}

LLVM IR (-O2):
@.str = [14 x i8] c"/dev/%s/%d:%d\00"
@.str.1 = [5 x i8] c"char\00"
@.str.2 = [6 x i8] c"block\00"
@O_RDWR = global i64 0
define i64 @syz_open_dev(i64 %0, i64 %1, i64 %2) {
%4 = alloca [128 x i8]
%5 = alloca [1024 x i8]
%6 = add i64 %0, -11
%7 = icmp ult i64 %6, 2
br i1 %7, label %8, label %17
8: ; preds = %3
%9 = icmp eq i64 %0, 12
%10 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%11 = select i1 %9, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0)
%12 = trunc i64 %1 to i32
%13 = trunc i64 %2 to i32
%14 = call i32 @sprintf(i8* %10, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %11, i32 %12, i32 %13)
%15 = load i64, i64* @O_RDWR
%16 = call i64 @open(i8* %10, i64 %15, i32 0)
br label %35
17: ; preds = %3
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%19 = inttoptr i64 %0 to i8*
%20 = call i32 @strncpy(i8* %18, i8* %19, i32 1024)
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 1023
store i8 0, i8* %21
%22 = call i8* @strchr(i8* %18, i8 signext 35)
%23 = icmp eq i8* %22, null
br i1 %23, label %33, label %24
24: ; preds = %17, %24
%25 = phi i8* [ %31, %24 ], [ %22, %17 ]
%26 = phi i64 [ %30, %24 ], [ %1, %17 ]
%27 = urem i64 %26, 10
%28 = trunc i64 %27 to i8
%29 = or i8 %28, 48
store i8 %29, i8* %25
%30 = udiv i64 %26, 10
%31 = call i8* @strchr(i8* %18, i8 signext 35)
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %24
33: ; preds = %24, %17
%34 = call i64 @open(i8* %18, i64 %2, i32 0)
br label %35
35: ; preds = %33, %8
%36 = phi i64 [ %16, %8 ], [ %34, %33 ]
ret i64 %36
}