#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*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ strcmp(char *, char *);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd;
  int num_bytes;
  char *buf;
  if (argc < 2 || strcmp(argv[1], "--help") == 0) {
    printf("Usage: %s num_bytes\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  num_bytes = atoi(argv[1]);
  if (num_bytes <= 0) {
    printf("num bytes should be greater than 0\n");
    exit(EXIT_FAILURE);
  }
  fd = open("foo.txt", O_WRONLY | O_CREAT | O_TRUNC,
            S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR);
  if (fd != -1) {
    buf = (char *)malloc(sizeof(char) * num_bytes);
    memset(buf, 0, num_bytes);
    if (write(fd, buf, num_bytes) == -1) {
      perror("Error on write, ");
      exit(EXIT_FAILURE);
    }
  } else {
    perror("Error on open, ");
    exit(EXIT_FAILURE);
  }
  close(fd);
  free(buf);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     11ee <main+0x3e>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe21(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%rax
jne    1211 <main+0x61>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe0b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jg     1247 <main+0x97>
lea    0xdea(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    0x2d4a(%rip),%rax
mov    (%rax),%esi
mov    0x2da1(%rip),%rax
or     (%rax),%esi
mov    0x2d90(%rip),%rax
or     (%rax),%esi
mov    0x2d47(%rip),%rax
mov    (%rax),%edx
mov    0x2d56(%rip),%rax
or     (%rax),%edx
mov    0x2d45(%rip),%rax
or     (%rax),%edx
mov    0x2d54(%rip),%rax
or     (%rax),%edx
lea    0xdb3(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     12f7 <main+0x147>
movslq -0x18(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
jne    12f2 <main+0x142>
lea    0xd69(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2ccd(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
jmp    1311 <main+0x161>
lea    0xd5b(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2cae(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    0x2c7f(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
cmp    $0x2,%edi
jl     11e1 <main+0x21>
mov    0x8(%rbx),%rdi
lea    0xe29(%rip),%rsi
call   1090 <strcmp@plt>
test   %rax,%rax
jne    1200 <main+0x40>
mov    (%rbx),%rsi
lea    0xe1c(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    0x8(%rbx),%rdi
call   10d0 <atoi@plt>
test   %eax,%eax
jg     121b <main+0x5b>
lea    0xe31(%rip),%rdi
call   1040 <puts@plt>
jmp    11f2 <main+0x32>
mov    %eax,%ebx
mov    0x2d74(%rip),%rax
mov    0x2dcd(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dba(%rip),%rax
or     (%rax),%esi
mov    0x2d71(%rip),%rax
mov    0x2d82(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d6f(%rip),%rax
or     (%rax),%edx
mov    0x2d7e(%rip),%rax
or     (%rax),%edx
lea    0xdb9(%rip),%rdi
call   10b0 <open@plt>
cmp    $0xffffffff,%eax
jne    1276 <main+0xb6>
lea    0xdc1(%rip),%rdi
jmp    12a6 <main+0xe6>
mov    %eax,%ebp
mov    %ebx,%edi
call   10a0 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebx,%edx
call   1070 <memset@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %ebx,%edx
call   1050 <write@plt>
cmp    $0xffffffff,%eax
jne    12b0 <main+0xf0>
lea    0xd7e(%rip),%rdi
call   10c0 <perror@plt>
jmp    11f2 <main+0x32>
mov    %ebp,%edi
call   1080 <close@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    0x2ce2(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"--help\00"
@.str.1 = [21 x i8] c"Usage: %s num_bytes\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [36 x i8] c"num bytes should be greater than 0\0A\00"
@.str.3 = [8 x i8] c"foo.txt\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@S_IWUSR = global i32 0
@.str.4 = [17 x i8] c"Error on write, \00"
@.str.5 = [16 x i8] c"Error on open, \00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 2
br i1 %10, label %17, label %11
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = call i64 @strcmp(i8* %14, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %11, %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %11
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
%28 = call i32 @atoi(i8* %27)
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = icmp sle i32 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %24
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0))
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %24
%36 = load i32, i32* @O_WRONLY
%37 = load i32, i32* @O_CREAT
%38 = or i32 %36, %37
%39 = load i32, i32* @O_TRUNC
%40 = or i32 %38, %39
%41 = load i32, i32* @S_IRUSR
%42 = load i32, i32* @S_IRGRP
%43 = or i32 %41, %42
%44 = load i32, i32* @S_IROTH
%45 = or i32 %43, %44
%46 = load i32, i32* @S_IWUSR
%47 = or i32 %45, %46
%48 = call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %40, i32 %47)
store i32 %48, i32* %6
%49 = load i32, i32* %6
%50 = icmp ne i32 %49, -1
br i1 %50, label %51, label %71
51: ; preds = %35
%52 = load i32, i32* %7
%53 = sext i32 %52 to i64
%54 = mul i64 1, %53
%55 = trunc i64 %54 to i32
%56 = call i64 @malloc(i32 %55)
%57 = inttoptr i64 %56 to i8*
store i8* %57, i8** %8
%58 = load i8*, i8** %8
%59 = load i32, i32* %7
%60 = call i32 @memset(i8* %58, i32 0, i32 %59)
%61 = load i32, i32* %6
%62 = load i8*, i8** %8
%63 = load i32, i32* %7
%64 = call i32 @write(i32 %61, i8* %62, i32 %63)
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %70
66: ; preds = %51
%67 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%68 = load i32, i32* @EXIT_FAILURE
%69 = call i32 @exit(i32 %68)
unreachable
70: ; preds = %51
br label %75
71: ; preds = %35
%72 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
%73 = load i32, i32* @EXIT_FAILURE
%74 = call i32 @exit(i32 %73)
unreachable
75: ; preds = %70
%76 = load i32, i32* %6
%77 = call i32 @close(i32 %76)
%78 = load i8*, i8** %8
%79 = call i32 @free(i8* %78)
%80 = load i32, i32* @EXIT_SUCCESS
%81 = call i32 @exit(i32 %80)
unreachable
}

LLVM IR (-O2):
@.str = [7 x i8] c"--help\00"
@.str.1 = [21 x i8] c"Usage: %s num_bytes\0A\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [8 x i8] c"foo.txt\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@S_IWUSR = global i32 0
@.str.4 = [17 x i8] c"Error on write, \00"
@.str.5 = [16 x i8] c"Error on open, \00"
@EXIT_SUCCESS = global i32 0
@str = [35 x i8] c"num bytes should be greater than 0\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %9, label %4
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i64 @strcmp(i8* %6, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %14
9: ; preds = %4, %2
%10 = load i8*, i8** %1
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %4
%15 = load i8*, i8** %5
%16 = tail call i32 @atoi(i8* %15)
%17 = icmp slt i32 %16, 1
br i1 %17, label %18, label %22
18: ; preds = %14
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
%20 = load i32, i32* @EXIT_FAILURE
%21 = tail call i32 @exit(i32 %20)
unreachable
22: ; preds = %14
%23 = load i32, i32* @O_WRONLY
%24 = load i32, i32* @O_CREAT
%25 = or i32 %24, %23
%26 = load i32, i32* @O_TRUNC
%27 = or i32 %25, %26
%28 = load i32, i32* @S_IRUSR
%29 = load i32, i32* @S_IRGRP
%30 = or i32 %29, %28
%31 = load i32, i32* @S_IROTH
%32 = or i32 %30, %31
%33 = load i32, i32* @S_IWUSR
%34 = or i32 %32, %33
%35 = tail call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32 %27, i32 %34)
%36 = icmp eq i32 %35, -1
br i1 %36, label %47, label %37
37: ; preds = %22
%38 = tail call i64 @malloc(i32 %16)
%39 = inttoptr i64 %38 to i8*
%40 = tail call i32 @memset(i8* %39, i32 0, i32 %16)
%41 = tail call i32 @write(i32 %35, i8* %39, i32 %16)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %51
43: ; preds = %37
%44 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%45 = load i32, i32* @EXIT_FAILURE
%46 = tail call i32 @exit(i32 %45)
unreachable
47: ; preds = %22
%48 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0))
%49 = load i32, i32* @EXIT_FAILURE
%50 = tail call i32 @exit(i32 %49)
unreachable
51: ; preds = %37
%52 = tail call i32 @close(i32 %35)
%53 = tail call i32 @free(i8* %39)
%54 = load i32, i32* @EXIT_SUCCESS
%55 = tail call i32 @exit(i32 %54)
unreachable
}