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

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ writestr(int, char *);
int main(int argc, char **argv) {
  int fd;
  char buf[100];
  int len;
  if (argc != 2) {
    fprintf(stderr, "Usage: %s device\n", argv[0]);
    return 1;
  }
  fd = open(argv[1], O_RDWR);
  if (fd < 0) {
    perror("open:");
    return 1;
  }
  writestr(fd, "Hello world!\n");
  writestr(fd, "What is your name?\n");
  len = read(fd, buf, sizeof(buf) - 1);
  assert(len >= 0);
  buf[len] = 0;
  printf("Got input: %s", buf);
  writestr(fd, "I ♥ ");
  writestr(fd, buf);
  writestr(fd, "a\t tab and a\n   line break in one write\n");
  len = read(fd, buf, sizeof(buf) - 1);
  assert(len >= 0);
  buf[len] = 0;
  printf("Got input: %s", buf);
  writestr(fd, "bye!\n");
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11cb <main+0x4b>
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe46(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12fa <main+0x17a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1206 <main+0x86>
lea    0xe1d(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12fa <main+0x17a>
mov    -0x14(%rbp),%edi
lea    0xe08(%rip),%rsi
call   1080 <writestr@plt>
mov    -0x14(%rbp),%edi
lea    0xe07(%rip),%rsi
call   1080 <writestr@plt>
mov    -0x14(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x63,%edx
call   1050 <read@plt>
mov    %eax,-0x84(%rbp)
cmpl   $0x0,-0x84(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
movslq -0x84(%rbp),%rax
movb   $0x0,-0x80(%rbp,%rax,1)
lea    -0x80(%rbp),%rsi
lea    0xdd4(%rip),%rdi
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
lea    0xdd3(%rip),%rsi
call   1080 <writestr@plt>
mov    -0x14(%rbp),%edi
lea    -0x80(%rbp),%rsi
call   1080 <writestr@plt>
mov    -0x14(%rbp),%edi
lea    0xdbf(%rip),%rsi
call   1080 <writestr@plt>
mov    -0x14(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x63,%edx
call   1050 <read@plt>
mov    %eax,-0x84(%rbp)
cmpl   $0x0,-0x84(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
movslq -0x84(%rbp),%rax
movb   $0x0,-0x80(%rbp,%rax,1)
lea    -0x80(%rbp),%rsi
lea    0xd63(%rip),%rdi
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
lea    0xd92(%rip),%rsi
call   1080 <writestr@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x68,%rsp
cmp    $0x2,%edi
jne    1272 <main+0xf2>
mov    0x8(%rsi),%rdi
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
test   %eax,%eax
js     129a <main+0x11a>
mov    %eax,%ebx
lea    0xe62(%rip),%rsi
mov    %eax,%edi
call   1080 <writestr@plt>
lea    0xe62(%rip),%rsi
mov    %ebx,%edi
call   1080 <writestr@plt>
mov    %rsp,%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x63,%edx
call   1050 <read@plt>
mov    %eax,%ebp
mov    %eax,%edi
not    %edi
shr    $0x1f,%edi
call   1060 <assert@plt>
movslq %ebp,%rax
movb   $0x0,(%rsp,%rax,1)
lea    0xe41(%rip),%r15
mov    %r15,%rdi
mov    %r14,%rsi
call   1030 <printf@plt>
lea    0xe3d(%rip),%rsi
mov    %ebx,%edi
call   1080 <writestr@plt>
mov    %ebx,%edi
mov    %r14,%rsi
call   1080 <writestr@plt>
lea    0xe2c(%rip),%rsi
mov    %ebx,%edi
call   1080 <writestr@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x63,%edx
call   1050 <read@plt>
mov    %eax,%ebp
mov    %eax,%edi
not    %edi
shr    $0x1f,%edi
call   1060 <assert@plt>
movslq %ebp,%rax
movb   $0x0,(%rsp,%rax,1)
mov    %r15,%rdi
mov    %r14,%rsi
call   1030 <printf@plt>
lea    0xe18(%rip),%rsi
mov    %ebx,%edi
call   1080 <writestr@plt>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
jmp    128f <main+0x10f>
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xd7b(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd71(%rip),%rdi
call   10a0 <perror@plt>
jmp    128a <main+0x10a>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [18 x i8] c"Usage: %s device\0A\00"
@O_RDWR = global i32 0
@.str.1 = [6 x i8] c"open:\00"
@.str.2 = [14 x i8] c"Hello world!\0A\00"
@.str.3 = [20 x i8] c"What is your name?\0A\00"
@.str.4 = [14 x i8] c"Got input: %s\00"
@.str.5 = [7 x i8] c"I \E2\99\A5 \00"
@.str.6 = [41 x i8] c"a\09 tab and a\0A line break in one write\0A\00"
@.str.7 = [6 x i8] c"bye!\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [100 x i8]
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %17
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 1, i32* %3
br label %67
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = load i32, i32* @O_RDWR
%22 = call i32 @open(i8* %20, i32 %21)
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %17
%26 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %67
27: ; preds = %17
%28 = load i32, i32* %6
%29 = call i32 @writestr(i32 %28, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%30 = load i32, i32* %6
%31 = call i32 @writestr(i32 %30, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%32 = load i32, i32* %6
%33 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%34 = call i32 @read(i32 %32, i8* %33, i32 99)
store i32 %34, i32* %8
%35 = load i32, i32* %8
%36 = icmp sge i32 %35, 0
%37 = zext i1 %36 to i32
%38 = call i32 @assert(i32 %37)
%39 = load i32, i32* %8
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 %40
store i8 0, i8* %41
%42 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%43 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i8* %42)
%44 = load i32, i32* %6
%45 = call i32 @writestr(i32 %44, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%46 = load i32, i32* %6
%47 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%48 = call i32 @writestr(i32 %46, i8* %47)
%49 = load i32, i32* %6
%50 = call i32 @writestr(i32 %49, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.6, i64 0, i64 0))
%51 = load i32, i32* %6
%52 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%53 = call i32 @read(i32 %51, i8* %52, i32 99)
store i32 %53, i32* %8
%54 = load i32, i32* %8
%55 = icmp sge i32 %54, 0
%56 = zext i1 %55 to i32
%57 = call i32 @assert(i32 %56)
%58 = load i32, i32* %8
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 %59
store i8 0, i8* %60
%61 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%62 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i8* %61)
%63 = load i32, i32* %6
%64 = call i32 @writestr(i32 %63, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
%65 = load i32, i32* %6
%66 = call i32 @close(i32 %65)
store i32 0, i32* %3
br label %67
67: ; preds = %27, %25, %11
%68 = load i32, i32* %3
ret i32 %68
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [18 x i8] c"Usage: %s device\0A\00"
@O_RDWR = global i32 0
@.str.1 = [6 x i8] c"open:\00"
@.str.2 = [14 x i8] c"Hello world!\0A\00"
@.str.3 = [20 x i8] c"What is your name?\0A\00"
@.str.4 = [14 x i8] c"Got input: %s\00"
@.str.5 = [7 x i8] c"I \E2\99\A5 \00"
@.str.6 = [41 x i8] c"a\09 tab and a\0A line break in one write\0A\00"
@.str.7 = [6 x i8] c"bye!\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [100 x i8]
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%5 = icmp eq i32 %0, 2
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %8)
br label %40
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_RDWR
%14 = tail call i32 @open(i8* %12, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %10
%17 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %40
18: ; preds = %10
%19 = tail call i32 @writestr(i32 %14, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%20 = tail call i32 @writestr(i32 %14, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%21 = call i32 @read(i32 %14, i8* %4, i32 99)
%22 = xor i32 %21, -1
%23 = lshr i32 %22, 31
%24 = tail call i32 @assert(i32 %23)
%25 = sext i32 %21 to i64
%26 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 %25
store i8 0, i8* %26
%27 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i8* %4)
%28 = call i32 @writestr(i32 %14, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%29 = call i32 @writestr(i32 %14, i8* %4)
%30 = call i32 @writestr(i32 %14, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.6, i64 0, i64 0))
%31 = call i32 @read(i32 %14, i8* %4, i32 99)
%32 = xor i32 %31, -1
%33 = lshr i32 %32, 31
%34 = call i32 @assert(i32 %33)
%35 = sext i32 %31 to i64
%36 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 %35
store i8 0, i8* %36
%37 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i8* %4)
%38 = call i32 @writestr(i32 %14, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
%39 = call i32 @close(i32 %14)
br label %40
40: ; preds = %18, %16, %6
%41 = phi i32 [ 1, %6 ], [ 1, %16 ], [ 0, %18 ]
ret i32 %41
}