#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*/ O_RDWR;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int err = 0;
  int fd = 0;
  char buf[20];
  int i = 0;
  if (argc != 2) {
    printf("please input device\n");
    return -1;
  }
  fd = open(argv[1], O_RDWR);
  if (fd < 0) {
    printf("open device error!!\n");
    return -1;
  }
  printf("open success!!\n");
  for (i = 0; i < 20; i++) {
    buf[i] = i;
  }
  err = write(fd, buf, 20);
  printf("write %d data\n", err);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x34(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     118f <main+0x4f>
lea    0xe84(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123a <main+0xfa>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11cc <main+0x8c>
lea    0xe5c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    123a <main+0xfa>
lea    0xe57(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x34(%rbp)
cmpl   $0x14,-0x34(%rbp)
jge    1206 <main+0xc6>
mov    -0x34(%rbp),%eax
mov    %al,%cl
movslq -0x34(%rbp),%rax
mov    %cl,-0x30(%rbp,%rax,1)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11e1 <main+0xa1>
mov    -0x18(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x14,%edx
call   1030 <write@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe16(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jne    11bc <main+0x6c>
mov    0x8(%rsi),%rdi
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     11c5 <main+0x75>
mov    %eax,%ebx
lea    0xea5(%rip),%rdi
call   1030 <puts@plt>
movaps 0xe7a(%rip),%xmm0
movaps %xmm0,(%rsp)
movl   $0x13121110,0x10(%rsp)
13
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x14,%edx
call   1040 <write@plt>
lea    0xe68(%rip),%rdi
xor    %ebp,%ebp
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
jmp    11d6 <main+0x86>
lea    0xe7f(%rip),%rdi
jmp    11cc <main+0x7c>
lea    0xe62(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [21 x i8] c"please input device\0A\00"
@O_RDWR = global i32 0
@.str.1 = [21 x i8] c"open device error!!\0A\00"
@.str.2 = [16 x i8] c"open success!!\0A\00"
@.str.3 = [15 x i8] c"write %d data\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [20 x i8]
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 0, i32* %7
store i32 0, i32* %9
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 2
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %46
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = load i32, i32* @O_RDWR
%19 = call i32 @open(i8* %17, i32 %18)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %14
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %46
24: ; preds = %14
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %9
br label %26
26: ; preds = %35, %24
%27 = load i32, i32* %9
%28 = icmp slt i32 %27, 20
br i1 %28, label %29, label %38
29: ; preds = %26
%30 = load i32, i32* %9
%31 = trunc i32 %30 to i8
%32 = load i32, i32* %9
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 %33
store i8 %31, i8* %34
br label %35
35: ; preds = %29
%36 = load i32, i32* %9
%37 = add nsw i32 %36, 1
store i32 %37, i32* %9
br label %26
38: ; preds = %26
%39 = load i32, i32* %7
%40 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%41 = call i32 @write(i32 %39, i8* %40, i32 20)
store i32 %41, i32* %6
%42 = load i32, i32* %6
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %42)
%44 = load i32, i32* %7
%45 = call i32 @close(i32 %44)
store i32 0, i32* %3
br label %46
46: ; preds = %38, %22, %12
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@.str.3 = [15 x i8] c"write %d data\0A\00"
@str = [15 x i8] c"open success!!\00"
@str.4 = [20 x i8] c"open device error!!\00"
@str.5 = [20 x i8] c"please input device\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [20 x i8]
%4 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 2
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.5, i64 0, i64 0))
br label %26
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDWR
%12 = tail call i32 @open(i8* %10, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %8
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.4, i64 0, i64 0))
br label %26
16: ; preds = %8
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%18 = bitcast [20 x i8]* %3 to <16 x i8>*
store <16 x i8> <i8 0, i8 1, i8 2, i8 3, i8 4, i8 5, i8 6, i8 7, i8 8, i8 9, i8 10, i8 11, i8 12, i8 13, i8 14, i8 15>, <16 x i8>* %18
%19 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 16
store i8 16, i8* %19
%20 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 17
store i8 17, i8* %20
%21 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 18
store i8 18, i8* %21
%22 = getelementptr inbounds [20 x i8], [20 x i8]* %3, i64 0, i64 19
store i8 19, i8* %22
%23 = call i32 @write(i32 %12, i8* %4, i32 20)
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %23)
%25 = tail call i32 @close(i32 %12)
br label %26
26: ; preds = %16, %14, %6
%27 = phi i32 [ -1, %6 ], [ -1, %14 ], [ 0, %16 ]
ret i32 %27
}