// #target: macos-x64

module test;

faultdef X;

fn int? test(int i)
{
	return i ?: X?;
}

fn int? test2(int i)
{
	return i ? X? : X?;
}


fn int? test3(int i)
{
	return i ? 2 : X?;
}


fn int? test4(int i)
{
	int? y = i;
	return (y!) ?: X?;
}
fn int? test5(int i)
{
	int? y = i;
	return (y!) ? X? : X?;
}

fn int? test6(int i)
{
	int? y = i;
	return (y!) ? 2 : X?;
}

fn int? test7(int i)
{
	int? y = i;
    return (y!) ? X? : 2;
}

fn void main()
{
	test(1)!!;
	test7(0)!!;
	test6(1)!!;
	int? i = test2(3);
}

/* expect: test.ll
source_filename = "test"
target datalayout = "e-m:o-p270:32:32-p271:32
target triple = "x86_64-apple

%.fault = type { i64, %"char[]" }
%"char[]" = type { i8*, i64 }
%.introspect = type { i8, i64, i64, i64, [0 x i64] }

@"test.Foo$X" = linkonce constant %.fault { i64 ptrtoint (%.introspect* @"$ct.test.Foo" to i64), %"char[]" { i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.fault, i64 0, i64 0), i64 1 } }, align 8
@.fault = internal constant [2 x i8] c"X\00", align 1
@"$ct.test.Foo" = linkonce constant %.introspect { i8 10, i64 8, i64 0, i64 1, [0 x i64] zeroinitializer }, align 8
@.zstr = internal constant [28 x i8] c"Runtime error force unwrap!\00", align 1
@.zstr.1 = internal constant [20 x i8] c"optional_ternary.c3\00", align 1
@.zstr.2 = internal constant [5 x i8] c"main\00", align 1
@.zstr.3 = internal constant [28 x i8] c"Runtime error force unwrap!\00", align 1
@.zstr.4 = internal constant [20 x i8] c"optional_ternary.c3\00", align 1
@.zstr.5 = internal constant [5 x i8] c"main\00", align 1
@.zstr.6 = internal constant [28 x i8] c"Runtime error force unwrap!\00", align 1
@.zstr.7 = internal constant [20 x i8] c"optional_ternary.c3\00", align 1
@.zstr.8 = internal constant [5 x i8] c"main\00", align 1


define i64 @test.test(i32* %0, i32 %1) #0 {
entry:
  %reterr = alloca i64, align 8
  %i2b = icmp ne i32 %1, 0
  br i1 %i2b, label %cond.phi, label %cond.rhs

cond.rhs:                                         ; preds = %entry
  store i64 ptrtoint (%.fault* @"test.Foo$X" to i64), i64* %reterr, align 8
  br label %err_retblock

cond.phi:                                         ; preds = %entry
  store i32 %1, i32* %0, align 4
  ret i64 0

err_retblock:                                     ; preds = %cond.rhs
  %2 = load i64, i64* %reterr, align 8
  ret i64 %2
}


define i64 @test.test2(i32* %0, i32 %1) #0 {
entry:
  %reterr = alloca i64, align 8
  %i2b = icmp ne i32 %1, 0
  br i1 %i2b, label %cond.lhs, label %cond.rhs

cond.lhs:                                         ; preds = %entry
  store i64 ptrtoint (%.fault* @"test.Foo$X" to i64), i64* %reterr, align 8
  br label %err_retblock

cond.rhs:                                         ; preds = %entry
  store i64 ptrtoint (%.fault* @"test.Foo$X" to i64), i64* %reterr, align 8
  br label %err_retblock

cond.phi:                                         ; No predecessors!
  store i32 undef, i32* %0, align 4
  ret i64 0

err_retblock:                                     ; preds = %cond.rhs, %cond.lhs
  %2 = load i64, i64* %reterr, align 8
  ret i64 %2
}


define i64 @test.test3(i32* %0, i32 %1) #0 {
entry:
  %reterr = alloca i64, align 8
  %i2b = icmp ne i32 %1, 0
  br i1 %i2b, label %cond.lhs, label %cond.rhs

cond.lhs:                                         ; preds = %entry
  br label %cond.phi

cond.rhs:                                         ; preds = %entry
  store i64 ptrtoint (%.fault* @"test.Foo$X" to i64), i64* %reterr, align 8
  br label %err_retblock

cond.phi:                                         ; preds = %cond.lhs
  store i32 2, i32* %0, align 4
  ret i64 0

err_retblock:                                     ; preds = %cond.rhs
  %2 = load i64, i64* %reterr, align 8
  ret i64 %2
}


define i64 @test.test4(i32* %0, i32 %1) #0 {
entry:
  %y = alloca i32, align 4
  %y.f = alloca i64, align 8
  %reterr = alloca i64, align 8
  %error_var = alloca i64, align 8
  store i32 %1, i32* %y, align 4
  store i64 0, i64* %y.f, align 8
  %optval = load i64, i64* %y.f, align 8
  %not_err = icmp eq i64 %optval, 0
  br i1 %not_err, label %after_check, label %assign_optional

assign_optional:                                  ; preds = %entry
  store i64 %optval, i64* %error_var, align 8
  br label %guard_block

after_check:                                      ; preds = %entry
  br label %noerr_block

guard_block:                                      ; preds = %assign_optional
  %2 = load i64, i64* %error_var, align 8
  ret i64 %2

noerr_block:                                      ; preds = %after_check
  %3 = load i32, i32* %y, align 4
  %i2b = icmp ne i32 %3, 0
  br i1 %i2b, label %cond.phi, label %cond.rhs

cond.rhs:                                         ; preds = %noerr_block
  store i64 ptrtoint (%.fault* @"test.Foo$X" to i64), i64* %reterr, align 8
  br label %err_retblock

cond.phi:                                         ; preds = %noerr_block
  store i32 %3, i32* %0, align 4
  ret i64 0

err_retblock:                                     ; preds = %cond.rhs
  %4 = load i64, i64* %reterr, align 8
  ret i64 %4
}


define i64 @test.test5(i32* %0, i32 %1) #0 {
entry:
  %y = alloca i32, align 4
  %y.f = alloca i64, align 8
  %reterr = alloca i64, align 8
  %error_var = alloca i64, align 8
  store i32 %1, i32* %y, align 4
  store i64 0, i64* %y.f, align 8
  %optval = load i64, i64* %y.f, align 8
  %not_err = icmp eq i64 %optval, 0
  br i1 %not_err, label %after_check, label %assign_optional

assign_optional:                                  ; preds = %entry
  store i64 %optval, i64* %error_var, align 8
  br label %guard_block

after_check:                                      ; preds = %entry
  br label %noerr_block

guard_block:                                      ; preds = %assign_optional
  %2 = load i64, i64* %error_var, align 8
  ret i64 %2

noerr_block:                                      ; preds = %after_check
  %3 = load i32, i32* %y, align 4
  %i2b = icmp ne i32 %3, 0
  br i1 %i2b, label %cond.lhs, label %cond.rhs

cond.lhs:                                         ; preds = %noerr_block
  store i64 ptrtoint (%.fault* @"test.Foo$X" to i64), i64* %reterr, align 8
  br label %err_retblock

cond.rhs:                                         ; preds = %noerr_block
  store i64 ptrtoint (%.fault* @"test.Foo$X" to i64), i64* %reterr, align 8
  br label %err_retblock

cond.phi:                                         ; No predecessors!
  store i32 undef, i32* %0, align 4
  ret i64 0

err_retblock:                                     ; preds = %cond.rhs, %cond.lhs
  %4 = load i64, i64* %reterr, align 8
  ret i64 %4
}


define i64 @test.test6(i32* %0, i32 %1) #0 {
entry:
  %y = alloca i32, align 4
  %y.f = alloca i64, align 8
  %reterr = alloca i64, align 8
  %error_var = alloca i64, align 8
  store i32 %1, i32* %y, align 4
  store i64 0, i64* %y.f, align 8
  %optval = load i64, i64* %y.f, align 8
  %not_err = icmp eq i64 %optval, 0
  br i1 %not_err, label %after_check, label %assign_optional

assign_optional:                                  ; preds = %entry
  store i64 %optval, i64* %error_var, align 8
  br label %guard_block

after_check:                                      ; preds = %entry
  br label %noerr_block

guard_block:                                      ; preds = %assign_optional
  %2 = load i64, i64* %error_var, align 8
  ret i64 %2

noerr_block:                                      ; preds = %after_check
  %3 = load i32, i32* %y, align 4
  %i2b = icmp ne i32 %3, 0
  br i1 %i2b, label %cond.lhs, label %cond.rhs

cond.lhs:                                         ; preds = %noerr_block
  br label %cond.phi

cond.rhs:                                         ; preds = %noerr_block
  store i64 ptrtoint (%.fault* @"test.Foo$X" to i64), i64* %reterr, align 8
  br label %err_retblock

cond.phi:                                         ; preds = %cond.lhs
  store i32 2, i32* %0, align 4
  ret i64 0

err_retblock:                                     ; preds = %cond.rhs
  %4 = load i64, i64* %reterr, align 8
  ret i64 %4
}


define i64 @test.test7(i32* %0, i32 %1) #0 {
entry:
  %y = alloca i32, align 4
  %y.f = alloca i64, align 8
  %reterr = alloca i64, align 8
  %error_var = alloca i64, align 8
  store i32 %1, i32* %y, align 4
  store i64 0, i64* %y.f, align 8
  %optval = load i64, i64* %y.f, align 8
  %not_err = icmp eq i64 %optval, 0
  br i1 %not_err, label %after_check, label %assign_optional

assign_optional:                                  ; preds = %entry
  store i64 %optval, i64* %error_var, align 8
  br label %guard_block

after_check:                                      ; preds = %entry
  br label %noerr_block

guard_block:                                      ; preds = %assign_optional
  %2 = load i64, i64* %error_var, align 8
  ret i64 %2

noerr_block:                                      ; preds = %after_check
  %3 = load i32, i32* %y, align 4
  %i2b = icmp ne i32 %3, 0
  br i1 %i2b, label %cond.lhs, label %cond.rhs

cond.lhs:                                         ; preds = %noerr_block
  store i64 ptrtoint (%.fault* @"test.Foo$X" to i64), i64* %reterr, align 8
  br label %err_retblock

cond.rhs:                                         ; preds = %noerr_block
  br label %cond.phi

cond.phi:                                         ; preds = %cond.rhs
  store i32 2, i32* %0, align 4
  ret i64 0

err_retblock:                                     ; preds = %cond.lhs
  %4 = load i64, i64* %reterr, align 8
  ret i64 %4
}


define void @test.main() #0 {
entry:
  %error_var = alloca i64, align 8
  %retparam = alloca i32, align 4
  %error_var1 = alloca i64, align 8
  %retparam2 = alloca i32, align 4
  %error_var8 = alloca i64, align 8
  %retparam9 = alloca i32, align 4
  %i = alloca i32, align 4
  %i.f = alloca i64, align 8
  %retparam15 = alloca i32, align 4
  %0 = call i64 @test.test(i32* %retparam, i32 1)
  %not_err = icmp eq i64 %0, 0
  br i1 %not_err, label %after_check, label %assign_optional

assign_optional:                                  ; preds = %entry
  store i64 %0, i64* %error_var, align 8
  br label %panic_block

after_check:                                      ; preds = %entry
  %1 = load i32, i32* %retparam, align 4
  br label %noerr_block

panic_block:                                      ; preds = %assign_optional
  call void @std.core.builtin.panic(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.zstr, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.zstr.1, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.zstr.2, i64 0, i64 0), i32 51)
  unreachable

noerr_block:                                      ; preds = %after_check
  %2 = call i64 @test.test7(i32* %retparam2, i32 0)
  %not_err3 = icmp eq i64 %2, 0
  br i1 %not_err3, label %after_check5, label %assign_optional4

assign_optional4:                                 ; preds = %noerr_block
  store i64 %2, i64* %error_var1, align 8
  br label %panic_block6

after_check5:                                     ; preds = %noerr_block
  %3 = load i32, i32* %retparam2, align 4
  br label %noerr_block7

panic_block6:                                     ; preds = %assign_optional4
  call void @std.core.builtin.panic(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.zstr.3, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.zstr.4, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.zstr.5, i64 0, i64 0), i32 52)
  unreachable

noerr_block7:                                     ; preds = %after_check5
  %4 = call i64 @test.test6(i32* %retparam9, i32 1)
  %not_err10 = icmp eq i64 %4, 0
  br i1 %not_err10, label %after_check12, label %assign_optional11

assign_optional11:                                ; preds = %noerr_block7
  store i64 %4, i64* %error_var8, align 8
  br label %panic_block13

after_check12:                                    ; preds = %noerr_block7
  %5 = load i32, i32* %retparam9, align 4
  br label %noerr_block14

panic_block13:                                    ; preds = %assign_optional11
  call void @std.core.builtin.panic(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.zstr.6, i64 0, i64 0), i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.zstr.7, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.zstr.8, i64 0, i64 0), i32 53)
  unreachable

noerr_block14:                                    ; preds = %after_check12
  %6 = call i64 @test.test2(i32* %retparam15, i32 3)
  %not_err16 = icmp eq i64 %6, 0
  br i1 %not_err16, label %after_check18, label %assign_optional17

assign_optional17:                                ; preds = %noerr_block14
  store i64 %6, i64* %i.f, align 8
  br label %after_assign

after_check18:                                    ; preds = %noerr_block14
  %7 = load i32, i32* %retparam15, align 4
  store i32 %7, i32* %i, align 4
  store i64 0, i64* %i.f, align 8
  br label %after_assign

after_assign:                                     ; preds = %after_check18, %assign_optional17
  ret void
}


define i32 @main(i32 %0, i8** %1) #0 {
entry:
  call void @test.main()
  ret i32 0
}

declare void @std.core.builtin.panic(i8*, i8*, i8*, i32)

attributes #0 = { nounwind }