#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_WRONLY;
int /*<<< orphan*/ close(int);
char *getenv(char *);
char *malloc(size_t);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char **argv) {
  char *home = getenv("HOME");
  char *path = "/tuberia";
  size_t len = strlen(home) + strlen(path) + 1;
  char *fullpath = malloc(len);
  strcpy(fullpath, home);
  strcat(fullpath, path);
  int fd = open(fullpath, O_WRONLY);
  write(fd, argv[1], strlen(argv[1]));
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe6a(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x18(%rbp)
lea    0xe5f(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x48(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%eax
add    %ecx,%eax
add    $0x1,%eax
cltq
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   10a0 <strcat@plt>
mov    -0x30(%rbp),%rdi
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <strlen@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    -0x34(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
lea    0xe6b(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r13
mov    %rax,%rdi
call   1060 <strlen@plt>
mov    %eax,%r15d
lea    0xe56(%rip),%r12
mov    %r12,%rdi
call   1060 <strlen@plt>
add    %r15d,%eax
add    $0x1,%eax
movslq %eax,%rdi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r13,%rsi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   10a0 <strcat@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
mov    %eax,%ebp
mov    0x8(%r14),%rbx
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <write@plt>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"HOME\00"
@.str.1 = [9 x i8] c"/tuberia\00"
@O_WRONLY = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
%10 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %10, i8** %5
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8** %6
%11 = load i8*, i8** %5
%12 = call i32 @strlen(i8* %11)
%13 = load i8*, i8** %6
%14 = call i32 @strlen(i8* %13)
%15 = add nsw i32 %12, %14
%16 = add nsw i32 %15, 1
%17 = sext i32 %16 to i64
store i64 %17, i64* %7
%18 = load i64, i64* %7
%19 = call i8* @malloc(i64 %18)
store i8* %19, i8** %8
%20 = load i8*, i8** %8
%21 = load i8*, i8** %5
%22 = call i32 @strcpy(i8* %20, i8* %21)
%23 = load i8*, i8** %8
%24 = load i8*, i8** %6
%25 = call i32 @strcat(i8* %23, i8* %24)
%26 = load i8*, i8** %8
%27 = load i32, i32* @O_WRONLY
%28 = call i32 @open(i8* %26, i32 %27)
store i32 %28, i32* %9
%29 = load i32, i32* %9
%30 = load i8**, i8*** %4
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = load i8**, i8*** %4
%34 = getelementptr inbounds i8*, i8** %33, i64 1
%35 = load i8*, i8** %34
%36 = call i32 @strlen(i8* %35)
%37 = call i32 @write(i32 %29, i8* %32, i32 %36)
%38 = load i32, i32* %9
%39 = call i32 @close(i32 %38)
ret i32 0
}

LLVM IR (-O2):
@.str = [5 x i8] c"HOME\00"
@.str.1 = [9 x i8] c"/tuberia\00"
@O_WRONLY = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%4 = tail call i32 @strlen(i8* %3)
%5 = tail call i32 @strlen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%6 = add i32 %4, 1
%7 = add i32 %6, %5
%8 = sext i32 %7 to i64
%9 = tail call i8* @malloc(i64 %8)
%10 = tail call i32 @strcpy(i8* %9, i8* %3)
%11 = tail call i32 @strcat(i8* %9, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
%12 = load i32, i32* @O_WRONLY
%13 = tail call i32 @open(i8* %9, i32 %12)
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = tail call i32 @strlen(i8* %15)
%17 = tail call i32 @write(i32 %13, i8* %15, i32 %16)
%18 = tail call i32 @close(i32 %13)
ret i32 0
}