#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 */

/* Variables and functions */
int alu_temp(int, int, int, int, int *, int *, int *);
int /*<<< orphan*/ bin32(int);
int get_msb(int);
int /*<<< orphan*/ header_print(char *);
int /*<<< orphan*/ line_print();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ scanf(char *, int *, ...);
int main() {
  int A, B, carryOut, overflow_bit, opcode, ng, zr;
  int alu_out;
  printf("Enter values A and B to ALU: ");
  scanf("%d %d", &A, &B);
  printf("Enter opcode: ");
  scanf("%d", &opcode);
  printf("Negate flag: ");
  scanf("%d", &ng);
  alu_out = alu_temp(A, B, opcode, ng, &carryOut, &zr, &overflow_bit);
  printf("%u + %u = %u\n", A, B, alu_out);
  printf("%#08x + %#08x = %#08x\n", A, B, alu_out);
  printf("ALU output: %d\n", alu_out);
  printf("|Co|Zr|Ov|\n");
  printf("|% 2d|% 2d|% 2d|\n", carryOut, zr, overflow_bit);
  line_print();
  header_print("Overflow Testing");
  int a, b, result, over_test;
  a = 2147483647;
  b = 1;
  result = a + b;
  over_test = get_msb((~a & ~b) & result | (a & b & (~result)));
  bin32(a);
  bin32(b);
  printf("   ---------------------------------------\n");
  bin32(result);
  printf("Overflow: %d\n", over_test);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe8a(%rip),%rdi
lea    -0x8(%rbp),%rsi
lea    -0xc(%rbp),%rdx
mov    $0x0,%al
call   1080 <scanf@plt>
lea    0xe7a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe69(%rip),%rdi
lea    -0x18(%rbp),%rsi
mov    $0x0,%al
call   1080 <scanf@plt>
lea    0xe69(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe49(%rip),%rdi
lea    -0x1c(%rbp),%rsi
mov    $0x0,%al
call   1080 <scanf@plt>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0x1c(%rbp),%ecx
lea    -0x10(%rbp),%r8
lea    -0x20(%rbp),%r9
lea    -0x14(%rbp),%rax
mov    %rax,(%rsp)
call   1060 <alu_temp@plt>
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
mov    -0x24(%rbp),%ecx
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
mov    -0x24(%rbp),%ecx
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x24(%rbp),%esi
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe29(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%esi
mov    -0x20(%rbp),%edx
mov    -0x14(%rbp),%ecx
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <line_print@plt>
lea    0xe1b(%rip),%rdi
call   1070 <header_print@plt>
movl   $0x7fffffff,-0x28(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x28(%rbp),%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%edi
xor    $0xffffffff,%edi
mov    -0x2c(%rbp),%eax
xor    $0xffffffff,%eax
and    %eax,%edi
and    -0x30(%rbp),%edi
mov    -0x28(%rbp),%eax
and    -0x2c(%rbp),%eax
mov    -0x30(%rbp),%ecx
xor    $0xffffffff,%ecx
and    %ecx,%eax
or     %eax,%edi
call   1030 <get_msb@plt>
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%edi
call   1090 <bin32@plt>
mov    -0x2c(%rbp),%edi
call   1090 <bin32@plt>
lea    0xdd0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x30(%rbp),%edi
call   1090 <bin32@plt>
mov    -0x34(%rbp),%esi
lea    0xde3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x30,%rsp
lea    0xe74(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe84(%rip),%rdi
lea    0x18(%rsp),%rsi
lea    0x14(%rsp),%rdx
xor    %eax,%eax
call   1090 <scanf@plt>
lea    0xe72(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe61(%rip),%rbx
lea    0x24(%rsp),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1090 <scanf@plt>
lea    0xe5d(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x20(%rsp),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1090 <scanf@plt>
mov    0x18(%rsp),%edi
mov    0x14(%rsp),%esi
mov    0x24(%rsp),%edx
mov    0x20(%rsp),%ecx
lea    0x28(%rsp),%rax
mov    %rax,(%rsp)
lea    0x2c(%rsp),%r8
lea    0x1c(%rsp),%r9
call   1070 <alu_temp@plt>
mov    %eax,%ebx
mov    0x18(%rsp),%esi
mov    0x14(%rsp),%edx
lea    0xe1c(%rip),%rdi
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x18(%rsp),%esi
mov    0x14(%rsp),%edx
lea    0xe12(%rip),%rdi
mov    %ebx,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe19(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe4a(%rip),%rdi
call   1030 <puts@plt>
mov    0x2c(%rsp),%esi
mov    0x1c(%rsp),%edx
mov    0x28(%rsp),%ecx
lea    0xe01(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
call   1060 <line_print@plt>
lea    0xdfe(%rip),%rdi
call   1080 <header_print@plt>
mov    $0x80000001,%edi
call   1040 <get_msb@plt>
mov    %eax,%ebx
mov    $0x7fffffff,%edi
call   10a0 <bin32@plt>
mov    $0x1,%edi
call   10a0 <bin32@plt>
lea    0xdfc(%rip),%rdi
call   1030 <puts@plt>
mov    $0x80000000,%edi
call   10a0 <bin32@plt>
lea    0xdcd(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [30 x i8] c"Enter values A and B to ALU: \00"
@.str.1 = [6 x i8] c"%d %d\00"
@.str.2 = [15 x i8] c"Enter opcode: \00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [14 x i8] c"Negate flag: \00"
@.str.5 = [14 x i8] c"%u + %u = %u\0A\00"
@.str.6 = [23 x i8] c"%x + %x = %x\0A\00"
@.str.7 = [16 x i8] c"ALU output: %d\0A\00"
@.str.8 = [12 x i8] c"|Co|Zr|Ov|\0A\00"
@.str.9 = [18 x i8] c"|% 2d|% 2d|% 2d|\0A\00"
@.str.10 = [17 x i8] c"Overflow Testing\00"
@.str.11 = [44 x i8] c" ---------------------------------------\0A\00"
@.str.12 = [14 x i8] c"Overflow: %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %1
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%15 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32* %2, i32* %3)
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%17 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %6)
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%19 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %7)
%20 = load i32, i32* %2
%21 = load i32, i32* %3
%22 = load i32, i32* %6
%23 = load i32, i32* %7
%24 = call i32 @alu_temp(i32 %20, i32 %21, i32 %22, i32 %23, i32* %4, i32* %8, i32* %5)
store i32 %24, i32* %9
%25 = load i32, i32* %2
%26 = load i32, i32* %3
%27 = load i32, i32* %9
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i32 %25, i32 %26, i32 %27)
%29 = load i32, i32* %2
%30 = load i32, i32* %3
%31 = load i32, i32* %9
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), i32 %29, i32 %30, i32 %31)
%33 = load i32, i32* %9
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i32 %33)
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0))
%36 = load i32, i32* %4
%37 = load i32, i32* %8
%38 = load i32, i32* %5
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.9, i64 0, i64 0), i32 %36, i32 %37, i32 %38)
%40 = call i32 (...) @line_print()
%41 = call i32 @header_print(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.10, i64 0, i64 0))
store i32 2147483647, i32* %10
store i32 1, i32* %11
%42 = load i32, i32* %10
%43 = load i32, i32* %11
%44 = add nsw i32 %42, %43
store i32 %44, i32* %12
%45 = load i32, i32* %10
%46 = xor i32 %45, -1
%47 = load i32, i32* %11
%48 = xor i32 %47, -1
%49 = and i32 %46, %48
%50 = load i32, i32* %12
%51 = and i32 %49, %50
%52 = load i32, i32* %10
%53 = load i32, i32* %11
%54 = and i32 %52, %53
%55 = load i32, i32* %12
%56 = xor i32 %55, -1
%57 = and i32 %54, %56
%58 = or i32 %51, %57
%59 = call i32 @get_msb(i32 %58)
store i32 %59, i32* %13
%60 = load i32, i32* %10
%61 = call i32 @bin32(i32 %60)
%62 = load i32, i32* %11
%63 = call i32 @bin32(i32 %62)
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.11, i64 0, i64 0))
%65 = load i32, i32* %12
%66 = call i32 @bin32(i32 %65)
%67 = load i32, i32* %13
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.12, i64 0, i64 0), i32 %67)
ret i32 0
}

LLVM IR (-O2):
@.str = [30 x i8] c"Enter values A and B to ALU: \00"
@.str.1 = [6 x i8] c"%d %d\00"
@.str.2 = [15 x i8] c"Enter opcode: \00"
@.str.3 = [3 x i8] c"%d\00"
@.str.4 = [14 x i8] c"Negate flag: \00"
@.str.5 = [14 x i8] c"%u + %u = %u\0A\00"
@.str.6 = [23 x i8] c"%x + %x = %x\0A\00"
@.str.7 = [16 x i8] c"ALU output: %d\0A\00"
@.str.9 = [18 x i8] c"|% 2d|% 2d|% 2d|\0A\00"
@.str.10 = [17 x i8] c"Overflow Testing\00"
@.str.12 = [14 x i8] c"Overflow: %d\0A\00"
@str = [11 x i8] c"|Co|Zr|Ov|\00"
@str.13 = [43 x i8] c" ---------------------------------------\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = bitcast i32* %1 to i8*
%9 = bitcast i32* %2 to i8*
%10 = bitcast i32* %3 to i8*
%11 = bitcast i32* %4 to i8*
%12 = bitcast i32* %5 to i8*
%13 = bitcast i32* %6 to i8*
%14 = bitcast i32* %7 to i8*
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%16 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32* %1, i32* %2)
%17 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%18 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %5)
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%20 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32* %6)
%21 = load i32, i32* %1
%22 = load i32, i32* %2
%23 = load i32, i32* %5
%24 = load i32, i32* %6
%25 = call i32 @alu_temp(i32 %21, i32 %22, i32 %23, i32 %24, i32* %3, i32* %7, i32* %4)
%26 = load i32, i32* %1
%27 = load i32, i32* %2
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i32 %26, i32 %27, i32 %25)
%29 = load i32, i32* %1
%30 = load i32, i32* %2
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), i32 %29, i32 %30, i32 %25)
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i32 %25)
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%34 = load i32, i32* %3
%35 = load i32, i32* %7
%36 = load i32, i32* %4
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.9, i64 0, i64 0), i32 %34, i32 %35, i32 %36)
%38 = call i32 (...) @line_print()
%39 = call i32 @header_print(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.10, i64 0, i64 0))
%40 = call i32 @get_msb(i32 -2147483647)
%41 = call i32 @bin32(i32 2147483647)
%42 = call i32 @bin32(i32 1)
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str.13, i64 0, i64 0))
%44 = call i32 @bin32(i32 -2147483648)
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.12, i64 0, i64 0), i32 %40)
ret i32 0
}