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

/* Variables and functions */
int /*<<< orphan*/ abort();
int /*<<< orphan*/ *connect_to_server();
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ free(char *);
size_t fwrite(char *, int, size_t, int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
size_t strlen(char *);
int printer_print(int *handle __attribute__((unused)), char *driver,
                  char *job_name, char *description, char *data) {
  size_t datasize = strlen(data);
  char *tmpfile_name = malloc(128);
  if (tmpfile_name == NULL) {
    perror("malloc");
    abort();
  }
  strcpy(tmpfile_name, "/tmp/print_job_");
  strcat(tmpfile_name, job_name);
  FILE *tmpfile = fopen(tmpfile_name, "w");
  if (tmpfile == NULL) {
    perror("fopen");
    free(tmpfile_name);
    abort();
  }
  size_t count = fwrite(data, 1, datasize, tmpfile);
  if (count != datasize) {
    perror("fwrite");
    fclose(tmpfile);
    free(tmpfile_name);
    abort();
  }
  if (fclose(tmpfile)) {
    perror("fclose");
    fclose(tmpfile);
    free(tmpfile_name);
    return -1;
  }
  FILE *conn = connect_to_server();
  fprintf(conn, "NEW\n");
  fprintf(conn, "NAME:%s\n", job_name);
  fprintf(conn, "FILE:%s\n", tmpfile_name);
  if (description != NULL) {
    fprintf(conn, "DESCRIPTION:%s\n", description);
  }
  fprintf(conn, "PRINTER:%s\n", driver);
  fprintf(conn, "PRINT\n");
  fprintf(conn, "EXIT\n");
  free(tmpfile_name);
  fclose(conn);
  return 0;
}


x86 assembly (clang, -O0):
<printer_print>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1080 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    $0x80,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1215 <printer_print+0x55>
lea    0xdf7(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x0,%al
call   1050 <abort@plt>
mov    -0x40(%rbp),%rdi
lea    0xde7(%rip),%rsi
call   1060 <strcpy@plt>
mov    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   10d0 <strcat@plt>
mov    -0x40(%rbp),%rdi
lea    0xdda(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    126d <printer_print+0xad>
lea    0xdc1(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x40(%rbp),%rdi
call   1040 <free@plt>
mov    $0x0,%al
call   1050 <abort@plt>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rdx
mov    -0x48(%rbp),%rcx
mov    $0x1,%esi
call   10e0 <fwrite@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
cmp    -0x38(%rbp),%rax
je     12ba <printer_print+0xfa>
lea    0xd83(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x48(%rbp),%rdi
call   1070 <fclose@plt>
mov    -0x40(%rbp),%rdi
call   1040 <free@plt>
mov    $0x0,%al
call   1050 <abort@plt>
mov    -0x48(%rbp),%rdi
call   1070 <fclose@plt>
cmp    $0x0,%rax
je     12f7 <printer_print+0x137>
lea    0xd52(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x48(%rbp),%rdi
call   1070 <fclose@plt>
mov    -0x40(%rbp),%rdi
call   1040 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b4 <printer_print+0x1f4>
mov    $0x0,%al
call   1030 <connect_to_server@plt>
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rdi
lea    0xd20(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x58(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xd0f(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x58(%rbp),%rdi
mov    -0x40(%rbp),%rdx
lea    0xd02(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
cmpq   $0x0,-0x28(%rbp)
je     1361 <printer_print+0x1a1>
mov    -0x58(%rbp),%rdi
mov    -0x28(%rbp),%rdx
lea    0xcea(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x58(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xce4(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x58(%rbp),%rdi
lea    0xcde(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x58(%rbp),%rdi
lea    0xcd3(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
mov    -0x40(%rbp),%rdi
call   1040 <free@plt>
mov    -0x58(%rbp),%rdi
call   1070 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<printer_print>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r12
mov    %rcx,%r15
mov    %rdx,%r13
mov    %rsi,(%rsp)
mov    %r8,%rdi
call   1080 <strlen@plt>
mov    %rax,%rbx
mov    $0x80,%edi
call   10a0 <malloc@plt>
test   %rax,%rax
je     134f <printer_print+0x18f>
mov    %rax,%r14
lea    0xe07(%rip),%rsi
mov    %rax,%rdi
call   1060 <strcpy@plt>
mov    %r14,%rdi
mov    %r13,%rsi
call   10d0 <strcat@plt>
lea    0xdfd(%rip),%rsi
mov    %r14,%rdi
call   10b0 <fopen@plt>
test   %rax,%rax
je     1362 <printer_print+0x1a2>
mov    %rax,%rbp
mov    %r12,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %rax,%rcx
call   10e0 <fwrite@plt>
cmp    %rbx,%rax
jne    1370 <printer_print+0x1b0>
mov    %rbp,%rdi
call   1070 <fclose@plt>
test   %rax,%rax
jne    132b <printer_print+0x16b>
xor    %r12d,%r12d
xor    %eax,%eax
call   1030 <connect_to_server@plt>
mov    %rax,%rbp
lea    0xdbe(%rip),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10e0 <fwrite@plt>
lea    0xdaa(%rip),%rsi
mov    %rbp,%rdi
mov    %r13,%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
lea    0xd9f(%rip),%rsi
mov    %rbp,%rdi
mov    %r14,%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
test   %r15,%r15
je     12c2 <printer_print+0x102>
lea    0xd8f(%rip),%rsi
mov    %rbp,%rdi
mov    %r15,%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
lea    0xd8b(%rip),%rsi
mov    %rbp,%rdi
mov    (%rsp),%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
lea    0xd82(%rip),%rdi
mov    $0x6,%esi
mov    $0x1,%edx
mov    %rbp,%rcx
call   10e0 <fwrite@plt>
lea    0xd70(%rip),%rdi
mov    $0x5,%esi
mov    $0x1,%edx
mov    %rbp,%rcx
call   10e0 <fwrite@plt>
mov    %r14,%rdi
call   1040 <free@plt>
mov    %rbp,%rdi
call   1070 <fclose@plt>
mov    %r12d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcf4(%rip),%rdi
call   10c0 <perror@plt>
mov    %rbp,%rdi
call   1070 <fclose@plt>
mov    %r14,%rdi
call   1040 <free@plt>
mov    $0xffffffff,%r12d
jmp    1319 <printer_print+0x159>
lea    0xcaa(%rip),%rdi
call   10c0 <perror@plt>
xor    %eax,%eax
call   1050 <abort@plt>
lea    0xcb0(%rip),%rdi
call   10c0 <perror@plt>
jmp    1384 <printer_print+0x1c4>
lea    0xca8(%rip),%rdi
call   10c0 <perror@plt>
mov    %rbp,%rdi
call   1070 <fclose@plt>
mov    %r14,%rdi
call   1040 <free@plt>
xor    %eax,%eax
call   1050 <abort@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"malloc\00"
@.str.1 = [16 x i8] c"/tmp/print_job_\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [6 x i8] c"fopen\00"
@.str.4 = [7 x i8] c"fwrite\00"
@.str.5 = [7 x i8] c"fclose\00"
@.str.6 = [5 x i8] c"NEW\0A\00"
@.str.7 = [9 x i8] c"NAME:%s\0A\00"
@.str.8 = [9 x i8] c"FILE:%s\0A\00"
@.str.9 = [16 x i8] c"DESCRIPTION:%s\0A\00"
@.str.10 = [12 x i8] c"PRINTER:%s\0A\00"
@.str.11 = [7 x i8] c"PRINT\0A\00"
@.str.12 = [6 x i8] c"EXIT\0A\00"
define i32 @printer_print(i32* %0, i8* %1, i8* %2, i8* %3, i8* %4) {
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i8*
%14 = alloca i32*
%15 = alloca i64
%16 = alloca i32*
store i32* %0, i32** %7
store i8* %1, i8** %8
store i8* %2, i8** %9
store i8* %3, i8** %10
store i8* %4, i8** %11
%17 = load i8*, i8** %11
%18 = call i64 @strlen(i8* %17)
store i64 %18, i64* %12
%19 = call i8* @malloc(i32 128)
store i8* %19, i8** %13
%20 = load i8*, i8** %13
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %25
22: ; preds = %5
%23 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%24 = call i32 (...) @abort()
unreachable
25: ; preds = %5
%26 = load i8*, i8** %13
%27 = call i32 @strcpy(i8* %26, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%28 = load i8*, i8** %13
%29 = load i8*, i8** %9
%30 = call i32 @strcat(i8* %28, i8* %29)
%31 = load i8*, i8** %13
%32 = call i32* @fopen(i8* %31, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %32, i32** %14
%33 = load i32*, i32** %14
%34 = icmp eq i32* %33, null
br i1 %34, label %35, label %40
35: ; preds = %25
%36 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%37 = load i8*, i8** %13
%38 = call i32 @free(i8* %37)
%39 = call i32 (...) @abort()
unreachable
40: ; preds = %25
%41 = load i8*, i8** %11
%42 = load i64, i64* %12
%43 = load i32*, i32** %14
%44 = call i64 @fwrite(i8* %41, i32 1, i64 %42, i32* %43)
store i64 %44, i64* %15
%45 = load i64, i64* %15
%46 = load i64, i64* %12
%47 = icmp ne i64 %45, %46
br i1 %47, label %48, label %55
48: ; preds = %40
%49 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%50 = load i32*, i32** %14
%51 = call i64 @fclose(i32* %50)
%52 = load i8*, i8** %13
%53 = call i32 @free(i8* %52)
%54 = call i32 (...) @abort()
unreachable
55: ; preds = %40
%56 = load i32*, i32** %14
%57 = call i64 @fclose(i32* %56)
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %65
59: ; preds = %55
%60 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%61 = load i32*, i32** %14
%62 = call i64 @fclose(i32* %61)
%63 = load i8*, i8** %13
%64 = call i32 @free(i8* %63)
store i32 -1, i32* %6
br label %93
65: ; preds = %55
%66 = call i32* (...) @connect_to_server()
store i32* %66, i32** %16
%67 = load i32*, i32** %16
%68 = call i32 (i32*, i8*, ...) @fprintf(i32* %67, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0))
%69 = load i32*, i32** %16
%70 = load i8*, i8** %9
%71 = call i32 (i32*, i8*, ...) @fprintf(i32* %69, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i8* %70)
%72 = load i32*, i32** %16
%73 = load i8*, i8** %13
%74 = call i32 (i32*, i8*, ...) @fprintf(i32* %72, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.8, i64 0, i64 0), i8* %73)
%75 = load i8*, i8** %10
%76 = icmp ne i8* %75, null
br i1 %76, label %77, label %81
77: ; preds = %65
%78 = load i32*, i32** %16
%79 = load i8*, i8** %10
%80 = call i32 (i32*, i8*, ...) @fprintf(i32* %78, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.9, i64 0, i64 0), i8* %79)
br label %81
81: ; preds = %77, %65
%82 = load i32*, i32** %16
%83 = load i8*, i8** %8
%84 = call i32 (i32*, i8*, ...) @fprintf(i32* %82, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.10, i64 0, i64 0), i8* %83)
%85 = load i32*, i32** %16
%86 = call i32 (i32*, i8*, ...) @fprintf(i32* %85, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.11, i64 0, i64 0))
%87 = load i32*, i32** %16
%88 = call i32 (i32*, i8*, ...) @fprintf(i32* %87, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.12, i64 0, i64 0))
%89 = load i8*, i8** %13
%90 = call i32 @free(i8* %89)
%91 = load i32*, i32** %16
%92 = call i64 @fclose(i32* %91)
store i32 0, i32* %6
br label %93
93: ; preds = %81, %59
%94 = load i32, i32* %6
ret i32 %94
}

LLVM IR (-O2):
@.str = [7 x i8] c"malloc\00"
@.str.1 = [16 x i8] c"/tmp/print_job_\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [6 x i8] c"fopen\00"
@.str.4 = [7 x i8] c"fwrite\00"
@.str.5 = [7 x i8] c"fclose\00"
@.str.6 = [5 x i8] c"NEW\0A\00"
@.str.7 = [9 x i8] c"NAME:%s\0A\00"
@.str.8 = [9 x i8] c"FILE:%s\0A\00"
@.str.9 = [16 x i8] c"DESCRIPTION:%s\0A\00"
@.str.10 = [12 x i8] c"PRINTER:%s\0A\00"
@.str.11 = [7 x i8] c"PRINT\0A\00"
@.str.12 = [6 x i8] c"EXIT\0A\00"
define i32 @printer_print(i32* nocapture readnone %0, i8* %1, i8* %2, i8* %3, i8* nocapture %4) {
%6 = tail call i64 @strlen(i8* dereferenceable(1) %4)
%7 = tail call dereferenceable_or_null(128) i8* @malloc(i32 128)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %12
9: ; preds = %5
%10 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%11 = tail call i32 (...) @abort()
unreachable
12: ; preds = %5
%13 = tail call i32 @strcpy(i8* %7, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
%14 = tail call i32 @strcat(i8* %7, i8* %2)
%15 = tail call i32* @fopen(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %21
17: ; preds = %12
%18 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%19 = tail call i32 @free(i8* %7)
%20 = tail call i32 (...) @abort()
unreachable
21: ; preds = %12
%22 = tail call i64 @fwrite(i8* %4, i32 1, i64 %6, i32* %15)
%23 = icmp eq i64 %22, %6
br i1 %23, label %29, label %24
24: ; preds = %21
%25 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
%26 = tail call i64 @fclose(i32* %15)
%27 = tail call i32 @free(i8* %7)
%28 = tail call i32 (...) @abort()
unreachable
29: ; preds = %21
%30 = tail call i64 @fclose(i32* %15)
%31 = icmp eq i64 %30, 0
br i1 %31, label %36, label %32
32: ; preds = %29
%33 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%34 = tail call i64 @fclose(i32* %15)
%35 = tail call i32 @free(i8* %7)
br label %50
36: ; preds = %29
%37 = tail call i32* (...) @connect_to_server()
%38 = tail call i64 bitcast (i64 (i8*, i32, i64, i32*)* @fwrite to i64 (i8*, i64, i64, i32*)*)(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i64 4, i64 1, i32* %37)
%39 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %37, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.7, i64 0, i64 0), i8* %2)
%40 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %37, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.8, i64 0, i64 0), i8* %7)
%41 = icmp eq i8* %3, null
br i1 %41, label %44, label %42
42: ; preds = %36
%43 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %37, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.9, i64 0, i64 0), i8* %3)
br label %44
44: ; preds = %42, %36
%45 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %37, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.10, i64 0, i64 0), i8* %1)
%46 = tail call i64 bitcast (i64 (i8*, i32, i64, i32*)* @fwrite to i64 (i8*, i64, i64, i32*)*)(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.11, i64 0, i64 0), i64 6, i64 1, i32* %37)
%47 = tail call i64 bitcast (i64 (i8*, i32, i64, i32*)* @fwrite to i64 (i8*, i64, i64, i32*)*)(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.12, i64 0, i64 0), i64 5, i64 1, i32* %37)
%48 = tail call i32 @free(i8* %7)
%49 = tail call i64 @fclose(i32* %37)
br label %50
50: ; preds = %44, %32
%51 = phi i32 [ -1, %32 ], [ 0, %44 ]
ret i32 %51
}