#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 /*<<< orphan*/ printf(char *, ...);
int strlen(char *);
int /*<<< orphan*/ write(int, char *, int);
int phone_process(int fd, int type, char *phone_number) {
  int nread, nwrite, i, j;
  char buff0[4] = "ATD";
  char buff1[4] = "ATA";
  char buff2[8] = "AT+CHUP";
  char buff3[10] = "AT+CLIP=1";
  char end = '\r';
  char end_sent = ';';
  if (type == 0) {
    printf("call Phone %s through serial fd %d\r\n", phone_number, fd);
    write(fd, buff0, 3);
    write(fd, phone_number, strlen(phone_number));
    write(fd, &end_sent, 1);
    write(fd, &end, 1);
  } else if (type == 1) {
    printf("open incoming phone number display on fd %d\r\n", fd);
    write(fd, buff3, 9);
    write(fd, &end, 1);
  } else if (type == 2) {
    printf("reject incoming call on fd %d\r\n", fd);
    write(fd, buff2, 7);
    write(fd, &end, 1);
  } else if (type == 3) {
    printf("accept incoming call on fd %d\r\n", fd);
    write(fd, buff1, 3);
    write(fd, &end, 1);
  }
  return 0;
}


x86 assembly (clang, -O0):
<phone_process>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    0xeb8(%rip),%eax
mov    %eax,-0x24(%rbp)
mov    0xeb3(%rip),%eax
mov    %eax,-0x28(%rbp)
mov    0xead(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xeaa(%rip),%rax
mov    %rax,-0x3a(%rbp)
mov    0xea7(%rip),%ax
mov    %ax,-0x32(%rbp)
movb   $0xd,-0x3b(%rbp)
movb   $0x3b,-0x3c(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    11f9 <phone_process+0xc9>
mov    -0x10(%rbp),%rsi
mov    -0x4(%rbp),%edx
lea    0xe85(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x4(%rbp),%edi
lea    -0x24(%rbp),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x4(%rbp),%edi
lea    -0x3c(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x4(%rbp),%edi
lea    -0x3b(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    12c9 <phone_process+0x199>
cmpl   $0x1,-0x8(%rbp)
jne    123b <phone_process+0x10b>
mov    -0x4(%rbp),%esi
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x4(%rbp),%edi
lea    -0x3a(%rbp),%rsi
mov    $0x9,%edx
call   1030 <write@plt>
mov    -0x4(%rbp),%edi
lea    -0x3b(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    12c4 <phone_process+0x194>
cmpl   $0x2,-0x8(%rbp)
jne    127d <phone_process+0x14d>
mov    -0x4(%rbp),%esi
lea    0xe1e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x7,%edx
call   1030 <write@plt>
mov    -0x4(%rbp),%edi
lea    -0x3b(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    12bf <phone_process+0x18f>
cmpl   $0x3,-0x8(%rbp)
jne    12ba <phone_process+0x18a>
mov    -0x4(%rbp),%esi
lea    0xdfc(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x4(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x3,%edx
call   1030 <write@plt>
mov    -0x4(%rbp),%edi
lea    -0x3b(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    12bf <phone_process+0x18f>
jmp    12c4 <phone_process+0x194>
jmp    12c9 <phone_process+0x199>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<phone_process>:
push   %r14
push   %rbx
sub    $0x28,%rsp
movl   $0x445441,0xc(%rsp)
movl   $0x415441,0x8(%rsp)
movabs $0x505548432b5441,%rax
mov    %rax,0x20(%rsp)
movabs $0x3d50494c432b5441,%rax
49 50 3d
mov    %rax,0x10(%rsp)
movw   $0x31,0x18(%rsp)
movb   $0xd,0x7(%rsp)
movb   $0x3b,0x6(%rsp)
cmp    $0x3,%esi
ja     124a <phone_process+0x11a>
mov    %edi,%ebx
mov    %esi,%eax
lea    0xe76(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    %rdx,%r14
lea    0xe7d(%rip),%rdi
mov    %rdx,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
mov    $0x3,%edx
call   1030 <write@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
lea    0x6(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
jmp    1234 <phone_process+0x104>
lea    0xe5c(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x9,%edx
jmp    1234 <phone_process+0x104>
lea    0xe6c(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x20(%rsp),%rsi
mov    %ebx,%edi
mov    $0x7,%edx
jmp    1234 <phone_process+0x104>
lea    0xe6e(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x3,%edx
call   1030 <write@plt>
lea    0x7(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1030 <write@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@__const.phone_process.buff0 = [4 x i8] c"ATD\00"
@__const.phone_process.buff1 = [4 x i8] c"ATA\00"
@__const.phone_process.buff2 = [8 x i8] c"AT+CHUP\00"
@__const.phone_process.buff3 = [10 x i8] c"AT+CLIP=1\00"
@.str = [37 x i8] c"call Phone %s through serial fd %d\0D\0A\00"
@.str.1 = [46 x i8] c"open incoming phone number display on fd %d\0D\0A\00"
@.str.2 = [32 x i8] c"reject incoming call on fd %d\0D\0A\00"
@.str.3 = [32 x i8] c"accept incoming call on fd %d\0D\0A\00"
define i32 @phone_process(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [4 x i8]
%12 = alloca [4 x i8]
%13 = alloca [8 x i8]
%14 = alloca [10 x i8]
%15 = alloca i8
%16 = alloca i8
store i32 %0, i32* %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%17 = bitcast [4 x i8]* %11 to i8*
%18 = bitcast [4 x i8]* %12 to i8*
%19 = bitcast [8 x i8]* %13 to i8*
%20 = bitcast [10 x i8]* %14 to i8*
store i8 13, i8* %15
store i8 59, i8* %16
%21 = load i32, i32* %5
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %39
23: ; preds = %3
%24 = load i8*, i8** %6
%25 = load i32, i32* %4
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %24, i32 %25)
%27 = load i32, i32* %4
%28 = getelementptr inbounds [4 x i8], [4 x i8]* %11, i64 0, i64 0
%29 = call i32 @write(i32 %27, i8* %28, i32 3)
%30 = load i32, i32* %4
%31 = load i8*, i8** %6
%32 = load i8*, i8** %6
%33 = call i32 @strlen(i8* %32)
%34 = call i32 @write(i32 %30, i8* %31, i32 %33)
%35 = load i32, i32* %4
%36 = call i32 @write(i32 %35, i8* %16, i32 1)
%37 = load i32, i32* %4
%38 = call i32 @write(i32 %37, i8* %15, i32 1)
br label %75
39: ; preds = %3
%40 = load i32, i32* %5
%41 = icmp eq i32 %40, 1
br i1 %41, label %42, label %50
42: ; preds = %39
%43 = load i32, i32* %4
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i32 %43)
%45 = load i32, i32* %4
%46 = getelementptr inbounds [10 x i8], [10 x i8]* %14, i64 0, i64 0
%47 = call i32 @write(i32 %45, i8* %46, i32 9)
%48 = load i32, i32* %4
%49 = call i32 @write(i32 %48, i8* %15, i32 1)
br label %74
50: ; preds = %39
%51 = load i32, i32* %5
%52 = icmp eq i32 %51, 2
br i1 %52, label %53, label %61
53: ; preds = %50
%54 = load i32, i32* %4
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %54)
%56 = load i32, i32* %4
%57 = getelementptr inbounds [8 x i8], [8 x i8]* %13, i64 0, i64 0
%58 = call i32 @write(i32 %56, i8* %57, i32 7)
%59 = load i32, i32* %4
%60 = call i32 @write(i32 %59, i8* %15, i32 1)
br label %73
61: ; preds = %50
%62 = load i32, i32* %5
%63 = icmp eq i32 %62, 3
br i1 %63, label %64, label %72
64: ; preds = %61
%65 = load i32, i32* %4
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %65)
%67 = load i32, i32* %4
%68 = getelementptr inbounds [4 x i8], [4 x i8]* %12, i64 0, i64 0
%69 = call i32 @write(i32 %67, i8* %68, i32 3)
%70 = load i32, i32* %4
%71 = call i32 @write(i32 %70, i8* %15, i32 1)
br label %72
72: ; preds = %64, %61
br label %73
73: ; preds = %72, %53
br label %74
74: ; preds = %73, %42
br label %75
75: ; preds = %74, %23
ret i32 0
}

LLVM IR (-O2):
@__const.phone_process.buff3 = [10 x i8] c"AT+CLIP=1\00"
@.str = [37 x i8] c"call Phone %s through serial fd %d\0D\0A\00"
@.str.1 = [46 x i8] c"open incoming phone number display on fd %d\0D\0A\00"
@.str.2 = [32 x i8] c"reject incoming call on fd %d\0D\0A\00"
@.str.3 = [32 x i8] c"accept incoming call on fd %d\0D\0A\00"
define i32 @phone_process(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca [10 x i8]
%8 = alloca i8
%9 = alloca i8
%10 = bitcast i32* %4 to i8*
store i32 4478017, i32* %4
%11 = bitcast i32* %5 to i8*
store i32 4281409, i32* %5
%12 = bitcast i64* %6 to i8*
store i64 22611766989771841, i64* %6
%13 = getelementptr inbounds [10 x i8], [10 x i8]* %7, i64 0, i64 0
store i8 13, i8* %8
store i8 59, i8* %9
switch i32 %1, label %31 [
i32 0, label %14
i32 1, label %20
i32 2, label %23
i32 3, label %26
]
14: ; preds = %3
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8* %2, i32 %0)
%16 = call i32 @write(i32 %0, i8* %10, i32 3)
%17 = tail call i32 @strlen(i8* %2)
%18 = tail call i32 @write(i32 %0, i8* %2, i32 %17)
%19 = call i32 @write(i32 %0, i8* %9, i32 1)
br label %29
20: ; preds = %3
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%22 = call i32 @write(i32 %0, i8* %13, i32 9)
br label %29
23: ; preds = %3
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %0)
%25 = call i32 @write(i32 %0, i8* %12, i32 7)
br label %29
26: ; preds = %3
%27 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %0)
%28 = call i32 @write(i32 %0, i8* %11, i32 3)
br label %29
29: ; preds = %14, %23, %26, %20
%30 = call i32 @write(i32 %0, i8* %8, i32 1)
br label %31
31: ; preds = %29, %3
ret i32 0
}