#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 O_APPEND;
int O_RDWR;
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int truncate(char *, int);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd;
  int len;
  int rest;
  int i;
  char buf[32];
  if (argc != 3) {
    len = 32;
    rest = 0;
  } else {
    len = atoi(argv[1]);
    rest = atoi(argv[2]);
  }
  if (truncate("test.txt", len) == -1) {
    perror("fail to truncate");
    exit(1);
  }
  fd = open("test.txt", O_RDWR | O_APPEND);
  if (fd == -1) {
    perror("fail to open");
    exit(1);
  }
  i = 0;
  while (i < rest) {
    buf[i] = '0';
    i++;
  }
  if (write(fd, buf, rest) == -1) {
    perror("fail to write");
    exit(1);
  }
  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)
cmpl   $0x3,-0x8(%rbp)
je     11a3 <main+0x33>
movl   $0x20,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
jmp    11c3 <main+0x53>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%esi
lea    0xe33(%rip),%rdi
call   1050 <truncate@plt>
cmp    $0xffffffff,%eax
jne    11f1 <main+0x81>
lea    0xe27(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
mov    0x2dcf(%rip),%rax
or     (%rax),%esi
lea    0xdf6(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1232 <main+0xc2>
lea    0xdf7(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    125c <main+0xec>
movslq -0x20(%rbp),%rax
movb   $0x30,-0x40(%rbp,%rax,1)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1239 <main+0xc9>
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    128a <main+0x11a>
lea    0xdac(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
cmp    $0x3,%edi
jne    11a6 <main+0x26>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
call   1090 <atoi@plt>
mov    %eax,%ebp
mov    0x10(%rbx),%rdi
call   1090 <atoi@plt>
mov    %eax,%ebx
jmp    11ad <main+0x2d>
xor    %ebx,%ebx
mov    $0x20,%ebp
lea    0xe4c(%rip),%rdi
mov    %ebp,%esi
call   1060 <truncate@plt>
cmp    $0xffffffff,%eax
je     1219 <main+0x99>
mov    0x2e21(%rip),%rax
mov    0x2e02(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe27(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1222 <main+0xa2>
mov    %eax,%ebp
test   %ebx,%ebx
jle    11f8 <main+0x78>
mov    %ebx,%edx
mov    %rsp,%rdi
mov    $0x30,%esi
call   1040 <memset@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    %ebx,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     122b <main+0xab>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xde9(%rip),%rdi
jmp    1232 <main+0xb2>
lea    0xdf1(%rip),%rdi
jmp    1232 <main+0xb2>
lea    0xdf5(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [17 x i8] c"fail to truncate\00"
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@.str.2 = [13 x i8] c"fail to open\00"
@.str.3 = [14 x i8] c"fail to write\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 i32
%9 = alloca i32
%10 = alloca [32 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 3
br i1 %12, label %13, label %14
13: ; preds = %2
store i32 32, i32* %7
store i32 0, i32* %8
br label %23
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @atoi(i8* %17)
store i32 %18, i32* %7
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 2
%21 = load i8*, i8** %20
%22 = call i32 @atoi(i8* %21)
store i32 %22, i32* %8
br label %23
23: ; preds = %14, %13
%24 = load i32, i32* %7
%25 = call i32 @truncate(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %23
%31 = load i32, i32* @O_RDWR
%32 = load i32, i32* @O_APPEND
%33 = or i32 %31, %32
%34 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %33)
store i32 %34, i32* %6
%35 = load i32, i32* %6
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %40
37: ; preds = %30
%38 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %30
store i32 0, i32* %9
br label %41
41: ; preds = %45, %40
%42 = load i32, i32* %9
%43 = load i32, i32* %8
%44 = icmp slt i32 %42, %43
br i1 %44, label %45, label %51
45: ; preds = %41
%46 = load i32, i32* %9
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 %47
store i8 48, i8* %48
%49 = load i32, i32* %9
%50 = add nsw i32 %49, 1
store i32 %50, i32* %9
br label %41
51: ; preds = %41
%52 = load i32, i32* %6
%53 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%54 = load i32, i32* %8
%55 = call i32 @write(i32 %52, i8* %53, i32 %54)
%56 = icmp eq i32 %55, -1
br i1 %56, label %57, label %60
57: ; preds = %51
%58 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%59 = call i32 @exit(i32 1)
unreachable
60: ; preds = %51
%61 = load i32, i32* %6
%62 = call i32 @close(i32 %61)
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.txt\00"
@.str.1 = [17 x i8] c"fail to truncate\00"
@O_RDWR = global i32 0
@O_APPEND = global i32 0
@.str.2 = [13 x i8] c"fail to open\00"
@.str.3 = [14 x i8] c"fail to write\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [32 x i8]
%4 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%5 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%6 = icmp eq i32 %0, 3
br i1 %6, label %7, label %14
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32 @atoi(i8* %9)
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
%13 = tail call i32 @atoi(i8* %12)
br label %14
14: ; preds = %2, %7
%15 = phi i32 [ %10, %7 ], [ 32, %2 ]
%16 = phi i32 [ %13, %7 ], [ 0, %2 ]
%17 = tail call i32 @truncate(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %15)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %14
%23 = load i32, i32* @O_RDWR
%24 = load i32, i32* @O_APPEND
%25 = or i32 %24, %23
%26 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %32, label %28
28: ; preds = %22
%29 = icmp sgt i32 %16, 0
br i1 %29, label %30, label %35
30: ; preds = %28
%31 = zext i32 %16 to i64
br label %35
32: ; preds = %22
%33 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%34 = tail call i32 @exit(i32 1)
unreachable
35: ; preds = %30, %28
%36 = call i32 @write(i32 %26, i8* %5, i32 %16)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%40 = tail call i32 @exit(i32 1)
unreachable
41: ; preds = %35
%42 = tail call i32 @close(i32 %26)
ret i32 0
}